Debian Codegraph @

Cheetah/Tests/TemplateCmdLineIface.py

raw symbols SCIP
1 try:
2 from io import BytesIO, TextIOWrapper, StringIO
3 except ImportError:
4 try:
5 from cStringIO import StringIO as BytesIO
6 except ImportError:
7 from StringIO import StringIO as BytesIO
8
9 from tempfile import NamedTemporaryFile
10
11 import os
12 import pickle
13 import json
14 import sys
15 import unittest
16
17 from Cheetah.Template import Template
18 from Cheetah.TemplateCmdLineIface import CmdLineIface
19
20
21 class TestHelp(unittest.TestCase):
22 def setUp(self):
23 self.NULL = NULL = open(os.devnull, 'w')
24 sys.stdout = NULL
25
26 def tearDown(self):
27 self.NULL.close()
28 sys.stdout = sys.__stdout__
29
30 def test_help(self):
31 klass = Template.compile(source='$foo')
32 t = klass()
33 cmdline = CmdLineIface(t, scriptName='test', cmdLineArgs=['-h'])
34 self.assertRaises(SystemExit, cmdline._processCmdLineArgs)
35
36
37 class TestEnv(unittest.TestCase):
38 def setUp(self):
39 os.environ['foo'] = 'test foo'
40
41 def tearDown(self):
42 del os.environ['foo']
43
44 def test_env(self):
45 klass = Template.compile(source='$foo')
46 t = klass()
47 cmdline = CmdLineIface(t, scriptName='test', cmdLineArgs=['--env'])
48 cmdline._processCmdLineArgs()
49 assert str(t) == 'test foo'
50
51
52 class TestPickleStdin(unittest.TestCase):
53
54 def setUp(self):
55 pickle_bytes = pickle.dumps({'foo': 'test foo'})
56 if hasattr(sys.stdin, 'buffer'):
57 sys.stdin = TextIOWrapper(BytesIO(pickle_bytes))
58 else:
59 sys.stdin = BytesIO(pickle_bytes)
60
61 def tearDown(self):
62 sys.stdin = sys.__stdin__
63
64 def test_pickle_stdin(self):
65 klass = Template.compile(source='$foo')
66 t = klass()
67 cmdline = CmdLineIface(t, scriptName='test',
68 cmdLineArgs=['--pickle=-'])
69 cmdline._processCmdLineArgs()
70 assert str(t) == 'test foo'
71
72
73 class TestPickleFile(unittest.TestCase):
74
75 def setUp(self):
76 self.pickle_file = pickle_file = \
77 NamedTemporaryFile(mode='wb', delete=False)
78 pickle.dump({'foo': 'test foo'}, pickle_file)
79 pickle_file.close()
80
81 def tearDown(self):
82 os.remove(self.pickle_file.name)
83
84 def test_pickle_file(self):
85 klass = Template.compile(source='$foo')
86 t = klass()
87 cmdline = CmdLineIface(t, scriptName='test',
88 cmdLineArgs=['--pickle', self.pickle_file.name])
89 cmdline._processCmdLineArgs()
90 assert str(t) == 'test foo'
91
92
93 class TestJsonStdin(unittest.TestCase):
94
95 def setUp(self):
96 json_string = json.dumps({'foo': 'test foo'})
97 if hasattr(sys.stdin, 'buffer'):
98 sys.stdin = TextIOWrapper(StringIO(json_string))
99 else:
100 sys.stdin = BytesIO(json_string.encode('utf-8'))
101
102 def tearDown(self):
103 sys.stdin = sys.__stdin__
104
105 def test_json_stdin(self):
106 klass = Template.compile(source='$foo')
107 t = klass()
108 cmdline = CmdLineIface(t, scriptName='test',
109 cmdLineArgs=['--json=-'])
110 cmdline._processCmdLineArgs()
111 assert (True)
112
113
114 class TestJsonFile(unittest.TestCase):
115
116 def setUp(self):
117 self.json_file = NamedTemporaryFile(mode='w', delete=False)
118 json.dump({'foo': 'test foo'}, self.json_file)
119 self.json_file.close()
120
121 def tearDown(self):
122 os.remove(self.json_file.name)
123
124 def test_json_file(self):
125 klass = Template.compile(source='$foo')
126 t = klass()
127 cmdline = CmdLineIface(t, scriptName='test',
128 cmdLineArgs=['--json', self.json_file.name])
129 cmdline._processCmdLineArgs()
130 assert str(t) == 'test foo'