1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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

View File

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