Skip to content

Commit

Permalink
remove: args, refactor: parse_tests (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
metaist committed Aug 28, 2024
1 parent b29cac9 commit 47f5ace
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 300 deletions.
6 changes: 3 additions & 3 deletions test/parsers/test_cargo_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
32 changes: 16 additions & 16 deletions test/parsers/test_composer_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,34 @@ 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:
"""Task help."""
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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -143,20 +143,20 @@ 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:
"""Not supported: error suppression"""
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:
Expand All @@ -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))
42 changes: 21 additions & 21 deletions test/parsers/test_ds_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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"]}}
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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))
26 changes: 14 additions & 12 deletions test/parsers/test_makefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,15 +36,15 @@ 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


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:
Expand Down Expand Up @@ -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,
}
}
}
}
)
Loading

0 comments on commit 47f5ace

Please sign in to comment.