2
0

tests.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import os
  2. from io import StringIO
  3. from unittest import mock
  4. from admin_scripts.tests import AdminScriptTestCase
  5. from django.apps import apps
  6. from django.core import management
  7. from django.core.checks import Tags
  8. from django.core.management import BaseCommand, CommandError, find_commands
  9. from django.core.management.utils import (
  10. find_command, get_random_secret_key, is_ignored_path,
  11. normalize_path_patterns, popen_wrapper,
  12. )
  13. from django.db import connection
  14. from django.test import SimpleTestCase, override_settings
  15. from django.test.utils import captured_stderr, extend_sys_path, ignore_warnings
  16. from django.utils import translation
  17. from django.utils.deprecation import RemovedInDjango41Warning
  18. from .management.commands import dance
  19. # A minimal set of apps to avoid system checks running on all apps.
  20. @override_settings(
  21. INSTALLED_APPS=[
  22. 'django.contrib.auth',
  23. 'django.contrib.contenttypes',
  24. 'user_commands',
  25. ],
  26. )
  27. class CommandTests(SimpleTestCase):
  28. def test_command(self):
  29. out = StringIO()
  30. management.call_command('dance', stdout=out)
  31. self.assertIn("I don't feel like dancing Rock'n'Roll.\n", out.getvalue())
  32. def test_command_style(self):
  33. out = StringIO()
  34. management.call_command('dance', style='Jive', stdout=out)
  35. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  36. # Passing options as arguments also works (thanks argparse)
  37. management.call_command('dance', '--style', 'Jive', stdout=out)
  38. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  39. def test_language_preserved(self):
  40. with translation.override('fr'):
  41. management.call_command('dance', verbosity=0)
  42. self.assertEqual(translation.get_language(), 'fr')
  43. def test_explode(self):
  44. """ An unknown command raises CommandError """
  45. with self.assertRaisesMessage(CommandError, "Unknown command: 'explode'"):
  46. management.call_command(('explode',))
  47. def test_system_exit(self):
  48. """ Exception raised in a command should raise CommandError with
  49. call_command, but SystemExit when run from command line
  50. """
  51. with self.assertRaises(CommandError) as cm:
  52. management.call_command('dance', example="raise")
  53. self.assertEqual(cm.exception.returncode, 3)
  54. dance.Command.requires_system_checks = []
  55. try:
  56. with captured_stderr() as stderr, self.assertRaises(SystemExit) as cm:
  57. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  58. self.assertEqual(cm.exception.code, 3)
  59. finally:
  60. dance.Command.requires_system_checks = '__all__'
  61. self.assertIn("CommandError", stderr.getvalue())
  62. def test_no_translations_deactivate_translations(self):
  63. """
  64. When the Command handle method is decorated with @no_translations,
  65. translations are deactivated inside the command.
  66. """
  67. current_locale = translation.get_language()
  68. with translation.override('pl'):
  69. result = management.call_command('no_translations')
  70. self.assertIsNone(result)
  71. self.assertEqual(translation.get_language(), current_locale)
  72. def test_find_command_without_PATH(self):
  73. """
  74. find_command should still work when the PATH environment variable
  75. doesn't exist (#22256).
  76. """
  77. current_path = os.environ.pop('PATH', None)
  78. try:
  79. self.assertIsNone(find_command('_missing_'))
  80. finally:
  81. if current_path is not None:
  82. os.environ['PATH'] = current_path
  83. def test_discover_commands_in_eggs(self):
  84. """
  85. Management commands can also be loaded from Python eggs.
  86. """
  87. egg_dir = '%s/eggs' % os.path.dirname(__file__)
  88. egg_name = '%s/basic.egg' % egg_dir
  89. with extend_sys_path(egg_name):
  90. with self.settings(INSTALLED_APPS=['commandegg']):
  91. cmds = find_commands(os.path.join(apps.get_app_config('commandegg').path, 'management'))
  92. self.assertEqual(cmds, ['eggcommand'])
  93. def test_call_command_option_parsing(self):
  94. """
  95. When passing the long option name to call_command, the available option
  96. key is the option dest name (#22985).
  97. """
  98. out = StringIO()
  99. management.call_command('dance', stdout=out, opt_3=True)
  100. self.assertIn("option3", out.getvalue())
  101. self.assertNotIn("opt_3", out.getvalue())
  102. self.assertNotIn("opt-3", out.getvalue())
  103. def test_call_command_option_parsing_non_string_arg(self):
  104. """
  105. It should be possible to pass non-string arguments to call_command.
  106. """
  107. out = StringIO()
  108. management.call_command('dance', 1, verbosity=0, stdout=out)
  109. self.assertIn("You passed 1 as a positional argument.", out.getvalue())
  110. def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
  111. out = StringIO()
  112. management.call_command('hal', "--empty", stdout=out)
  113. self.assertEqual(out.getvalue(), "\nDave, I can't do that.\n")
  114. def test_calling_command_with_app_labels_and_parameters_should_be_ok(self):
  115. out = StringIO()
  116. management.call_command('hal', 'myapp', "--verbosity", "3", stdout=out)
  117. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  118. def test_calling_command_with_parameters_and_app_labels_at_the_end_should_be_ok(self):
  119. out = StringIO()
  120. management.call_command('hal', "--verbosity", "3", "myapp", stdout=out)
  121. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  122. def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self):
  123. with self.assertRaises(CommandError):
  124. management.call_command('hal')
  125. def test_output_transaction(self):
  126. output = management.call_command('transaction', stdout=StringIO(), no_color=True)
  127. self.assertTrue(output.strip().startswith(connection.ops.start_transaction_sql()))
  128. self.assertTrue(output.strip().endswith(connection.ops.end_transaction_sql()))
  129. def test_call_command_no_checks(self):
  130. """
  131. By default, call_command should not trigger the check framework, unless
  132. specifically asked.
  133. """
  134. self.counter = 0
  135. def patched_check(self_, **kwargs):
  136. self.counter += 1
  137. self.kwargs = kwargs
  138. saved_check = BaseCommand.check
  139. BaseCommand.check = patched_check
  140. try:
  141. management.call_command("dance", verbosity=0)
  142. self.assertEqual(self.counter, 0)
  143. management.call_command("dance", verbosity=0, skip_checks=False)
  144. self.assertEqual(self.counter, 1)
  145. self.assertEqual(self.kwargs, {})
  146. finally:
  147. BaseCommand.check = saved_check
  148. def test_requires_system_checks_empty(self):
  149. with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check:
  150. management.call_command('no_system_checks')
  151. self.assertIs(mocked_check.called, False)
  152. def test_requires_system_checks_specific(self):
  153. with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check:
  154. management.call_command('specific_system_checks')
  155. mocked_check.called_once_with(tags=[Tags.staticfiles, Tags.models])
  156. def test_requires_system_checks_invalid(self):
  157. class Command(BaseCommand):
  158. requires_system_checks = 'x'
  159. msg = 'requires_system_checks must be a list or tuple.'
  160. with self.assertRaisesMessage(TypeError, msg):
  161. Command()
  162. def test_check_migrations(self):
  163. requires_migrations_checks = dance.Command.requires_migrations_checks
  164. self.assertIs(requires_migrations_checks, False)
  165. try:
  166. with mock.patch.object(BaseCommand, 'check_migrations') as check_migrations:
  167. management.call_command('dance', verbosity=0)
  168. self.assertFalse(check_migrations.called)
  169. dance.Command.requires_migrations_checks = True
  170. management.call_command('dance', verbosity=0)
  171. self.assertTrue(check_migrations.called)
  172. finally:
  173. dance.Command.requires_migrations_checks = requires_migrations_checks
  174. def test_call_command_unrecognized_option(self):
  175. msg = (
  176. 'Unknown option(s) for dance command: unrecognized. Valid options '
  177. 'are: example, force_color, help, integer, no_color, opt_3, '
  178. 'option3, pythonpath, settings, skip_checks, stderr, stdout, '
  179. 'style, traceback, verbosity, version.'
  180. )
  181. with self.assertRaisesMessage(TypeError, msg):
  182. management.call_command('dance', unrecognized=1)
  183. msg = (
  184. 'Unknown option(s) for dance command: unrecognized, unrecognized2. '
  185. 'Valid options are: example, force_color, help, integer, no_color, '
  186. 'opt_3, option3, pythonpath, settings, skip_checks, stderr, '
  187. 'stdout, style, traceback, verbosity, version.'
  188. )
  189. with self.assertRaisesMessage(TypeError, msg):
  190. management.call_command('dance', unrecognized=1, unrecognized2=1)
  191. def test_call_command_with_required_parameters_in_options(self):
  192. out = StringIO()
  193. management.call_command('required_option', need_me='foo', needme2='bar', stdout=out)
  194. self.assertIn('need_me', out.getvalue())
  195. self.assertIn('needme2', out.getvalue())
  196. def test_call_command_with_required_parameters_in_mixed_options(self):
  197. out = StringIO()
  198. management.call_command('required_option', '--need-me=foo', needme2='bar', stdout=out)
  199. self.assertIn('need_me', out.getvalue())
  200. self.assertIn('needme2', out.getvalue())
  201. def test_command_add_arguments_after_common_arguments(self):
  202. out = StringIO()
  203. management.call_command('common_args', stdout=out)
  204. self.assertIn('Detected that --version already exists', out.getvalue())
  205. def test_mutually_exclusive_group_required_options(self):
  206. out = StringIO()
  207. management.call_command('mutually_exclusive_required', foo_id=1, stdout=out)
  208. self.assertIn('foo_id', out.getvalue())
  209. management.call_command('mutually_exclusive_required', foo_name='foo', stdout=out)
  210. self.assertIn('foo_name', out.getvalue())
  211. msg = (
  212. 'Error: one of the arguments --foo-id --foo-name --foo-list '
  213. '--append_const --const --count --flag_false --flag_true is '
  214. 'required'
  215. )
  216. with self.assertRaisesMessage(CommandError, msg):
  217. management.call_command('mutually_exclusive_required', stdout=out)
  218. def test_mutually_exclusive_group_required_const_options(self):
  219. tests = [
  220. ('append_const', [42]),
  221. ('const', 31),
  222. ('count', 1),
  223. ('flag_false', False),
  224. ('flag_true', True),
  225. ]
  226. for arg, value in tests:
  227. out = StringIO()
  228. expected_output = '%s=%s' % (arg, value)
  229. with self.subTest(arg=arg):
  230. management.call_command(
  231. 'mutually_exclusive_required',
  232. '--%s' % arg,
  233. stdout=out,
  234. )
  235. self.assertIn(expected_output, out.getvalue())
  236. out.truncate(0)
  237. management.call_command(
  238. 'mutually_exclusive_required',
  239. **{arg: value, 'stdout': out},
  240. )
  241. self.assertIn(expected_output, out.getvalue())
  242. def test_required_list_option(self):
  243. tests = [
  244. (('--foo-list', [1, 2]), {}),
  245. ((), {'foo_list': [1, 2]}),
  246. ]
  247. for command in ['mutually_exclusive_required', 'required_list_option']:
  248. for args, kwargs in tests:
  249. with self.subTest(command=command, args=args, kwargs=kwargs):
  250. out = StringIO()
  251. management.call_command(
  252. command,
  253. *args,
  254. **{**kwargs, 'stdout': out},
  255. )
  256. self.assertIn('foo_list=[1, 2]', out.getvalue())
  257. def test_required_const_options(self):
  258. args = {
  259. 'append_const': [42],
  260. 'const': 31,
  261. 'count': 1,
  262. 'flag_false': False,
  263. 'flag_true': True,
  264. }
  265. expected_output = '\n'.join(
  266. '%s=%s' % (arg, value) for arg, value in args.items()
  267. )
  268. out = StringIO()
  269. management.call_command(
  270. 'required_constant_option',
  271. '--append_const',
  272. '--const',
  273. '--count',
  274. '--flag_false',
  275. '--flag_true',
  276. stdout=out,
  277. )
  278. self.assertIn(expected_output, out.getvalue())
  279. out.truncate(0)
  280. management.call_command('required_constant_option', **{**args, 'stdout': out})
  281. self.assertIn(expected_output, out.getvalue())
  282. def test_subparser(self):
  283. out = StringIO()
  284. management.call_command('subparser', 'foo', 12, stdout=out)
  285. self.assertIn('bar', out.getvalue())
  286. def test_subparser_dest_args(self):
  287. out = StringIO()
  288. management.call_command('subparser_dest', 'foo', bar=12, stdout=out)
  289. self.assertIn('bar', out.getvalue())
  290. def test_subparser_dest_required_args(self):
  291. out = StringIO()
  292. management.call_command('subparser_required', 'foo_1', 'foo_2', bar=12, stdout=out)
  293. self.assertIn('bar', out.getvalue())
  294. def test_subparser_invalid_option(self):
  295. msg = "Error: invalid choice: 'test' (choose from 'foo')"
  296. with self.assertRaisesMessage(CommandError, msg):
  297. management.call_command('subparser', 'test', 12)
  298. msg = 'Error: the following arguments are required: subcommand'
  299. with self.assertRaisesMessage(CommandError, msg):
  300. management.call_command('subparser_dest', subcommand='foo', bar=12)
  301. def test_create_parser_kwargs(self):
  302. """BaseCommand.create_parser() passes kwargs to CommandParser."""
  303. epilog = 'some epilog text'
  304. parser = BaseCommand().create_parser('prog_name', 'subcommand', epilog=epilog)
  305. self.assertEqual(parser.epilog, epilog)
  306. def test_outputwrapper_flush(self):
  307. out = StringIO()
  308. with mock.patch.object(out, 'flush') as mocked_flush:
  309. management.call_command('outputwrapper', stdout=out)
  310. self.assertIn('Working...', out.getvalue())
  311. self.assertIs(mocked_flush.called, True)
  312. class CommandRunTests(AdminScriptTestCase):
  313. """
  314. Tests that need to run by simulating the command line, not by call_command.
  315. """
  316. def test_script_prefix_set_in_commands(self):
  317. self.write_settings('settings.py', apps=['user_commands'], sdict={
  318. 'ROOT_URLCONF': '"user_commands.urls"',
  319. 'FORCE_SCRIPT_NAME': '"/PREFIX/"',
  320. })
  321. out, err = self.run_manage(['reverse_url'])
  322. self.assertNoOutput(err)
  323. self.assertEqual(out.strip(), '/PREFIX/some/url/')
  324. def test_disallowed_abbreviated_options(self):
  325. """
  326. To avoid conflicts with custom options, commands don't allow
  327. abbreviated forms of the --setting and --pythonpath options.
  328. """
  329. self.write_settings('settings.py', apps=['user_commands'])
  330. out, err = self.run_manage(['set_option', '--set', 'foo'])
  331. self.assertNoOutput(err)
  332. self.assertEqual(out.strip(), 'Set foo')
  333. def test_skip_checks(self):
  334. self.write_settings('settings.py', apps=['django.contrib.staticfiles', 'user_commands'], sdict={
  335. # (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or
  336. # list.
  337. 'STATICFILES_DIRS': '"foo"',
  338. })
  339. out, err = self.run_manage(['set_option', '--skip-checks', '--set', 'foo'])
  340. self.assertNoOutput(err)
  341. self.assertEqual(out.strip(), 'Set foo')
  342. class UtilsTests(SimpleTestCase):
  343. def test_no_existent_external_program(self):
  344. msg = 'Error executing a_42_command_that_doesnt_exist_42'
  345. with self.assertRaisesMessage(CommandError, msg):
  346. popen_wrapper(['a_42_command_that_doesnt_exist_42'])
  347. def test_get_random_secret_key(self):
  348. key = get_random_secret_key()
  349. self.assertEqual(len(key), 50)
  350. for char in key:
  351. self.assertIn(char, 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
  352. def test_is_ignored_path_true(self):
  353. patterns = (
  354. ['foo/bar/baz'],
  355. ['baz'],
  356. ['foo/bar/baz'],
  357. ['*/baz'],
  358. ['*'],
  359. ['b?z'],
  360. ['[abc]az'],
  361. ['*/ba[!z]/baz'],
  362. )
  363. for ignore_patterns in patterns:
  364. with self.subTest(ignore_patterns=ignore_patterns):
  365. self.assertIs(is_ignored_path('foo/bar/baz', ignore_patterns=ignore_patterns), True)
  366. def test_is_ignored_path_false(self):
  367. self.assertIs(is_ignored_path('foo/bar/baz', ignore_patterns=['foo/bar/bat', 'bar', 'flub/blub']), False)
  368. def test_normalize_path_patterns_truncates_wildcard_base(self):
  369. expected = [os.path.normcase(p) for p in ['foo/bar', 'bar/*/']]
  370. self.assertEqual(normalize_path_patterns(['foo/bar/*', 'bar/*/']), expected)
  371. class DeprecationTests(SimpleTestCase):
  372. def test_requires_system_checks_warning(self):
  373. class Command(BaseCommand):
  374. pass
  375. msg = (
  376. "Using a boolean value for requires_system_checks is deprecated. "
  377. "Use '__all__' instead of True, and [] (an empty list) instead of "
  378. "False."
  379. )
  380. for value in [False, True]:
  381. Command.requires_system_checks = value
  382. with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
  383. Command()
  384. @ignore_warnings(category=RemovedInDjango41Warning)
  385. def test_requires_system_checks_true(self):
  386. class Command(BaseCommand):
  387. requires_system_checks = True
  388. def handle(self, *args, **options):
  389. pass
  390. command = Command()
  391. with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check:
  392. management.call_command(command, skip_checks=False)
  393. mocked_check.assert_called_once_with()
  394. @ignore_warnings(category=RemovedInDjango41Warning)
  395. def test_requires_system_checks_false(self):
  396. class Command(BaseCommand):
  397. requires_system_checks = False
  398. def handle(self, *args, **options):
  399. pass
  400. command = Command()
  401. with mock.patch('django.core.management.base.BaseCommand.check') as mocked_check:
  402. management.call_command(command)
  403. self.assertIs(mocked_check.called, False)