From 47f5ace7f3ee4a088a593ae27a2516fdc7b26cf0 Mon Sep 17 00:00:00 2001 From: The Metaist Date: Wed, 28 Aug 2024 11:56:39 -0400 Subject: [PATCH] remove: args, refactor: parse_tests (#77) --- test/parsers/test_cargo_toml.py | 6 +- test/parsers/test_composer_json.py | 32 ++--- test/parsers/test_ds_toml.py | 42 +++--- test/parsers/test_makefile.py | 26 ++-- test/parsers/test_package_json.py | 22 +-- test/parsers/test_pyproject_pdm.py | 36 ++--- test/parsers/test_pyproject_poetry.py | 8 +- test/parsers/test_pyproject_rye.py | 32 ++--- test/parsers/test_pyproject_toml.py | 4 +- test/parsers/test_uv_toml.py | 2 +- test/test_parsers.py | 196 -------------------------- 11 files changed, 106 insertions(+), 300 deletions(-) delete mode 100644 test/test_parsers.py diff --git a/test/parsers/test_cargo_toml.py b/test/parsers/test_cargo_toml.py index f899670..2b32f7b 100644 --- a/test/parsers/test_cargo_toml.py +++ b/test/parsers/test_cargo_toml.py @@ -65,7 +65,7 @@ def test_tasks_missing() -> None: args = Args() config = Config(PATH, {}) with pytest.raises(KeyError): - parse_tasks(args, config) + parse_tasks(config) def test_tasks() -> None: @@ -75,11 +75,11 @@ def test_tasks() -> None: expected = { "a": replace(TASK, origin_key="workspace.metadata.scripts", name="a", cmd="b") } - assert parse_tasks(args, config) == expected + assert parse_tasks(config) == expected args = Args() config = Config(PATH, nest("package.metadata.scripts", {"a": "b"})) expected = { "a": replace(TASK, origin_key="package.metadata.scripts", name="a", cmd="b") } - assert parse_tasks(args, config) == expected + assert parse_tasks(config) == expected diff --git a/test/parsers/test_composer_json.py b/test/parsers/test_composer_json.py index 99eee9a..e54fded 100644 --- a/test/parsers/test_composer_json.py +++ b/test/parsers/test_composer_json.py @@ -41,26 +41,26 @@ def test_format() -> None: path = EXAMPLE_FORMATS / "composer.json" args = Args(file=path) config = Config(path, loads(path.read_text())) - tasks = parse_tasks(args, config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """Missing tasks.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty tasks.""" data = nest(KEY, {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_disabled() -> None: """Disabled task.""" data = nest(KEY, {"#a": "b"}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_help() -> None: @@ -68,7 +68,7 @@ def test_task_help() -> None: data = nest(KEY, {"a": "b"}) data["scripts-descriptions"] = {"a": "run things"} expected = {"a": replace(TASK, name="a", cmd="b", help="run things")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_aliases() -> None: @@ -90,14 +90,14 @@ def test_task_aliases() -> None: depends=[replace(TASK, name=TASK_COMPOSITE, cmd="a")], ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_cmd() -> None: """Basic task.""" data = nest(KEY, {"a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_call() -> None: @@ -107,14 +107,14 @@ def test_task_call() -> None: expected = { "a": replace(TASK, name="a", cmd=PHP_CALL.format(fn="MyPackage\\MyClass")) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # static method data = nest(KEY, {"a": "MyPackage\\MyClass::func"}) expected = { "a": replace(TASK, name="a", cmd=PHP_CALL.format(fn="MyPackage\\MyClass::func")) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_composite() -> None: @@ -130,7 +130,7 @@ def test_task_composite() -> None: ], ) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_args() -> None: @@ -143,12 +143,12 @@ def test_task_args() -> None: TASK, name="b", depends=[replace(TASK, name=TASK_COMPOSITE, cmd="a -lah")] ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # argument interpolation: via CLI data = nest(KEY, {"a": "ls $1"}) expected = {"a": replace(TASK, name="a", cmd="ls $1")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_keep_going() -> None: @@ -156,7 +156,7 @@ def test_keep_going() -> None: data = nest(KEY, {"a": f"{TASK_KEEP_GOING}b"}) expected = {"a": replace(TASK, name="a", cmd=f"{TASK_KEEP_GOING}b")} # NOTE: `keep_going` NOT set - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env() -> None: @@ -170,11 +170,11 @@ def test_task_env() -> None: depends=[replace(TASK, name=TASK_COMPOSITE, cmd="flask $PORT")], ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_bad_syntax() -> None: """Syntax errors.""" data = nest(KEY, {"a": False}) - with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + with pytest.raises(TypeError): + parse_tasks(Config(PATH, data)) diff --git a/test/parsers/test_ds_toml.py b/test/parsers/test_ds_toml.py index 9b8865f..b51f011 100644 --- a/test/parsers/test_ds_toml.py +++ b/test/parsers/test_ds_toml.py @@ -97,53 +97,53 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "pyproject-ds.toml" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks path = EXAMPLE_FORMATS / "ds.toml" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """Missing tasks.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty tasks.""" data = nest("tool.ds.scripts", {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_disabled() -> None: """Disabled task.""" data = nest(KEY, {"#a": "b"}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_help() -> None: """Task help.""" data = nest(KEY, {"a": {"cmd": "b", "help": "run things"}}) expected = {"a": replace(TASK, name="a", cmd="b", help="run things")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_cmd() -> None: """`cmd` task.""" data = nest(KEY, {"a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": {"cmd": ["ls", "-lah"]}}) expected = {"a": replace(TASK, name="a", cmd="ls -lah")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": {"shell": "ls -lah", "verbatim": True}}) expected = {"a": replace(TASK, name="a", cmd="ls -lah", verbatim=True)} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_call() -> None: @@ -152,12 +152,12 @@ def test_task_call() -> None: expected = { "a": replace(TASK, name="a", cmd=PYTHON_CALL.format(pkg="ds", fn="main()")) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # call outside of pyproject.toml not allowed data = {"scripts": {"a": {"call": "ds:main"}}} with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(Path("ds.toml"), data)) + parse_tasks(Config(Path("ds.toml"), data)) def test_task_composite() -> None: @@ -175,7 +175,7 @@ def test_task_composite() -> None: ], ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # short-form data = {"scripts": {"a": "b", "c": "d", "e": ["a", "c"]}} @@ -191,7 +191,7 @@ def test_task_composite() -> None: ], ), } - assert parse_tasks(Args(), Config(Path("ds.toml"), data)) == expected + assert parse_tasks(Config(Path("ds.toml"), data)) == expected def test_task_keep_going() -> None: @@ -217,7 +217,7 @@ def test_task_keep_going() -> None: keep_going=True, ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env() -> None: @@ -226,7 +226,7 @@ def test_task_env() -> None: expected = { "a": replace(TASK, name="a", cmd="flask $PORT", env={"PORT": "8080"}), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env_file() -> None: @@ -242,7 +242,7 @@ def test_task_env_file() -> None: env_file=EXAMPLE_FORMATS / ".env", ), } - assert parse_tasks(Args(), Config(path, data)) == expected + assert parse_tasks(Config(path, data)) == expected def test_working_dir() -> None: @@ -251,12 +251,12 @@ def test_working_dir() -> None: expected = { "a": replace(TASK, name="a", cmd="ls -la", cwd=Path("test").resolve()), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # don't allow multiple aliases for same value data = {"scripts": {"a": {"cmd": "ls -la", "cwd": "test1", "working_dir": "test2"}}} with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(Path("ds.toml"), data)) + parse_tasks(Config(Path("ds.toml"), data)) def test_shared_options() -> None: @@ -278,11 +278,11 @@ def test_shared_options() -> None: env={"PORT": "8080", "OTHER": "val"}, ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_bad_syntax() -> None: """Syntax errors.""" data = nest(KEY, {"a": False}) - with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + with pytest.raises(TypeError): + parse_tasks(Config(PATH, data)) diff --git a/test/parsers/test_makefile.py b/test/parsers/test_makefile.py index 4a88653..3aee566 100644 --- a/test/parsers/test_makefile.py +++ b/test/parsers/test_makefile.py @@ -10,7 +10,6 @@ # pkg from . import EXAMPLE_FORMATS from . import nest -from ds.args import Args from ds.parsers import Config from ds.parsers.makefile import loads from ds.parsers.makefile import parse_tasks @@ -37,7 +36,7 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "Makefile" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks @@ -45,7 +44,7 @@ def test_tasks_basic() -> None: """Basic task.""" data = nest(KEY, {"a": {"composite": [], "shell": "b", "verbatim": True}}) expected = {"a": replace(TASK, name="a", cmd="b", verbatim=True)} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_makefile_loads() -> None: @@ -114,19 +113,22 @@ def test_makefile_loads() -> None: } # line continuation & .RECIPEPREFIX - assert loads( - """ + assert ( + loads( + """ .RECIPEPREFIX=> target: >echo "Hello \\ >world" """ - ) == { - "recipes": { - "target": { - "composite": [], - "shell": 'echo "Hello \\\nworld"\n', - "verbatim": True, + ) + == { + "recipes": { + "target": { + "composite": [], + "shell": 'echo "Hello \\\nworld"\n', + "verbatim": True, + } } } - } + ) diff --git a/test/parsers/test_package_json.py b/test/parsers/test_package_json.py index b29dd87..f7ac64c 100644 --- a/test/parsers/test_package_json.py +++ b/test/parsers/test_package_json.py @@ -81,40 +81,40 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "package.json" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """Missing tasks.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty tasks.""" data = nest(KEY, {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_disabled() -> None: """Disabled task.""" data = nest(KEY, {"#a": "b"}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_help() -> None: """Task help.""" data = nest(KEY, {"#a": "run things", "a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b", help="run things")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_cmd() -> None: """Basic task.""" data = nest(KEY, {"a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_args() -> None: @@ -125,7 +125,7 @@ def test_task_args() -> None: "a": replace(TASK, name="a", cmd="ls"), "b": replace(TASK, name="b", cmd="a -lah"), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # argument interpolation: via CLI data = nest(KEY, {"a": "ls $1", "b": "a -lah"}) @@ -133,7 +133,7 @@ def test_task_args() -> None: "a": replace(TASK, name="a", cmd="ls $1"), "b": replace(TASK, name="b", cmd="a -lah"), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_reference() -> None: @@ -141,7 +141,7 @@ def test_task_reference() -> None: # task reference: apparent self-reference (but actually ok) data = nest(KEY, {"ls": "ls"}) expected = {"ls": replace(TASK, name="ls", cmd="ls")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected # task reference: loop (not ok) data = nest(KEY, {"a": "b", "b": "a"}) @@ -149,7 +149,7 @@ def test_task_reference() -> None: "a": replace(TASK, name="a", cmd="b"), "b": replace(TASK, name="b", cmd="a"), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_keep_going() -> None: @@ -157,4 +157,4 @@ def test_keep_going() -> None: data = nest(KEY, {"a": f"{TASK_KEEP_GOING}b"}) expected = {"a": replace(TASK, name="a", cmd=f"{TASK_KEEP_GOING}b")} # NOTE: `keep_going` NOT set - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected diff --git a/test/parsers/test_pyproject_pdm.py b/test/parsers/test_pyproject_pdm.py index 180385f..7d5a2e2 100644 --- a/test/parsers/test_pyproject_pdm.py +++ b/test/parsers/test_pyproject_pdm.py @@ -58,48 +58,48 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "pyproject-pdm.toml" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """Missing tasks.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty tasks.""" data = nest(KEY, {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_disabled() -> None: """Disabled task.""" data = nest(KEY, {"#a": "b"}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_help() -> None: """Task help.""" data = nest(KEY, {"a": {"cmd": "b", "help": "run things"}}) expected = {"a": replace(TASK, name="a", cmd="b", help="run things")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_cmd() -> None: """`cmd` task.""" data = nest(KEY, {"a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": {"cmd": ["ls", "-lah"]}}) expected = {"a": replace(TASK, name="a", cmd="ls -lah")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": {"shell": "ls -lah"}}) expected = {"a": replace(TASK, name="a", cmd="ls -lah")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_call() -> None: @@ -108,7 +108,7 @@ def test_task_call() -> None: expected = { "a": replace(TASK, name="a", cmd=PYTHON_CALL.format(pkg="ds", fn="main()")) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_composite() -> None: @@ -126,7 +126,7 @@ def test_task_composite() -> None: ], ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_keep_going() -> None: @@ -152,7 +152,7 @@ def test_task_keep_going() -> None: keep_going=True, ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env() -> None: @@ -161,7 +161,7 @@ def test_task_env() -> None: expected = { "a": replace(TASK, name="a", cmd="flask $PORT", env={"PORT": "8080"}), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env_file() -> None: @@ -177,7 +177,7 @@ def test_task_env_file() -> None: env_file=EXAMPLE_FORMATS / ".env", ), } - assert parse_tasks(Args(), Config(path, data)) == expected + assert parse_tasks(Config(path, data)) == expected def test_working_dir() -> None: @@ -186,7 +186,7 @@ def test_working_dir() -> None: expected = { "a": replace(TASK, name="a", cmd="ls -la", cwd=Path("test").resolve()), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_shared_options() -> None: @@ -208,15 +208,15 @@ def test_shared_options() -> None: env={"PORT": "8080", "OTHER": "val"}, ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_bad_syntax() -> None: """Syntax errors.""" data = nest(KEY, {"a": False}) - with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + with pytest.raises(TypeError): + parse_tasks(Config(PATH, data)) data = nest(KEY, {"a": {"unknown": "missing required keys"}}) with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + parse_tasks(Config(PATH, data)) diff --git a/test/parsers/test_pyproject_poetry.py b/test/parsers/test_pyproject_poetry.py index e5032df..a4a417b 100644 --- a/test/parsers/test_pyproject_poetry.py +++ b/test/parsers/test_pyproject_poetry.py @@ -104,20 +104,20 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "pyproject-poetry.toml" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """File without scripts.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty scripts.""" data = nest(KEY, {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_cmd() -> None: @@ -126,4 +126,4 @@ def test_task_cmd() -> None: expected = { "a": replace(TASK, name="a", cmd=PYTHON_CALL.format(pkg="pkg", fn="func()")) } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected diff --git a/test/parsers/test_pyproject_rye.py b/test/parsers/test_pyproject_rye.py index 9870492..a9f17ba 100644 --- a/test/parsers/test_pyproject_rye.py +++ b/test/parsers/test_pyproject_rye.py @@ -86,55 +86,55 @@ def test_format() -> None: """End-to-end test of the format.""" path = EXAMPLE_FORMATS / "pyproject-rye.toml" config = Config(path, loads(path.read_text())) - tasks = parse_tasks(Args(file=path), config) + tasks = parse_tasks(config) assert tasks def test_tasks_missing() -> None: """Missing tasks.""" with pytest.raises(KeyError): - parse_tasks(Args(), Config(PATH, {})) + parse_tasks(Config(PATH, {})) def test_tasks_empty() -> None: """Empty tasks.""" data = nest(KEY, {}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_disabled() -> None: """Disabled task.""" data = nest(KEY, {"#a": "b"}) - assert parse_tasks(Args(), Config(PATH, data)) == {} + assert parse_tasks(Config(PATH, data)) == {} def test_task_help() -> None: """Task help.""" data = nest(KEY, {"a": {"cmd": "b", "help": "run things"}}) expected = {"a": replace(TASK, name="a", cmd="b", help="run things")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_cmd() -> None: """`cmd` task.""" data = nest(KEY, {"a": "b"}) expected = {"a": replace(TASK, name="a", cmd="b")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": ["ls", "-lah"]}) expected = {"a": replace(TASK, name="a", cmd="ls -lah")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected data = nest(KEY, {"a": {"cmd": ["ls", "-lah"]}}) expected = {"a": replace(TASK, name="a", cmd="ls -lah")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_call() -> None: """`call` task.""" data = nest(KEY, {"a": {"call": "http.server"}}) expected = {"a": replace(TASK, name="a", cmd="python -m http.server")} - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_chain() -> None: @@ -152,7 +152,7 @@ def test_task_chain() -> None: ], ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env() -> None: @@ -163,7 +163,7 @@ def test_task_env() -> None: expected = { "a": replace(TASK, name="a", cmd="flask $PORT", env={"PORT": "8080"}), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_task_env_file() -> None: @@ -179,7 +179,7 @@ def test_task_env_file() -> None: env_file=EXAMPLE_FORMATS / ".env", ), } - assert parse_tasks(Args(), Config(path, data)) == expected + assert parse_tasks(Config(path, data)) == expected # with missing file data = nest(KEY, {"a": {"cmd": "flask $PORT", "env-file": ".env"}}) @@ -191,15 +191,15 @@ def test_task_env_file() -> None: env_file=Path(".env").resolve(), ), } - assert parse_tasks(Args(), Config(PATH, data)) == expected + assert parse_tasks(Config(PATH, data)) == expected def test_bad_syntax() -> None: """Syntax errors.""" data = nest(KEY, {"a": False}) - with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + with pytest.raises(TypeError): + parse_tasks(Config(PATH, data)) data = nest(KEY, {"a": {"unknown": "missing required keys"}}) with pytest.raises(SyntaxError): - parse_tasks(Args(), Config(PATH, data)) + parse_tasks(Config(PATH, data)) diff --git a/test/parsers/test_pyproject_toml.py b/test/parsers/test_pyproject_toml.py index f031ceb..66bf076 100644 --- a/test/parsers/test_pyproject_toml.py +++ b/test/parsers/test_pyproject_toml.py @@ -38,10 +38,10 @@ def test_tasks() -> None: args = Args() path = EXAMPLE_FORMATS / "pyproject-ds.toml" config = Config(path, loads(path.read_text())) - assert parse_tasks(args, config) + assert parse_tasks(config) # using a non-task file path = EXAMPLE_WORKSPACE / "pyproject-uv.toml" config = Config(path, loads(path.read_text())) with pytest.raises(KeyError): - parse_tasks(args, config) + parse_tasks(config) diff --git a/test/parsers/test_uv_toml.py b/test/parsers/test_uv_toml.py index 36b1859..fedcb23 100644 --- a/test/parsers/test_uv_toml.py +++ b/test/parsers/test_uv_toml.py @@ -74,4 +74,4 @@ def test_tasks_not_implemented() -> None: args = Args() config = Config(Path("pyproject.toml"), {}) with pytest.raises(NotImplementedError): - parse_tasks(args, config) + parse_tasks(config) diff --git a/test/test_parsers.py b/test/test_parsers.py deleted file mode 100644 index d2fd572..0000000 --- a/test/test_parsers.py +++ /dev/null @@ -1,196 +0,0 @@ -"""Test parsing formats.""" - -# std -from dataclasses import replace -from pathlib import Path -from shlex import split - -# lib -import pytest - -# pkg -from ds.parsers import parse_tasks -from ds.symbols import TASK_COMPOSITE -from ds.symbols import TASK_DISABLED -from ds.symbols import TASK_KEEP_GOING -from ds.tasks import check_cycles -from ds.tasks import CycleError -from ds.tasks import Task - - -def test_no_key() -> None: - """Parse an empty config.""" - assert parse_tasks({}) == (False, {}) - - -def test_skipped() -> None: - """Parse configs that are skipped.""" - # tasks can be disabled - assert parse_tasks({"scripts": {f"{TASK_DISABLED}disabled": "ls -la"}})[1] == {} - - # tasks with empty or all-whitespace names are disabled - assert parse_tasks({"scripts": {"": "ls -la", " ": "exit 1"}})[1] == {} - - -def test_nop() -> None: - """Parse an empty task.""" - assert parse_tasks({"scripts": {"nop": ""}})[1] == { - "nop": Task(origin_key="scripts", name="nop") - } - - -def test_string() -> None: - """Parse basic string task.""" - assert parse_tasks({"scripts": {"ls": "ls -la"}})[1] == { - "ls": Task(origin_key="scripts", name="ls", cmd="ls -la") - } - - # Tasks can suppress errors. - assert parse_tasks({"scripts": {"ls": f"{TASK_KEEP_GOING}ls -la"}})[1] == { - "ls": Task( - origin_key="scripts", - name="ls", - cmd="ls -la", - keep_going=True, - ) - } - - -def test_composite() -> None: - """Parse a `composite` task.""" - cmd = [f"{TASK_KEEP_GOING}clean", "build"] - expected = { - "all": Task( - origin_key="scripts", - name="all", - depends=[ - Task( - origin_key="scripts", - name=TASK_COMPOSITE, - cmd="clean", - keep_going=True, - ), - Task( - origin_key="scripts", - name=TASK_COMPOSITE, - cmd="build", - ), - ], - ) - } - expected["all"].pprint() # test printing - - # ds-style - assert parse_tasks({"scripts": {"all": cmd}})[1] == expected - - # pdm-style - assert parse_tasks({"scripts": {"all": {"composite": cmd}}})[1] == expected - - # rye-style - assert parse_tasks({"scripts": {"all": {"chain": cmd}}})[1] == expected - - -def test_composite_and_shell() -> None: - """Parse a `composite` with a `shell`.""" - expected = { - "build": Task( - origin_key="scripts", - name="build", - depends=[Task(origin_key="scripts", name=TASK_COMPOSITE, cmd="cd ..")], - cmd="ls -la", - verbatim=True, - ) - } - - assert ( - parse_tasks( - { - "scripts": { - "build": { - "composite": ["cd .."], - "shell": "ls -la", - "verbatim": True, - } - } - } - )[1] - == expected - ) - - -def test_shell_cmd() -> None: - """Parse a `shell` or `cmd` task.""" - cmd = f"{TASK_KEEP_GOING}ls -la" - expected = { - "ls": Task( - origin_key="scripts", - name="ls", - cmd="ls -la", - keep_going=True, - ) - } - expected["ls"].pprint() # test printing - - # shell - assert parse_tasks({"scripts": {"ls": {"shell": cmd}}})[1] == expected - - # cmd (str) - assert parse_tasks({"scripts": {"ls": {"cmd": cmd}}})[1] == expected - - # cmd (list) - assert parse_tasks({"scripts": {"ls": {"cmd": split(cmd)}}})[1] == expected - - # rye-style - task2 = replace(expected["ls"], origin_key="tool.rye.scripts") - assert parse_tasks({"tool": {"rye": {"scripts": {"ls": split(cmd)}}}})[1] == { - "ls": task2 - } - - -def test_call() -> None: - """Fail to parse `call` task.""" - with pytest.raises(ValueError): - parse_tasks({"scripts": {"ls": {"call": "ds:main"}}}) - - -def test_cwd() -> None: - """Parse `cwd` and `working_dir` options.""" - cmd = "ls -la" - expected = { - "ls": Task( - origin=Path(), origin_key="scripts", name="ls", cmd=cmd, cwd=Path("test") - ) - } - expected["ls"].pprint() # test printing - assert ( - parse_tasks({"scripts": {"ls": {"cmd": cmd, "cwd": "test"}}}, Path())[1] - == expected - ) - - -def test_bad_types() -> None: - """Handle bad types.""" - # Unsupported task type. - with pytest.raises(TypeError): - parse_tasks({"scripts": {"X": False}}) - - # Unsupported mapping type. - with pytest.raises(TypeError): - parse_tasks({"scripts": {"X": {"bad": ["A", "B", "C"]}}}) - - -def test_ok_loop() -> None: - """Parse a ok loop.""" - task = Task(name=TASK_COMPOSITE, cmd="a") - check_cycles({"a": Task(depends=[task])}) - - -def test_bad_loop() -> None: - """Try to parse a loop.""" - with pytest.raises(CycleError): - check_cycles( - { - "a": Task(depends=[Task(name=TASK_COMPOSITE, cmd="b")]), - "b": Task(depends=[Task(name=TASK_COMPOSITE, cmd="a")]), - } - )