1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #18296 -- Created custom target directory if missing in startproject and startapp

This commit is contained in:
Bruno Alla 2024-07-31 14:40:12 +01:00
parent 57307bbc7d
commit 101bb9bb53
No known key found for this signature in database
4 changed files with 32 additions and 17 deletions

View File

@ -105,10 +105,10 @@ class TemplateCommand(BaseCommand):
if app_or_project == "app":
self.validate_name(os.path.basename(top_dir), "directory")
if not os.path.exists(top_dir):
raise CommandError(
"Destination directory '%s' does not "
"exist, please create it first." % top_dir
)
try:
os.makedirs(top_dir)
except OSError as e:
raise CommandError(e)
# Find formatters, which are external executables, before input
# from the templates can sneak into the path.

View File

@ -1247,9 +1247,9 @@ By default, :source:`the new directory <django/conf/app_template>` contains a
``models.py`` file and other app template files. If only the app name is given,
the app directory will be created in the current working directory.
If the optional destination is provided, Django will use that existing
directory rather than creating a new one. You can use '.' to denote the current
working directory.
If the optional destination is provided, Django will use that name instead. If
the directory with the given name doesn't exist, it will be created. You can
use '.' to denote the current working directory.
For example:
@ -1359,9 +1359,10 @@ If only the project name is given, both the project directory and project
package will be named ``<projectname>`` and the project directory
will be created in the current working directory.
If the optional destination is provided, Django will use that existing
directory as the project directory, and create ``manage.py`` and the project
package within it. Use '.' to denote the current working directory.
If the optional destination is provided, Django will use that name as the
project directory, and create ``manage.py`` and the project package within it.
If the directory with the given name doesn't exist, it will be created. Use '.'
to denote the current working directory.
For example:

View File

@ -199,6 +199,9 @@ Management Commands
setting the :envvar:`HIDE_PRODUCTION_WARNING` environment variable to
``"true"``.
* The :djadmin:`startproject` and :djadmin:`startapp` commands now create the
custom target directory if it doesn't exist.
Migrations
~~~~~~~~~~

View File

@ -2793,7 +2793,7 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
def test_custom_project_destination_missing(self):
"""
Make sure an exception is raised when the provided
Create the directory when the provided
destination directory doesn't exist
"""
template_path = os.path.join(custom_templates_dir, "project_template")
@ -2807,12 +2807,8 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
testproject_dir = os.path.join(self.test_dir, "project_dir2")
out, err = self.run_django_admin(args)
self.assertNoOutput(out)
self.assertOutput(
err,
"Destination directory '%s' does not exist, please create it first."
% testproject_dir,
)
self.assertFalse(os.path.exists(testproject_dir))
self.assertNoOutput(err)
self.assertTrue(os.path.exists(testproject_dir))
def test_custom_project_template_with_non_ascii_templates(self):
"""
@ -3039,6 +3035,21 @@ class StartApp(AdminScriptTestCase):
content,
)
def test_custom_app_destination_missing(self):
"""
Create the directory when the provided destination directory doesn't exist
"""
args = [
"startapp",
"my_app",
"my_app",
]
testapp_dir = os.path.join(self.test_dir, "my_app")
out, err = self.run_django_admin(args)
self.assertNoOutput(out)
self.assertNoOutput(err)
self.assertTrue(os.path.exists(testapp_dir))
class DiffSettings(AdminScriptTestCase):
"""Tests for diffsettings management command."""