From 840c5fa86fa7ead66cb45d6d419ac227f5784bcc Mon Sep 17 00:00:00 2001 From: dzhuang Date: Thu, 3 May 2018 02:39:03 +0800 Subject: [PATCH] Add tests for manage.get_local_test_settings_file --- manage.py | 8 +++----- tests/test_misc.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/manage.py b/manage.py index 9f7f9211e..a47f2ea67 100755 --- a/manage.py +++ b/manage.py @@ -10,15 +10,13 @@ def get_local_test_settings_file(argv): local_settings_dir = os.path.split(argv[0])[0] assert os.path.isfile(os.path.join(local_settings_dir, "manage.py")) - from django.core.management.base import CommandParser + from django.core.management import CommandParser, CommandError parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False) parser.add_argument('--local_test_settings', dest="local_test_settings") - try: - options, args = parser.parse_known_args(argv) - except CommandError: - raise + + options, args = parser.parse_known_args(argv) if options.local_test_settings is None: local_settings_file = "local_settings_example.py" diff --git a/tests/test_misc.py b/tests/test_misc.py index 137fab753..3dea98c9b 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -33,6 +33,7 @@ from django.utils.formats import date_format, get_format from django.utils.dateformat import format from django.utils.translation import ugettext_lazy as _ +from django.core.management import CommandError from course.models import Course from course.views import EditCourseForm @@ -41,6 +42,8 @@ is_maintenance_mode, render_email_template, get_outbound_mail_connection, format_datetime_local) +from manage import get_local_test_settings_file + from tests.base_test_mixins import SingleCourseTestMixin from tests.utils import LocmemBackendTestsMixin, mail, mock from tests.constants import DATE_TIME_PICKER_TIME_FORMAT @@ -578,4 +581,43 @@ def test_attribute_error2(self): # }}} + +class GetLocalTestSettingsFileTest(unittest.TestCase): + """test manage.get_local_test_settings_file""" + + def test_use_default_local_settings_example(self): + self.assertEqual(get_local_test_settings_file( + ["manage.py", "test", "foo"]), "local_settings_example.py") + + def test_error_use_local_settings(self): + """test error when use local_settings.py as test settings""" + with self.assertRaises(CommandError) as cm: + get_local_test_settings_file( + ["manage.py", "test", "--local_test_settings", + "local_settings.py"]) + + self.assertIn( + "Using production local_settings for tests is not " + "allowed due to security reason.", str(cm.exception)) + + def test_error_local_test_setting_file_does_not_exist(self): + """test error when use local_settings.py as test settings""" + invalid_file = "foo/local_test_settings.py" + with self.assertRaises(CommandError) as cm: + get_local_test_settings_file( + ["manage.py", "test", "--local_test_settings", + invalid_file]) + + self.assertIn( + "file '%s' does not exist" % invalid_file, str(cm.exception)) + + def test_custom_local_test_setting_file(self): + settings_file = "foo/local_test_settings.py" + with mock.patch("os.path.isfile") as mock_is_file: + mock_is_file.return_value = True + self.assertEqual(get_local_test_settings_file( + ["manage.py", "test", "foo", + "--local_test_settings", settings_file]), + settings_file) + # vim: foldmethod=marker