PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/admin_scripts/tests.py

https://gitlab.com/Guy1394/django
Python | 1102 lines | 1055 code | 21 blank | 26 comment | 78 complexity | cbce6512a768a7b8f562dc78f1e06955 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. A series of tests to establish that the command-line management tools work as
  4. advertised - especially with regards to the handling of the
  5. DJANGO_SETTINGS_MODULE and default settings.py files.
  6. """
  7. from __future__ import unicode_literals
  8. import codecs
  9. import os
  10. import re
  11. import shutil
  12. import socket
  13. import subprocess
  14. import sys
  15. import tempfile
  16. import unittest
  17. import django
  18. from django import conf, get_version
  19. from django.conf import settings
  20. from django.core.management import (
  21. BaseCommand, CommandError, call_command, color,
  22. )
  23. from django.db import ConnectionHandler
  24. from django.db.migrations.exceptions import MigrationSchemaMissing
  25. from django.db.migrations.recorder import MigrationRecorder
  26. from django.test import (
  27. LiveServerTestCase, SimpleTestCase, TestCase, mock, override_settings,
  28. )
  29. from django.test.runner import DiscoverRunner
  30. from django.utils._os import npath, upath
  31. from django.utils.encoding import force_text
  32. from django.utils.six import PY2, PY3, StringIO
  33. custom_templates_dir = os.path.join(os.path.dirname(upath(__file__)), 'custom_templates')
  34. SYSTEM_CHECK_MSG = 'System check identified no issues'
  35. class AdminScriptTestCase(unittest.TestCase):
  36. @classmethod
  37. def setUpClass(cls):
  38. super(AdminScriptTestCase, cls).setUpClass()
  39. cls.test_dir = os.path.realpath(os.path.join(
  40. tempfile.gettempdir(),
  41. cls.__name__,
  42. 'test_project',
  43. ))
  44. if not os.path.exists(cls.test_dir):
  45. os.makedirs(cls.test_dir)
  46. with open(os.path.join(cls.test_dir, '__init__.py'), 'w'):
  47. pass
  48. @classmethod
  49. def tearDownClass(cls):
  50. shutil.rmtree(cls.test_dir)
  51. super(AdminScriptTestCase, cls).tearDownClass()
  52. def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=None):
  53. if is_dir:
  54. settings_dir = os.path.join(self.test_dir, filename)
  55. os.mkdir(settings_dir)
  56. settings_file_path = os.path.join(settings_dir, '__init__.py')
  57. else:
  58. settings_file_path = os.path.join(self.test_dir, filename)
  59. with open(settings_file_path, 'w') as settings_file:
  60. settings_file.write('# -*- coding: utf-8 -*\n')
  61. settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
  62. if extra:
  63. settings_file.write("%s\n" % extra)
  64. exports = [
  65. 'DATABASES',
  66. 'ROOT_URLCONF',
  67. 'SECRET_KEY',
  68. ]
  69. for s in exports:
  70. if hasattr(settings, s):
  71. o = getattr(settings, s)
  72. if not isinstance(o, (dict, tuple, list)):
  73. o = "'%s'" % o
  74. settings_file.write("%s = %s\n" % (s, o))
  75. if apps is None:
  76. apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts']
  77. settings_file.write("INSTALLED_APPS = %s\n" % apps)
  78. if sdict:
  79. for k, v in sdict.items():
  80. settings_file.write("%s = %s\n" % (k, v))
  81. def remove_settings(self, filename, is_dir=False):
  82. full_name = os.path.join(self.test_dir, filename)
  83. if is_dir:
  84. shutil.rmtree(full_name)
  85. else:
  86. os.remove(full_name)
  87. # Also try to remove the compiled file; if it exists, it could
  88. # mess up later tests that depend upon the .py file not existing
  89. try:
  90. if sys.platform.startswith('java'):
  91. # Jython produces module$py.class files
  92. os.remove(re.sub(r'\.py$', '$py.class', full_name))
  93. else:
  94. # CPython produces module.pyc files
  95. os.remove(full_name + 'c')
  96. except OSError:
  97. pass
  98. # Also remove a __pycache__ directory, if it exists
  99. cache_name = os.path.join(self.test_dir, '__pycache__')
  100. if os.path.isdir(cache_name):
  101. shutil.rmtree(cache_name)
  102. def _ext_backend_paths(self):
  103. """
  104. Returns the paths for any external backend packages.
  105. """
  106. paths = []
  107. first_package_re = re.compile(r'(^[^\.]+)\.')
  108. for backend in settings.DATABASES.values():
  109. result = first_package_re.findall(backend['ENGINE'])
  110. if result and result != ['django']:
  111. backend_pkg = __import__(result[0])
  112. backend_dir = os.path.dirname(backend_pkg.__file__)
  113. paths.append(os.path.dirname(backend_dir))
  114. return paths
  115. def run_test(self, script, args, settings_file=None, apps=None):
  116. base_dir = os.path.dirname(self.test_dir)
  117. # The base dir for Django's tests is one level up.
  118. tests_dir = os.path.dirname(os.path.dirname(upath(__file__)))
  119. # The base dir for Django is one level above the test dir. We don't use
  120. # `import django` to figure that out, so we don't pick up a Django
  121. # from site-packages or similar.
  122. django_dir = os.path.dirname(tests_dir)
  123. ext_backend_base_dirs = self._ext_backend_paths()
  124. # Define a temporary environment for the subprocess
  125. test_environ = os.environ.copy()
  126. if sys.platform.startswith('java'):
  127. python_path_var_name = 'JYTHONPATH'
  128. else:
  129. python_path_var_name = 'PYTHONPATH'
  130. old_cwd = os.getcwd()
  131. # Set the test environment
  132. if settings_file:
  133. test_environ['DJANGO_SETTINGS_MODULE'] = str(settings_file)
  134. elif 'DJANGO_SETTINGS_MODULE' in test_environ:
  135. del test_environ['DJANGO_SETTINGS_MODULE']
  136. python_path = [base_dir, django_dir, tests_dir]
  137. python_path.extend(ext_backend_base_dirs)
  138. # Use native strings for better compatibility
  139. test_environ[str(python_path_var_name)] = npath(os.pathsep.join(python_path))
  140. test_environ[str('PYTHONWARNINGS')] = str('')
  141. # Move to the test directory and run
  142. os.chdir(self.test_dir)
  143. out, err = subprocess.Popen([sys.executable, script] + args,
  144. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  145. env=test_environ, universal_newlines=True).communicate()
  146. # Move back to the old working directory
  147. os.chdir(old_cwd)
  148. return out, err
  149. def run_django_admin(self, args, settings_file=None):
  150. script_dir = os.path.abspath(os.path.join(os.path.dirname(upath(django.__file__)), 'bin'))
  151. return self.run_test(os.path.join(script_dir, 'django-admin.py'), args, settings_file)
  152. def run_manage(self, args, settings_file=None):
  153. def safe_remove(path):
  154. try:
  155. os.remove(path)
  156. except OSError:
  157. pass
  158. conf_dir = os.path.dirname(upath(conf.__file__))
  159. template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py')
  160. test_manage_py = os.path.join(self.test_dir, 'manage.py')
  161. shutil.copyfile(template_manage_py, test_manage_py)
  162. with open(test_manage_py, 'r') as fp:
  163. manage_py_contents = fp.read()
  164. manage_py_contents = manage_py_contents.replace(
  165. "{{ project_name }}", "test_project")
  166. with open(test_manage_py, 'w') as fp:
  167. fp.write(manage_py_contents)
  168. self.addCleanup(safe_remove, test_manage_py)
  169. return self.run_test('./manage.py', args, settings_file)
  170. def assertNoOutput(self, stream):
  171. "Utility assertion: assert that the given stream is empty"
  172. self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
  173. def assertOutput(self, stream, msg, regex=False):
  174. "Utility assertion: assert that the given message exists in the output"
  175. stream = force_text(stream)
  176. if regex:
  177. self.assertIsNotNone(re.search(msg, stream),
  178. "'%s' does not match actual output text '%s'" % (msg, stream))
  179. else:
  180. self.assertIn(msg, stream, "'%s' does not match actual output text '%s'" % (msg, stream))
  181. def assertNotInOutput(self, stream, msg):
  182. "Utility assertion: assert that the given message doesn't exist in the output"
  183. stream = force_text(stream)
  184. self.assertNotIn(msg, stream, "'%s' matches actual output text '%s'" % (msg, stream))
  185. ##########################################################################
  186. # DJANGO ADMIN TESTS
  187. # This first series of test classes checks the environment processing
  188. # of the django-admin.py script
  189. ##########################################################################
  190. class DjangoAdminNoSettings(AdminScriptTestCase):
  191. "A series of tests for django-admin.py when there is no settings.py file."
  192. def test_builtin_command(self):
  193. "no settings: django-admin builtin commands fail with an error when no settings provided"
  194. args = ['check', 'admin_scripts']
  195. out, err = self.run_django_admin(args)
  196. self.assertNoOutput(out)
  197. self.assertOutput(err, 'settings are not configured')
  198. def test_builtin_with_bad_settings(self):
  199. "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  200. args = ['check', '--settings=bad_settings', 'admin_scripts']
  201. out, err = self.run_django_admin(args)
  202. self.assertNoOutput(out)
  203. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  204. def test_builtin_with_bad_environment(self):
  205. "no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  206. args = ['check', 'admin_scripts']
  207. out, err = self.run_django_admin(args, 'bad_settings')
  208. self.assertNoOutput(out)
  209. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  210. class DjangoAdminDefaultSettings(AdminScriptTestCase):
  211. """A series of tests for django-admin.py when using a settings.py file that
  212. contains the test application.
  213. """
  214. def setUp(self):
  215. self.write_settings('settings.py')
  216. def tearDown(self):
  217. self.remove_settings('settings.py')
  218. def test_builtin_command(self):
  219. "default: django-admin builtin commands fail with an error when no settings provided"
  220. args = ['check', 'admin_scripts']
  221. out, err = self.run_django_admin(args)
  222. self.assertNoOutput(out)
  223. self.assertOutput(err, 'settings are not configured')
  224. def test_builtin_with_settings(self):
  225. "default: django-admin builtin commands succeed if settings are provided as argument"
  226. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  227. out, err = self.run_django_admin(args)
  228. self.assertNoOutput(err)
  229. self.assertOutput(out, SYSTEM_CHECK_MSG)
  230. def test_builtin_with_environment(self):
  231. "default: django-admin builtin commands succeed if settings are provided in the environment"
  232. args = ['check', 'admin_scripts']
  233. out, err = self.run_django_admin(args, 'test_project.settings')
  234. self.assertNoOutput(err)
  235. self.assertOutput(out, SYSTEM_CHECK_MSG)
  236. def test_builtin_with_bad_settings(self):
  237. "default: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  238. args = ['check', '--settings=bad_settings', 'admin_scripts']
  239. out, err = self.run_django_admin(args)
  240. self.assertNoOutput(out)
  241. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  242. def test_builtin_with_bad_environment(self):
  243. "default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  244. args = ['check', 'admin_scripts']
  245. out, err = self.run_django_admin(args, 'bad_settings')
  246. self.assertNoOutput(out)
  247. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  248. def test_custom_command(self):
  249. "default: django-admin can't execute user commands if it isn't provided settings"
  250. args = ['noargs_command']
  251. out, err = self.run_django_admin(args)
  252. self.assertNoOutput(out)
  253. self.assertOutput(err, "No Django settings specified")
  254. self.assertOutput(err, "Unknown command: 'noargs_command'")
  255. def test_custom_command_with_settings(self):
  256. "default: django-admin can execute user commands if settings are provided as argument"
  257. args = ['noargs_command', '--settings=test_project.settings']
  258. out, err = self.run_django_admin(args)
  259. self.assertNoOutput(err)
  260. self.assertOutput(out, "EXECUTE: noargs_command")
  261. def test_custom_command_with_environment(self):
  262. "default: django-admin can execute user commands if settings are provided in environment"
  263. args = ['noargs_command']
  264. out, err = self.run_django_admin(args, 'test_project.settings')
  265. self.assertNoOutput(err)
  266. self.assertOutput(out, "EXECUTE: noargs_command")
  267. class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
  268. """A series of tests for django-admin.py when using a settings.py file that
  269. contains the test application specified using a full path.
  270. """
  271. def setUp(self):
  272. self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes',
  273. 'admin_scripts', 'admin_scripts.complex_app'])
  274. def tearDown(self):
  275. self.remove_settings('settings.py')
  276. def test_builtin_command(self):
  277. "fulldefault: django-admin builtin commands fail with an error when no settings provided"
  278. args = ['check', 'admin_scripts']
  279. out, err = self.run_django_admin(args)
  280. self.assertNoOutput(out)
  281. self.assertOutput(err, 'settings are not configured')
  282. def test_builtin_with_settings(self):
  283. "fulldefault: django-admin builtin commands succeed if a settings file is provided"
  284. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  285. out, err = self.run_django_admin(args)
  286. self.assertNoOutput(err)
  287. self.assertOutput(out, SYSTEM_CHECK_MSG)
  288. def test_builtin_with_environment(self):
  289. "fulldefault: django-admin builtin commands succeed if the environment contains settings"
  290. args = ['check', 'admin_scripts']
  291. out, err = self.run_django_admin(args, 'test_project.settings')
  292. self.assertNoOutput(err)
  293. self.assertOutput(out, SYSTEM_CHECK_MSG)
  294. def test_builtin_with_bad_settings(self):
  295. "fulldefault: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  296. args = ['check', '--settings=bad_settings', 'admin_scripts']
  297. out, err = self.run_django_admin(args)
  298. self.assertNoOutput(out)
  299. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  300. def test_builtin_with_bad_environment(self):
  301. "fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  302. args = ['check', 'admin_scripts']
  303. out, err = self.run_django_admin(args, 'bad_settings')
  304. self.assertNoOutput(out)
  305. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  306. def test_custom_command(self):
  307. "fulldefault: django-admin can't execute user commands unless settings are provided"
  308. args = ['noargs_command']
  309. out, err = self.run_django_admin(args)
  310. self.assertNoOutput(out)
  311. self.assertOutput(err, "No Django settings specified")
  312. self.assertOutput(err, "Unknown command: 'noargs_command'")
  313. def test_custom_command_with_settings(self):
  314. "fulldefault: django-admin can execute user commands if settings are provided as argument"
  315. args = ['noargs_command', '--settings=test_project.settings']
  316. out, err = self.run_django_admin(args)
  317. self.assertNoOutput(err)
  318. self.assertOutput(out, "EXECUTE: noargs_command")
  319. def test_custom_command_with_environment(self):
  320. "fulldefault: django-admin can execute user commands if settings are provided in environment"
  321. args = ['noargs_command']
  322. out, err = self.run_django_admin(args, 'test_project.settings')
  323. self.assertNoOutput(err)
  324. self.assertOutput(out, "EXECUTE: noargs_command")
  325. class DjangoAdminMinimalSettings(AdminScriptTestCase):
  326. """A series of tests for django-admin.py when using a settings.py file that
  327. doesn't contain the test application.
  328. """
  329. def setUp(self):
  330. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  331. def tearDown(self):
  332. self.remove_settings('settings.py')
  333. def test_builtin_command(self):
  334. "minimal: django-admin builtin commands fail with an error when no settings provided"
  335. args = ['check', 'admin_scripts']
  336. out, err = self.run_django_admin(args)
  337. self.assertNoOutput(out)
  338. self.assertOutput(err, 'settings are not configured')
  339. def test_builtin_with_settings(self):
  340. "minimal: django-admin builtin commands fail if settings are provided as argument"
  341. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  342. out, err = self.run_django_admin(args)
  343. self.assertNoOutput(out)
  344. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  345. def test_builtin_with_environment(self):
  346. "minimal: django-admin builtin commands fail if settings are provided in the environment"
  347. args = ['check', 'admin_scripts']
  348. out, err = self.run_django_admin(args, 'test_project.settings')
  349. self.assertNoOutput(out)
  350. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  351. def test_builtin_with_bad_settings(self):
  352. "minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  353. args = ['check', '--settings=bad_settings', 'admin_scripts']
  354. out, err = self.run_django_admin(args)
  355. self.assertNoOutput(out)
  356. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  357. def test_builtin_with_bad_environment(self):
  358. "minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  359. args = ['check', 'admin_scripts']
  360. out, err = self.run_django_admin(args, 'bad_settings')
  361. self.assertNoOutput(out)
  362. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  363. def test_custom_command(self):
  364. "minimal: django-admin can't execute user commands unless settings are provided"
  365. args = ['noargs_command']
  366. out, err = self.run_django_admin(args)
  367. self.assertNoOutput(out)
  368. self.assertOutput(err, "No Django settings specified")
  369. self.assertOutput(err, "Unknown command: 'noargs_command'")
  370. def test_custom_command_with_settings(self):
  371. "minimal: django-admin can't execute user commands, even if settings are provided as argument"
  372. args = ['noargs_command', '--settings=test_project.settings']
  373. out, err = self.run_django_admin(args)
  374. self.assertNoOutput(out)
  375. self.assertOutput(err, "Unknown command: 'noargs_command'")
  376. def test_custom_command_with_environment(self):
  377. "minimal: django-admin can't execute user commands, even if settings are provided in environment"
  378. args = ['noargs_command']
  379. out, err = self.run_django_admin(args, 'test_project.settings')
  380. self.assertNoOutput(out)
  381. self.assertOutput(err, "Unknown command: 'noargs_command'")
  382. class DjangoAdminAlternateSettings(AdminScriptTestCase):
  383. """A series of tests for django-admin.py when using a settings file
  384. with a name other than 'settings.py'.
  385. """
  386. def setUp(self):
  387. self.write_settings('alternate_settings.py')
  388. def tearDown(self):
  389. self.remove_settings('alternate_settings.py')
  390. def test_builtin_command(self):
  391. "alternate: django-admin builtin commands fail with an error when no settings provided"
  392. args = ['check', 'admin_scripts']
  393. out, err = self.run_django_admin(args)
  394. self.assertNoOutput(out)
  395. self.assertOutput(err, 'settings are not configured')
  396. def test_builtin_with_settings(self):
  397. "alternate: django-admin builtin commands succeed if settings are provided as argument"
  398. args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
  399. out, err = self.run_django_admin(args)
  400. self.assertNoOutput(err)
  401. self.assertOutput(out, SYSTEM_CHECK_MSG)
  402. def test_builtin_with_environment(self):
  403. "alternate: django-admin builtin commands succeed if settings are provided in the environment"
  404. args = ['check', 'admin_scripts']
  405. out, err = self.run_django_admin(args, 'test_project.alternate_settings')
  406. self.assertNoOutput(err)
  407. self.assertOutput(out, SYSTEM_CHECK_MSG)
  408. def test_builtin_with_bad_settings(self):
  409. "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  410. args = ['check', '--settings=bad_settings', 'admin_scripts']
  411. out, err = self.run_django_admin(args)
  412. self.assertNoOutput(out)
  413. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  414. def test_builtin_with_bad_environment(self):
  415. "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  416. args = ['check', 'admin_scripts']
  417. out, err = self.run_django_admin(args, 'bad_settings')
  418. self.assertNoOutput(out)
  419. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  420. def test_custom_command(self):
  421. "alternate: django-admin can't execute user commands unless settings are provided"
  422. args = ['noargs_command']
  423. out, err = self.run_django_admin(args)
  424. self.assertNoOutput(out)
  425. self.assertOutput(err, "No Django settings specified")
  426. self.assertOutput(err, "Unknown command: 'noargs_command'")
  427. def test_custom_command_with_settings(self):
  428. "alternate: django-admin can execute user commands if settings are provided as argument"
  429. args = ['noargs_command', '--settings=test_project.alternate_settings']
  430. out, err = self.run_django_admin(args)
  431. self.assertNoOutput(err)
  432. self.assertOutput(out, "EXECUTE: noargs_command")
  433. def test_custom_command_with_environment(self):
  434. "alternate: django-admin can execute user commands if settings are provided in environment"
  435. args = ['noargs_command']
  436. out, err = self.run_django_admin(args, 'test_project.alternate_settings')
  437. self.assertNoOutput(err)
  438. self.assertOutput(out, "EXECUTE: noargs_command")
  439. class DjangoAdminMultipleSettings(AdminScriptTestCase):
  440. """A series of tests for django-admin.py when multiple settings files
  441. (including the default 'settings.py') are available. The default settings
  442. file is insufficient for performing the operations described, so the
  443. alternate settings must be used by the running script.
  444. """
  445. def setUp(self):
  446. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  447. self.write_settings('alternate_settings.py')
  448. def tearDown(self):
  449. self.remove_settings('settings.py')
  450. self.remove_settings('alternate_settings.py')
  451. def test_builtin_command(self):
  452. "alternate: django-admin builtin commands fail with an error when no settings provided"
  453. args = ['check', 'admin_scripts']
  454. out, err = self.run_django_admin(args)
  455. self.assertNoOutput(out)
  456. self.assertOutput(err, 'settings are not configured')
  457. def test_builtin_with_settings(self):
  458. "alternate: django-admin builtin commands succeed if settings are provided as argument"
  459. args = ['check', '--settings=test_project.alternate_settings', 'admin_scripts']
  460. out, err = self.run_django_admin(args)
  461. self.assertNoOutput(err)
  462. self.assertOutput(out, SYSTEM_CHECK_MSG)
  463. def test_builtin_with_environment(self):
  464. "alternate: django-admin builtin commands succeed if settings are provided in the environment"
  465. args = ['check', 'admin_scripts']
  466. out, err = self.run_django_admin(args, 'test_project.alternate_settings')
  467. self.assertNoOutput(err)
  468. self.assertOutput(out, SYSTEM_CHECK_MSG)
  469. def test_builtin_with_bad_settings(self):
  470. "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  471. args = ['check', '--settings=bad_settings', 'admin_scripts']
  472. out, err = self.run_django_admin(args)
  473. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  474. def test_builtin_with_bad_environment(self):
  475. "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  476. args = ['check', 'admin_scripts']
  477. out, err = self.run_django_admin(args, 'bad_settings')
  478. self.assertNoOutput(out)
  479. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  480. def test_custom_command(self):
  481. "alternate: django-admin can't execute user commands unless settings are provided"
  482. args = ['noargs_command']
  483. out, err = self.run_django_admin(args)
  484. self.assertNoOutput(out)
  485. self.assertOutput(err, "No Django settings specified")
  486. self.assertOutput(err, "Unknown command: 'noargs_command'")
  487. def test_custom_command_with_settings(self):
  488. "alternate: django-admin can execute user commands if settings are provided as argument"
  489. args = ['noargs_command', '--settings=test_project.alternate_settings']
  490. out, err = self.run_django_admin(args)
  491. self.assertNoOutput(err)
  492. self.assertOutput(out, "EXECUTE: noargs_command")
  493. def test_custom_command_with_environment(self):
  494. "alternate: django-admin can execute user commands if settings are provided in environment"
  495. args = ['noargs_command']
  496. out, err = self.run_django_admin(args, 'test_project.alternate_settings')
  497. self.assertNoOutput(err)
  498. self.assertOutput(out, "EXECUTE: noargs_command")
  499. class DjangoAdminSettingsDirectory(AdminScriptTestCase):
  500. """
  501. A series of tests for django-admin.py when the settings file is in a
  502. directory. (see #9751).
  503. """
  504. def setUp(self):
  505. self.write_settings('settings', is_dir=True)
  506. def tearDown(self):
  507. self.remove_settings('settings', is_dir=True)
  508. def test_setup_environ(self):
  509. "directory: startapp creates the correct directory"
  510. args = ['startapp', 'settings_test']
  511. app_path = os.path.join(self.test_dir, 'settings_test')
  512. out, err = self.run_django_admin(args, 'test_project.settings')
  513. self.addCleanup(shutil.rmtree, app_path)
  514. self.assertNoOutput(err)
  515. self.assertTrue(os.path.exists(app_path))
  516. unicode_literals_import = "from __future__ import unicode_literals\n"
  517. with open(os.path.join(app_path, 'apps.py'), 'r') as f:
  518. content = f.read()
  519. self.assertIn("class SettingsTestConfig(AppConfig)", content)
  520. self.assertIn("name = 'settings_test'", content)
  521. if not PY3:
  522. self.assertIn(unicode_literals_import, content)
  523. if not PY3:
  524. with open(os.path.join(app_path, 'models.py'), 'r') as fp:
  525. content = fp.read()
  526. self.assertIn(unicode_literals_import, content)
  527. def test_setup_environ_custom_template(self):
  528. "directory: startapp creates the correct directory with a custom template"
  529. template_path = os.path.join(custom_templates_dir, 'app_template')
  530. args = ['startapp', '--template', template_path, 'custom_settings_test']
  531. app_path = os.path.join(self.test_dir, 'custom_settings_test')
  532. out, err = self.run_django_admin(args, 'test_project.settings')
  533. self.addCleanup(shutil.rmtree, app_path)
  534. self.assertNoOutput(err)
  535. self.assertTrue(os.path.exists(app_path))
  536. self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
  537. @unittest.skipIf(PY2, "Python 2 doesn't support Unicode package names.")
  538. def test_startapp_unicode_name(self):
  539. "directory: startapp creates the correct directory with unicode characters"
  540. args = ['startapp', 'こんにちは']
  541. app_path = os.path.join(self.test_dir, 'こんにちは')
  542. out, err = self.run_django_admin(args, 'test_project.settings')
  543. self.addCleanup(shutil.rmtree, app_path)
  544. self.assertNoOutput(err)
  545. self.assertTrue(os.path.exists(app_path))
  546. with open(os.path.join(app_path, 'apps.py'), 'r', encoding='utf8') as f:
  547. content = f.read()
  548. self.assertIn("class こんにちはConfig(AppConfig)", content)
  549. self.assertIn("name = 'こんにちは'", content)
  550. def test_builtin_command(self):
  551. "directory: django-admin builtin commands fail with an error when no settings provided"
  552. args = ['check', 'admin_scripts']
  553. out, err = self.run_django_admin(args)
  554. self.assertNoOutput(out)
  555. self.assertOutput(err, 'settings are not configured')
  556. def test_builtin_with_bad_settings(self):
  557. "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  558. args = ['check', '--settings=bad_settings', 'admin_scripts']
  559. out, err = self.run_django_admin(args)
  560. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  561. def test_builtin_with_bad_environment(self):
  562. "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  563. args = ['check', 'admin_scripts']
  564. out, err = self.run_django_admin(args, 'bad_settings')
  565. self.assertNoOutput(out)
  566. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  567. def test_custom_command(self):
  568. "directory: django-admin can't execute user commands unless settings are provided"
  569. args = ['noargs_command']
  570. out, err = self.run_django_admin(args)
  571. self.assertNoOutput(out)
  572. self.assertOutput(err, "No Django settings specified")
  573. self.assertOutput(err, "Unknown command: 'noargs_command'")
  574. def test_builtin_with_settings(self):
  575. "directory: django-admin builtin commands succeed if settings are provided as argument"
  576. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  577. out, err = self.run_django_admin(args)
  578. self.assertNoOutput(err)
  579. self.assertOutput(out, SYSTEM_CHECK_MSG)
  580. def test_builtin_with_environment(self):
  581. "directory: django-admin builtin commands succeed if settings are provided in the environment"
  582. args = ['check', 'admin_scripts']
  583. out, err = self.run_django_admin(args, 'test_project.settings')
  584. self.assertNoOutput(err)
  585. self.assertOutput(out, SYSTEM_CHECK_MSG)
  586. ##########################################################################
  587. # MANAGE.PY TESTS
  588. # This next series of test classes checks the environment processing
  589. # of the generated manage.py script
  590. ##########################################################################
  591. class ManageNoSettings(AdminScriptTestCase):
  592. "A series of tests for manage.py when there is no settings.py file."
  593. def test_builtin_command(self):
  594. "no settings: manage.py builtin commands fail with an error when no settings provided"
  595. args = ['check', 'admin_scripts']
  596. out, err = self.run_manage(args)
  597. self.assertNoOutput(out)
  598. self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
  599. def test_builtin_with_bad_settings(self):
  600. "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  601. args = ['check', '--settings=bad_settings', 'admin_scripts']
  602. out, err = self.run_manage(args)
  603. self.assertNoOutput(out)
  604. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  605. def test_builtin_with_bad_environment(self):
  606. "no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  607. args = ['check', 'admin_scripts']
  608. out, err = self.run_manage(args, 'bad_settings')
  609. self.assertNoOutput(out)
  610. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  611. class ManageDefaultSettings(AdminScriptTestCase):
  612. """A series of tests for manage.py when using a settings.py file that
  613. contains the test application.
  614. """
  615. def setUp(self):
  616. self.write_settings('settings.py')
  617. def tearDown(self):
  618. self.remove_settings('settings.py')
  619. def test_builtin_command(self):
  620. "default: manage.py builtin commands succeed when default settings are appropriate"
  621. args = ['check', 'admin_scripts']
  622. out, err = self.run_manage(args)
  623. self.assertNoOutput(err)
  624. self.assertOutput(out, SYSTEM_CHECK_MSG)
  625. def test_builtin_with_settings(self):
  626. "default: manage.py builtin commands succeed if settings are provided as argument"
  627. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  628. out, err = self.run_manage(args)
  629. self.assertNoOutput(err)
  630. self.assertOutput(out, SYSTEM_CHECK_MSG)
  631. def test_builtin_with_environment(self):
  632. "default: manage.py builtin commands succeed if settings are provided in the environment"
  633. args = ['check', 'admin_scripts']
  634. out, err = self.run_manage(args, 'test_project.settings')
  635. self.assertNoOutput(err)
  636. self.assertOutput(out, SYSTEM_CHECK_MSG)
  637. def test_builtin_with_bad_settings(self):
  638. "default: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
  639. args = ['check', '--settings=bad_settings', 'admin_scripts']
  640. out, err = self.run_manage(args)
  641. self.assertNoOutput(out)
  642. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  643. def test_builtin_with_bad_environment(self):
  644. "default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  645. args = ['check', 'admin_scripts']
  646. out, err = self.run_manage(args, 'bad_settings')
  647. self.assertNoOutput(out)
  648. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  649. def test_custom_command(self):
  650. "default: manage.py can execute user commands when default settings are appropriate"
  651. args = ['noargs_command']
  652. out, err = self.run_manage(args)
  653. self.assertNoOutput(err)
  654. self.assertOutput(out, "EXECUTE: noargs_command")
  655. def test_custom_command_with_settings(self):
  656. "default: manage.py can execute user commands when settings are provided as argument"
  657. args = ['noargs_command', '--settings=test_project.settings']
  658. out, err = self.run_manage(args)
  659. self.assertNoOutput(err)
  660. self.assertOutput(out, "EXECUTE: noargs_command")
  661. def test_custom_command_with_environment(self):
  662. "default: manage.py can execute user commands when settings are provided in environment"
  663. args = ['noargs_command']
  664. out, err = self.run_manage(args, 'test_project.settings')
  665. self.assertNoOutput(err)
  666. self.assertOutput(out, "EXECUTE: noargs_command")
  667. class ManageFullPathDefaultSettings(AdminScriptTestCase):
  668. """A series of tests for manage.py when using a settings.py file that
  669. contains the test application specified using a full path.
  670. """
  671. def setUp(self):
  672. self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'admin_scripts'])
  673. def tearDown(self):
  674. self.remove_settings('settings.py')
  675. def test_builtin_command(self):
  676. "fulldefault: manage.py builtin commands succeed when default settings are appropriate"
  677. args = ['check', 'admin_scripts']
  678. out, err = self.run_manage(args)
  679. self.assertNoOutput(err)
  680. self.assertOutput(out, SYSTEM_CHECK_MSG)
  681. def test_builtin_with_settings(self):
  682. "fulldefault: manage.py builtin commands succeed if settings are provided as argument"
  683. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  684. out, err = self.run_manage(args)
  685. self.assertNoOutput(err)
  686. self.assertOutput(out, SYSTEM_CHECK_MSG)
  687. def test_builtin_with_environment(self):
  688. "fulldefault: manage.py builtin commands succeed if settings are provided in the environment"
  689. args = ['check', 'admin_scripts']
  690. out, err = self.run_manage(args, 'test_project.settings')
  691. self.assertNoOutput(err)
  692. self.assertOutput(out, SYSTEM_CHECK_MSG)
  693. def test_builtin_with_bad_settings(self):
  694. "fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
  695. args = ['check', '--settings=bad_settings', 'admin_scripts']
  696. out, err = self.run_manage(args)
  697. self.assertNoOutput(out)
  698. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  699. def test_builtin_with_bad_environment(self):
  700. "fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  701. args = ['check', 'admin_scripts']
  702. out, err = self.run_manage(args, 'bad_settings')
  703. self.assertNoOutput(out)
  704. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  705. def test_custom_command(self):
  706. "fulldefault: manage.py can execute user commands when default settings are appropriate"
  707. args = ['noargs_command']
  708. out, err = self.run_manage(args)
  709. self.assertNoOutput(err)
  710. self.assertOutput(out, "EXECUTE: noargs_command")
  711. def test_custom_command_with_settings(self):
  712. "fulldefault: manage.py can execute user commands when settings are provided as argument"
  713. args = ['noargs_command', '--settings=test_project.settings']
  714. out, err = self.run_manage(args)
  715. self.assertNoOutput(err)
  716. self.assertOutput(out, "EXECUTE: noargs_command")
  717. def test_custom_command_with_environment(self):
  718. "fulldefault: manage.py can execute user commands when settings are provided in environment"
  719. args = ['noargs_command']
  720. out, err = self.run_manage(args, 'test_project.settings')
  721. self.assertNoOutput(err)
  722. self.assertOutput(out, "EXECUTE: noargs_command")
  723. class ManageMinimalSettings(AdminScriptTestCase):
  724. """A series of tests for manage.py when using a settings.py file that
  725. doesn't contain the test application.
  726. """
  727. def setUp(self):
  728. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  729. def tearDown(self):
  730. self.remove_settings('settings.py')
  731. def test_builtin_command(self):
  732. "minimal: manage.py builtin commands fail with an error when no settings provided"
  733. args = ['check', 'admin_scripts']
  734. out, err = self.run_manage(args)
  735. self.assertNoOutput(out)
  736. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  737. def test_builtin_with_settings(self):
  738. "minimal: manage.py builtin commands fail if settings are provided as argument"
  739. args = ['check', '--settings=test_project.settings', 'admin_scripts']
  740. out, err = self.run_manage(args)
  741. self.assertNoOutput(out)
  742. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  743. def test_builtin_with_environment(self):
  744. "minimal: manage.py builtin commands fail if settings are provided in the environment"
  745. args = ['check', 'admin_scripts']
  746. out, err = self.run_manage(args, 'test_project.settings')
  747. self.assertNoOutput(out)
  748. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  749. def test_builtin_with_bad_settings(self):
  750. "minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  751. args = ['check', '--settings=bad_settings', 'admin_scripts']
  752. out, err = self.run_manage(args)
  753. self.assertNoOutput(out)
  754. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  755. def test_builtin_with_bad_environment(self):
  756. "minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  757. args = ['check', 'admin_scripts']
  758. out, err = self.run_manage(args, 'bad_settings')
  759. self.assertNoOutput(out)
  760. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  761. def test_custom_command(self):
  762. "minimal: manage.py can't execute user commands without appropriate settings"
  763. args = ['noargs_command']
  764. out, err = self.run_manage(args)
  765. self.assertNoOutput(out)
  766. self.assertOutput(err, "Unknown command: 'noargs_command'")
  767. def test_custom_command_with_settings(self):
  768. "minimal: manage.py can't execute user commands, even if settings are provided as argument"
  769. args = ['noargs_command', '--settings=test_project.settings']
  770. out, err = self.run_manage(args)
  771. self.assertNoOutput(out)
  772. self.assertOutput(err, "Unknown command: 'noargs_command'")
  773. def test_custom_command_with_environment(self):
  774. "minimal: manage.py can't execute user commands, even if settings are provided in environment"
  775. args = ['noargs_command']
  776. out, err = self.run_manage(args, 'test_project.settings')
  777. self.assertNoOutput(out)
  778. self.assertOutput(err, "Unknown command: 'noargs_command'")
  779. class ManageAlternateSettings(AdminScriptTestCase):
  780. """A series of tests for manage.py when using a settings file
  781. with a name other than 'settings.py'.
  782. """
  783. def setUp(self):
  784. self.write_settings('alternate_settings.py')
  785. def tearDown(self):
  786. self.remove_settings('alternate_settings.py')
  787. def test_builtin_command(self):
  788. "alternate: manage.py builtin commands fail with an error when no default settings provided"
  789. args = ['check', 'admin_scripts']
  790. out, err = self.run_manage(args)
  791. self.assertNoOutput(out)
  792. self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
  793. def test_builtin_with_settings(self):
  794. "alternate: manage.py builtin commands work with settings provided as argument"
  795. args = ['check', '--settings=alternate_settings', 'admin_scripts']
  796. out, err = self.run_manage(args)
  797. self.assertOutput(out, SYSTEM_CHECK_MSG)
  798. self.assertNoOutput(err)
  799. def test_builtin_with_environment(self):
  800. "alternate: manage.py builtin commands work if settings are provided in the environment"
  801. args = ['check', 'admin_scripts']
  802. out, err = self.run_manage(args, 'alternate_settings')
  803. self.assertOutput(out, SYSTEM_CHECK_MSG)
  804. self.assertNoOutput(err)
  805. def test_builtin_with_bad_settings(self):
  806. "alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  807. args = ['check', '--settings=bad_settings', 'admin_scripts']
  808. out, err = self.run_manage(args)
  809. self.assertNoOutput(out)
  810. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  811. def test_builtin_with_bad_environment(self):
  812. "alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  813. args = ['check', 'admin_scripts']
  814. out, err = self.run_manage(args, 'bad_settings')
  815. self.assertNoOutput(out)
  816. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  817. def test_custom_command(self):
  818. "alternate: manage.py can't execute user commands without settings"
  819. args = ['noargs_command']
  820. out, err = self.run_manage(args)
  821. self.assertNoOutput(out)
  822. self.assertOutput(err, "No module named '?(test_project\.)?settings'?", regex=True)
  823. def test_custom_command_with_settings(self):
  824. "alternate: manage.py can execute user commands if settings are provided as argument"
  825. args = ['noargs_command', '--settings=alternate_settings']
  826. out, err = self.run_manage(args)
  827. self.assertOutput(
  828. out,
  829. "EXECUTE: noargs_command options=[('no_color', False), "
  830. "('pythonpath', None), ('settings', 'alternate_settings'), "
  831. "('traceback', False), ('verbosity', 1)]"
  832. )
  833. self.assertNoOutput(err)
  834. def test_custom_command_with_environment(self):
  835. "alternate: manage.py can execute user commands if settings are provided in environment"
  836. args = ['noargs_command']
  837. out, err = self.run_manage(args, 'alternate_settings')
  838. self.assertOutput(
  839. out,
  840. "EXECUTE: noargs_command options=[('no_color', False), "
  841. "('pythonpath', None), ('settings', None), ('traceback', False), "
  842. "('verbosity', 1)]"
  843. )
  844. self.assertNoOutput(err)
  845. def test_custom_command_output_color(self):
  846. "alternate: manage.py output syntax color can be deactivated with the `--no-color` option"
  847. args = ['noargs_command', '--no-color', '--settings=alternate_settings']
  848. out, err = self.run_manage(args)
  849. self.assertOutput(
  850. out,
  851. "EXECUTE: noargs_command options=[('no_color', True), "
  852. "('pythonpath', None), ('settings', 'alternate_settings'), "
  853. "('traceback', False), ('verbosity', 1)]"
  854. )
  855. self.assertNoOutput(err)
  856. class ManageMultipleSettings(AdminScriptTestCase):
  857. """A series of tests for manage.py when multiple settings files
  858. (including the default 'settings.py') are available. The default settings
  859. file is insufficient for performing the operations described, so the
  860. alternate settings must be used by the running script.
  861. """
  862. def setUp(self):
  863. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  864. self.write_settings('alternate_settings.py')
  865. def tearDown(self):
  866. self.remove_settings('settings.py')
  867. self.remove_settings('alternate_settings.py')
  868. def test_builtin_command(self):
  869. "multiple: manage.py builtin commands fail with an error when no settings provided"
  870. args = ['check', 'admin_scripts']
  871. out, err = self.run_manage(args)
  872. self.assertNoOutput(out)
  873. self.assertOutput(err, "No installed app with label 'admin_scripts'.")
  874. def test_builtin_with_settings(self):
  875. "multiple: manage.py builtin commands succeed if settings are provided as argument"
  876. args = ['check', '--settings=alternate_settings', 'admin_scripts']
  877. out, err = self.run_manage(args)
  878. self.assertNoOutput(err)
  879. self.assertOutput(out, SYSTEM_CHECK_MSG)
  880. def test_builtin_with_environment(self):
  881. "multiple: manage.py can execute builtin commands if settings are provided in the environment"
  882. args = ['check', 'admin_scripts']
  883. out, err = self.run_manage(args, 'alternate_settings')
  884. self.assertNoOutput(err)
  885. self.assertOutput(out, SYSTEM_CHECK_MSG)
  886. def test_builtin_with_bad_settings(self):
  887. "multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  888. args = ['check', '--settings=bad_settings', 'admin_scripts']
  889. out, err = self.run_manage(args)
  890. self.assertNoOutput(out)
  891. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  892. def test_builtin_with_bad_environment(self):
  893. "multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  894. args = ['check', 'admin_scripts']
  895. out, err = self.run_manage(args, 'bad_settings')
  896. self.assertNoOutput(out)
  897. self.assertOutput(err, "No module named '?bad_settings'?", regex=True)
  898. def test_custom_command(self):
  899. "multiple: manage.py can't execute user commands using default settings"
  900. args = ['noargs_command']
  901. out, err = self.run_manage(args)
  902. self.assertNoOutput(out)
  903. self.assertOutput(err, "Unknown command: 'noargs_command'")
  904. def test_custom_command_with_settings(self):
  905. "multiple: manage.py can execute user commands if settings are provided as argument"
  906. args = ['noargs_command', '--settings=alternate_settings']
  907. out, err = self.run_manage(args)
  908. self.assertNoOutput(err)
  909. self.assertOutput(out, "EXECUTE: noargs_command")
  910. def test_custom_command_with_environment(self):
  911. "multiple: manage.py can execute user commands if settings are provided in environment"
  912. args = ['noargs_command']
  913. out, err = self.run_manage(args, 'alternate_settings')
  914. self.assertNoOutput(err)
  915. self.assertOutput(out, "EXECUTE: noargs_command")
  916. class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
  917. """
  918. Tests for manage.py when using the default settings.py file containing
  919. runtime errors.
  920. """
  921. def tearDown(self):
  922. self.remove_settings('settings.py')
  923. def write_settings_with_import_error(self, filename):
  924. settings_file_path = os.path.join(self.test_dir, filename)
  925. with open(settings_file_path, 'w') as settings_file:
  926. settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
  927. settings_file.write('# The next line will cause an import error:\nimport foo42bar\n')
  928. def test_import_error(self):
  929. """
  930. import error: manage.py builtin commands shows useful diagnostic info
  931. when settings wit