Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force flag for project creation in non-empty directories #1499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/hatch/cli/new/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@
@click.option('--interactive', '-i', 'interactive', is_flag=True, help='Interactively choose details about the project')
@click.option('--cli', 'feature_cli', is_flag=True, help='Give the project a command line interface')
@click.option('--init', 'initialize', is_flag=True, help='Initialize an existing project')
@click.option(
'--force',
'-f',
is_flag=True,
help=(
'Force project creation if location is not empty. '
'Any pre-existing files that are also part of the project template will be overwritten. '
'Use with caution as it may lead to loss of data.'
),
)
@click.option('-so', 'setuptools_options', multiple=True, hidden=True)
@click.pass_obj
def new(app, name, location, interactive, feature_cli, initialize, setuptools_options):
def new(app, name, location, interactive, feature_cli, initialize, force, setuptools_options):
"""Create or initialize a project."""
import sys
from copy import deepcopy
Expand Down Expand Up @@ -46,7 +56,7 @@ def new(app, name, location, interactive, feature_cli, initialize, setuptools_op
if location.is_file():
app.abort(f'Path `{location}` points to a file.')
elif location.is_dir() and any(location.iterdir()):
if not initialize:
if not initialize and not force:
app.abort(f'Directory `{location}` is not empty.')

needs_config_update = (location / 'pyproject.toml').is_file()
Expand Down
30 changes: 30 additions & 0 deletions tests/cli/new/test_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from hatch.config.constants import ConfigEnvVars
from hatch.template import File


def remove_trailing_spaces(text):
Expand Down Expand Up @@ -104,6 +105,35 @@ def test_default_explicit_path(hatch, helpers, temp_dir):
)


def test_default_explicit_path_forced(hatch, helpers, temp_dir):
project_name = 'My.App'

with temp_dir.as_cwd():
path = temp_dir / 'foo'
path.touch()

result = hatch('new', project_name, '.', '-f')

expected_files = [*helpers.get_template_files('new.default', project_name), File('foo')]
helpers.assert_files(temp_dir, expected_files)

assert result.exit_code == 0, result.output
assert remove_trailing_spaces(result.output) == helpers.dedent(
"""
src
└── my_app
├── __about__.py
└── __init__.py
tests
└── __init__.py
LICENSE.txt
README.md
foo
pyproject.toml
"""
)


def test_default_empty_plugins_table(hatch, helpers, config_file, temp_dir):
project_name = 'My.App'
config_file.model.template.plugins = {}
Expand Down