Commit fa8f6d8a authored by Jérome Perrin's avatar Jérome Perrin

console: set __file__ like python interpreter would do

parent b9904d9f
...@@ -94,6 +94,7 @@ class ConsoleCommand(ClientConfigCommand): ...@@ -94,6 +94,7 @@ class ConsoleCommand(ClientConfigCommand):
if args.script_file: if args.script_file:
with open(args.script_file) as f: with open(args.script_file) as f:
code = compile(f.read(), args.script_file, 'exec') code = compile(f.read(), args.script_file, 'exec')
local['__file__'] = args.script_file
return exec_(code, globals(), local) return exec_(code, globals(), local)
if not any([args.python, args.ipython, args.bpython]): if not any([args.python, args.ipython, args.bpython]):
......
...@@ -93,6 +93,7 @@ def init(conf, logger): ...@@ -93,6 +93,7 @@ def init(conf, logger):
key_file=conf.key_file, cert_file=conf.cert_file, key_file=conf.key_file, cert_file=conf.cert_file,
slapgrid_rest_uri=conf.master_rest_url) slapgrid_rest_uri=conf.master_rest_url)
local = globals().copy() local = globals().copy()
local.pop('__file__', None)
local['slap'] = slap local['slap'] = slap
# Create global shortcut functions to request instance and software # Create global shortcut functions to request instance and software
def shorthandRequest(*args, **kwargs): def shorthandRequest(*args, **kwargs):
......
...@@ -718,7 +718,7 @@ class TestCliSupervisorctl(CliMixin): ...@@ -718,7 +718,7 @@ class TestCliSupervisorctl(CliMixin):
class TestCliConsole(unittest.TestCase): class TestCliConsole(unittest.TestCase):
script = """\ default_script = """\
print(request('software_release', 'instance').getInstanceParameterDict()['parameter_name']) print(request('software_release', 'instance').getInstanceParameterDict()['parameter_name'])
""" """
...@@ -732,22 +732,55 @@ print(request('software_release', 'instance').getInstanceParameterDict()['parame ...@@ -732,22 +732,55 @@ print(request('software_release', 'instance').getInstanceParameterDict()['parame
tempfile.NamedTemporaryFile() as config_file: tempfile.NamedTemporaryFile() as config_file:
config_file.write(b'[slapos]\nmaster_url=null\n') config_file.write(b'[slapos]\nmaster_url=null\n')
config_file.flush() config_file.flush()
yield slapos.cli.entry.SlapOSApp(), config_file.name yield slapos.cli.entry.SlapOSApp(), \
config_file.name, \
mock_request, \
app_stdout
def test_console_interactive(self):
with self._test_console() as (app, config_file, mock_request, stdout), \
patch.object(sys, 'stdin', StringIO(self.default_script)):
app.run(('console', '--cfg', config_file))
self.assertIn('parameter_value', stdout.getvalue())
mock_request.assert_called_once_with( mock_request.assert_called_once_with(
'software_release', 'instance') 'software_release', 'instance')
self.assertIn('parameter_value', app_stdout.getvalue())
def test_console_interactive(self): def test_console_interactive_no__file__(self):
with self._test_console() as (app, config_file), \ script = '''if 1:
patch.object(sys, 'stdin', StringIO(self.script)): try:
print('FAIL __file__ is set to', __file__)
except NameError:
print('OK __file__ is not set')
'''
with self._test_console() as (app, config_file, _, stdout), \
patch.object(sys, 'stdin', StringIO(script)):
app.run(('console', '--cfg', config_file)) app.run(('console', '--cfg', config_file))
self.assertIn('OK __file__ is not set', stdout.getvalue())
def test_console_script(self): def test_console_script(self):
with self._test_console() as (app, config_file), \ with self._test_console() as (app, config_file, mock_request, stdout), \
tempfile.NamedTemporaryFile('w') as script: tempfile.NamedTemporaryFile('w') as script:
script.write(self.script) script.write(self.default_script)
script.flush()
app.run(('console', '--cfg', config_file, script.name))
self.assertIn('parameter_value', stdout.getvalue())
mock_request.assert_called_once_with(
'software_release', 'instance')
def test_console_script__file__(self):
with self._test_console() as (app, config_file, _, stdout),\
tempfile.NamedTemporaryFile('w') as script:
script.write('''if 1:
if __file__ == %r:
print('OK __file__ is set to script')
else:
print('FAIL __file__ is set to', __file__)
''' % script.name)
script.flush() script.flush()
app.run(('console', '--cfg', config_file, script.name)) app.run(('console', '--cfg', config_file, script.name))
self.assertIn('OK __file__ is set to script', stdout.getvalue())
class TestCliComplete(CliMixin): class TestCliComplete(CliMixin):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment