From 53f6cc055fb9bbc1cb15f393ef0cec434101eb82 Mon Sep 17 00:00:00 2001 From: cwandrews Date: Wed, 11 Oct 2017 21:49:39 -0400 Subject: [PATCH] Fixed bug involved in finding env when running module. --- bonobo/commands/run.py | 4 +- bonobo/examples/environment/env_files/.env2 | 2 - .../examples/environment/env_files/.env_two | 2 + tests/test_commands.py | 87 ++++++++++++------- 4 files changed, 59 insertions(+), 36 deletions(-) delete mode 100644 bonobo/examples/environment/env_files/.env2 create mode 100644 bonobo/examples/environment/env_files/.env_two diff --git a/bonobo/commands/run.py b/bonobo/commands/run.py index 033e17c..7b23b9d 100644 --- a/bonobo/commands/run.py +++ b/bonobo/commands/run.py @@ -71,14 +71,14 @@ def read(filename, module, install=False, quiet=False, verbose=False, default_en requirements = os.path.join(os.path.dirname(filename), 'requirements.txt') _install_requirements(requirements) context = runpy.run_path(filename, run_name='__bonobo__') - env_dir = Path(filename).parent elif module: context = runpy.run_module(module, run_name='__bonobo__') filename = context['__file__'] - env_dir = Path(module) else: raise RuntimeError('UNEXPECTED: argparse should not allow this.') + env_dir = Path(filename).parent or Path(module).parent + if default_env_file: for f in default_env_file: env_file_path = env_dir.joinpath(f) diff --git a/bonobo/examples/environment/env_files/.env2 b/bonobo/examples/environment/env_files/.env2 deleted file mode 100644 index 1b91848..0000000 --- a/bonobo/examples/environment/env_files/.env2 +++ /dev/null @@ -1,2 +0,0 @@ -TEST_USER_PASSWORD='not_sweet_password' -PATH='abril' \ No newline at end of file diff --git a/bonobo/examples/environment/env_files/.env_two b/bonobo/examples/environment/env_files/.env_two new file mode 100644 index 0000000..672d6d2 --- /dev/null +++ b/bonobo/examples/environment/env_files/.env_two @@ -0,0 +1,2 @@ +TEST_USER_PASSWORD=not_sweet_password +PATH='abril' \ No newline at end of file diff --git a/tests/test_commands.py b/tests/test_commands.py index adb0317..59f3db8 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -98,9 +98,8 @@ def test_version(runner, capsys): assert __version__ in out +@all_runners class TestDefaultEnvFile(object): - - @all_runners def test_run_file_with_default_env_file(self, runner, capsys): runner( 'run', '--quiet', '--default-env-file', '.env', @@ -112,11 +111,10 @@ class TestDefaultEnvFile(object): assert out[1] == 'sweetpassword' assert out[2] != 'marzo' - @all_runners def test_run_file_with_multiple_default_env_files(self, runner, capsys): runner( 'run', '--quiet', '--default-env-file', '.env', - '--default-env-file', '.env2', + '--default-env-file', '.env_two', get_examples_path('environment/env_files/get_passed_env_file.py') ) out, err = capsys.readouterr() @@ -125,7 +123,6 @@ class TestDefaultEnvFile(object): assert out[1] == 'sweetpassword' assert out[2] != 'marzo' - @all_runners def test_run_module_with_default_env_file(self, runner, capsys): runner( 'run', '--quiet', '-m', @@ -138,12 +135,11 @@ class TestDefaultEnvFile(object): assert out[1] == 'sweetpassword' assert out[2] != 'marzo' - @all_runners def test_run_module_with_multiple_default_env_files(self, runner, capsys): runner( 'run', '--quiet', '-m', 'bonobo.examples.environment.env_files.get_passed_env_file', - '--default-env-file', '.env', '--default-env-file', '.env2', + '--default-env-file', '.env', '--default-env-file', '.env_two', ) out, err = capsys.readouterr() out = out.split('\n') @@ -153,36 +149,62 @@ class TestDefaultEnvFile(object): @all_runners -def test_run_file_with_env_file(runner, capsys): - runner( - 'run', '--quiet', '--env-file', '.env', - get_examples_path('environment/env_files/get_passed_env_file.py') - ) - out, err = capsys.readouterr() - out = out.split('\n') - assert out[0] == '321' - assert out[1] == 'sweetpassword' - assert out[2] == 'marzo' +class TestEnvFile(object): + def test_run_file_with_file(self, runner, capsys): + runner( + 'run', '--quiet', + get_examples_path('environment/env_files/get_passed_env_file.py'), + '--env-file', '.env', + ) + out, err = capsys.readouterr() + out = out.split('\n') + assert out[0] == '321' + assert out[1] == 'sweetpassword' + assert out[2] == 'marzo' + def test_run_file_with_multiple_files(self, runner, capsys): + runner( + 'run', '--quiet', + get_examples_path('environment/env_files/get_passed_env_file.py'), + '--env-file', '.env', '--env-file', '.env_two', + ) + out, err = capsys.readouterr() + out = out.split('\n') + assert out[0] == '321' + assert out[1] == 'not_sweet_password' + assert out[2] == 'abril' -@all_runners -def test_run_file_with_multiple_env_files(runner, capsys): - runner( - 'run', '--quiet', '--env-file', '.env', '--env-file', '.env2', - get_examples_path('environment/env_files/get_passed_env_file.py') - ) - out, err = capsys.readouterr() - out = out.split('\n') - assert out[0] == '321' - assert out[1] == 'not_sweet_password' - assert out[2] == 'abril' + def test_run_module_with_file(self, runner, capsys): + runner( + 'run', '--quiet', '-m', + 'bonobo.examples.environment.env_files.get_passed_env_file', + '--env-file', '.env', + ) + out, err = capsys.readouterr() + out = out.split('\n') + assert out[0] == '321' + assert out[1] == 'sweetpassword' + assert out[2] == 'marzo' + + def test_run_module_with_multiple_files(self, runner, capsys): + runner( + 'run', '--quiet', '-m', + 'bonobo.examples.environment.env_files.get_passed_env_file', + '--env-file', '.env', '--env-file', '.env_two', + ) + out, err = capsys.readouterr() + out = out.split('\n') + assert out[0] == '321' + assert out[1] == 'not_sweet_password' + assert out[2] == 'abril' @all_runners def test_run_file_with_default_env_file_and_env_file(runner, capsys): runner( - 'run', '--quiet', '--default-env-file', '.env', '--env-file', '.env2', - get_examples_path('environment/env_files/get_passed_env_file.py') + 'run', '--quiet', + get_examples_path('environment/env_files/get_passed_env_file.py'), + '--default-env-file', '.env', '--env-file', '.env_two', ) out, err = capsys.readouterr() out = out.split('\n') @@ -194,9 +216,10 @@ def test_run_file_with_default_env_file_and_env_file(runner, capsys): @all_runners def test_run_file_with_default_env_file_and_env_file_and_env_vars(runner, capsys): runner( - 'run', '--quiet', '--default-env-file', '.env', '--env-file', '.env2', + 'run', '--quiet', + get_examples_path('environment/env_files/get_passed_env_file.py'), + '--default-env-file', '.env', '--env-file', '.env_two', '--env', 'TEST_USER_PASSWORD=SWEETpassWORD', '--env', 'MY_SECRET=444', - get_examples_path('environment/env_files/get_passed_env_file.py') ) out, err = capsys.readouterr() out = out.split('\n')