From b5928e920c04c3585cc47fe445382bfc5c1ac3dd Mon Sep 17 00:00:00 2001 From: iamdefinitelyahuman Date: Mon, 20 Apr 2020 15:11:28 +0400 Subject: [PATCH] fix: do not initialize new project within a non-empty directory --- brownie/_cli/init.py | 9 +++++---- brownie/project/main.py | 13 +++++++++---- tests/cli/test_cli_main.py | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/brownie/_cli/init.py b/brownie/_cli/init.py index bf6789278..6859ef370 100644 --- a/brownie/_cli/init.py +++ b/brownie/_cli/init.py @@ -10,7 +10,8 @@ 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 @@ -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[""] or ".", args["--force"]) - notify("SUCCESS", f"Brownie environment has been initiated at {path}") + path = project.new(args[""] or ".", args["--force"], args["--force"]) + notify("SUCCESS", f"A new Brownie project has been initialized at {path}") diff --git a/brownie/project/main.py b/brownie/project/main.py index ea956b9c7..7202741bb 100644 --- a/brownie/project/main.py +++ b/brownie/project/main.py @@ -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) @@ -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() @@ -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: diff --git a/tests/cli/test_cli_main.py b/tests/cli/test_cli_main.py index b0e6e9a35..f8247dec3 100644 --- a/tests/cli/test_cli_main.py +++ b/tests/cli/test_cli_main.py @@ -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)