Michael Merickel
2017-03-29 e1d1af88e314fe59d9197182f8c2b56ecdcbd115
commit | author | age
823ac4 1 import os
262cea 2 import unittest
CM 3 from pyramid.tests.test_scripts import dummy
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
MM 229     def test_command_default_shell_option(self):
230         command = self._makeOne()
231         ipshell = dummy.DummyShell()
232         dshell = dummy.DummyShell()
233         self._makeEntryPoints(
234             command,
235             {
b7350e 236                 'ipython': ipshell,
MM 237                 'python': dshell,
208e7b 238             }
MM 239         )
e1d1af 240         self.loader.settings = {'pshell': {
MM 241             'default_shell': 'bpython python\nipython'}}
208e7b 242         command.run()
MM 243         self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
244         self.assertTrue(dshell.called)
262cea 245
CM 246     def test_command_loads_check_variable_override_order(self):
247         command = self._makeOne()
248         model = dummy.Dummy()
249         def setup(env):
250             env['a'] = 1
251             env['m'] = 'model override'
252             env['root'] = 'root override'
e1d1af 253         self.loader.settings = {'pshell': {'setup': setup, 'm': model}}
262cea 254         shell = dummy.DummyShell()
CM 255         command.run(shell)
256         self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
257         self.assertEqual(shell.env, {
258             'app':self.bootstrap.app, 'root':'root override',
259             'registry':self.bootstrap.registry,
260             'request':self.bootstrap.request,
261             'root_factory':self.bootstrap.root_factory,
262             'a':1, 'm':model,
263         })
264         self.assertTrue(self.bootstrap.closer.called)
265         self.assertTrue(shell.help)
266
267     def test_command_loads_setup_from_options(self):
268         command = self._makeOne()
269         def setup(env):
270             env['a'] = 1
271             env['root'] = 'root override'
272         model = dummy.Dummy()
e1d1af 273         self.loader.settings = {'pshell': {'setup': 'abc', 'm': model}}
53937c 274         command.args.setup = setup
262cea 275         shell = dummy.DummyShell()
CM 276         command.run(shell)
277         self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
278         self.assertEqual(shell.env, {
279             'app':self.bootstrap.app, 'root':'root override',
280             'registry':self.bootstrap.registry,
281             'request':self.bootstrap.request,
282             'root_factory':self.bootstrap.root_factory,
283             'a':1, 'm':model,
284         })
285         self.assertTrue(self.bootstrap.closer.called)
286         self.assertTrue(shell.help)
287
288     def test_command_custom_section_override(self):
289         command = self._makeOne()
290         dummy_ = dummy.Dummy()
e1d1af 291         self.loader.settings = {'pshell': {
MM 292             'app': dummy_, 'root': dummy_, 'registry': dummy_,
293             'request': dummy_}}
262cea 294         shell = dummy.DummyShell()
CM 295         command.run(shell)
296         self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
297         self.assertEqual(shell.env, {
298             'app':dummy_, 'root':dummy_, 'registry':dummy_, 'request':dummy_,
299             'root_factory':self.bootstrap.root_factory,
300         })
301         self.assertTrue(self.bootstrap.closer.called)
302         self.assertTrue(shell.help)
303
f3a567 304     def test_command_loads_pythonstartup(self):
823ac4 305         command = self._makeOne()
MM 306         command.pystartup = (
f3a567 307             os.path.abspath(
MM 308                 os.path.join(
309                     os.path.dirname(__file__),
14126c 310                     'pystartup.txt')))
823ac4 311         shell = dummy.DummyShell()
MM 312         command.run(shell)
313         self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
314         self.assertEqual(shell.env, {
315             'app':self.bootstrap.app, 'root':self.bootstrap.root,
316             'registry':self.bootstrap.registry,
317             'request':self.bootstrap.request,
318             'root_factory':self.bootstrap.root_factory,
319             'foo':1,
320         })
321         self.assertTrue(self.bootstrap.closer.called)
322         self.assertTrue(shell.help)
d29151 323
208e7b 324     def test_list_shells(self):
MM 325         command = self._makeOne()
326
327         dshell = dummy.DummyShell()
328         out_calls = []
329
330         def out(msg):
331             out_calls.append(msg)
332
333         command.out = out
334
335         self._makeEntryPoints(
336             command,
337             {
b7350e 338                 'ipython': dshell,
MM 339                 'python': dshell,
208e7b 340             }
MM 341         )
342
53937c 343         command.args.list = True
208e7b 344         result = command.run()
MM 345         self.assertEqual(result, 0)
346         self.assertEqual(out_calls, [
347             'Available shells:',
348             '  ipython',
349             '  python',
350         ])
b7350e 351
MM 352
353 class Test_python_shell_runner(unittest.TestCase):
354     def _callFUT(self, env, help, interact):
355         from pyramid.scripts.pshell import python_shell_runner
356         return python_shell_runner(env, help, interact=interact)
357
358     def test_it(self):
359         interact = dummy.DummyInteractor()
360         self._callFUT({'foo': 'bar'}, 'a help message', interact)
361         self.assertEqual(interact.local, {'foo': 'bar'})
362         self.assertTrue('a help message' in interact.banner)
208e7b 363
d29151 364 class Test_main(unittest.TestCase):
CM 365     def _callFUT(self, argv):
366         from pyramid.scripts.pshell import main
367         return main(argv, quiet=True)
368
369     def test_it(self):
370         result = self._callFUT(['pshell'])
d58614 371         self.assertEqual(result, 2)