H5AVZMA7X5KIKSR7LZPDCYTYU5E25XGKWESBYC6S5DR2BVI3QWJAC
pypkgs = with pkgs.python39Packages; [
# typer doesn't support click 8 which is what's packaged upstream
# https://github.com/NixOS/nixpkgs/issues/129479
# override it here 👍👍
python39Packages = pkgs.python39Packages.override {
overrides = self: super: {
click = super.click.overrideAttrs(old: rec {
version = "7.1.2";
src = super.fetchPypi {
inherit version;
pname = old.pname;
sha256 = "d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a";
};
});
};
};
pypkgs = with python39Packages; [
from unittest.mock import patch
import pytest
from typer.testing import CliRunner
from scripts.path_finder.cli import cli
from scripts.path_finder.lib import PathFinder
@pytest.fixture
def invoke_cli():
def invoke(*args):
return CliRunner(mix_stderr=False).invoke(cli, args)
yield invoke
def test_cli_help_parent_name_no_bad_default(invoke_cli):
"""
typer produces [default: ] if main's signature for parent_name isn't Optional
so yeah we 💯 want to enforce a nice help message by making main's signature
a little more annoying
"""
result = invoke_cli("--help")
for line in result.stdout.splitlines():
if "--parent-name TEXT" in line:
assert "default" not in line
break
else:
pytest.fail("expected --parent-name TEXT in stdout")
@pytest.mark.parametrize(
"arg_find_all,search_results,expected_output",
[
("--no-find-all", ["one", "two"], ["one"]),
("--find-all", ["one", "two"], ["one", "two"]),
("--find-all", ["two"], ["two"]),
("--no-find-all", ["two", "one"], ["two"]),
("--find-all", [], []),
("--no-find-all", [], []),
],
)
def test_cli_find_all(arg_find_all, search_results, expected_output, invoke_cli):
with patch.object(PathFinder, "find", return_value=iter(search_results)):
result = invoke_cli("whatever", arg_find_all)
assert expected_output == result.stdout.splitlines()
if __name__ == "__main__":
if 2 > len(sys.argv) < 3:
print("USAGE:path_finder name [parent_name]", file=sys.stderr)
sys.exit(1)
_, name, *rest = sys.argv
if rest:
parent_name = rest[0]
else:
parent_name = ""
if path_finder := PathFinder.from_env().find_first(name, parent_name=parent_name):
print(path_finder)
from typing import Optional
import typer
from .lib import PathFinder
cli = typer.Typer()
@cli.command()
def main(name: str, find_all: bool = False, parent_name: Optional[str] = None):
parent_name = parent_name or ""
path_finder = PathFinder.from_env()
paths = path_finder.find(name, parent_name=parent_name)
if not find_all:
path = next(paths, None)
paths = [path] if path else []
for path in paths:
typer.echo(path)
if __name__ == "__main__":
cli()