1
+ import os
2
+ import subprocess
1
3
import sys
2
4
import unittest
3
5
from unittest import mock
@@ -23,25 +25,85 @@ class ShellCommandTestCase(SimpleTestCase):
23
25
24
26
def test_command_option (self ):
25
27
with self .assertLogs ("test" , "INFO" ) as cm :
26
- call_command (
27
- "shell" ,
28
- command = (
29
- "import django; from logging import getLogger; "
30
- 'getLogger("test").info(django.__version__)'
31
- ),
32
- )
28
+ with captured_stdout ():
29
+ call_command (
30
+ "shell" ,
31
+ command = (
32
+ "import django; from logging import getLogger; "
33
+ 'getLogger("test").info(django.__version__)'
34
+ ),
35
+ )
33
36
self .assertEqual (cm .records [0 ].getMessage (), __version__ )
34
37
35
38
def test_command_option_globals (self ):
36
39
with captured_stdout () as stdout :
37
- call_command ("shell" , command = self .script_globals )
40
+ call_command ("shell" , command = self .script_globals , verbosity = 0 )
38
41
self .assertEqual (stdout .getvalue ().strip (), "True" )
39
42
40
43
def test_command_option_inline_function_call (self ):
41
44
with captured_stdout () as stdout :
42
- call_command ("shell" , command = self .script_with_inline_function )
45
+ call_command ("shell" , command = self .script_with_inline_function , verbosity = 0 )
43
46
self .assertEqual (stdout .getvalue ().strip (), __version__ )
44
47
48
+ @override_settings (INSTALLED_APPS = ["shell" ])
49
+ def test_no_settings (self ):
50
+ test_environ = os .environ .copy ()
51
+ if "DJANGO_SETTINGS_MODULE" in test_environ :
52
+ del test_environ ["DJANGO_SETTINGS_MODULE" ]
53
+ error = (
54
+ "Automatic imports are disabled since settings are not configured.\n "
55
+ "DJANGO_SETTINGS_MODULE value is None.\n "
56
+ "HINT: Ensure that the settings module is configured and set.\n \n "
57
+ )
58
+ for verbosity , assertError in [
59
+ ("0" , self .assertNotIn ),
60
+ ("1" , self .assertIn ),
61
+ ("2" , self .assertIn ),
62
+ ]:
63
+ with self .subTest (verbosity = verbosity , get_auto_imports = "models" ):
64
+ p = subprocess .run (
65
+ [
66
+ sys .executable ,
67
+ "-m" ,
68
+ "django" ,
69
+ "shell" ,
70
+ "-c" ,
71
+ "print(globals())" ,
72
+ "-v" ,
73
+ verbosity ,
74
+ ],
75
+ capture_output = True ,
76
+ env = test_environ ,
77
+ text = True ,
78
+ umask = - 1 ,
79
+ )
80
+ assertError (error , p .stdout )
81
+ self .assertNotIn ("Marker" , p .stdout )
82
+
83
+ with self .subTest (verbosity = verbosity , get_auto_imports = "without-models" ):
84
+ with mock .patch (
85
+ "django.core.management.commands.shell.Command.get_auto_imports" ,
86
+ return_value = ["django.urls.resolve" ],
87
+ ):
88
+ p = subprocess .run (
89
+ [
90
+ sys .executable ,
91
+ "-m" ,
92
+ "django" ,
93
+ "shell" ,
94
+ "-c" ,
95
+ "print(globals())" ,
96
+ "-v" ,
97
+ verbosity ,
98
+ ],
99
+ capture_output = True ,
100
+ env = test_environ ,
101
+ text = True ,
102
+ umask = - 1 ,
103
+ )
104
+ assertError (error , p .stdout )
105
+ self .assertNotIn ("resolve" , p .stdout )
106
+
45
107
@unittest .skipIf (
46
108
sys .platform == "win32" , "Windows select() doesn't support file descriptors."
47
109
)
@@ -50,7 +112,7 @@ def test_stdin_read(self, select):
50
112
with captured_stdin () as stdin , captured_stdout () as stdout :
51
113
stdin .write ("print(100)\n " )
52
114
stdin .seek (0 )
53
- call_command ("shell" )
115
+ call_command ("shell" , verbosity = 0 )
54
116
self .assertEqual (stdout .getvalue ().strip (), "100" )
55
117
56
118
@unittest .skipIf (
@@ -62,7 +124,7 @@ def test_stdin_read_globals(self, select):
62
124
with captured_stdin () as stdin , captured_stdout () as stdout :
63
125
stdin .write (self .script_globals )
64
126
stdin .seek (0 )
65
- call_command ("shell" )
127
+ call_command ("shell" , verbosity = 0 )
66
128
self .assertEqual (stdout .getvalue ().strip (), "True" )
67
129
68
130
@unittest .skipIf (
@@ -74,7 +136,7 @@ def test_stdin_read_inline_function_call(self, select):
74
136
with captured_stdin () as stdin , captured_stdout () as stdout :
75
137
stdin .write (self .script_with_inline_function )
76
138
stdin .seek (0 )
77
- call_command ("shell" )
139
+ call_command ("shell" , verbosity = 0 )
78
140
self .assertEqual (stdout .getvalue ().strip (), __version__ )
79
141
80
142
def test_ipython (self ):
0 commit comments