Fixed bug involved in finding env when running module.

This commit is contained in:
cwandrews
2017-10-11 21:49:39 -04:00
parent 489d22cbcb
commit 53f6cc055f
4 changed files with 59 additions and 36 deletions

View File

@ -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') requirements = os.path.join(os.path.dirname(filename), 'requirements.txt')
_install_requirements(requirements) _install_requirements(requirements)
context = runpy.run_path(filename, run_name='__bonobo__') context = runpy.run_path(filename, run_name='__bonobo__')
env_dir = Path(filename).parent
elif module: elif module:
context = runpy.run_module(module, run_name='__bonobo__') context = runpy.run_module(module, run_name='__bonobo__')
filename = context['__file__'] filename = context['__file__']
env_dir = Path(module)
else: else:
raise RuntimeError('UNEXPECTED: argparse should not allow this.') raise RuntimeError('UNEXPECTED: argparse should not allow this.')
env_dir = Path(filename).parent or Path(module).parent
if default_env_file: if default_env_file:
for f in default_env_file: for f in default_env_file:
env_file_path = env_dir.joinpath(f) env_file_path = env_dir.joinpath(f)

View File

@ -1,2 +0,0 @@
TEST_USER_PASSWORD='not_sweet_password'
PATH='abril'

View File

@ -0,0 +1,2 @@
TEST_USER_PASSWORD=not_sweet_password
PATH='abril'

View File

@ -98,9 +98,8 @@ def test_version(runner, capsys):
assert __version__ in out assert __version__ in out
@all_runners
class TestDefaultEnvFile(object): class TestDefaultEnvFile(object):
@all_runners
def test_run_file_with_default_env_file(self, runner, capsys): def test_run_file_with_default_env_file(self, runner, capsys):
runner( runner(
'run', '--quiet', '--default-env-file', '.env', 'run', '--quiet', '--default-env-file', '.env',
@ -112,11 +111,10 @@ class TestDefaultEnvFile(object):
assert out[1] == 'sweetpassword' assert out[1] == 'sweetpassword'
assert out[2] != 'marzo' assert out[2] != 'marzo'
@all_runners
def test_run_file_with_multiple_default_env_files(self, runner, capsys): def test_run_file_with_multiple_default_env_files(self, runner, capsys):
runner( runner(
'run', '--quiet', '--default-env-file', '.env', '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') get_examples_path('environment/env_files/get_passed_env_file.py')
) )
out, err = capsys.readouterr() out, err = capsys.readouterr()
@ -125,7 +123,6 @@ class TestDefaultEnvFile(object):
assert out[1] == 'sweetpassword' assert out[1] == 'sweetpassword'
assert out[2] != 'marzo' assert out[2] != 'marzo'
@all_runners
def test_run_module_with_default_env_file(self, runner, capsys): def test_run_module_with_default_env_file(self, runner, capsys):
runner( runner(
'run', '--quiet', '-m', 'run', '--quiet', '-m',
@ -138,12 +135,11 @@ class TestDefaultEnvFile(object):
assert out[1] == 'sweetpassword' assert out[1] == 'sweetpassword'
assert out[2] != 'marzo' assert out[2] != 'marzo'
@all_runners
def test_run_module_with_multiple_default_env_files(self, runner, capsys): def test_run_module_with_multiple_default_env_files(self, runner, capsys):
runner( runner(
'run', '--quiet', '-m', 'run', '--quiet', '-m',
'bonobo.examples.environment.env_files.get_passed_env_file', '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, err = capsys.readouterr()
out = out.split('\n') out = out.split('\n')
@ -153,10 +149,12 @@ class TestDefaultEnvFile(object):
@all_runners @all_runners
def test_run_file_with_env_file(runner, capsys): class TestEnvFile(object):
def test_run_file_with_file(self, runner, capsys):
runner( runner(
'run', '--quiet', '--env-file', '.env', 'run', '--quiet',
get_examples_path('environment/env_files/get_passed_env_file.py') get_examples_path('environment/env_files/get_passed_env_file.py'),
'--env-file', '.env',
) )
out, err = capsys.readouterr() out, err = capsys.readouterr()
out = out.split('\n') out = out.split('\n')
@ -164,12 +162,35 @@ def test_run_file_with_env_file(runner, capsys):
assert out[1] == 'sweetpassword' assert out[1] == 'sweetpassword'
assert out[2] == 'marzo' assert out[2] == 'marzo'
def test_run_file_with_multiple_files(self, runner, capsys):
@all_runners
def test_run_file_with_multiple_env_files(runner, capsys):
runner( runner(
'run', '--quiet', '--env-file', '.env', '--env-file', '.env2', 'run', '--quiet',
get_examples_path('environment/env_files/get_passed_env_file.py') 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'
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, err = capsys.readouterr()
out = out.split('\n') out = out.split('\n')
@ -181,8 +202,9 @@ def test_run_file_with_multiple_env_files(runner, capsys):
@all_runners @all_runners
def test_run_file_with_default_env_file_and_env_file(runner, capsys): def test_run_file_with_default_env_file_and_env_file(runner, capsys):
runner( runner(
'run', '--quiet', '--default-env-file', '.env', '--env-file', '.env2', 'run', '--quiet',
get_examples_path('environment/env_files/get_passed_env_file.py') get_examples_path('environment/env_files/get_passed_env_file.py'),
'--default-env-file', '.env', '--env-file', '.env_two',
) )
out, err = capsys.readouterr() out, err = capsys.readouterr()
out = out.split('\n') out = out.split('\n')
@ -194,9 +216,10 @@ def test_run_file_with_default_env_file_and_env_file(runner, capsys):
@all_runners @all_runners
def test_run_file_with_default_env_file_and_env_file_and_env_vars(runner, capsys): def test_run_file_with_default_env_file_and_env_file_and_env_vars(runner, capsys):
runner( 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', '--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, err = capsys.readouterr()
out = out.split('\n') out = out.split('\n')