Refs #25680 -- Added shell tests for globals available in passed commands.

This commit is contained in:
Mariusz Felisiak 2020-11-11 09:01:44 +01:00
parent 50c3ac6fa9
commit d26d1c196d
1 changed files with 18 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from django.test.utils import captured_stdin, captured_stdout
class ShellCommandTestCase(SimpleTestCase):
script_globals = 'print("__name__" in globals())'
def test_command_option(self):
with self.assertLogs('test', 'INFO') as cm:
@ -21,6 +22,11 @@ class ShellCommandTestCase(SimpleTestCase):
)
self.assertEqual(cm.records[0].getMessage(), __version__)
def test_command_option_globals(self):
with captured_stdout() as stdout:
call_command('shell', command=self.script_globals)
self.assertEqual(stdout.getvalue().strip(), 'True')
@unittest.skipIf(sys.platform == 'win32', "Windows select() doesn't support file descriptors.")
@mock.patch('django.core.management.commands.shell.select')
def test_stdin_read(self, select):
@ -30,6 +36,18 @@ class ShellCommandTestCase(SimpleTestCase):
call_command('shell')
self.assertEqual(stdout.getvalue().strip(), '100')
@unittest.skipIf(
sys.platform == 'win32',
"Windows select() doesn't support file descriptors.",
)
@mock.patch('django.core.management.commands.shell.select') # [1]
def test_stdin_read_globals(self, select):
with captured_stdin() as stdin, captured_stdout() as stdout:
stdin.write(self.script_globals)
stdin.seek(0)
call_command('shell')
self.assertEqual(stdout.getvalue().strip(), 'True')
@mock.patch('django.core.management.commands.shell.select.select') # [1]
@mock.patch.dict('sys.modules', {'IPython': None})
def test_shell_with_ipython_not_installed(self, select):