Skip to content

Commit

Permalink
fix: do not initialize new project within a non-empty directory
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed Apr 20, 2020
1 parent edf8b74 commit d70c8fc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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

0 comments on commit d70c8fc

Please sign in to comment.