1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
# -*- coding: utf-8 -*-
import os
import sys
import importlib
from io import StringIO
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase, override_settings
from django_extensions.management.commands.runscript import (
Command,
BadCustomDirectoryException,
DirPolicyChoices,
)
class RunScriptTests(TestCase):
def setUp(self):
sys.stdout = StringIO()
sys.stderr = StringIO()
def get_command(self):
cmd = Command()
cmd.running_tests = True
return cmd
def test_runs(self):
# lame test...does it run?
call_command("runscript", "sample_script", verbosity=2)
self.assertIn(
"Found script 'tests.testapp.scripts.sample_script'", sys.stdout.getvalue()
)
self.assertIn(
"Running script 'tests.testapp.scripts.sample_script'",
sys.stdout.getvalue(),
)
def test_runs_appconfig(self):
with self.modify_settings(
INSTALLED_APPS={
"append": "tests.testapp.apps.TestAppConfig",
"remove": "tests.testapp",
}
):
call_command("runscript", "sample_script", verbosity=2)
self.assertIn(
"Found script 'tests.testapp.scripts.sample_script'",
sys.stdout.getvalue(),
)
self.assertIn(
"Running script 'tests.testapp.scripts.sample_script'",
sys.stdout.getvalue(),
)
class NonExistentScriptsTests(RunScriptTests):
def test_prints_error_on_nonexistent_script(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "non_existent_script", verbosity=2)
self.assertIn(
"No (valid) module for script 'non_existent_script' found",
sys.stdout.getvalue(),
)
self.assertEqual(cmd.last_exit_code, 1)
def test_prints_nothing_for_nonexistent_script_when_silent(self):
cmd = self.get_command()
call_command(cmd, "non_existent_script", silent=True)
self.assertEqual("", sys.stdout.getvalue())
self.assertEqual(cmd.last_exit_code, 1)
def test_doesnt_print_exception_for_nonexistent_script_when_no_traceback(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "non_existent_script", no_traceback=True)
self.assertEqual("", sys.stderr.getvalue())
self.assertEqual(cmd.last_exit_code, 1)
class InvalidImportScriptsTests(RunScriptTests):
def test_prints_additional_info_on_nonexistent_script_by_default(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "non_existent_script")
self.assertIn(
"No (valid) module for script 'non_existent_script' found",
sys.stdout.getvalue(),
)
self.assertIn(
"Try running with a higher verbosity level like: -v2 or -v3",
sys.stdout.getvalue(),
)
self.assertEqual(cmd.last_exit_code, 1)
def test_prints_import_error_on_script_with_invalid_imports_by_default(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "invalid_import_script")
self.assertIn(
"Cannot import module 'tests.testapp.scripts.invalid_import_script'",
sys.stdout.getvalue(),
)
self.assertRegex(
sys.stdout.getvalue(), "No module named (')?(invalidpackage)\1?"
)
self.assertEqual(cmd.last_exit_code, 1)
def test_prints_import_error_on_script_with_invalid_imports_reliably(self):
cmd = self.get_command()
if hasattr(importlib, "util") and hasattr(importlib.util, "find_spec"):
with self.settings(BASE_DIR=os.path.dirname(os.path.abspath(__file__))):
with self.assertRaises(CommandError):
call_command(cmd, "invalid_import_script")
self.assertIn(
"Cannot import module 'tests.testapp.scripts.invalid_import_script'",
sys.stdout.getvalue(),
)
self.assertRegex(
sys.stdout.getvalue(), "No module named (')?(invalidpackage)\1?"
)
self.assertEqual(cmd.last_exit_code, 1)
class InvalidScriptsTests(RunScriptTests):
def test_raises_error_message_on_invalid_script_by_default(self):
cmd = self.get_command()
with self.assertRaises(Exception):
call_command(cmd, "error_script")
self.assertIn("Exception while running run() in", sys.stdout.getvalue())
def test_prints_nothing_for_invalid_script_when_silent(self):
cmd = self.get_command()
call_command(cmd, "error_script", silent=True)
self.assertEqual(cmd.last_exit_code, 1)
self.assertEqual("", sys.stdout.getvalue())
def test_doesnt_print_exception_for_nonexistent_script_when_no_traceback(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "error_script", no_traceback=True)
self.assertEqual("", sys.stderr.getvalue())
self.assertIn("Exception while running run() in", sys.stdout.getvalue())
self.assertEqual(cmd.last_exit_code, 1)
class RunFunctionTests(RunScriptTests):
def test_prints_error_message_for_script_without_run(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "script_no_run_function")
self.assertIn(
"No (valid) module for script 'script_no_run_function' found",
sys.stdout.getvalue(),
)
self.assertIn(
"Try running with a higher verbosity level like: -v2 or -v3",
sys.stdout.getvalue(),
)
self.assertEqual(cmd.last_exit_code, 1)
def test_prints_additional_info_for_script__run_extra_verbosity(self):
cmd = self.get_command()
with self.assertRaises(CommandError):
call_command(cmd, "script_no_run_function", verbosity=2)
self.assertIn(
"No (valid) module for script 'script_no_run_function' found",
sys.stdout.getvalue(),
)
self.assertIn("Found script", sys.stdout.getvalue())
self.assertEqual(cmd.last_exit_code, 1)
def test_prints_nothing_for_script_without_run(self):
cmd = self.get_command()
call_command(cmd, "script_no_run_function", silent=True)
self.assertEqual("", sys.stdout.getvalue())
project_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
class ChangingDirectoryTests(RunScriptTests):
def setUp(self):
super().setUp()
self.curwd = os.getcwd()
os.chdir(project_path)
def tearDown(self):
super().setUp()
os.chdir(self.curwd)
def _execute_script_with_chdir(
self, dir_policy, start_path, expected_path, chdir=None
):
os.chdir(os.path.join(project_path, *start_path))
expected_path = os.path.join(project_path, *expected_path)
call_command(
"runscript", "directory_checker_script", dir_policy=dir_policy, chdir=chdir
)
output = sys.stdout.getvalue().split("Script called from: ")[1]
self.assertEqual(output, expected_path + "\n")
def test_none_policy_command_run(self):
self._execute_script_with_chdir(DirPolicyChoices.NONE, [], [])
def test_none_policy_command_run_with_chdir(self):
self._execute_script_with_chdir(DirPolicyChoices.NONE, ["tests"], ["tests"])
def test_none_policy_freezing_start_directory(self):
self._execute_script_with_chdir(DirPolicyChoices.NONE, ["tests"], ["tests"])
self._execute_script_with_chdir(DirPolicyChoices.NONE, ["tests"], ["tests"])
def test_root_policy_command_run(self):
self._execute_script_with_chdir(DirPolicyChoices.ROOT, ["tests"], [])
def test_each_policy_command_run(self):
os.chdir(os.path.join(project_path, "tests"))
call_command(
"runscript",
"directory_checker_script",
"other_directory_checker_script",
dir_policy=DirPolicyChoices.EACH,
)
output = sys.stdout.getvalue()
first_output = output.split("Script called from: ")[1].split(
"Cannot import module "
)[0]
self.assertEqual(
first_output,
os.path.join(project_path, "tests", "testapp", "scripts") + "\n",
)
second_output = output.split("Script called from: ")[2].split(
"Cannot import module "
)[0]
self.assertEqual(
second_output,
os.path.join(
project_path, "tests", "testapp_with_no_models_file", "scripts"
)
+ "\n",
)
def test_chdir_specified(self):
execution_path = os.path.join(project_path, "django_extensions", "management")
self._execute_script_with_chdir(
DirPolicyChoices.ROOT,
["tests"],
["django_extensions", "management"],
chdir=execution_path,
)
@override_settings(RUNSCRIPT_CHDIR=os.path.join(project_path, "tests"))
def test_policy_from_cli_and_chdir_from_settings(self):
self._execute_script_with_chdir(DirPolicyChoices.ROOT, ["tests"], [])
@override_settings(
RUNSCRIPT_CHDIR=os.path.join(project_path, "django_extensions", "management"),
RUNSCRIPT_CHDIR_POLICY=DirPolicyChoices.ROOT,
)
def test_chdir_from_settings_and_policy_from_settings(self):
self._execute_script_with_chdir(
None, ["tests"], ["django_extensions", "management"]
)
@override_settings(RUNSCRIPT_CHDIR_POLICY=DirPolicyChoices.EACH)
def test_policy_from_settings(self):
self._execute_script_with_chdir(
None, ["tests"], ["tests", "testapp", "scripts"]
)
@override_settings(RUNSCRIPT_CHDIR=os.path.join(project_path, "tests"))
def test_chdir_django_settings(self):
self._execute_script_with_chdir(None, [], ["tests"])
@override_settings(RUNSCRIPT_CHDIR="bad path")
def test_custom_policy_django_settings_bad_path(self):
with self.assertRaisesRegex(
BadCustomDirectoryException,
"bad path is not a directory! If --dir-policy is custom than you must set "
"correct directory in --dir option or in settings.RUNSCRIPT_CHDIR",
):
self._execute_script_with_chdir(None, [], ["tests"])
def test_skip_printing_modules_which_does_not_exist(self):
call_command("runscript", "directory_checker_script")
self.assertNotIn("No module named", sys.stdout.getvalue())
self.assertNotIn("No module named", sys.stderr.getvalue())
|