From d26d1c196dbb82bf034608576b1019b163086e51 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 11 Nov 2020 09:01:44 +0100 Subject: [PATCH] Refs #25680 -- Added shell tests for globals available in passed commands. --- tests/shell/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/shell/tests.py b/tests/shell/tests.py index f33a9ae701d..13cad10c216 100644 --- a/tests/shell/tests.py +++ b/tests/shell/tests.py @@ -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):