forked from django-extensions/django-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runscript.py
167 lines (128 loc) · 8.02 KB
/
test_runscript.py
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
# -*- coding: utf-8 -*-
import os
import six
import sys
from django.core.management import call_command
from django.test import TestCase, override_settings
from django_extensions.management.commands.runscript import BadCustomDirectoryException, DirPolicyChoices
class RunScriptTests(TestCase):
def setUp(self):
sys.stdout = six.StringIO()
sys.stderr = six.StringIO()
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):
call_command('runscript', 'non_existent_script', verbosity=2)
self.assertIn("No (valid) module for script 'non_existent_script' found", sys.stdout.getvalue())
def test_prints_nothing_for_nonexistent_script_when_silent(self):
call_command('runscript', 'non_existent_script', silent=True)
self.assertEqual("", sys.stdout.getvalue())
def test_doesnt_print_exception_for_nonexistent_script_when_no_traceback(self):
call_command('runscript', 'non_existent_script', no_traceback=True)
self.assertEqual("", sys.stderr.getvalue())
class InvalidImportScriptsTests(RunScriptTests):
def test_prints_additional_info_on_nonexistent_script_by_default(self):
call_command('runscript', '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())
def test_prints_import_error_on_script_with_invalid_imports_by_default(self):
call_command('runscript', 'invalid_import_script')
self.assertIn("Cannot import module 'tests.testapp.scripts.invalid_import_script'", sys.stdout.getvalue())
self.assertRegexpMatches(sys.stdout.getvalue(), 'No module named (\')?(invalidpackage)\1?')
class InvalidScriptsTests(RunScriptTests):
def test_raises_error_message_on_invalid_script_by_default(self):
with self.assertRaises(Exception):
call_command('runscript', 'error_script')
self.assertIn("Exception while running run() in", sys.stdout.getvalue())
def test_prints_nothing_for_invalid_script_when_silent(self):
call_command('runscript', 'error_script', silent=True)
self.assertEqual("", sys.stdout.getvalue())
def test_doesnt_print_exception_for_nonexistent_script_when_no_traceback(self):
call_command('runscript', 'error_script', no_traceback=True)
self.assertEqual("", sys.stderr.getvalue())
self.assertIn("Exception while running run() in", sys.stdout.getvalue())
class RunFunctionTests(RunScriptTests):
def test_prints_error_message_for_script_without_run(self):
call_command('runscript', '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())
def test_prints_additional_info_for_script__run_extra_verbosity(self):
call_command('runscript', '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())
def test_prints_nothing_for_script_without_run(self):
call_command('runscript', '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(ChangingDirectoryTests, self).setUp()
os.chdir(project_path)
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.assertRaisesRegexp(
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())