/usr/lib/python2.7/dist-packages/pyramid/tests/test_scripts/test_pshell.py is in python-pyramid 1.4.5+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | import unittest
from pyramid.tests.test_scripts import dummy
class TestPShellCommand(unittest.TestCase):
def _getTargetClass(self):
from pyramid.scripts.pshell import PShellCommand
return PShellCommand
def _makeOne(self, patch_bootstrap=True, patch_config=True,
patch_args=True, patch_options=True):
cmd = self._getTargetClass()([])
if patch_bootstrap:
self.bootstrap = dummy.DummyBootstrap()
cmd.bootstrap = (self.bootstrap,)
if patch_config:
self.config_factory = dummy.DummyConfigParserFactory()
cmd.ConfigParser = self.config_factory
if patch_args:
self.args = ('/foo/bar/myapp.ini#myapp',)
cmd.args = self.args
if patch_options:
class Options(object): pass
self.options = Options()
self.options.python_shell = ''
self.options.setup = None
cmd.options = self.options
return cmd
def test_make_default_shell(self):
command = self._makeOne()
interact = dummy.DummyInteractor()
shell = command.make_default_shell(interact)
shell({'foo': 'bar'}, 'a help message')
self.assertEqual(interact.local, {'foo': 'bar'})
self.assertTrue('a help message' in interact.banner)
def test_make_bpython_shell(self):
command = self._makeOne()
bpython = dummy.DummyBPythonShell()
shell = command.make_bpython_shell(bpython)
shell({'foo': 'bar'}, 'a help message')
self.assertEqual(bpython.locals_, {'foo': 'bar'})
self.assertTrue('a help message' in bpython.banner)
def test_make_ipython_v0_11_shell(self):
command = self._makeOne()
ipshell_factory = dummy.DummyIPShellFactory()
shell = command.make_ipython_v0_11_shell(ipshell_factory)
shell({'foo': 'bar'}, 'a help message')
self.assertEqual(ipshell_factory.kw['user_ns'], {'foo': 'bar'})
self.assertTrue('a help message' in ipshell_factory.kw['banner2'])
self.assertTrue(ipshell_factory.shell.called)
def test_make_ipython_v0_10_shell(self):
command = self._makeOne()
ipshell_factory = dummy.DummyIPShellFactory()
shell = command.make_ipython_v0_10_shell(ipshell_factory)
shell({'foo': 'bar'}, 'a help message')
self.assertEqual(ipshell_factory.kw['argv'], [])
self.assertEqual(ipshell_factory.kw['user_ns'], {'foo': 'bar'})
self.assertTrue('a help message' in ipshell_factory.shell.banner)
self.assertTrue(ipshell_factory.shell.called)
def test_command_loads_default_shell(self):
command = self._makeOne()
shell = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: None
command.make_ipython_v0_10_shell = lambda: None
command.make_bpython_shell = lambda: None
command.make_default_shell = lambda: shell
command.run()
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_default_shell_with_unknown_shell(self):
command = self._makeOne()
shell = dummy.DummyShell()
bad_shell = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: bad_shell
command.make_ipython_v0_10_shell = lambda: bad_shell
command.make_bpython_shell = lambda: bad_shell
command.make_default_shell = lambda: shell
command.options.python_shell = 'unknow_python_shell'
command.run()
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
})
self.assertEqual(bad_shell.env, {})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_ipython_v0_11(self):
command = self._makeOne()
shell = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: shell
command.make_ipython_v0_10_shell = lambda: None
command.make_bpython_shell = lambda: None
command.make_default_shell = lambda: None
command.options.python_shell = 'ipython'
command.run()
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_ipython_v0_10(self):
command = self._makeOne()
shell = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: None
command.make_ipython_v0_10_shell = lambda: shell
command.make_bpython_shell = lambda: None
command.make_default_shell = lambda: None
command.options.python_shell = 'ipython'
command.run()
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_bpython_shell(self):
command = self._makeOne()
shell = dummy.DummyBPythonShell()
command.make_ipython_v0_11_shell = lambda: None
command.make_ipython_v0_10_shell = lambda: None
command.make_bpython_shell = lambda: shell
command.options.python_shell = 'bpython'
command.run()
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.locals_, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.banner)
def test_shell_ipython_ordering(self):
command = self._makeOne()
shell0_11 = dummy.DummyShell()
shell0_10 = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: shell0_11
command.make_ipython_v0_10_shell = lambda: shell0_10
command.make_bpython_shell = lambda: None
shell = command.make_shell()
self.assertEqual(shell, shell0_11)
command.options.python_shell = 'ipython'
shell = command.make_shell()
self.assertEqual(shell, shell0_11)
def test_shell_ordering(self):
command = self._makeOne()
ipshell = dummy.DummyShell()
bpshell = dummy.DummyShell()
dshell = dummy.DummyShell()
command.make_ipython_v0_11_shell = lambda: None
command.make_ipython_v0_10_shell = lambda: None
command.make_bpython_shell = lambda: None
command.make_default_shell = lambda: dshell
shell = command.make_shell()
self.assertEqual(shell, dshell)
command.options.python_shell = 'ipython'
shell = command.make_shell()
self.assertEqual(shell, dshell)
command.options.python_shell = 'bpython'
shell = command.make_shell()
self.assertEqual(shell, dshell)
command.make_ipython_v0_11_shell = lambda: ipshell
command.make_bpython_shell = lambda: bpshell
command.options.python_shell = 'ipython'
shell = command.make_shell()
self.assertEqual(shell, ipshell)
command.options.python_shell = 'bpython'
shell = command.make_shell()
self.assertEqual(shell, bpshell)
command.options.python_shell = 'python'
shell = command.make_shell()
self.assertEqual(shell, dshell)
def test_command_loads_custom_items(self):
command = self._makeOne()
model = dummy.Dummy()
self.config_factory.items = [('m', model)]
shell = dummy.DummyShell()
command.run(shell)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':self.bootstrap.root,
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
'm':model,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_setup(self):
command = self._makeOne()
def setup(env):
env['a'] = 1
env['root'] = 'root override'
self.config_factory.items = [('setup', setup)]
shell = dummy.DummyShell()
command.run(shell)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':'root override',
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
'a':1,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_check_variable_override_order(self):
command = self._makeOne()
model = dummy.Dummy()
def setup(env):
env['a'] = 1
env['m'] = 'model override'
env['root'] = 'root override'
self.config_factory.items = [('setup', setup), ('m', model)]
shell = dummy.DummyShell()
command.run(shell)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':'root override',
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
'a':1, 'm':model,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_loads_setup_from_options(self):
command = self._makeOne()
def setup(env):
env['a'] = 1
env['root'] = 'root override'
model = dummy.Dummy()
self.config_factory.items = [('setup', 'abc'),
('m', model)]
command.options.setup = setup
shell = dummy.DummyShell()
command.run(shell)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':self.bootstrap.app, 'root':'root override',
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
'a':1, 'm':model,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
def test_command_custom_section_override(self):
command = self._makeOne()
dummy_ = dummy.Dummy()
self.config_factory.items = [('app', dummy_), ('root', dummy_),
('registry', dummy_), ('request', dummy_)]
shell = dummy.DummyShell()
command.run(shell)
self.assertTrue(self.config_factory.parser)
self.assertEqual(self.config_factory.parser.filename,
'/foo/bar/myapp.ini')
self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp')
self.assertEqual(shell.env, {
'app':dummy_, 'root':dummy_, 'registry':dummy_, 'request':dummy_,
'root_factory':self.bootstrap.root_factory,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
class Test_main(unittest.TestCase):
def _callFUT(self, argv):
from pyramid.scripts.pshell import main
return main(argv, quiet=True)
def test_it(self):
result = self._callFUT(['pshell'])
self.assertEqual(result, 2)
|