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