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

fix: do not initialize new project within a non-empty directory #428

Merged
merged 1 commit into from
Apr 20, 2020
Merged
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
9 changes: 5 additions & 4 deletions brownie/_cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<path> Path to initialize (default is the current path)

Options:
--force -f Allow init inside a project subfolder
--force -f Allow initialization inside a directory that is not
empty, or a subdirectory of an existing project
--help -h Display this message

brownie init is used to create new brownie projects. It creates the default
Expand All @@ -22,10 +23,10 @@
reports/ Report files for contract analysis
scripts/ Scripts for deployment and interaction
tests/ Scripts for project testing
brownie-config.yaml Project configuration file"""
"""


def main():
args = docopt(__doc__)
path = project.new(args["<path>"] or ".", args["--force"])
notify("SUCCESS", f"Brownie environment has been initiated at {path}")
path = project.new(args["<path>"] or ".", args["--force"], args["--force"])
notify("SUCCESS", f"A new Brownie project has been initialized at {path}")
13 changes: 9 additions & 4 deletions brownie/project/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,21 @@ def get_loaded_projects() -> List:
return _loaded_projects.copy()


def new(project_path_str: str = ".", ignore_subfolder: bool = False) -> str:
def new(
project_path_str: str = ".", ignore_subfolder: bool = False, ignore_existing: bool = False
) -> str:
"""Initializes a new project.

Args:
project_path: Path to initialize the project at. If not exists, it will be created.
ignore_subfolders: If True, will not raise if initializing in a project subfolder.
ignore_subfolder: If True, will not raise if initializing in a project subdirectory.
ignore_existing: If True, will not raise when initiating in a non-empty directory.

Returns the path to the project as a string.
"""
project_path = _new_checks(project_path_str, ignore_subfolder)
if not ignore_existing and project_path.exists() and list(project_path.glob("*")):
raise FileExistsError(f"Directory is not empty: {project_path}")
project_path.mkdir(exist_ok=True)
_create_folders(project_path)
_create_gitfiles(project_path)
Expand Down Expand Up @@ -551,7 +556,7 @@ def _install_from_ethpm(uri: str) -> str:
raise FileExistsError("Package is aleady installed")

try:
new(str(install_path))
new(str(install_path), ignore_existing=True)
ethpm.install_package(install_path, uri)
project = load(install_path)
project.close()
Expand Down Expand Up @@ -619,7 +624,7 @@ def _install_from_github(package_id: str) -> str:
try:
if not install_path.joinpath("contracts").exists():
raise Exception
new(str(install_path))
new(str(install_path), ignore_existing=True)
project = load(install_path)
project.close()
except Exception:
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/test_cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def close(self):
def test_cli_init(cli_tester):
cli_tester.monkeypatch.setattr("brownie.project.new", cli_tester.mock_subroutines)

args = (".", False)
args = (".", False, False)
kwargs = {}
parameters = (args, kwargs)
cli_tester.run_and_test_parameters("init", parameters)

args = ("test/path", True)
args = ("test/path", True, True)
parameters = (args, kwargs)
cli_tester.run_and_test_parameters("init test/path --force", parameters)

Expand Down