Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external: Support multiple external paths #266

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/otk/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def path_for(exe):
env = os.getenv("OTK_EXTERNAL_PATH", None)

if env is not None:
paths = [env] + paths
paths = env.split(":") + paths

for pathname in paths:
path = pathlib.Path(pathname) / exe
Expand Down
42 changes: 27 additions & 15 deletions test/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,24 @@ def test_exe_from_directive(text, exe):
assert exe_from_directive(text) == exe


def make_fake_external(tmp_path, script):
fake_external_path = tmp_path / "test"
def make_fake_external(tmp_path, name, script):
fake_external_path = tmp_path / name
fake_external_path.write_text(script)
os.chmod(fake_external_path, 0o755)
return fake_external_path


def test_external_not_found():
fake_directive = "otk.external.not_available"
fake_tree = {}
with pytest.raises(RuntimeError) as exc:
otk.external.call(State(""), fake_directive, fake_tree)
assert "could not find 'not_available' in any search path" in str(exc.value)


def test_integration_happy(tmp_path):
os.environ["OTK_EXTERNAL_PATH"] = os.fspath(tmp_path)
fake_external_path = make_fake_external(tmp_path, textwrap.dedent("""\
def run_fake_external_ok(external_path, name):
os.makedirs(external_path, exist_ok=True)
fake_external_path = make_fake_external(external_path, name, textwrap.dedent("""\
#!/bin/sh
cat - > "$0".stdin
echo '{"tree": {"some": "result"}}'
"""))

fake_directive = "otk.external.test"
fake_directive = f"otk.external.{name}"
fake_tree = {
"name": name,
"foo": "bar",
"sub": {
"baz": "foobar",
Expand All @@ -61,9 +54,28 @@ def test_integration_happy(tmp_path):
assert json.loads(inp) == {"tree": fake_tree}


def test_external_not_found():
fake_directive = "otk.external.not_available"
fake_tree = {}
with pytest.raises(RuntimeError) as exc:
otk.external.call(State(""), fake_directive, fake_tree)
assert "could not find 'not_available' in any search path" in str(exc.value)


def test_integration_happy(tmp_path):
os.environ["OTK_EXTERNAL_PATH"] = os.fspath(tmp_path)
run_fake_external_ok(tmp_path, "test")


def test_external_multipath(tmp_path):
os.environ["OTK_EXTERNAL_PATH"] = f"{tmp_path}/dir1:{tmp_path}/dir2"
run_fake_external_ok(tmp_path / "dir1", "test-1")
run_fake_external_ok(tmp_path / "dir2", "test-2")


def test_integration_error(tmp_path):
os.environ["OTK_EXTERNAL_PATH"] = os.fspath(tmp_path)
make_fake_external(tmp_path, textwrap.dedent("""\
make_fake_external(tmp_path, "test", textwrap.dedent("""\
#!/bin/sh
cat - > "$0".stdin
echo "some output"
Expand Down
Loading