diff --git a/src/luamb/_entrypoint.py b/src/luamb/_entrypoint.py index a774971..82fb992 100644 --- a/src/luamb/_entrypoint.py +++ b/src/luamb/_entrypoint.py @@ -1,12 +1,18 @@ import sys +from luamb._datadirs import ENVS_DIR + def main(): # This part should execute as fast as possible so as not to slow down # shell startup. For this reason we do not use ArgumentParser here and # do not import anything on module level except for some standard modules. if len(sys.argv) == 2 and sys.argv[1] == 'shellsrc': + import shlex + from luamb._shell import shellsrc + + print(f'__luamb_envs_dir={shlex.quote(str(ENVS_DIR))}') print(shellsrc) sys.exit() @@ -17,7 +23,6 @@ def error(msg, exit_status=1): import os - from ._datadirs import ENVS_DIR from ._exceptions import LuambException from ._hererocks import import_hererocks from ._luamb import Luamb diff --git a/src/luamb/_shell.py b/src/luamb/_shell.py index 2e7313e..1638abf 100644 --- a/src/luamb/_shell.py +++ b/src/luamb/_shell.py @@ -38,6 +38,8 @@ # The renamed original 'deactivate-lua'. # Unset if no active environment. +declare __luamb_envs_dir > /dev/null + __luamb_check_exists() { type "$@" > /dev/null 2>&1 @@ -72,7 +74,7 @@ local env_name=$1 __luamb_check_env_name "$env_name" || return 1 local env_path - env_path=$($__luamb_readlink -e "$LUAMB_DIR/$env_name") + env_path=$($__luamb_readlink -e "$__luamb_envs_dir/$env_name") if [ ! -d "$env_path" ]; then echo "environment doesn't exist: $env_name" return 1 @@ -138,11 +140,6 @@ } -if [ -z "$LUAMB_DIR" ]; then - echo "LUAMB_DIR variable not set" - return 1 -fi - if [ "$(uname)" = "Darwin" ]; then __luamb_readlink="greadlink" if ! __luamb_check_exists $__luamb_readlink; then @@ -168,7 +165,8 @@ ls list" ;; on|enable|activate|rm|remove|del|delete|info|show) - COMPLETION_OPTS=$(find "$LUAMB_DIR" -mindepth 1 -maxdepth 1 \ + COMPLETION_OPTS=$(find "$__luamb_envs_dir" \ + -mindepth 1 -maxdepth 1 \ -type d -printf "%f ") ;; *) diff --git a/tests/shell/conftest.py b/tests/shell/conftest.py index a495777..06f8c5e 100644 --- a/tests/shell/conftest.py +++ b/tests/shell/conftest.py @@ -53,15 +53,15 @@ def shellsrc_path(tmp_path_factory): return str(path) -@pytest.fixture() -def luamb_dir(tmp_path): - return str(tmp_path / 'luambenvs') +@pytest.fixture +def envs_dir(tmp_path): + return str(tmp_path / 'envs') -@pytest.fixture() -def script_runner(request, shellsrc_path, luamb_dir): +@pytest.fixture +def script_runner(request, shellsrc_path, envs_dir): return ScriptRunner( shell=request.param, shellsrc_path=shellsrc_path, - luamb_dir=luamb_dir, + envs_dir=envs_dir, ) diff --git a/tests/shell/lib.py b/tests/shell/lib.py index a0856d3..e61b2b3 100644 --- a/tests/shell/lib.py +++ b/tests/shell/lib.py @@ -77,11 +77,11 @@ class ScriptRunner(object): _proc = None _output = None - def __init__(self, shell, shellsrc_path, luamb_dir): + def __init__(self, shell, shellsrc_path, envs_dir): self._shell = shell self._shellsrc_path = shellsrc_path self._env = { - 'LUAMB_DIR': luamb_dir, + '__luamb_envs_dir': envs_dir, } def __call__(self, script): diff --git a/tests/shell/test_shell.py b/tests/shell/test_shell.py index f7419fc..6c962b8 100644 --- a/tests/shell/test_shell.py +++ b/tests/shell/test_shell.py @@ -1,30 +1,43 @@ import pytest -def test_shellsrc(script_runner): +def test_script_itself(script_runner): exit_status = script_runner('') + + assert exit_status == 0 + assert script_runner.output == '' + + +def test_envs_dir(script_runner, envs_dir): + exit_status = script_runner('echo $__luamb_envs_dir') + + assert exit_status == 0 + assert script_runner.output == envs_dir + + +@pytest.mark.parametrize('env_name', [ + '...', + 'foo', + 'foo bar', +]) +def test_check_env_name_valid(script_runner, env_name): + exit_status = script_runner(f"__luamb_check_env_name '{env_name}'") + assert exit_status == 0 assert script_runner.output == '' -@pytest.mark.parametrize('env_name,is_valid', [ - ('', False), - ('.', False), - ('..', False), - ('foo/bar', False), - ('/foo', False), - ('foo/', False), - ('/', False), - ('...', True), - ('foo', True), - ('foo bar', True), +@pytest.mark.parametrize('env_name', [ + '', + '.', + '..', + 'foo/bar', + '/foo', + 'foo/', + '/', ]) -def test_check_env_name(script_runner, env_name, is_valid): - exit_status = script_runner("__luamb_check_env_name '{}'".format(env_name)) - output = script_runner.output - if is_valid: - assert exit_status == 0 - assert output == '' - else: - assert exit_status != 0 - assert output == "invalid env name: '{}'".format(env_name) +def test_check_env_name_invalid(script_runner, env_name): + exit_status = script_runner(f"__luamb_check_env_name '{env_name}'") + + assert exit_status != 0 + assert script_runner.output == f"invalid env name: '{env_name}'"