tests.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.management import BaseCommand, CommandError, find_commands
  8. from django.core.management.utils import (
  9. find_command, get_random_secret_key, is_ignored_path,
  10. normalize_path_patterns, popen_wrapper,
  11. )
  12. from django.db import connection
  13. from django.test import SimpleTestCase, override_settings
  14. from django.test.utils import captured_stderr, extend_sys_path
  15. from django.utils import translation
  16. from .management.commands import dance
  17. # A minimal set of apps to avoid system checks running on all apps.
  18. @override_settings(
  19. INSTALLED_APPS=[
  20. 'django.contrib.auth',
  21. 'django.contrib.contenttypes',
  22. 'user_commands',
  23. ],
  24. )
  25. class CommandTests(SimpleTestCase):
  26. def test_command(self):
  27. out = StringIO()
  28. management.call_command('dance', stdout=out)
  29. self.assertIn("I don't feel like dancing Rock'n'Roll.\n", out.getvalue())
  30. def test_command_style(self):
  31. out = StringIO()
  32. management.call_command('dance', style='Jive', stdout=out)
  33. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  34. # Passing options as arguments also works (thanks argparse)
  35. management.call_command('dance', '--style', 'Jive', stdout=out)
  36. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  37. def test_language_preserved(self):
  38. out = StringIO()
  39. with translation.override('fr'):
  40. management.call_command('dance', stdout=out)
  41. self.assertEqual(translation.get_language(), 'fr')
  42. def test_explode(self):
  43. """ An unknown command raises CommandError """
  44. with self.assertRaisesMessage(CommandError, "Unknown command: 'explode'"):
  45. management.call_command(('explode',))
  46. def test_system_exit(self):
  47. """ Exception raised in a command should raise CommandError with
  48. call_command, but SystemExit when run from command line
  49. """
  50. with self.assertRaises(CommandError):
  51. management.call_command('dance', example="raise")
  52. dance.Command.requires_system_checks = False
  53. try:
  54. with captured_stderr() as stderr, self.assertRaises(SystemExit):
  55. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  56. finally:
  57. dance.Command.requires_system_checks = True
  58. self.assertIn("CommandError", stderr.getvalue())
  59. def test_no_translations_deactivate_translations(self):
  60. """
  61. When the Command handle method is decorated with @no_translations,
  62. translations are deactivated inside the command.
  63. """
  64. current_locale = translation.get_language()
  65. with translation.override('pl'):
  66. result = management.call_command('no_translations', stdout=StringIO())
  67. self.assertIsNone(result)
  68. self.assertEqual(translation.get_language(), current_locale)
  69. def test_find_command_without_PATH(self):
  70. """
  71. find_command should still work when the PATH environment variable
  72. doesn't exist (#22256).
  73. """
  74. current_path = os.environ.pop('PATH', None)
  75. try:
  76. self.assertIsNone(find_command('_missing_'))
  77. finally:
  78. if current_path is not None:
  79. os.environ['PATH'] = current_path
  80. def test_discover_commands_in_eggs(self):
  81. """
  82. Management commands can also be loaded from Python eggs.
  83. """
  84. egg_dir = '%s/eggs' % os.path.dirname(__file__)
  85. egg_name = '%s/basic.egg' % egg_dir
  86. with extend_sys_path(egg_name):
  87. with self.settings(INSTALLED_APPS=['commandegg']):
  88. cmds = find_commands(os.path.join(apps.get_app_config('commandegg').path, 'management'))
  89. self.assertEqual(cmds, ['eggcommand'])
  90. def test_call_command_option_parsing(self):
  91. """
  92. When passing the long option name to call_command, the available option
  93. key is the option dest name (#22985).
  94. """
  95. out = StringIO()
  96. management.call_command('dance', stdout=out, opt_3=True)
  97. self.assertIn("option3", out.getvalue())
  98. self.assertNotIn("opt_3", out.getvalue())
  99. self.assertNotIn("opt-3", out.getvalue())
  100. def test_call_command_option_parsing_non_string_arg(self):
  101. """
  102. It should be possible to pass non-string arguments to call_command.
  103. """
  104. out = StringIO()
  105. management.call_command('dance', 1, verbosity=0, stdout=out)
  106. self.assertIn("You passed 1 as a positional argument.", out.getvalue())
  107. def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
  108. out = StringIO()
  109. management.call_command('hal', "--empty", stdout=out)
  110. self.assertIn("Dave, I can't do that.\n", out.getvalue())
  111. def test_calling_command_with_app_labels_and_parameters_should_be_ok(self):
  112. out = StringIO()
  113. management.call_command('hal', 'myapp', "--verbosity", "3", stdout=out)
  114. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  115. def test_calling_command_with_parameters_and_app_labels_at_the_end_should_be_ok(self):
  116. out = StringIO()
  117. management.call_command('hal', "--verbosity", "3", "myapp", stdout=out)
  118. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  119. def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self):
  120. with self.assertRaises(CommandError):
  121. management.call_command('hal', stdout=StringIO())
  122. def test_output_transaction(self):
  123. output = management.call_command('transaction', stdout=StringIO(), no_color=True)
  124. self.assertTrue(output.strip().startswith(connection.ops.start_transaction_sql()))
  125. self.assertTrue(output.strip().endswith(connection.ops.end_transaction_sql()))
  126. def test_call_command_no_checks(self):
  127. """
  128. By default, call_command should not trigger the check framework, unless
  129. specifically asked.
  130. """
  131. self.counter = 0
  132. def patched_check(self_, **kwargs):
  133. self.counter += 1
  134. saved_check = BaseCommand.check
  135. BaseCommand.check = patched_check
  136. try:
  137. management.call_command("dance", verbosity=0)
  138. self.assertEqual(self.counter, 0)
  139. management.call_command("dance", verbosity=0, skip_checks=False)
  140. self.assertEqual(self.counter, 1)
  141. finally:
  142. BaseCommand.check = saved_check
  143. def test_check_migrations(self):
  144. requires_migrations_checks = dance.Command.requires_migrations_checks
  145. self.assertIs(requires_migrations_checks, False)
  146. try:
  147. with mock.patch.object(BaseCommand, 'check_migrations') as check_migrations:
  148. management.call_command('dance', verbosity=0)
  149. self.assertFalse(check_migrations.called)
  150. dance.Command.requires_migrations_checks = True
  151. management.call_command('dance', verbosity=0)
  152. self.assertTrue(check_migrations.called)
  153. finally:
  154. dance.Command.requires_migrations_checks = requires_migrations_checks
  155. def test_call_command_unrecognized_option(self):
  156. msg = (
  157. 'Unknown option(s) for dance command: unrecognized. Valid options '
  158. 'are: example, force_color, help, integer, no_color, opt_3, '
  159. 'option3, pythonpath, settings, skip_checks, stderr, stdout, '
  160. 'style, traceback, verbosity, version.'
  161. )
  162. with self.assertRaisesMessage(TypeError, msg):
  163. management.call_command('dance', unrecognized=1)
  164. msg = (
  165. 'Unknown option(s) for dance command: unrecognized, unrecognized2. '
  166. 'Valid options are: example, force_color, help, integer, no_color, '
  167. 'opt_3, option3, pythonpath, settings, skip_checks, stderr, '
  168. 'stdout, style, traceback, verbosity, version.'
  169. )
  170. with self.assertRaisesMessage(TypeError, msg):
  171. management.call_command('dance', unrecognized=1, unrecognized2=1)
  172. def test_call_command_with_required_parameters_in_options(self):
  173. out = StringIO()
  174. management.call_command('required_option', need_me='foo', needme2='bar', stdout=out)
  175. self.assertIn('need_me', out.getvalue())
  176. self.assertIn('needme2', out.getvalue())
  177. def test_call_command_with_required_parameters_in_mixed_options(self):
  178. out = StringIO()
  179. management.call_command('required_option', '--need-me=foo', needme2='bar', stdout=out)
  180. self.assertIn('need_me', out.getvalue())
  181. self.assertIn('needme2', out.getvalue())
  182. def test_command_add_arguments_after_common_arguments(self):
  183. out = StringIO()
  184. management.call_command('common_args', stdout=out)
  185. self.assertIn('Detected that --version already exists', out.getvalue())
  186. def test_subparser(self):
  187. out = StringIO()
  188. management.call_command('subparser', 'foo', 12, stdout=out)
  189. self.assertIn('bar', out.getvalue())
  190. def test_subparser_invalid_option(self):
  191. msg = "Error: invalid choice: 'test' (choose from 'foo')"
  192. with self.assertRaisesMessage(CommandError, msg):
  193. management.call_command('subparser', 'test', 12)
  194. def test_create_parser_kwargs(self):
  195. """BaseCommand.create_parser() passes kwargs to CommandParser."""
  196. epilog = 'some epilog text'
  197. parser = BaseCommand().create_parser('prog_name', 'subcommand', epilog=epilog)
  198. self.assertEqual(parser.epilog, epilog)
  199. class CommandRunTests(AdminScriptTestCase):
  200. """
  201. Tests that need to run by simulating the command line, not by call_command.
  202. """
  203. def test_script_prefix_set_in_commands(self):
  204. self.write_settings('settings.py', apps=['user_commands'], sdict={
  205. 'ROOT_URLCONF': '"user_commands.urls"',
  206. 'FORCE_SCRIPT_NAME': '"/PREFIX/"',
  207. })
  208. out, err = self.run_manage(['reverse_url'])
  209. self.assertNoOutput(err)
  210. self.assertEqual(out.strip(), '/PREFIX/some/url/')
  211. def test_disallowed_abbreviated_options(self):
  212. """
  213. To avoid conflicts with custom options, commands don't allow
  214. abbreviated forms of the --setting and --pythonpath options.
  215. """
  216. self.write_settings('settings.py', apps=['user_commands'])
  217. out, err = self.run_manage(['set_option', '--set', 'foo'])
  218. self.assertNoOutput(err)
  219. self.assertEqual(out.strip(), 'Set foo')
  220. def test_skip_checks(self):
  221. self.write_settings('settings.py', apps=['django.contrib.staticfiles', 'user_commands'], sdict={
  222. # (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or
  223. # list.
  224. 'STATICFILES_DIRS': '"foo"',
  225. })
  226. out, err = self.run_manage(['set_option', '--skip-checks', '--set', 'foo'])
  227. self.assertNoOutput(err)
  228. self.assertEqual(out.strip(), 'Set foo')
  229. class UtilsTests(SimpleTestCase):
  230. def test_no_existent_external_program(self):
  231. msg = 'Error executing a_42_command_that_doesnt_exist_42'
  232. with self.assertRaisesMessage(CommandError, msg):
  233. popen_wrapper(['a_42_command_that_doesnt_exist_42'])
  234. def test_get_random_secret_key(self):
  235. key = get_random_secret_key()
  236. self.assertEqual(len(key), 50)
  237. for char in key:
  238. self.assertIn(char, 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
  239. def test_is_ignored_path_true(self):
  240. patterns = (
  241. ['foo/bar/baz'],
  242. ['baz'],
  243. ['foo/bar/baz'],
  244. ['*/baz'],
  245. ['*'],
  246. ['b?z'],
  247. ['[abc]az'],
  248. ['*/ba[!z]/baz'],
  249. )
  250. for ignore_patterns in patterns:
  251. with self.subTest(ignore_patterns=ignore_patterns):
  252. self.assertIs(is_ignored_path('foo/bar/baz', ignore_patterns=ignore_patterns), True)
  253. def test_is_ignored_path_false(self):
  254. self.assertIs(is_ignored_path('foo/bar/baz', ignore_patterns=['foo/bar/bat', 'bar', 'flub/blub']), False)
  255. def test_normalize_path_patterns_truncates_wildcard_base(self):
  256. expected = [os.path.normcase(p) for p in ['foo/bar', 'bar/*/']]
  257. self.assertEqual(normalize_path_patterns(['foo/bar/*', 'bar/*/']), expected)