commit | author | age
|
823ac4
|
1 |
import os |
262cea
|
2 |
import unittest |
dd3cc8
|
3 |
from . import dummy |
262cea
|
4 |
|
cb9202
|
5 |
|
262cea
|
6 |
class TestPShellCommand(unittest.TestCase): |
CM |
7 |
def _getTargetClass(self): |
|
8 |
from pyramid.scripts.pshell import PShellCommand |
|
9 |
return PShellCommand |
|
10 |
|
e1d1af
|
11 |
def _makeOne(self, patch_bootstrap=True, patch_loader=True, |
262cea
|
12 |
patch_args=True, patch_options=True): |
CM |
13 |
cmd = self._getTargetClass()([]) |
cb9202
|
14 |
|
262cea
|
15 |
if patch_bootstrap: |
CM |
16 |
self.bootstrap = dummy.DummyBootstrap() |
e1d1af
|
17 |
cmd.bootstrap = self.bootstrap |
MM |
18 |
if patch_loader: |
|
19 |
self.loader = dummy.DummyLoader() |
|
20 |
cmd.get_config_loader = self.loader |
262cea
|
21 |
if patch_args: |
2d9c13
|
22 |
class Args(object): pass |
SP |
23 |
self.args = Args() |
|
24 |
self.args.config_uri = '/foo/bar/myapp.ini#myapp' |
|
25 |
cmd.args.config_uri = self.args.config_uri |
262cea
|
26 |
if patch_options: |
CM |
27 |
class Options(object): pass |
|
28 |
self.options = Options() |
2cf5d2
|
29 |
self.options.python_shell = '' |
262cea
|
30 |
self.options.setup = None |
208e7b
|
31 |
self.options.list = None |
262cea
|
32 |
cmd.options = self.options |
cb9202
|
33 |
|
823ac4
|
34 |
# default to None to prevent side-effects from running tests in |
MM |
35 |
# unknown environments |
|
36 |
cmd.pystartup = None |
262cea
|
37 |
return cmd |
cb9202
|
38 |
|
JA |
39 |
def _makeEntryPoints(self, command, shells): |
|
40 |
command.pkg_resources = dummy.DummyPkgResources(shells) |
262cea
|
41 |
|
CM |
42 |
def test_command_loads_default_shell(self): |
|
43 |
command = self._makeOne() |
|
44 |
shell = dummy.DummyShell() |
b7350e
|
45 |
self._makeEntryPoints(command, {}) |
cb9202
|
46 |
|
b7350e
|
47 |
command.default_runner = shell |
262cea
|
48 |
command.run() |
CM |
49 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
50 |
self.assertEqual(shell.env, { |
|
51 |
'app':self.bootstrap.app, 'root':self.bootstrap.root, |
|
52 |
'registry':self.bootstrap.registry, |
|
53 |
'request':self.bootstrap.request, |
|
54 |
'root_factory':self.bootstrap.root_factory, |
|
55 |
}) |
|
56 |
self.assertTrue(self.bootstrap.closer.called) |
|
57 |
self.assertTrue(shell.help) |
|
58 |
|
208e7b
|
59 |
def test_command_errors_with_unknown_shell(self): |
262cea
|
60 |
command = self._makeOne() |
b932a4
|
61 |
out_calls = [] |
JA |
62 |
|
|
63 |
def out(msg): |
|
64 |
out_calls.append(msg) |
|
65 |
|
|
66 |
command.out = out |
|
67 |
|
262cea
|
68 |
shell = dummy.DummyShell() |
cb9202
|
69 |
|
b7350e
|
70 |
self._makeEntryPoints(command, {}) |
cb9202
|
71 |
|
b7350e
|
72 |
command.default_runner = shell |
53937c
|
73 |
command.args.python_shell = 'unknown_python_shell' |
b932a4
|
74 |
result = command.run() |
JA |
75 |
self.assertEqual(result, 1) |
|
76 |
self.assertEqual( |
|
77 |
out_calls, ['could not find a shell named "unknown_python_shell"'] |
|
78 |
) |
262cea
|
79 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
CM |
80 |
self.assertTrue(self.bootstrap.closer.called) |
|
81 |
|
208e7b
|
82 |
def test_command_loads_ipython(self): |
b74535
|
83 |
command = self._makeOne() |
MM |
84 |
shell = dummy.DummyShell() |
208e7b
|
85 |
bad_shell = dummy.DummyShell() |
cb9202
|
86 |
self._makeEntryPoints( |
JA |
87 |
command, |
|
88 |
{ |
b7350e
|
89 |
'ipython': shell, |
MM |
90 |
'bpython': bad_shell, |
cb9202
|
91 |
} |
JA |
92 |
) |
b74535
|
93 |
|
53937c
|
94 |
command.args.python_shell = 'ipython' |
262cea
|
95 |
|
CM |
96 |
command.run() |
|
97 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
98 |
self.assertEqual(shell.env, { |
|
99 |
'app':self.bootstrap.app, 'root':self.bootstrap.root, |
|
100 |
'registry':self.bootstrap.registry, |
|
101 |
'request':self.bootstrap.request, |
|
102 |
'root_factory':self.bootstrap.root_factory, |
|
103 |
}) |
|
104 |
self.assertTrue(self.bootstrap.closer.called) |
|
105 |
self.assertTrue(shell.help) |
|
106 |
|
cb9202
|
107 |
def test_shell_entry_points(self): |
3808f7
|
108 |
command = self._makeOne() |
cb9202
|
109 |
dshell = dummy.DummyShell() |
b74535
|
110 |
|
cb9202
|
111 |
self._makeEntryPoints( |
JA |
112 |
command, |
|
113 |
{ |
b7350e
|
114 |
'ipython': dshell, |
MM |
115 |
'bpython': dshell, |
cb9202
|
116 |
} |
JA |
117 |
) |
3808f7
|
118 |
|
b7350e
|
119 |
command.default_runner = None |
3808f7
|
120 |
shell = command.make_shell() |
cb9202
|
121 |
self.assertEqual(shell, dshell) |
3808f7
|
122 |
|
208e7b
|
123 |
def test_shell_override(self): |
3808f7
|
124 |
command = self._makeOne() |
MM |
125 |
ipshell = dummy.DummyShell() |
|
126 |
bpshell = dummy.DummyShell() |
|
127 |
dshell = dummy.DummyShell() |
cb9202
|
128 |
|
b7350e
|
129 |
self._makeEntryPoints(command, {}) |
cb9202
|
130 |
|
b7350e
|
131 |
command.default_runner = dshell |
3808f7
|
132 |
|
MM |
133 |
shell = command.make_shell() |
|
134 |
self.assertEqual(shell, dshell) |
|
135 |
|
53937c
|
136 |
command.args.python_shell = 'ipython' |
208e7b
|
137 |
self.assertRaises(ValueError, command.make_shell) |
3808f7
|
138 |
|
cb9202
|
139 |
self._makeEntryPoints( |
JA |
140 |
command, |
|
141 |
{ |
b7350e
|
142 |
'ipython': ipshell, |
MM |
143 |
'bpython': bpshell, |
|
144 |
'python': dshell, |
cb9202
|
145 |
} |
JA |
146 |
) |
|
147 |
|
53937c
|
148 |
command.args.python_shell = 'ipython' |
3808f7
|
149 |
shell = command.make_shell() |
MM |
150 |
self.assertEqual(shell, ipshell) |
|
151 |
|
53937c
|
152 |
command.args.python_shell = 'bpython' |
3808f7
|
153 |
shell = command.make_shell() |
MM |
154 |
self.assertEqual(shell, bpshell) |
|
155 |
|
53937c
|
156 |
command.args.python_shell = 'python' |
208e7b
|
157 |
shell = command.make_shell() |
MM |
158 |
self.assertEqual(shell, dshell) |
|
159 |
|
|
160 |
def test_shell_ordering(self): |
|
161 |
command = self._makeOne() |
|
162 |
ipshell = dummy.DummyShell() |
|
163 |
bpshell = dummy.DummyShell() |
|
164 |
dshell = dummy.DummyShell() |
|
165 |
|
|
166 |
self._makeEntryPoints( |
|
167 |
command, |
|
168 |
{ |
b7350e
|
169 |
'ipython': ipshell, |
MM |
170 |
'bpython': bpshell, |
|
171 |
'python': dshell, |
208e7b
|
172 |
} |
MM |
173 |
) |
|
174 |
|
b7350e
|
175 |
command.default_runner = dshell |
208e7b
|
176 |
|
MM |
177 |
command.preferred_shells = ['ipython', 'bpython'] |
|
178 |
shell = command.make_shell() |
|
179 |
self.assertEqual(shell, ipshell) |
|
180 |
|
|
181 |
command.preferred_shells = ['bpython', 'python'] |
|
182 |
shell = command.make_shell() |
|
183 |
self.assertEqual(shell, bpshell) |
|
184 |
|
|
185 |
command.preferred_shells = ['python', 'ipython'] |
3808f7
|
186 |
shell = command.make_shell() |
MM |
187 |
self.assertEqual(shell, dshell) |
|
188 |
|
262cea
|
189 |
def test_command_loads_custom_items(self): |
CM |
190 |
command = self._makeOne() |
|
191 |
model = dummy.Dummy() |
6c0d2a
|
192 |
user = dummy.Dummy() |
e1d1af
|
193 |
self.loader.settings = {'pshell': {'m': model, 'User': user}} |
262cea
|
194 |
shell = dummy.DummyShell() |
CM |
195 |
command.run(shell) |
|
196 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
197 |
self.assertEqual(shell.env, { |
|
198 |
'app':self.bootstrap.app, 'root':self.bootstrap.root, |
|
199 |
'registry':self.bootstrap.registry, |
|
200 |
'request':self.bootstrap.request, |
|
201 |
'root_factory':self.bootstrap.root_factory, |
|
202 |
'm':model, |
6c0d2a
|
203 |
'User': user, |
262cea
|
204 |
}) |
CM |
205 |
self.assertTrue(self.bootstrap.closer.called) |
|
206 |
self.assertTrue(shell.help) |
|
207 |
|
|
208 |
def test_command_setup(self): |
|
209 |
command = self._makeOne() |
|
210 |
def setup(env): |
|
211 |
env['a'] = 1 |
|
212 |
env['root'] = 'root override' |
1c1c90
|
213 |
env['none'] = None |
e1d1af
|
214 |
self.loader.settings = {'pshell': {'setup': setup}} |
262cea
|
215 |
shell = dummy.DummyShell() |
CM |
216 |
command.run(shell) |
|
217 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
218 |
self.assertEqual(shell.env, { |
|
219 |
'app':self.bootstrap.app, 'root':'root override', |
|
220 |
'registry':self.bootstrap.registry, |
|
221 |
'request':self.bootstrap.request, |
|
222 |
'root_factory':self.bootstrap.root_factory, |
|
223 |
'a':1, |
1c1c90
|
224 |
'none': None, |
262cea
|
225 |
}) |
CM |
226 |
self.assertTrue(self.bootstrap.closer.called) |
|
227 |
self.assertTrue(shell.help) |
208e7b
|
228 |
|
990fb0
|
229 |
def test_command_setup_generator(self): |
MM |
230 |
command = self._makeOne() |
|
231 |
did_resume_after_yield = {} |
|
232 |
def setup(env): |
|
233 |
env['a'] = 1 |
|
234 |
env['root'] = 'root override' |
|
235 |
env['none'] = None |
|
236 |
request = env['request'] |
|
237 |
yield |
|
238 |
did_resume_after_yield['result'] = True |
|
239 |
self.assertEqual(request.dummy_attr, 1) |
|
240 |
self.loader.settings = {'pshell': {'setup': setup}} |
|
241 |
shell = dummy.DummyShell() |
|
242 |
command.run(shell) |
|
243 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
244 |
self.assertEqual(shell.env, { |
|
245 |
'app':self.bootstrap.app, 'root':'root override', |
|
246 |
'registry':self.bootstrap.registry, |
|
247 |
'request':self.bootstrap.request, |
|
248 |
'root_factory':self.bootstrap.root_factory, |
|
249 |
'a':1, |
|
250 |
'none': None, |
|
251 |
}) |
|
252 |
self.assertTrue(did_resume_after_yield['result']) |
|
253 |
self.assertTrue(self.bootstrap.closer.called) |
|
254 |
self.assertTrue(shell.help) |
|
255 |
|
208e7b
|
256 |
def test_command_default_shell_option(self): |
MM |
257 |
command = self._makeOne() |
|
258 |
ipshell = dummy.DummyShell() |
|
259 |
dshell = dummy.DummyShell() |
|
260 |
self._makeEntryPoints( |
|
261 |
command, |
|
262 |
{ |
b7350e
|
263 |
'ipython': ipshell, |
MM |
264 |
'python': dshell, |
208e7b
|
265 |
} |
MM |
266 |
) |
e1d1af
|
267 |
self.loader.settings = {'pshell': { |
MM |
268 |
'default_shell': 'bpython python\nipython'}} |
208e7b
|
269 |
command.run() |
MM |
270 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
271 |
self.assertTrue(dshell.called) |
262cea
|
272 |
|
CM |
273 |
def test_command_loads_check_variable_override_order(self): |
|
274 |
command = self._makeOne() |
|
275 |
model = dummy.Dummy() |
|
276 |
def setup(env): |
|
277 |
env['a'] = 1 |
|
278 |
env['m'] = 'model override' |
|
279 |
env['root'] = 'root override' |
e1d1af
|
280 |
self.loader.settings = {'pshell': {'setup': setup, 'm': model}} |
262cea
|
281 |
shell = dummy.DummyShell() |
CM |
282 |
command.run(shell) |
|
283 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
284 |
self.assertEqual(shell.env, { |
|
285 |
'app':self.bootstrap.app, 'root':'root override', |
|
286 |
'registry':self.bootstrap.registry, |
|
287 |
'request':self.bootstrap.request, |
|
288 |
'root_factory':self.bootstrap.root_factory, |
990fb0
|
289 |
'a':1, 'm':'model override', |
262cea
|
290 |
}) |
CM |
291 |
self.assertTrue(self.bootstrap.closer.called) |
|
292 |
self.assertTrue(shell.help) |
|
293 |
|
|
294 |
def test_command_loads_setup_from_options(self): |
|
295 |
command = self._makeOne() |
|
296 |
def setup(env): |
|
297 |
env['a'] = 1 |
|
298 |
env['root'] = 'root override' |
|
299 |
model = dummy.Dummy() |
e1d1af
|
300 |
self.loader.settings = {'pshell': {'setup': 'abc', 'm': model}} |
53937c
|
301 |
command.args.setup = setup |
262cea
|
302 |
shell = dummy.DummyShell() |
CM |
303 |
command.run(shell) |
|
304 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
305 |
self.assertEqual(shell.env, { |
|
306 |
'app':self.bootstrap.app, 'root':'root override', |
|
307 |
'registry':self.bootstrap.registry, |
|
308 |
'request':self.bootstrap.request, |
|
309 |
'root_factory':self.bootstrap.root_factory, |
|
310 |
'a':1, 'm':model, |
|
311 |
}) |
|
312 |
self.assertTrue(self.bootstrap.closer.called) |
|
313 |
self.assertTrue(shell.help) |
|
314 |
|
|
315 |
def test_command_custom_section_override(self): |
|
316 |
command = self._makeOne() |
|
317 |
dummy_ = dummy.Dummy() |
e1d1af
|
318 |
self.loader.settings = {'pshell': { |
MM |
319 |
'app': dummy_, 'root': dummy_, 'registry': dummy_, |
|
320 |
'request': dummy_}} |
262cea
|
321 |
shell = dummy.DummyShell() |
CM |
322 |
command.run(shell) |
|
323 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
324 |
self.assertEqual(shell.env, { |
|
325 |
'app':dummy_, 'root':dummy_, 'registry':dummy_, 'request':dummy_, |
|
326 |
'root_factory':self.bootstrap.root_factory, |
|
327 |
}) |
|
328 |
self.assertTrue(self.bootstrap.closer.called) |
|
329 |
self.assertTrue(shell.help) |
|
330 |
|
f3a567
|
331 |
def test_command_loads_pythonstartup(self): |
823ac4
|
332 |
command = self._makeOne() |
MM |
333 |
command.pystartup = ( |
f3a567
|
334 |
os.path.abspath( |
MM |
335 |
os.path.join( |
|
336 |
os.path.dirname(__file__), |
14126c
|
337 |
'pystartup.txt'))) |
823ac4
|
338 |
shell = dummy.DummyShell() |
MM |
339 |
command.run(shell) |
|
340 |
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') |
|
341 |
self.assertEqual(shell.env, { |
|
342 |
'app':self.bootstrap.app, 'root':self.bootstrap.root, |
|
343 |
'registry':self.bootstrap.registry, |
|
344 |
'request':self.bootstrap.request, |
|
345 |
'root_factory':self.bootstrap.root_factory, |
|
346 |
'foo':1, |
|
347 |
}) |
|
348 |
self.assertTrue(self.bootstrap.closer.called) |
|
349 |
self.assertTrue(shell.help) |
d29151
|
350 |
|
208e7b
|
351 |
def test_list_shells(self): |
MM |
352 |
command = self._makeOne() |
|
353 |
|
|
354 |
dshell = dummy.DummyShell() |
|
355 |
out_calls = [] |
|
356 |
|
|
357 |
def out(msg): |
|
358 |
out_calls.append(msg) |
|
359 |
|
|
360 |
command.out = out |
|
361 |
|
|
362 |
self._makeEntryPoints( |
|
363 |
command, |
|
364 |
{ |
b7350e
|
365 |
'ipython': dshell, |
MM |
366 |
'python': dshell, |
208e7b
|
367 |
} |
MM |
368 |
) |
|
369 |
|
53937c
|
370 |
command.args.list = True |
208e7b
|
371 |
result = command.run() |
MM |
372 |
self.assertEqual(result, 0) |
|
373 |
self.assertEqual(out_calls, [ |
|
374 |
'Available shells:', |
|
375 |
' ipython', |
|
376 |
' python', |
|
377 |
]) |
b7350e
|
378 |
|
MM |
379 |
|
|
380 |
class Test_python_shell_runner(unittest.TestCase): |
|
381 |
def _callFUT(self, env, help, interact): |
|
382 |
from pyramid.scripts.pshell import python_shell_runner |
|
383 |
return python_shell_runner(env, help, interact=interact) |
|
384 |
|
|
385 |
def test_it(self): |
|
386 |
interact = dummy.DummyInteractor() |
|
387 |
self._callFUT({'foo': 'bar'}, 'a help message', interact) |
|
388 |
self.assertEqual(interact.local, {'foo': 'bar'}) |
|
389 |
self.assertTrue('a help message' in interact.banner) |
208e7b
|
390 |
|
d29151
|
391 |
class Test_main(unittest.TestCase): |
CM |
392 |
def _callFUT(self, argv): |
|
393 |
from pyramid.scripts.pshell import main |
|
394 |
return main(argv, quiet=True) |
|
395 |
|
|
396 |
def test_it(self): |
|
397 |
result = self._callFUT(['pshell']) |
d58614
|
398 |
self.assertEqual(result, 2) |