Skip to content

Commit

Permalink
Add filter test
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed Feb 7, 2025
1 parent 319f5d3 commit 653e01b
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions test/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ def setUp(self) -> None:
"file1_info.json": {
"schema_version": MINIMUM_SCHEMA_VERSION,
"prefix": "test1",
"filter_this": "yes",
},
"file2_info.json": {
"schema_version": MINIMUM_SCHEMA_VERSION,
"prefix": "test2",
"filter_this": "no",
},
"file3_info.json": {"schema_version": "0.1.0", "prefix": "old_version"},
"not_matching.json": {
Expand All @@ -181,15 +183,19 @@ def tearDown(self) -> None:
os.chdir(self.old_cwd)
self.temp_dir.cleanup()

def _run_ls(self, paths: list[str], fmt: str) -> str:
def _run_ls(
self, paths: list[str], fmt: str, args: argparse.Namespace = None
) -> str:
"""Helper function to run ls() and capture stdout."""
args = argparse.Namespace(
paths=[os.path.join(self.temp_dir.name, path) for path in paths],
colors=False,
fields=["prefix", "schema_version"],
format=fmt,
func=ls,
)
if args is None:
args = argparse.Namespace(
paths=[os.path.join(self.temp_dir.name, path) for path in paths],
colors=False,
fields=["prefix", "schema_version"],
eval_filter=None,
format=fmt,
func=ls,
)
buf = StringIO()
with contextlib.redirect_stdout(buf):
exit_code = ls(args)
Expand All @@ -210,6 +216,30 @@ def test_ls_sanity(self) -> None:
assert len(prefixes) == 1
assert any("file1" in p for p in prefixes)

def test_ls_with_filter(self) -> None:
"""Basic sanity test to ensure ls() runs without crashing."""
paths = ["file1_info.json", "file2_info.json"]
args = argparse.Namespace(
paths=[os.path.join(self.temp_dir.name, path) for path in paths],
colors=False,
fields=["prefix", "schema_version"],
eval_filter="filter_this=='yes'",
format="summaries",
func=ls,
)
result = self._run_ls(paths, "summaries", args)

assert "Prefix:" in result
prefixes = [
line.split(":", 1)[1].strip()
for line in result.splitlines()
if line.startswith("Prefix:")
]
assert len(prefixes) == 1
assert any("file1" in p for p in prefixes)
# filter_this == 'no'
assert "file2" not in result

def test_ls_no_pos_args(self) -> None:
result = self._run_ls([], "summaries")

Expand Down

0 comments on commit 653e01b

Please sign in to comment.