tests.py 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. """
  2. A series of tests to establish that the command-line managment tools work as
  3. advertised - especially with regards to the handling of the DJANGO_SETTINGS_MODULE
  4. and default settings.py files.
  5. """
  6. import os
  7. import re
  8. import shutil
  9. import socket
  10. import subprocess
  11. import sys
  12. from django import conf, bin, get_version
  13. from django.conf import settings
  14. from django.test.simple import DjangoTestSuiteRunner
  15. from django.utils import unittest
  16. from django.test import LiveServerTestCase
  17. test_dir = os.path.dirname(os.path.dirname(__file__))
  18. expected_query_re = re.compile(r'CREATE TABLE [`"]admin_scripts_article[`"]', re.IGNORECASE)
  19. class AdminScriptTestCase(unittest.TestCase):
  20. def write_settings(self, filename, apps=None, is_dir=False, sdict=None):
  21. test_dir = os.path.dirname(os.path.dirname(__file__))
  22. if is_dir:
  23. settings_dir = os.path.join(test_dir, filename)
  24. os.mkdir(settings_dir)
  25. settings_file = open(os.path.join(settings_dir, '__init__.py'), 'w')
  26. else:
  27. settings_file = open(os.path.join(test_dir, filename), 'w')
  28. settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
  29. exports = [
  30. 'DATABASES',
  31. 'ROOT_URLCONF',
  32. 'SECRET_KEY',
  33. ]
  34. for s in exports:
  35. if hasattr(settings, s):
  36. o = getattr(settings, s)
  37. if not isinstance(o, dict):
  38. o = "'%s'" % o
  39. settings_file.write("%s = %s\n" % (s, o))
  40. if apps is None:
  41. apps = ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts']
  42. settings_file.write("INSTALLED_APPS = %s\n" % apps)
  43. if sdict:
  44. for k, v in sdict.items():
  45. settings_file.write("%s = %s\n" % (k, v))
  46. settings_file.close()
  47. def remove_settings(self, filename, is_dir=False):
  48. full_name = os.path.join(test_dir, filename)
  49. if is_dir:
  50. shutil.rmtree(full_name)
  51. else:
  52. os.remove(full_name)
  53. # Also try to remove the compiled file; if it exists, it could
  54. # mess up later tests that depend upon the .py file not existing
  55. try:
  56. if sys.platform.startswith('java'):
  57. # Jython produces module$py.class files
  58. os.remove(re.sub(r'\.py$', '$py.class', full_name))
  59. else:
  60. # CPython produces module.pyc files
  61. os.remove(full_name + 'c')
  62. except OSError:
  63. pass
  64. def _ext_backend_paths(self):
  65. """
  66. Returns the paths for any external backend packages.
  67. """
  68. paths = []
  69. first_package_re = re.compile(r'(^[^\.]+)\.')
  70. for backend in settings.DATABASES.values():
  71. result = first_package_re.findall(backend['ENGINE'])
  72. if result and result != 'django':
  73. backend_pkg = __import__(result[0])
  74. backend_dir = os.path.dirname(backend_pkg.__file__)
  75. paths.append(os.path.dirname(backend_dir))
  76. return paths
  77. def run_test(self, script, args, settings_file=None, apps=None):
  78. project_dir = os.path.dirname(test_dir)
  79. base_dir = os.path.dirname(project_dir)
  80. ext_backend_base_dirs = self._ext_backend_paths()
  81. # Remember the old environment
  82. old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None)
  83. if sys.platform.startswith('java'):
  84. python_path_var_name = 'JYTHONPATH'
  85. else:
  86. python_path_var_name = 'PYTHONPATH'
  87. old_python_path = os.environ.get(python_path_var_name, None)
  88. old_cwd = os.getcwd()
  89. # Set the test environment
  90. if settings_file:
  91. os.environ['DJANGO_SETTINGS_MODULE'] = settings_file
  92. elif 'DJANGO_SETTINGS_MODULE' in os.environ:
  93. del os.environ['DJANGO_SETTINGS_MODULE']
  94. python_path = [project_dir, base_dir]
  95. python_path.extend(ext_backend_base_dirs)
  96. os.environ[python_path_var_name] = os.pathsep.join(python_path)
  97. # Silence the DeprecationWarning caused by having a locale directory
  98. # in the project directory.
  99. cmd = [sys.executable, '-Wignore:::django.utils.translation', script]
  100. # Move to the test directory and run
  101. os.chdir(test_dir)
  102. out, err = subprocess.Popen(cmd + args,
  103. stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  104. # Restore the old environment
  105. if old_django_settings_module:
  106. os.environ['DJANGO_SETTINGS_MODULE'] = old_django_settings_module
  107. if old_python_path:
  108. os.environ[python_path_var_name] = old_python_path
  109. # Move back to the old working directory
  110. os.chdir(old_cwd)
  111. return out, err
  112. def run_django_admin(self, args, settings_file=None):
  113. bin_dir = os.path.abspath(os.path.dirname(bin.__file__))
  114. return self.run_test(os.path.join(bin_dir, 'django-admin.py'), args, settings_file)
  115. def run_manage(self, args, settings_file=None):
  116. conf_dir = os.path.dirname(conf.__file__)
  117. template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py')
  118. test_manage_py = os.path.join(test_dir, 'manage.py')
  119. shutil.copyfile(template_manage_py, test_manage_py)
  120. with open(test_manage_py, 'r') as fp:
  121. manage_py_contents = fp.read()
  122. manage_py_contents = manage_py_contents.replace(
  123. "{{ project_name }}", "regressiontests")
  124. with open(test_manage_py, 'w') as fp:
  125. fp.write(manage_py_contents)
  126. stdout, stderr = self.run_test('./manage.py', args, settings_file)
  127. # Cleanup - remove the generated manage.py script
  128. os.remove(test_manage_py)
  129. return stdout, stderr
  130. def assertNoOutput(self, stream):
  131. "Utility assertion: assert that the given stream is empty"
  132. self.assertEqual(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
  133. def assertOutput(self, stream, msg):
  134. "Utility assertion: assert that the given message exists in the output"
  135. self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
  136. def assertNotInOutput(self, stream, msg):
  137. "Utility assertion: assert that the given message doesn't exist in the output"
  138. self.assertFalse(msg in stream, "'%s' matches actual output text '%s'" % (msg, stream))
  139. ##########################################################################
  140. # DJANGO ADMIN TESTS
  141. # This first series of test classes checks the environment processing
  142. # of the django-admin.py script
  143. ##########################################################################
  144. class DjangoAdminNoSettings(AdminScriptTestCase):
  145. "A series of tests for django-admin.py when there is no settings.py file."
  146. def test_builtin_command(self):
  147. "no settings: django-admin builtin commands fail with an import error when no settings provided"
  148. args = ['sqlall', 'admin_scripts']
  149. out, err = self.run_django_admin(args)
  150. self.assertNoOutput(out)
  151. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  152. def test_builtin_with_bad_settings(self):
  153. "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  154. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  155. out, err = self.run_django_admin(args)
  156. self.assertNoOutput(out)
  157. self.assertOutput(err, "Could not import settings 'bad_settings'")
  158. def test_builtin_with_bad_environment(self):
  159. "no settings: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  160. args = ['sqlall', 'admin_scripts']
  161. out, err = self.run_django_admin(args, 'bad_settings')
  162. self.assertNoOutput(out)
  163. self.assertOutput(err, "Could not import settings 'bad_settings'")
  164. class DjangoAdminDefaultSettings(AdminScriptTestCase):
  165. """A series of tests for django-admin.py when using a settings.py file that
  166. contains the test application.
  167. """
  168. def setUp(self):
  169. self.write_settings('settings.py')
  170. def tearDown(self):
  171. self.remove_settings('settings.py')
  172. def test_builtin_command(self):
  173. "default: django-admin builtin commands fail with an import error when no settings provided"
  174. args = ['sqlall', 'admin_scripts']
  175. out, err = self.run_django_admin(args)
  176. self.assertNoOutput(out)
  177. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  178. def test_builtin_with_settings(self):
  179. "default: django-admin builtin commands succeed if settings are provided as argument"
  180. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  181. out, err = self.run_django_admin(args)
  182. self.assertNoOutput(err)
  183. self.assertOutput(out, 'CREATE TABLE')
  184. def test_builtin_with_environment(self):
  185. "default: django-admin builtin commands succeed if settings are provided in the environment"
  186. args = ['sqlall', 'admin_scripts']
  187. out, err = self.run_django_admin(args, 'regressiontests.settings')
  188. self.assertNoOutput(err)
  189. self.assertOutput(out, 'CREATE TABLE')
  190. def test_builtin_with_bad_settings(self):
  191. "default: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  192. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  193. out, err = self.run_django_admin(args)
  194. self.assertNoOutput(out)
  195. self.assertOutput(err, "Could not import settings 'bad_settings'")
  196. def test_builtin_with_bad_environment(self):
  197. "default: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  198. args = ['sqlall', 'admin_scripts']
  199. out, err = self.run_django_admin(args, 'bad_settings')
  200. self.assertNoOutput(out)
  201. self.assertOutput(err, "Could not import settings 'bad_settings'")
  202. def test_custom_command(self):
  203. "default: django-admin can't execute user commands if it isn't provided settings"
  204. args = ['noargs_command']
  205. out, err = self.run_django_admin(args)
  206. self.assertNoOutput(out)
  207. self.assertOutput(err, "Unknown command: 'noargs_command'")
  208. def test_custom_command_with_settings(self):
  209. "default: django-admin can execute user commands if settings are provided as argument"
  210. args = ['noargs_command', '--settings=regressiontests.settings']
  211. out, err = self.run_django_admin(args)
  212. self.assertNoOutput(err)
  213. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  214. def test_custom_command_with_environment(self):
  215. "default: django-admin can execute user commands if settings are provided in environment"
  216. args = ['noargs_command']
  217. out, err = self.run_django_admin(args, 'regressiontests.settings')
  218. self.assertNoOutput(err)
  219. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  220. class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
  221. """A series of tests for django-admin.py when using a settings.py file that
  222. contains the test application specified using a full path.
  223. """
  224. def setUp(self):
  225. self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts'])
  226. def tearDown(self):
  227. self.remove_settings('settings.py')
  228. def test_builtin_command(self):
  229. "fulldefault: django-admin builtin commands fail with an import error when no settings provided"
  230. args = ['sqlall', 'admin_scripts']
  231. out, err = self.run_django_admin(args)
  232. self.assertNoOutput(out)
  233. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  234. def test_builtin_with_settings(self):
  235. "fulldefault: django-admin builtin commands succeed if a settings file is provided"
  236. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  237. out, err = self.run_django_admin(args)
  238. self.assertNoOutput(err)
  239. self.assertOutput(out, 'CREATE TABLE')
  240. def test_builtin_with_environment(self):
  241. "fulldefault: django-admin builtin commands succeed if the environment contains settings"
  242. args = ['sqlall', 'admin_scripts']
  243. out, err = self.run_django_admin(args, 'regressiontests.settings')
  244. self.assertNoOutput(err)
  245. self.assertOutput(out, 'CREATE TABLE')
  246. def test_builtin_with_bad_settings(self):
  247. "fulldefault: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  248. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  249. out, err = self.run_django_admin(args)
  250. self.assertNoOutput(out)
  251. self.assertOutput(err, "Could not import settings 'bad_settings'")
  252. def test_builtin_with_bad_environment(self):
  253. "fulldefault: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  254. args = ['sqlall', 'admin_scripts']
  255. out, err = self.run_django_admin(args, 'bad_settings')
  256. self.assertNoOutput(out)
  257. self.assertOutput(err, "Could not import settings 'bad_settings'")
  258. def test_custom_command(self):
  259. "fulldefault: django-admin can't execute user commands unless settings are provided"
  260. args = ['noargs_command']
  261. out, err = self.run_django_admin(args)
  262. self.assertNoOutput(out)
  263. self.assertOutput(err, "Unknown command: 'noargs_command'")
  264. def test_custom_command_with_settings(self):
  265. "fulldefault: django-admin can execute user commands if settings are provided as argument"
  266. args = ['noargs_command', '--settings=regressiontests.settings']
  267. out, err = self.run_django_admin(args)
  268. self.assertNoOutput(err)
  269. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  270. def test_custom_command_with_environment(self):
  271. "fulldefault: django-admin can execute user commands if settings are provided in environment"
  272. args = ['noargs_command']
  273. out, err = self.run_django_admin(args, 'regressiontests.settings')
  274. self.assertNoOutput(err)
  275. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  276. class DjangoAdminMinimalSettings(AdminScriptTestCase):
  277. """A series of tests for django-admin.py when using a settings.py file that
  278. doesn't contain the test application.
  279. """
  280. def setUp(self):
  281. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  282. def tearDown(self):
  283. self.remove_settings('settings.py')
  284. def test_builtin_command(self):
  285. "minimal: django-admin builtin commands fail with an import error when no settings provided"
  286. args = ['sqlall', 'admin_scripts']
  287. out, err = self.run_django_admin(args)
  288. self.assertNoOutput(out)
  289. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  290. def test_builtin_with_settings(self):
  291. "minimal: django-admin builtin commands fail if settings are provided as argument"
  292. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  293. out, err = self.run_django_admin(args)
  294. self.assertNoOutput(out)
  295. self.assertOutput(err, 'App with label admin_scripts could not be found')
  296. def test_builtin_with_environment(self):
  297. "minimal: django-admin builtin commands fail if settings are provided in the environment"
  298. args = ['sqlall', 'admin_scripts']
  299. out, err = self.run_django_admin(args, 'regressiontests.settings')
  300. self.assertNoOutput(out)
  301. self.assertOutput(err, 'App with label admin_scripts could not be found')
  302. def test_builtin_with_bad_settings(self):
  303. "minimal: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  304. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  305. out, err = self.run_django_admin(args)
  306. self.assertNoOutput(out)
  307. self.assertOutput(err, "Could not import settings 'bad_settings'")
  308. def test_builtin_with_bad_environment(self):
  309. "minimal: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  310. args = ['sqlall', 'admin_scripts']
  311. out, err = self.run_django_admin(args, 'bad_settings')
  312. self.assertNoOutput(out)
  313. self.assertOutput(err, "Could not import settings 'bad_settings'")
  314. def test_custom_command(self):
  315. "minimal: django-admin can't execute user commands unless settings are provided"
  316. args = ['noargs_command']
  317. out, err = self.run_django_admin(args)
  318. self.assertNoOutput(out)
  319. self.assertOutput(err, "Unknown command: 'noargs_command'")
  320. def test_custom_command_with_settings(self):
  321. "minimal: django-admin can't execute user commands, even if settings are provided as argument"
  322. args = ['noargs_command', '--settings=regressiontests.settings']
  323. out, err = self.run_django_admin(args)
  324. self.assertNoOutput(out)
  325. self.assertOutput(err, "Unknown command: 'noargs_command'")
  326. def test_custom_command_with_environment(self):
  327. "minimal: django-admin can't execute user commands, even if settings are provided in environment"
  328. args = ['noargs_command']
  329. out, err = self.run_django_admin(args, 'regressiontests.settings')
  330. self.assertNoOutput(out)
  331. self.assertOutput(err, "Unknown command: 'noargs_command'")
  332. class DjangoAdminAlternateSettings(AdminScriptTestCase):
  333. """A series of tests for django-admin.py when using a settings file
  334. with a name other than 'settings.py'.
  335. """
  336. def setUp(self):
  337. self.write_settings('alternate_settings.py')
  338. def tearDown(self):
  339. self.remove_settings('alternate_settings.py')
  340. def test_builtin_command(self):
  341. "alternate: django-admin builtin commands fail with an import error when no settings provided"
  342. args = ['sqlall', 'admin_scripts']
  343. out, err = self.run_django_admin(args)
  344. self.assertNoOutput(out)
  345. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  346. def test_builtin_with_settings(self):
  347. "alternate: django-admin builtin commands succeed if settings are provided as argument"
  348. args = ['sqlall', '--settings=regressiontests.alternate_settings', 'admin_scripts']
  349. out, err = self.run_django_admin(args)
  350. self.assertNoOutput(err)
  351. self.assertOutput(out, 'CREATE TABLE')
  352. def test_builtin_with_environment(self):
  353. "alternate: django-admin builtin commands succeed if settings are provided in the environment"
  354. args = ['sqlall', 'admin_scripts']
  355. out, err = self.run_django_admin(args, 'regressiontests.alternate_settings')
  356. self.assertNoOutput(err)
  357. self.assertOutput(out, 'CREATE TABLE')
  358. def test_builtin_with_bad_settings(self):
  359. "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  360. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  361. out, err = self.run_django_admin(args)
  362. self.assertNoOutput(out)
  363. self.assertOutput(err, "Could not import settings 'bad_settings'")
  364. def test_builtin_with_bad_environment(self):
  365. "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  366. args = ['sqlall', 'admin_scripts']
  367. out, err = self.run_django_admin(args, 'bad_settings')
  368. self.assertNoOutput(out)
  369. self.assertOutput(err, "Could not import settings 'bad_settings'")
  370. def test_custom_command(self):
  371. "alternate: django-admin can't execute user commands unless settings are provided"
  372. args = ['noargs_command']
  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_settings(self):
  377. "alternate: django-admin can execute user commands if settings are provided as argument"
  378. args = ['noargs_command', '--settings=regressiontests.alternate_settings']
  379. out, err = self.run_django_admin(args)
  380. self.assertNoOutput(err)
  381. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  382. def test_custom_command_with_environment(self):
  383. "alternate: django-admin can execute user commands if settings are provided in environment"
  384. args = ['noargs_command']
  385. out, err = self.run_django_admin(args, 'regressiontests.alternate_settings')
  386. self.assertNoOutput(err)
  387. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  388. class DjangoAdminMultipleSettings(AdminScriptTestCase):
  389. """A series of tests for django-admin.py when multiple settings files
  390. (including the default 'settings.py') are available. The default settings
  391. file is insufficient for performing the operations described, so the
  392. alternate settings must be used by the running script.
  393. """
  394. def setUp(self):
  395. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  396. self.write_settings('alternate_settings.py')
  397. def tearDown(self):
  398. self.remove_settings('settings.py')
  399. self.remove_settings('alternate_settings.py')
  400. def test_builtin_command(self):
  401. "alternate: django-admin builtin commands fail with an import error when no settings provided"
  402. args = ['sqlall', 'admin_scripts']
  403. out, err = self.run_django_admin(args)
  404. self.assertNoOutput(out)
  405. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  406. def test_builtin_with_settings(self):
  407. "alternate: django-admin builtin commands succeed if settings are provided as argument"
  408. args = ['sqlall', '--settings=regressiontests.alternate_settings', 'admin_scripts']
  409. out, err = self.run_django_admin(args)
  410. self.assertNoOutput(err)
  411. self.assertOutput(out, 'CREATE TABLE')
  412. def test_builtin_with_environment(self):
  413. "alternate: django-admin builtin commands succeed if settings are provided in the environment"
  414. args = ['sqlall', 'admin_scripts']
  415. out, err = self.run_django_admin(args, 'regressiontests.alternate_settings')
  416. self.assertNoOutput(err)
  417. self.assertOutput(out, 'CREATE TABLE')
  418. def test_builtin_with_bad_settings(self):
  419. "alternate: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  420. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  421. out, err = self.run_django_admin(args)
  422. self.assertOutput(err, "Could not import settings 'bad_settings'")
  423. def test_builtin_with_bad_environment(self):
  424. "alternate: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  425. args = ['sqlall', 'admin_scripts']
  426. out, err = self.run_django_admin(args, 'bad_settings')
  427. self.assertNoOutput(out)
  428. self.assertOutput(err, "Could not import settings 'bad_settings'")
  429. def test_custom_command(self):
  430. "alternate: django-admin can't execute user commands unless settings are provided"
  431. args = ['noargs_command']
  432. out, err = self.run_django_admin(args)
  433. self.assertNoOutput(out)
  434. self.assertOutput(err, "Unknown command: 'noargs_command'")
  435. def test_custom_command_with_settings(self):
  436. "alternate: django-admin can execute user commands if settings are provided as argument"
  437. args = ['noargs_command', '--settings=regressiontests.alternate_settings']
  438. out, err = self.run_django_admin(args)
  439. self.assertNoOutput(err)
  440. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  441. def test_custom_command_with_environment(self):
  442. "alternate: django-admin can execute user commands if settings are provided in environment"
  443. args = ['noargs_command']
  444. out, err = self.run_django_admin(args, 'regressiontests.alternate_settings')
  445. self.assertNoOutput(err)
  446. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  447. class DjangoAdminSettingsDirectory(AdminScriptTestCase):
  448. """
  449. A series of tests for django-admin.py when the settings file is in a
  450. directory. (see #9751).
  451. """
  452. def setUp(self):
  453. self.write_settings('settings', is_dir=True)
  454. def tearDown(self):
  455. self.remove_settings('settings', is_dir=True)
  456. def test_setup_environ(self):
  457. "directory: startapp creates the correct directory"
  458. args = ['startapp', 'settings_test']
  459. app_path = os.path.join(test_dir, 'settings_test')
  460. out, err = self.run_django_admin(args, 'regressiontests.settings')
  461. self.addCleanup(shutil.rmtree, app_path)
  462. self.assertNoOutput(err)
  463. self.assertTrue(os.path.exists(app_path))
  464. def test_setup_environ_custom_template(self):
  465. "directory: startapp creates the correct directory with a custom template"
  466. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'app_template')
  467. args = ['startapp', '--template', template_path, 'custom_settings_test']
  468. app_path = os.path.join(test_dir, 'custom_settings_test')
  469. out, err = self.run_django_admin(args, 'regressiontests.settings')
  470. self.addCleanup(shutil.rmtree, app_path)
  471. self.assertNoOutput(err)
  472. self.assertTrue(os.path.exists(app_path))
  473. self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
  474. def test_builtin_command(self):
  475. "directory: django-admin builtin commands fail with an import error when no settings provided"
  476. args = ['sqlall', 'admin_scripts']
  477. out, err = self.run_django_admin(args)
  478. self.assertNoOutput(out)
  479. self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined')
  480. def test_builtin_with_bad_settings(self):
  481. "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
  482. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  483. out, err = self.run_django_admin(args)
  484. self.assertOutput(err, "Could not import settings 'bad_settings'")
  485. def test_builtin_with_bad_environment(self):
  486. "directory: django-admin builtin commands fail if settings file (from environment) doesn't exist"
  487. args = ['sqlall', 'admin_scripts']
  488. out, err = self.run_django_admin(args, 'bad_settings')
  489. self.assertNoOutput(out)
  490. self.assertOutput(err, "Could not import settings 'bad_settings'")
  491. def test_custom_command(self):
  492. "directory: django-admin can't execute user commands unless settings are provided"
  493. args = ['noargs_command']
  494. out, err = self.run_django_admin(args)
  495. self.assertNoOutput(out)
  496. self.assertOutput(err, "Unknown command: 'noargs_command'")
  497. def test_builtin_with_settings(self):
  498. "directory: django-admin builtin commands succeed if settings are provided as argument"
  499. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  500. out, err = self.run_django_admin(args)
  501. self.assertNoOutput(err)
  502. self.assertOutput(out, 'CREATE TABLE')
  503. def test_builtin_with_environment(self):
  504. "directory: django-admin builtin commands succeed if settings are provided in the environment"
  505. args = ['sqlall', 'admin_scripts']
  506. out, err = self.run_django_admin(args, 'regressiontests.settings')
  507. self.assertNoOutput(err)
  508. self.assertOutput(out, 'CREATE TABLE')
  509. ##########################################################################
  510. # MANAGE.PY TESTS
  511. # This next series of test classes checks the environment processing
  512. # of the generated manage.py script
  513. ##########################################################################
  514. class ManageNoSettings(AdminScriptTestCase):
  515. "A series of tests for manage.py when there is no settings.py file."
  516. def test_builtin_command(self):
  517. "no settings: manage.py builtin commands fail with an import error when no settings provided"
  518. args = ['sqlall', 'admin_scripts']
  519. out, err = self.run_manage(args)
  520. self.assertNoOutput(out)
  521. self.assertOutput(err, "Could not import settings 'regressiontests.settings'")
  522. def test_builtin_with_bad_settings(self):
  523. "no settings: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  524. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  525. out, err = self.run_manage(args)
  526. self.assertNoOutput(out)
  527. self.assertOutput(err, "Could not import settings 'bad_settings'")
  528. def test_builtin_with_bad_environment(self):
  529. "no settings: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  530. args = ['sqlall', 'admin_scripts']
  531. out, err = self.run_manage(args, 'bad_settings')
  532. self.assertNoOutput(out)
  533. self.assertOutput(err, "Could not import settings 'bad_settings'")
  534. class ManageDefaultSettings(AdminScriptTestCase):
  535. """A series of tests for manage.py when using a settings.py file that
  536. contains the test application.
  537. """
  538. def setUp(self):
  539. self.write_settings('settings.py')
  540. def tearDown(self):
  541. self.remove_settings('settings.py')
  542. def test_builtin_command(self):
  543. "default: manage.py builtin commands succeed when default settings are appropriate"
  544. args = ['sqlall', 'admin_scripts']
  545. out, err = self.run_manage(args)
  546. self.assertNoOutput(err)
  547. self.assertOutput(out, 'CREATE TABLE')
  548. def test_builtin_with_settings(self):
  549. "default: manage.py builtin commands succeed if settings are provided as argument"
  550. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  551. out, err = self.run_manage(args)
  552. self.assertNoOutput(err)
  553. self.assertOutput(out, 'CREATE TABLE')
  554. def test_builtin_with_environment(self):
  555. "default: manage.py builtin commands succeed if settings are provided in the environment"
  556. args = ['sqlall', 'admin_scripts']
  557. out, err = self.run_manage(args, 'regressiontests.settings')
  558. self.assertNoOutput(err)
  559. self.assertOutput(out, 'CREATE TABLE')
  560. def test_builtin_with_bad_settings(self):
  561. "default: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
  562. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  563. out, err = self.run_manage(args)
  564. self.assertNoOutput(out)
  565. self.assertOutput(err, "Could not import settings 'bad_settings'")
  566. def test_builtin_with_bad_environment(self):
  567. "default: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  568. args = ['sqlall', 'admin_scripts']
  569. out, err = self.run_manage(args, 'bad_settings')
  570. self.assertNoOutput(out)
  571. self.assertOutput(err, "Could not import settings 'bad_settings'")
  572. def test_custom_command(self):
  573. "default: manage.py can execute user commands when default settings are appropriate"
  574. args = ['noargs_command']
  575. out, err = self.run_manage(args)
  576. self.assertNoOutput(err)
  577. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  578. def test_custom_command_with_settings(self):
  579. "default: manage.py can execute user commands when settings are provided as argument"
  580. args = ['noargs_command', '--settings=regressiontests.settings']
  581. out, err = self.run_manage(args)
  582. self.assertNoOutput(err)
  583. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  584. def test_custom_command_with_environment(self):
  585. "default: manage.py can execute user commands when settings are provided in environment"
  586. args = ['noargs_command']
  587. out, err = self.run_manage(args, 'regressiontests.settings')
  588. self.assertNoOutput(err)
  589. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  590. class ManageFullPathDefaultSettings(AdminScriptTestCase):
  591. """A series of tests for manage.py when using a settings.py file that
  592. contains the test application specified using a full path.
  593. """
  594. def setUp(self):
  595. self.write_settings('settings.py', ['django.contrib.auth', 'django.contrib.contenttypes', 'regressiontests.admin_scripts'])
  596. def tearDown(self):
  597. self.remove_settings('settings.py')
  598. def test_builtin_command(self):
  599. "fulldefault: manage.py builtin commands succeed when default settings are appropriate"
  600. args = ['sqlall', 'admin_scripts']
  601. out, err = self.run_manage(args)
  602. self.assertNoOutput(err)
  603. self.assertOutput(out, 'CREATE TABLE')
  604. def test_builtin_with_settings(self):
  605. "fulldefault: manage.py builtin commands succeed if settings are provided as argument"
  606. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  607. out, err = self.run_manage(args)
  608. self.assertNoOutput(err)
  609. self.assertOutput(out, 'CREATE TABLE')
  610. def test_builtin_with_environment(self):
  611. "fulldefault: manage.py builtin commands succeed if settings are provided in the environment"
  612. args = ['sqlall', 'admin_scripts']
  613. out, err = self.run_manage(args, 'regressiontests.settings')
  614. self.assertNoOutput(err)
  615. self.assertOutput(out, 'CREATE TABLE')
  616. def test_builtin_with_bad_settings(self):
  617. "fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn't exist"
  618. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  619. out, err = self.run_manage(args)
  620. self.assertNoOutput(out)
  621. self.assertOutput(err, "Could not import settings 'bad_settings'")
  622. def test_builtin_with_bad_environment(self):
  623. "fulldefault: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  624. args = ['sqlall', 'admin_scripts']
  625. out, err = self.run_manage(args, 'bad_settings')
  626. self.assertNoOutput(out)
  627. self.assertOutput(err, "Could not import settings 'bad_settings'")
  628. def test_custom_command(self):
  629. "fulldefault: manage.py can execute user commands when default settings are appropriate"
  630. args = ['noargs_command']
  631. out, err = self.run_manage(args)
  632. self.assertNoOutput(err)
  633. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  634. def test_custom_command_with_settings(self):
  635. "fulldefault: manage.py can execute user commands when settings are provided as argument"
  636. args = ['noargs_command', '--settings=regressiontests.settings']
  637. out, err = self.run_manage(args)
  638. self.assertNoOutput(err)
  639. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  640. def test_custom_command_with_environment(self):
  641. "fulldefault: manage.py can execute user commands when settings are provided in environment"
  642. args = ['noargs_command']
  643. out, err = self.run_manage(args, 'regressiontests.settings')
  644. self.assertNoOutput(err)
  645. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  646. class ManageMinimalSettings(AdminScriptTestCase):
  647. """A series of tests for manage.py when using a settings.py file that
  648. doesn't contain the test application.
  649. """
  650. def setUp(self):
  651. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  652. def tearDown(self):
  653. self.remove_settings('settings.py')
  654. def test_builtin_command(self):
  655. "minimal: manage.py builtin commands fail with an import error when no settings provided"
  656. args = ['sqlall', 'admin_scripts']
  657. out, err = self.run_manage(args)
  658. self.assertNoOutput(out)
  659. self.assertOutput(err, 'App with label admin_scripts could not be found')
  660. def test_builtin_with_settings(self):
  661. "minimal: manage.py builtin commands fail if settings are provided as argument"
  662. args = ['sqlall', '--settings=regressiontests.settings', 'admin_scripts']
  663. out, err = self.run_manage(args)
  664. self.assertNoOutput(out)
  665. self.assertOutput(err, 'App with label admin_scripts could not be found')
  666. def test_builtin_with_environment(self):
  667. "minimal: manage.py builtin commands fail if settings are provided in the environment"
  668. args = ['sqlall', 'admin_scripts']
  669. out, err = self.run_manage(args, 'regressiontests.settings')
  670. self.assertNoOutput(out)
  671. self.assertOutput(err, 'App with label admin_scripts could not be found')
  672. def test_builtin_with_bad_settings(self):
  673. "minimal: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  674. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  675. out, err = self.run_manage(args)
  676. self.assertNoOutput(out)
  677. self.assertOutput(err, "Could not import settings 'bad_settings'")
  678. def test_builtin_with_bad_environment(self):
  679. "minimal: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  680. args = ['sqlall', 'admin_scripts']
  681. out, err = self.run_manage(args, 'bad_settings')
  682. self.assertNoOutput(out)
  683. self.assertOutput(err, "Could not import settings 'bad_settings'")
  684. def test_custom_command(self):
  685. "minimal: manage.py can't execute user commands without appropriate settings"
  686. args = ['noargs_command']
  687. out, err = self.run_manage(args)
  688. self.assertNoOutput(out)
  689. self.assertOutput(err, "Unknown command: 'noargs_command'")
  690. def test_custom_command_with_settings(self):
  691. "minimal: manage.py can't execute user commands, even if settings are provided as argument"
  692. args = ['noargs_command', '--settings=regressiontests.settings']
  693. out, err = self.run_manage(args)
  694. self.assertNoOutput(out)
  695. self.assertOutput(err, "Unknown command: 'noargs_command'")
  696. def test_custom_command_with_environment(self):
  697. "minimal: manage.py can't execute user commands, even if settings are provided in environment"
  698. args = ['noargs_command']
  699. out, err = self.run_manage(args, 'regressiontests.settings')
  700. self.assertNoOutput(out)
  701. self.assertOutput(err, "Unknown command: 'noargs_command'")
  702. class ManageAlternateSettings(AdminScriptTestCase):
  703. """A series of tests for manage.py when using a settings file
  704. with a name other than 'settings.py'.
  705. """
  706. def setUp(self):
  707. self.write_settings('alternate_settings.py')
  708. def tearDown(self):
  709. self.remove_settings('alternate_settings.py')
  710. def test_builtin_command(self):
  711. "alternate: manage.py builtin commands fail with an import error when no default settings provided"
  712. args = ['sqlall', 'admin_scripts']
  713. out, err = self.run_manage(args)
  714. self.assertNoOutput(out)
  715. self.assertOutput(err, "Could not import settings 'regressiontests.settings'")
  716. def test_builtin_with_settings(self):
  717. "alternate: manage.py builtin commands work with settings provided as argument"
  718. args = ['sqlall', '--settings=alternate_settings', 'admin_scripts']
  719. out, err = self.run_manage(args)
  720. self.assertRegexpMatches(out, expected_query_re)
  721. self.assertNoOutput(err)
  722. def test_builtin_with_environment(self):
  723. "alternate: manage.py builtin commands work if settings are provided in the environment"
  724. args = ['sqlall', 'admin_scripts']
  725. out, err = self.run_manage(args, 'alternate_settings')
  726. self.assertRegexpMatches(out, expected_query_re)
  727. self.assertNoOutput(err)
  728. def test_builtin_with_bad_settings(self):
  729. "alternate: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  730. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  731. out, err = self.run_manage(args)
  732. self.assertNoOutput(out)
  733. self.assertOutput(err, "Could not import settings 'bad_settings'")
  734. def test_builtin_with_bad_environment(self):
  735. "alternate: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  736. args = ['sqlall', 'admin_scripts']
  737. out, err = self.run_manage(args, 'bad_settings')
  738. self.assertNoOutput(out)
  739. self.assertOutput(err, "Could not import settings 'bad_settings'")
  740. def test_custom_command(self):
  741. "alternate: manage.py can't execute user commands without settings"
  742. args = ['noargs_command']
  743. out, err = self.run_manage(args)
  744. self.assertNoOutput(out)
  745. self.assertOutput(err, "Unknown command: 'noargs_command'")
  746. def test_custom_command_with_settings(self):
  747. "alternate: manage.py can execute user commands if settings are provided as argument"
  748. args = ['noargs_command', '--settings=alternate_settings']
  749. out, err = self.run_manage(args)
  750. self.assertOutput(out, "EXECUTE:NoArgsCommand options=[('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  751. self.assertNoOutput(err)
  752. def test_custom_command_with_environment(self):
  753. "alternate: manage.py can execute user commands if settings are provided in environment"
  754. args = ['noargs_command']
  755. out, err = self.run_manage(args, 'alternate_settings')
  756. self.assertOutput(out, "EXECUTE:NoArgsCommand options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  757. self.assertNoOutput(err)
  758. class ManageMultipleSettings(AdminScriptTestCase):
  759. """A series of tests for manage.py when multiple settings files
  760. (including the default 'settings.py') are available. The default settings
  761. file is insufficient for performing the operations described, so the
  762. alternate settings must be used by the running script.
  763. """
  764. def setUp(self):
  765. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  766. self.write_settings('alternate_settings.py')
  767. def tearDown(self):
  768. self.remove_settings('settings.py')
  769. self.remove_settings('alternate_settings.py')
  770. def test_builtin_command(self):
  771. "multiple: manage.py builtin commands fail with an import error when no settings provided"
  772. args = ['sqlall', 'admin_scripts']
  773. out, err = self.run_manage(args)
  774. self.assertNoOutput(out)
  775. self.assertOutput(err, 'App with label admin_scripts could not be found.')
  776. def test_builtin_with_settings(self):
  777. "multiple: manage.py builtin commands succeed if settings are provided as argument"
  778. args = ['sqlall', '--settings=alternate_settings', 'admin_scripts']
  779. out, err = self.run_manage(args)
  780. self.assertNoOutput(err)
  781. self.assertOutput(out, 'CREATE TABLE')
  782. def test_builtin_with_environment(self):
  783. "multiple: manage.py can execute builtin commands if settings are provided in the environment"
  784. args = ['sqlall', 'admin_scripts']
  785. out, err = self.run_manage(args, 'alternate_settings')
  786. self.assertNoOutput(err)
  787. self.assertOutput(out, 'CREATE TABLE')
  788. def test_builtin_with_bad_settings(self):
  789. "multiple: manage.py builtin commands fail if settings file (from argument) doesn't exist"
  790. args = ['sqlall', '--settings=bad_settings', 'admin_scripts']
  791. out, err = self.run_manage(args)
  792. self.assertNoOutput(out)
  793. self.assertOutput(err, "Could not import settings 'bad_settings'")
  794. def test_builtin_with_bad_environment(self):
  795. "multiple: manage.py builtin commands fail if settings file (from environment) doesn't exist"
  796. args = ['sqlall', 'admin_scripts']
  797. out, err = self.run_manage(args, 'bad_settings')
  798. self.assertNoOutput(out)
  799. self.assertOutput(err, "Could not import settings 'bad_settings'")
  800. def test_custom_command(self):
  801. "multiple: manage.py can't execute user commands using default settings"
  802. args = ['noargs_command']
  803. out, err = self.run_manage(args)
  804. self.assertNoOutput(out)
  805. self.assertOutput(err, "Unknown command: 'noargs_command'")
  806. def test_custom_command_with_settings(self):
  807. "multiple: manage.py can execute user commands if settings are provided as argument"
  808. args = ['noargs_command', '--settings=alternate_settings']
  809. out, err = self.run_manage(args)
  810. self.assertNoOutput(err)
  811. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  812. def test_custom_command_with_environment(self):
  813. "multiple: manage.py can execute user commands if settings are provided in environment"
  814. args = ['noargs_command']
  815. out, err = self.run_manage(args, 'alternate_settings')
  816. self.assertNoOutput(err)
  817. self.assertOutput(out, "EXECUTE:NoArgsCommand")
  818. class ManageSettingsWithImportError(AdminScriptTestCase):
  819. """Tests for manage.py when using the default settings.py file
  820. with an import error. Ticket #14130.
  821. """
  822. def setUp(self):
  823. self.write_settings_with_import_error('settings.py')
  824. def tearDown(self):
  825. self.remove_settings('settings.py')
  826. def write_settings_with_import_error(self, filename, apps=None, is_dir=False, sdict=None):
  827. if is_dir:
  828. settings_dir = os.path.join(test_dir, filename)
  829. os.mkdir(settings_dir)
  830. settings_file = open(os.path.join(settings_dir, '__init__.py'), 'w')
  831. else:
  832. settings_file = open(os.path.join(test_dir, filename), 'w')
  833. settings_file.write('# Settings file automatically generated by regressiontests.admin_scripts test case\n')
  834. settings_file.write('# The next line will cause an import error:\nimport foo42bar\n')
  835. settings_file.close()
  836. def test_builtin_command(self):
  837. "import error: manage.py builtin commands shows useful diagnostic info when settings with import errors is provided"
  838. args = ['sqlall', 'admin_scripts']
  839. out, err = self.run_manage(args)
  840. self.assertNoOutput(out)
  841. self.assertOutput(err, "No module named foo42bar")
  842. class ManageValidate(AdminScriptTestCase):
  843. def tearDown(self):
  844. self.remove_settings('settings.py')
  845. def test_nonexistent_app(self):
  846. "manage.py validate reports an error on a non-existent app in INSTALLED_APPS"
  847. self.write_settings('settings.py', apps=['admin_scriptz.broken_app'], sdict={'USE_I18N': False})
  848. args = ['validate']
  849. out, err = self.run_manage(args)
  850. self.assertNoOutput(out)
  851. self.assertOutput(err, 'No module named admin_scriptz')
  852. def test_broken_app(self):
  853. "manage.py validate reports an ImportError if an app's models.py raises one on import"
  854. self.write_settings('settings.py', apps=['admin_scripts.broken_app'])
  855. args = ['validate']
  856. out, err = self.run_manage(args)
  857. self.assertNoOutput(out)
  858. self.assertOutput(err, 'ImportError')
  859. def test_complex_app(self):
  860. "manage.py validate does not raise an ImportError validating a complex app with nested calls to load_app"
  861. self.write_settings('settings.py',
  862. apps=['admin_scripts.complex_app', 'admin_scripts.simple_app'],
  863. sdict={'DEBUG': True})
  864. args = ['validate']
  865. out, err = self.run_manage(args)
  866. self.assertNoOutput(err)
  867. self.assertOutput(out, '0 errors found')
  868. def test_app_with_import(self):
  869. "manage.py validate does not raise errors when an app imports a base class that itself has an abstract base"
  870. self.write_settings('settings.py',
  871. apps=['admin_scripts.app_with_import',
  872. 'django.contrib.comments',
  873. 'django.contrib.auth',
  874. 'django.contrib.contenttypes',
  875. 'django.contrib.sites'],
  876. sdict={'DEBUG': True})
  877. args = ['validate']
  878. out, err = self.run_manage(args)
  879. self.assertNoOutput(err)
  880. self.assertOutput(out, '0 errors found')
  881. class CustomTestRunner(DjangoTestSuiteRunner):
  882. def __init__(self, *args, **kwargs):
  883. assert 'liveserver' not in kwargs
  884. super(CustomTestRunner, self).__init__(*args, **kwargs)
  885. def run_tests(self, test_labels, extra_tests=None, **kwargs):
  886. pass
  887. class ManageTestCommand(AdminScriptTestCase):
  888. def setUp(self):
  889. from django.core.management.commands.test import Command as TestCommand
  890. self.cmd = TestCommand()
  891. def test_liveserver(self):
  892. """
  893. Ensure that the --liveserver option sets the environment variable
  894. correctly.
  895. Refs #2879.
  896. """
  897. # Backup original state
  898. address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ
  899. old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS')
  900. self.cmd.handle(verbosity=0, testrunner='regressiontests.admin_scripts.tests.CustomTestRunner')
  901. # Original state hasn't changed
  902. self.assertEqual('DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ, address_predefined)
  903. self.assertEqual(os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS'), old_address)
  904. self.cmd.handle(verbosity=0, testrunner='regressiontests.admin_scripts.tests.CustomTestRunner',
  905. liveserver='blah')
  906. # Variable was correctly set
  907. self.assertEqual(os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'], 'blah')
  908. # Restore original state
  909. if address_predefined:
  910. os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = old_address
  911. else:
  912. del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']
  913. class ManageRunserver(AdminScriptTestCase):
  914. def setUp(self):
  915. from django.core.management.commands.runserver import Command
  916. def monkey_run(*args, **options):
  917. return
  918. self.cmd = Command()
  919. self.cmd.run = monkey_run
  920. def assertServerSettings(self, addr, port, ipv6=None, raw_ipv6=False):
  921. self.assertEqual(self.cmd.addr, addr)
  922. self.assertEqual(self.cmd.port, port)
  923. self.assertEqual(self.cmd.use_ipv6, ipv6)
  924. self.assertEqual(self.cmd._raw_ipv6, raw_ipv6)
  925. def test_runserver_addrport(self):
  926. self.cmd.handle()
  927. self.assertServerSettings('127.0.0.1', '8000')
  928. self.cmd.handle(addrport="1.2.3.4:8000")
  929. self.assertServerSettings('1.2.3.4', '8000')
  930. self.cmd.handle(addrport="7000")
  931. self.assertServerSettings('127.0.0.1', '7000')
  932. @unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
  933. def test_runner_addrport_ipv6(self):
  934. self.cmd.handle(addrport="", use_ipv6=True)
  935. self.assertServerSettings('::1', '8000', ipv6=True, raw_ipv6=True)
  936. self.cmd.handle(addrport="7000", use_ipv6=True)
  937. self.assertServerSettings('::1', '7000', ipv6=True, raw_ipv6=True)
  938. self.cmd.handle(addrport="[2001:0db8:1234:5678::9]:7000")
  939. self.assertServerSettings('2001:0db8:1234:5678::9', '7000', ipv6=True, raw_ipv6=True)
  940. def test_runner_hostname(self):
  941. self.cmd.handle(addrport="localhost:8000")
  942. self.assertServerSettings('localhost', '8000')
  943. self.cmd.handle(addrport="test.domain.local:7000")
  944. self.assertServerSettings('test.domain.local', '7000')
  945. @unittest.skipUnless(socket.has_ipv6, "platform doesn't support IPv6")
  946. def test_runner_hostname_ipv6(self):
  947. self.cmd.handle(addrport="test.domain.local:7000", use_ipv6=True)
  948. self.assertServerSettings('test.domain.local', '7000', ipv6=True)
  949. def test_runner_ambiguous(self):
  950. # Only 4 characters, all of which could be in an ipv6 address
  951. self.cmd.handle(addrport="beef:7654")
  952. self.assertServerSettings('beef', '7654')
  953. # Uses only characters that could be in an ipv6 address
  954. self.cmd.handle(addrport="deadbeef:7654")
  955. self.assertServerSettings('deadbeef', '7654')
  956. ##########################################################################
  957. # COMMAND PROCESSING TESTS
  958. # Check that user-space commands are correctly handled - in particular,
  959. # that arguments to the commands are correctly parsed and processed.
  960. ##########################################################################
  961. class CommandTypes(AdminScriptTestCase):
  962. "Tests for the various types of base command types that can be defined."
  963. def setUp(self):
  964. self.write_settings('settings.py')
  965. def tearDown(self):
  966. self.remove_settings('settings.py')
  967. def test_version(self):
  968. "version is handled as a special case"
  969. args = ['version']
  970. out, err = self.run_manage(args)
  971. self.assertNoOutput(err)
  972. self.assertOutput(out, get_version())
  973. def test_version_alternative(self):
  974. "--version is equivalent to version"
  975. args1, args2 = ['version'], ['--version']
  976. self.assertEqual(self.run_manage(args1), self.run_manage(args2))
  977. def test_help(self):
  978. "help is handled as a special case"
  979. args = ['help']
  980. out, err = self.run_manage(args)
  981. self.assertOutput(out, "Usage: manage.py subcommand [options] [args]")
  982. self.assertOutput(out, "Type 'manage.py help <subcommand>' for help on a specific subcommand.")
  983. self.assertOutput(out, '[django]')
  984. self.assertOutput(out, 'startapp')
  985. self.assertOutput(out, 'startproject')
  986. def test_help_commands(self):
  987. "help --commands shows the list of all available commands"
  988. args = ['help', '--commands']
  989. out, err = self.run_manage(args)
  990. self.assertNotInOutput(out, 'Usage:')
  991. self.assertNotInOutput(out, 'Options:')
  992. self.assertNotInOutput(out, '[django]')
  993. self.assertOutput(out, 'startapp')
  994. self.assertOutput(out, 'startproject')
  995. self.assertNotInOutput(out, '\n\n')
  996. def test_help_alternative(self):
  997. "--help is equivalent to help"
  998. args1, args2 = ['help'], ['--help']
  999. self.assertEqual(self.run_manage(args1), self.run_manage(args2))
  1000. def test_help_short_altert(self):
  1001. "-h is handled as a short form of --help"
  1002. args1, args2 = ['--help'], ['-h']
  1003. self.assertEqual(self.run_manage(args1), self.run_manage(args2))
  1004. def test_specific_help(self):
  1005. "--help can be used on a specific command"
  1006. args = ['sqlall', '--help']
  1007. out, err = self.run_manage(args)
  1008. self.assertNoOutput(err)
  1009. self.assertOutput(out, "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s).")
  1010. def test_base_command(self):
  1011. "User BaseCommands can execute when a label is provided"
  1012. args = ['base_command', 'testlabel']
  1013. out, err = self.run_manage(args)
  1014. self.assertNoOutput(err)
  1015. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1016. def test_base_command_no_label(self):
  1017. "User BaseCommands can execute when no labels are provided"
  1018. args = ['base_command']
  1019. out, err = self.run_manage(args)
  1020. self.assertNoOutput(err)
  1021. self.assertOutput(out, "EXECUTE:BaseCommand labels=(), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1022. def test_base_command_multiple_label(self):
  1023. "User BaseCommands can execute when no labels are provided"
  1024. args = ['base_command', 'testlabel', 'anotherlabel']
  1025. out, err = self.run_manage(args)
  1026. self.assertNoOutput(err)
  1027. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel', 'anotherlabel'), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1028. def test_base_command_with_option(self):
  1029. "User BaseCommands can execute with options when a label is provided"
  1030. args = ['base_command', 'testlabel', '--option_a=x']
  1031. out, err = self.run_manage(args)
  1032. self.assertNoOutput(err)
  1033. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1034. def test_base_command_with_options(self):
  1035. "User BaseCommands can execute with multiple options when a label is provided"
  1036. args = ['base_command', 'testlabel', '-a', 'x', '--option_b=y']
  1037. out, err = self.run_manage(args)
  1038. self.assertNoOutput(err)
  1039. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1040. def test_noargs(self):
  1041. "NoArg Commands can be executed"
  1042. args = ['noargs_command']
  1043. out, err = self.run_manage(args)
  1044. self.assertNoOutput(err)
  1045. self.assertOutput(out, "EXECUTE:NoArgsCommand options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1046. def test_noargs_with_args(self):
  1047. "NoArg Commands raise an error if an argument is provided"
  1048. args = ['noargs_command', 'argument']
  1049. out, err = self.run_manage(args)
  1050. self.assertOutput(err, "Error: Command doesn't accept any arguments")
  1051. def test_app_command(self):
  1052. "User AppCommands can execute when a single app name is provided"
  1053. args = ['app_command', 'auth']
  1054. out, err = self.run_manage(args)
  1055. self.assertNoOutput(err)
  1056. self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'")
  1057. self.assertOutput(out, os.sep.join(['django', 'contrib', 'auth', 'models.py']))
  1058. self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1059. def test_app_command_no_apps(self):
  1060. "User AppCommands raise an error when no app name is provided"
  1061. args = ['app_command']
  1062. out, err = self.run_manage(args)
  1063. self.assertOutput(err, 'Error: Enter at least one appname.')
  1064. def test_app_command_multiple_apps(self):
  1065. "User AppCommands raise an error when multiple app names are provided"
  1066. args = ['app_command', 'auth', 'contenttypes']
  1067. out, err = self.run_manage(args)
  1068. self.assertNoOutput(err)
  1069. self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'")
  1070. self.assertOutput(out, os.sep.join(['django', 'contrib', 'auth', 'models.py']))
  1071. self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1072. self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.contenttypes.models'")
  1073. self.assertOutput(out, os.sep.join(['django', 'contrib', 'contenttypes', 'models.py']))
  1074. self.assertOutput(out, "'>, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1075. def test_app_command_invalid_appname(self):
  1076. "User AppCommands can execute when a single app name is provided"
  1077. args = ['app_command', 'NOT_AN_APP']
  1078. out, err = self.run_manage(args)
  1079. self.assertOutput(err, "App with label NOT_AN_APP could not be found")
  1080. def test_app_command_some_invalid_appnames(self):
  1081. "User AppCommands can execute when some of the provided app names are invalid"
  1082. args = ['app_command', 'auth', 'NOT_AN_APP']
  1083. out, err = self.run_manage(args)
  1084. self.assertOutput(err, "App with label NOT_AN_APP could not be found")
  1085. def test_label_command(self):
  1086. "User LabelCommands can execute when a label is provided"
  1087. args = ['label_command', 'testlabel']
  1088. out, err = self.run_manage(args)
  1089. self.assertNoOutput(err)
  1090. self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1091. def test_label_command_no_label(self):
  1092. "User LabelCommands raise an error if no label is provided"
  1093. args = ['label_command']
  1094. out, err = self.run_manage(args)
  1095. self.assertOutput(err, 'Enter at least one label')
  1096. def test_label_command_multiple_label(self):
  1097. "User LabelCommands are executed multiple times if multiple labels are provided"
  1098. args = ['label_command', 'testlabel', 'anotherlabel']
  1099. out, err = self.run_manage(args)
  1100. self.assertNoOutput(err)
  1101. self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1102. self.assertOutput(out, "EXECUTE:LabelCommand label=anotherlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
  1103. class ArgumentOrder(AdminScriptTestCase):
  1104. """Tests for 2-stage argument parsing scheme.
  1105. django-admin command arguments are parsed in 2 parts; the core arguments
  1106. (--settings, --traceback and --pythonpath) are parsed using a Lax parser.
  1107. This Lax parser ignores any unknown options. Then the full settings are
  1108. passed to the command parser, which extracts commands of interest to the
  1109. individual command.
  1110. """
  1111. def setUp(self):
  1112. self.write_settings('settings.py', apps=['django.contrib.auth', 'django.contrib.contenttypes'])
  1113. self.write_settings('alternate_settings.py')
  1114. def tearDown(self):
  1115. self.remove_settings('settings.py')
  1116. self.remove_settings('alternate_settings.py')
  1117. def test_setting_then_option(self):
  1118. "Options passed after settings are correctly handled"
  1119. args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x']
  1120. out, err = self.run_manage(args)
  1121. self.assertNoOutput(err)
  1122. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  1123. def test_setting_then_short_option(self):
  1124. "Short options passed after settings are correctly handled"
  1125. args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x']
  1126. out, err = self.run_manage(args)
  1127. self.assertNoOutput(err)
  1128. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  1129. def test_option_then_setting(self):
  1130. "Options passed before settings are correctly handled"
  1131. args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings']
  1132. out, err = self.run_manage(args)
  1133. self.assertNoOutput(err)
  1134. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  1135. def test_short_option_then_setting(self):
  1136. "Short options passed before settings are correctly handled"
  1137. args = ['base_command', 'testlabel', '-a', 'x', '--settings=alternate_settings']
  1138. out, err = self.run_manage(args)
  1139. self.assertNoOutput(err)
  1140. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  1141. def test_option_then_setting_then_option(self):
  1142. "Options are correctly handled when they are passed before and after a setting"
  1143. args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings', '--option_b=y']
  1144. out, err = self.run_manage(args)
  1145. self.assertNoOutput(err)
  1146. self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None), ('settings', 'alternate_settings'), ('traceback', None), ('verbosity', '1')]")
  1147. class StartProject(LiveServerTestCase, AdminScriptTestCase):
  1148. def test_wrong_args(self):
  1149. "Make sure passing the wrong kinds of arguments raises a CommandError"
  1150. out, err = self.run_django_admin(['startproject'])
  1151. self.assertNoOutput(out)
  1152. self.assertOutput(err, "you must provide a project name")
  1153. def test_simple_project(self):
  1154. "Make sure the startproject management command creates a project"
  1155. args = ['startproject', 'testproject']
  1156. testproject_dir = os.path.join(test_dir, 'testproject')
  1157. out, err = self.run_django_admin(args)
  1158. self.addCleanup(shutil.rmtree, testproject_dir)
  1159. self.assertNoOutput(err)
  1160. self.assertTrue(os.path.isdir(testproject_dir))
  1161. # running again..
  1162. out, err = self.run_django_admin(args)
  1163. self.assertNoOutput(out)
  1164. self.assertOutput(err, "already exists")
  1165. def test_invalid_project_name(self):
  1166. "Make sure the startproject management command validates a project name"
  1167. def cleanup(p):
  1168. if os.path.exists(p):
  1169. shutil.rmtree(p)
  1170. args = ['startproject', '7testproject']
  1171. testproject_dir = os.path.join(test_dir, '7testproject')
  1172. out, err = self.run_django_admin(args)
  1173. self.addCleanup(cleanup, testproject_dir)
  1174. self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
  1175. self.assertFalse(os.path.exists(testproject_dir))
  1176. def test_simple_project_different_directory(self):
  1177. "Make sure the startproject management command creates a project in a specific directory"
  1178. args = ['startproject', 'testproject', 'othertestproject']
  1179. testproject_dir = os.path.join(test_dir, 'othertestproject')
  1180. os.mkdir(testproject_dir)
  1181. out, err = self.run_django_admin(args)
  1182. self.addCleanup(shutil.rmtree, testproject_dir)
  1183. self.assertNoOutput(err)
  1184. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'manage.py')))
  1185. # running again..
  1186. out, err = self.run_django_admin(args)
  1187. self.assertNoOutput(out)
  1188. self.assertOutput(err, "already exists")
  1189. def test_custom_project_template(self):
  1190. "Make sure the startproject management command is able to use a different project template"
  1191. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
  1192. args = ['startproject', '--template', template_path, 'customtestproject']
  1193. testproject_dir = os.path.join(test_dir, 'customtestproject')
  1194. out, err = self.run_django_admin(args)
  1195. self.addCleanup(shutil.rmtree, testproject_dir)
  1196. self.assertNoOutput(err)
  1197. self.assertTrue(os.path.isdir(testproject_dir))
  1198. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
  1199. def test_template_dir_with_trailing_slash(self):
  1200. "Ticket 17475: Template dir passed has a trailing path separator"
  1201. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template' + os.sep)
  1202. args = ['startproject', '--template', template_path, 'customtestproject']
  1203. testproject_dir = os.path.join(test_dir, 'customtestproject')
  1204. out, err = self.run_django_admin(args)
  1205. self.addCleanup(shutil.rmtree, testproject_dir)
  1206. self.assertNoOutput(err)
  1207. self.assertTrue(os.path.isdir(testproject_dir))
  1208. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
  1209. def test_custom_project_template_from_tarball_by_path(self):
  1210. "Make sure the startproject management command is able to use a different project template from a tarball"
  1211. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template.tgz')
  1212. args = ['startproject', '--template', template_path, 'tarballtestproject']
  1213. testproject_dir = os.path.join(test_dir, 'tarballtestproject')
  1214. out, err = self.run_django_admin(args)
  1215. self.addCleanup(shutil.rmtree, testproject_dir)
  1216. self.assertNoOutput(err)
  1217. self.assertTrue(os.path.isdir(testproject_dir))
  1218. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
  1219. def test_custom_project_template_from_tarball_to_alternative_location(self):
  1220. "Startproject can use a project template from a tarball and create it in a specified location"
  1221. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template.tgz')
  1222. args = ['startproject', '--template', template_path, 'tarballtestproject', 'altlocation']
  1223. testproject_dir = os.path.join(test_dir, 'altlocation')
  1224. os.mkdir(testproject_dir)
  1225. out, err = self.run_django_admin(args)
  1226. self.addCleanup(shutil.rmtree, testproject_dir)
  1227. self.assertNoOutput(err)
  1228. self.assertTrue(os.path.isdir(testproject_dir))
  1229. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
  1230. def test_custom_project_template_from_tarball_by_url(self):
  1231. "Make sure the startproject management command is able to use a different project template from a tarball via a url"
  1232. template_url = '%s/admin_scripts/custom_templates/project_template.tgz' % self.live_server_url
  1233. args = ['startproject', '--template', template_url, 'urltestproject']
  1234. testproject_dir = os.path.join(test_dir, 'urltestproject')
  1235. out, err = self.run_django_admin(args)
  1236. self.addCleanup(shutil.rmtree, testproject_dir)
  1237. self.assertNoOutput(err)
  1238. self.assertTrue(os.path.isdir(testproject_dir))
  1239. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
  1240. def test_project_template_tarball_url(self):
  1241. "Startproject management command handles project template tar/zip balls from non-canonical urls"
  1242. template_url = '%s/admin_scripts/custom_templates/project_template.tgz/' % self.live_server_url
  1243. args = ['startproject', '--template', template_url, 'urltestproject']
  1244. testproject_dir = os.path.join(test_dir, 'urltestproject')
  1245. out, err = self.run_django_admin(args)
  1246. self.addCleanup(shutil.rmtree, testproject_dir)
  1247. self.assertNoOutput(err)
  1248. self.assertTrue(os.path.isdir(testproject_dir))
  1249. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
  1250. def test_file_without_extension(self):
  1251. "Make sure the startproject management command is able to render custom files"
  1252. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
  1253. args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile']
  1254. testproject_dir = os.path.join(test_dir, 'customtestproject')
  1255. out, err = self.run_django_admin(args)
  1256. self.addCleanup(shutil.rmtree, testproject_dir)
  1257. self.assertNoOutput(err)
  1258. self.assertTrue(os.path.isdir(testproject_dir))
  1259. self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
  1260. base_path = os.path.join(testproject_dir, 'additional_dir')
  1261. for f in ('Procfile', 'additional_file.py', 'requirements.txt'):
  1262. self.assertTrue(os.path.exists(os.path.join(base_path, f)))
  1263. with open(os.path.join(base_path, f)) as fh:
  1264. self.assertEqual(fh.read(),
  1265. '# some file for customtestproject test project')
  1266. def test_custom_project_template_context_variables(self):
  1267. "Make sure template context variables are rendered with proper values"
  1268. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
  1269. args = ['startproject', '--template', template_path, 'another_project', 'project_dir']
  1270. testproject_dir = os.path.join(test_dir, 'project_dir')
  1271. os.mkdir(testproject_dir)
  1272. out, err = self.run_django_admin(args)
  1273. self.addCleanup(shutil.rmtree, testproject_dir)
  1274. self.assertNoOutput(err)
  1275. test_manage_py = os.path.join(testproject_dir, 'manage.py')
  1276. with open(test_manage_py, 'r') as fp:
  1277. content = fp.read()
  1278. self.assertIn("project_name = 'another_project'", content)
  1279. self.assertIn("project_directory = '%s'" % testproject_dir, content)
  1280. def test_custom_project_destination_missing(self):
  1281. """
  1282. Make sure an exception is raised when the provided
  1283. destination directory doesn't exist
  1284. """
  1285. template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
  1286. args = ['startproject', '--template', template_path, 'yet_another_project', 'project_dir2']
  1287. testproject_dir = os.path.join(test_dir, 'project_dir2')
  1288. out, err = self.run_django_admin(args)
  1289. self.assertNoOutput(out)
  1290. self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir)
  1291. self.assertFalse(os.path.exists(testproject_dir))