tests.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import os
  2. from django.apps import apps
  3. from django.core import management
  4. from django.core.management import BaseCommand, CommandError, find_commands
  5. from django.core.management.utils import find_command, popen_wrapper
  6. from django.db import connection
  7. from django.test import SimpleTestCase, override_settings
  8. from django.test.utils import captured_stderr, extend_sys_path
  9. from django.utils import translation
  10. from django.utils._os import upath
  11. from django.utils.six import StringIO
  12. # A minimal set of apps to avoid system checks running on all apps.
  13. @override_settings(
  14. INSTALLED_APPS=[
  15. 'django.contrib.auth',
  16. 'django.contrib.contenttypes',
  17. 'user_commands',
  18. ],
  19. )
  20. class CommandTests(SimpleTestCase):
  21. def test_command(self):
  22. out = StringIO()
  23. management.call_command('dance', stdout=out)
  24. self.assertIn("I don't feel like dancing Rock'n'Roll.\n", out.getvalue())
  25. def test_command_style(self):
  26. out = StringIO()
  27. management.call_command('dance', style='Jive', stdout=out)
  28. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  29. # Passing options as arguments also works (thanks argparse)
  30. management.call_command('dance', '--style', 'Jive', stdout=out)
  31. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  32. def test_language_preserved(self):
  33. out = StringIO()
  34. with translation.override('fr'):
  35. management.call_command('dance', stdout=out)
  36. self.assertEqual(translation.get_language(), 'fr')
  37. def test_explode(self):
  38. """ Test that an unknown command raises CommandError """
  39. self.assertRaises(CommandError, management.call_command, ('explode',))
  40. def test_system_exit(self):
  41. """ Exception raised in a command should raise CommandError with
  42. call_command, but SystemExit when run from command line
  43. """
  44. with self.assertRaises(CommandError):
  45. management.call_command('dance', example="raise")
  46. with captured_stderr() as stderr, self.assertRaises(SystemExit):
  47. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  48. self.assertIn("CommandError", stderr.getvalue())
  49. def test_deactivate_locale_set(self):
  50. # Deactivate translation when set to true
  51. out = StringIO()
  52. with translation.override('pl'):
  53. management.call_command('leave_locale_alone_false', stdout=out)
  54. self.assertEqual(out.getvalue(), "")
  55. def test_configured_locale_preserved(self):
  56. # Leaves locale from settings when set to false
  57. out = StringIO()
  58. with translation.override('pl'):
  59. management.call_command('leave_locale_alone_true', stdout=out)
  60. self.assertEqual(out.getvalue(), "pl\n")
  61. def test_find_command_without_PATH(self):
  62. """
  63. find_command should still work when the PATH environment variable
  64. doesn't exist (#22256).
  65. """
  66. current_path = os.environ.pop('PATH', None)
  67. try:
  68. self.assertIsNone(find_command('_missing_'))
  69. finally:
  70. if current_path is not None:
  71. os.environ['PATH'] = current_path
  72. def test_discover_commands_in_eggs(self):
  73. """
  74. Test that management commands can also be loaded from Python eggs.
  75. """
  76. egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
  77. egg_name = '%s/basic.egg' % egg_dir
  78. with extend_sys_path(egg_name):
  79. with self.settings(INSTALLED_APPS=['commandegg']):
  80. cmds = find_commands(os.path.join(apps.get_app_config('commandegg').path, 'management'))
  81. self.assertEqual(cmds, ['eggcommand'])
  82. def test_call_command_option_parsing(self):
  83. """
  84. When passing the long option name to call_command, the available option
  85. key is the option dest name (#22985).
  86. """
  87. out = StringIO()
  88. management.call_command('dance', stdout=out, opt_3=True)
  89. self.assertIn("option3", out.getvalue())
  90. self.assertNotIn("opt_3", out.getvalue())
  91. self.assertNotIn("opt-3", out.getvalue())
  92. def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
  93. out = StringIO()
  94. management.call_command('hal', "--empty", stdout=out)
  95. self.assertIn("Dave, I can't do that.\n", out.getvalue())
  96. def test_calling_command_with_app_labels_and_parameters_should_be_ok(self):
  97. out = StringIO()
  98. management.call_command('hal', 'myapp', "--verbosity", "3", stdout=out)
  99. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  100. def test_calling_command_with_parameters_and_app_labels_at_the_end_should_be_ok(self):
  101. out = StringIO()
  102. management.call_command('hal', "--verbosity", "3", "myapp", stdout=out)
  103. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  104. def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self):
  105. out = StringIO()
  106. with self.assertRaises(CommandError):
  107. management.call_command('hal', stdout=out)
  108. def test_output_transaction(self):
  109. out = StringIO()
  110. management.call_command('transaction', stdout=out, no_color=True)
  111. output = out.getvalue().strip()
  112. self.assertTrue(output.startswith(connection.ops.start_transaction_sql()))
  113. self.assertTrue(output.endswith(connection.ops.end_transaction_sql()))
  114. def test_call_command_no_checks(self):
  115. """
  116. By default, call_command should not trigger the check framework, unless
  117. specifically asked.
  118. """
  119. self.counter = 0
  120. def patched_check(self_, **kwargs):
  121. self.counter = self.counter + 1
  122. saved_check = BaseCommand.check
  123. BaseCommand.check = patched_check
  124. try:
  125. management.call_command("dance", verbosity=0)
  126. self.assertEqual(self.counter, 0)
  127. management.call_command("dance", verbosity=0, skip_checks=False)
  128. self.assertEqual(self.counter, 1)
  129. finally:
  130. BaseCommand.check = saved_check
  131. class UtilsTests(SimpleTestCase):
  132. def test_no_existent_external_program(self):
  133. self.assertRaises(CommandError, popen_wrapper, ['a_42_command_that_doesnt_exist_42'])