Skip to content

Commit

Permalink
fix: pass envs dir path to shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
un-def committed Apr 17, 2024
1 parent dbb77a9 commit fd5c013
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
7 changes: 6 additions & 1 deletion src/luamb/_entrypoint.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/luamb/_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 ")
;;
*)
Expand Down
12 changes: 6 additions & 6 deletions tests/shell/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
4 changes: 2 additions & 2 deletions tests/shell/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
55 changes: 34 additions & 21 deletions tests/shell/test_shell.py
Original file line number Diff line number Diff line change
@@ -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}'"

0 comments on commit fd5c013

Please sign in to comment.