diff --git a/CHANGELOG.md b/CHANGELOG.md index 96c73f5..3c08637 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -BUG: fix `baballe digest > file.json` +- BUG: fix `baballe digest > file.json` +- BUG: fix a bug where `idfx digest` would choke on files where no line is captured ## [3.2.0] - 2023-09-06 diff --git a/src/idefix_cli/_commands/digest.py b/src/idefix_cli/_commands/digest.py index 8bc91dd..4ad1697 100644 --- a/src/idefix_cli/_commands/digest.py +++ b/src/idefix_cli/_commands/digest.py @@ -112,12 +112,18 @@ def command( data: list[list[str]] = [] _success = False - for log in log_files: + + for log in log_files.copy(): captured: list[str] = [] for line in log.read_text().splitlines(): if (match := _log_line_regexp.fullmatch(line)) is not None: captured.append(match.group("data")) _success = True + if not captured: + # dynamically exclude files without any data + log_files.remove(log) + continue + data.append(captured) if not _success: diff --git a/tests/data/OrszagTang3D/empty.log b/tests/data/OrszagTang3D/empty.log new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_digest.py b/tests/test_digest.py index 785d322..c06f901 100644 --- a/tests/test_digest.py +++ b/tests/test_digest.py @@ -139,3 +139,29 @@ def test_digest_multiple_input(capsys): assert err2 == "" assert out == out2 + + +def test_digest_empty_file(capsys): + ret = main(["digest", "--dir", str(BASE_SETUP.absolute()), "--input", "empty.log"]) + assert ret != 0 + out, err = capsys.readouterr() + assert out == "" + assert err == "💥 Failed to parse any data\n" + + +@pytest.mark.parametrize( + "logs", + [ + ("empty.log", "idefix.0.log"), + ("empty.log", "idefix.1.log"), + # order shouldn't matter + ("idefix.0.log", "empty.log"), + ("idefix.1.log", "empty.log"), + ], +) +def test_digest_mixed_contents(capsys, logs): + ret = main(["digest", "--dir", str(BASE_SETUP.absolute()), "--input", *logs]) + assert ret == 0 + out, err = capsys.readouterr() + json.loads(out) # validate output + assert err == ""