From f6d78ceeb5e7e020284e99822468ae03b447f2e5 Mon Sep 17 00:00:00 2001 From: arimbr Date: Sun, 15 Oct 2017 16:02:47 +0200 Subject: [PATCH 1/6] Set cookiecutter overwrite_if_exists parameter to True if current directory is empty --- bonobo/commands/init.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bonobo/commands/init.py b/bonobo/commands/init.py index 948f747..9a157ca 100644 --- a/bonobo/commands/init.py +++ b/bonobo/commands/init.py @@ -1,4 +1,6 @@ -def execute(name, branch): +import os + +def execute(name, branch, overwrite_if_exists=False): try: from cookiecutter.main import cookiecutter except ImportError as exc: @@ -6,11 +8,15 @@ def execute(name, branch): 'You must install "cookiecutter" to use this command.\n\n $ pip install cookiecutter\n' ) from exc + if os.listdir(os.getcwd()) == []: + overwrite_if_exists = True + return cookiecutter( 'https://github.com/python-bonobo/cookiecutter-bonobo.git', extra_context={'name': name}, no_input=True, - checkout=branch + checkout=branch, + overwrite_if_exists=overwrite_if_exists ) From f1e9969a8843c493fd5155066cf35bf33b937384 Mon Sep 17 00:00:00 2001 From: arimbr Date: Sun, 15 Oct 2017 23:54:26 +0200 Subject: [PATCH 2/6] Add tests for bonobo init new directory and init within empty directory --- tests/test_commands.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index a29465c..e9ba1bd 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,6 +1,7 @@ import os import runpy import sys +import shutil from unittest.mock import patch import pkg_resources @@ -43,6 +44,27 @@ def test_no_command(runner, capsys): assert 'error: the following arguments are required: command' in err +@all_runners +def test_init(runner, capsys): + runner('init', 'project-name') + out, err = capsys.readouterr() + out = out.strip() + shutil.rmtree('project-name') + assert out == '' + + +@all_runners +def test_init_within_empty_directory(runner, capsys): + os.mkdir('empty-directory') + os.chdir('empty-directory') + runner('init', '.') + out, err = capsys.readouterr() + out = out.strip() + os.chdir('..') + shutil.rmtree('empty-directory') + assert out == '' + + @all_runners def test_run(runner, capsys): runner('run', '--quiet', get_examples_path('types/strings.py')) From df45251622e6b935b27022e36fcbd79e9228f989 Mon Sep 17 00:00:00 2001 From: arimbr Date: Sun, 22 Oct 2017 23:05:58 +0200 Subject: [PATCH 3/6] Check if target directory is empty instead of current directory and remove overwrite_if_exists argument --- bonobo/commands/init.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bonobo/commands/init.py b/bonobo/commands/init.py index 9a157ca..e69156c 100644 --- a/bonobo/commands/init.py +++ b/bonobo/commands/init.py @@ -1,6 +1,6 @@ import os -def execute(name, branch, overwrite_if_exists=False): +def execute(name, branch): try: from cookiecutter.main import cookiecutter except ImportError as exc: @@ -8,7 +8,9 @@ def execute(name, branch, overwrite_if_exists=False): 'You must install "cookiecutter" to use this command.\n\n $ pip install cookiecutter\n' ) from exc - if os.listdir(os.getcwd()) == []: + overwrite_if_exists = False + project_path = os.path.join(os.getcwd(), name) + if os.path.isdir(project_path) and not os.listdir(project_path): overwrite_if_exists = True return cookiecutter( From 9820fca2b4ecb2c4abffba173c1cc0b52bf9ee14 Mon Sep 17 00:00:00 2001 From: arimbr Date: Sun, 22 Oct 2017 23:08:25 +0200 Subject: [PATCH 4/6] Use pytest tmpdir fixture and add more init tests --- tests/test_commands.py | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index e9ba1bd..fadf6e2 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -6,9 +6,11 @@ from unittest.mock import patch import pkg_resources import pytest +from cookiecutter.exceptions import OutputDirExistsException from bonobo import __main__, __version__, get_examples_path from bonobo.commands import entrypoint +from bonobo.commands.run import DEFAULT_GRAPH_FILENAMES def runner_entrypoint(*args): @@ -45,24 +47,37 @@ def test_no_command(runner, capsys): @all_runners -def test_init(runner, capsys): - runner('init', 'project-name') - out, err = capsys.readouterr() - out = out.strip() - shutil.rmtree('project-name') - assert out == '' +def test_init(runner, tmpdir): + name = 'project' + os.chdir(tmpdir) + runner('init', name) + assert os.path.isdir(name) + assert set(os.listdir(name)) & set(DEFAULT_GRAPH_FILENAMES) @all_runners -def test_init_within_empty_directory(runner, capsys): - os.mkdir('empty-directory') - os.chdir('empty-directory') +def test_init_in_empty_directory(runner, tmpdir): + name = 'project' + os.chdir(tmpdir) + os.mkdir(name) + runner('init', name) + assert set(os.listdir(name)) & set(DEFAULT_GRAPH_FILENAMES) + + +@all_runners +def test_init_in_non_empty_directory(runner, tmpdir): + name = 'project' + os.chdir(tmpdir) + runner('init', name) + with pytest.raises(OutputDirExistsException): + runner('init', name) + + +@all_runners +def test_init_within_empty_directory(runner, tmpdir): + os.chdir(tmpdir) runner('init', '.') - out, err = capsys.readouterr() - out = out.strip() - os.chdir('..') - shutil.rmtree('empty-directory') - assert out == '' + assert set(os.listdir()) & set(DEFAULT_GRAPH_FILENAMES) @all_runners From 7c0071bfdfc3a543d64a0cfe6f28f82b710330b3 Mon Sep 17 00:00:00 2001 From: arimbr Date: Sun, 22 Oct 2017 23:30:23 +0200 Subject: [PATCH 5/6] Remove unused shutil import --- tests/test_commands.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index fadf6e2..1ff3a3b 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,7 +1,6 @@ import os import runpy import sys -import shutil from unittest.mock import patch import pkg_resources From 4641425e4cef83d619ed39ea62ea2e6963ad6416 Mon Sep 17 00:00:00 2001 From: arimbr Date: Mon, 23 Oct 2017 00:30:10 +0200 Subject: [PATCH 6/6] Fix python 3.5 os.chdir not accepting LocalPath --- tests/test_commands.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 1ff3a3b..069cead 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -48,7 +48,7 @@ def test_no_command(runner, capsys): @all_runners def test_init(runner, tmpdir): name = 'project' - os.chdir(tmpdir) + tmpdir.chdir() runner('init', name) assert os.path.isdir(name) assert set(os.listdir(name)) & set(DEFAULT_GRAPH_FILENAMES) @@ -57,7 +57,7 @@ def test_init(runner, tmpdir): @all_runners def test_init_in_empty_directory(runner, tmpdir): name = 'project' - os.chdir(tmpdir) + tmpdir.chdir() os.mkdir(name) runner('init', name) assert set(os.listdir(name)) & set(DEFAULT_GRAPH_FILENAMES) @@ -66,7 +66,7 @@ def test_init_in_empty_directory(runner, tmpdir): @all_runners def test_init_in_non_empty_directory(runner, tmpdir): name = 'project' - os.chdir(tmpdir) + tmpdir.chdir() runner('init', name) with pytest.raises(OutputDirExistsException): runner('init', name) @@ -74,7 +74,7 @@ def test_init_in_non_empty_directory(runner, tmpdir): @all_runners def test_init_within_empty_directory(runner, tmpdir): - os.chdir(tmpdir) + tmpdir.chdir() runner('init', '.') assert set(os.listdir()) & set(DEFAULT_GRAPH_FILENAMES)