This is an automated email from the ASF dual-hosted git repository.

sbp pushed a commit to branch sbp
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git


The following commit(s) were added to refs/heads/sbp by this push:
     new 78bf4740 Add unit tests for license file checks
78bf4740 is described below

commit 78bf4740594b013eb414a463459594212607c8f5
Author: Sean B. Palmer <[email protected]>
AuthorDate: Wed Mar 11 14:35:31 2026 +0000

    Add unit tests for license file checks
---
 atr/tasks/checks/license.py       | 10 +++++
 tests/unit/test_checks_license.py | 77 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/atr/tasks/checks/license.py b/atr/tasks/checks/license.py
index 86fe9f27..8720e7de 100644
--- a/atr/tasks/checks/license.py
+++ b/atr/tasks/checks/license.py
@@ -234,6 +234,16 @@ def _files_check_core_logic(cache_dir: pathlib.Path, 
is_podling: bool) -> Iterat
     notice_results: dict[str, tuple[bool, list[str], str]] = {}
     disclaimer_found = False
 
+    if not cache_dir.is_dir():
+        # Already protected by the caller
+        # We add it here again to make unit testing cleaner
+        yield ArtifactResult(
+            status=sql.CheckResultStatus.FAILURE,
+            message="Cache directory is not available",
+            data=None,
+        )
+        return
+
     # Check for license files in the root directory
     top_entries = sorted(e for e in os.listdir(cache_dir) if not 
e.startswith("._"))
     root_dirs = [e for e in top_entries if (cache_dir / e).is_dir()]
diff --git a/tests/unit/test_checks_license.py 
b/tests/unit/test_checks_license.py
index b1d8d6ab..fdc16131 100644
--- a/tests/unit/test_checks_license.py
+++ b/tests/unit/test_checks_license.py
@@ -17,10 +17,79 @@
 
 import pathlib
 
+import atr.constants as constants
+import atr.models.sql as sql
 import atr.tasks.checks.license as license
 
 TEST_ARCHIVE = pathlib.Path(__file__).parent.parent / "e2e" / "test_files" / 
"apache-test-0.2.tar.gz"
 
+NOTICE_VALID: str = (
+    "Apache Test\n"
+    "Copyright 2024 The Apache Software Foundation\n"
+    "\n"
+    "This product includes software developed at\n"
+    "The Apache Software Foundation (http://www.apache.org/).\n"
+)
+
+
+def test_files_missing_cache_dir():
+    results = 
list(license._files_check_core_logic(pathlib.Path("/nonexistent"), 
is_podling=False))
+    assert len(results) == 1
+    assert results[0].status == sql.CheckResultStatus.FAILURE
+    assert "not available" in results[0].message.lower()
+
+
+def test_files_multiple_root_dirs(tmp_path):
+    cache_dir = tmp_path / "cache"
+    cache_dir.mkdir()
+    (cache_dir / "root-a").mkdir()
+    (cache_dir / "root-b").mkdir()
+    results = list(license._files_check_core_logic(cache_dir, 
is_podling=False))
+    assert len(results) >= 1
+    assert results[0].status == sql.CheckResultStatus.FAILURE
+    assert "root directory" in results[0].message.lower()
+
+
+def test_files_no_root_dirs(tmp_path):
+    cache_dir = tmp_path / "cache"
+    cache_dir.mkdir()
+    (cache_dir / "LICENSE").write_text("stray file")
+    results = list(license._files_check_core_logic(cache_dir, 
is_podling=False))
+    assert len(results) >= 1
+    assert results[0].status == sql.CheckResultStatus.FAILURE
+    assert "0" in results[0].message
+
+
+def test_files_podling_without_disclaimer(tmp_path):
+    cache_dir = _cache_with_root(tmp_path)
+    root = cache_dir / "apache-test-0.2"
+    (root / "LICENSE").write_text(constants.APACHE_LICENSE_2_0)
+    (root / "NOTICE").write_text(NOTICE_VALID)
+    results = list(license._files_check_core_logic(cache_dir, is_podling=True))
+    assert any(isinstance(r, license.ArtifactResult) and (r.status == 
sql.CheckResultStatus.BLOCKER) for r in results)
+
+
+def test_files_single_root_with_stray_top_level_file(tmp_path):
+    cache_dir = _cache_with_root(tmp_path)
+    root = cache_dir / "apache-test-0.2"
+    root.mkdir(exist_ok=True)
+    (root / "LICENSE").write_text(constants.APACHE_LICENSE_2_0)
+    (root / "NOTICE").write_text(NOTICE_VALID)
+    (cache_dir / "stray.txt").write_text("ignored")
+    results = list(license._files_check_core_logic(cache_dir, 
is_podling=False))
+    statuses = [r.status for r in results if isinstance(r, 
license.ArtifactResult)]
+    assert sql.CheckResultStatus.SUCCESS in statuses
+
+
+def test_files_valid_license_and_notice(tmp_path):
+    cache_dir = _cache_with_root(tmp_path)
+    root = cache_dir / "apache-test-0.2"
+    (root / "LICENSE").write_text(constants.APACHE_LICENSE_2_0)
+    (root / "NOTICE").write_text(NOTICE_VALID)
+    results = list(license._files_check_core_logic(cache_dir, 
is_podling=False))
+    artifact_results = [r for r in results if isinstance(r, 
license.ArtifactResult)]
+    assert all(r.status == sql.CheckResultStatus.SUCCESS for r in 
artifact_results)
+
 
 def test_headers_check_data_fields_match_model():
     results = list(license._headers_check_core_logic(str(TEST_ARCHIVE), [], 
"none"))
@@ -59,3 +128,11 @@ def test_headers_check_includes_excludes_source_policy():
     artifact_results = [r for r in results if isinstance(r, 
license.ArtifactResult)]
     final_result = artifact_results[-1]
     assert final_result.data["excludes_source"] == "policy"
+
+
+def _cache_with_root(tmp_path: pathlib.Path) -> pathlib.Path:
+    cache_dir = tmp_path / "cache"
+    cache_dir.mkdir()
+    root = cache_dir / "apache-test-0.2"
+    root.mkdir()
+    return cache_dir


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to