|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Tests for the repository SPDX header scanner.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from scripts.check_license_headers import source_python_files |
| 11 | + |
| 12 | + |
| 13 | +def _python_file(path: Path) -> Path: |
| 14 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 15 | + path.write_text("print('test')\n") |
| 16 | + return path |
| 17 | + |
| 18 | + |
| 19 | +def _git(root: Path, *args: str) -> None: |
| 20 | + subprocess.run(["git", *args], cwd=root, check=True, capture_output=True) |
| 21 | + |
| 22 | + |
| 23 | +def test_source_files_follow_standard_git_excludes(tmp_path: Path) -> None: |
| 24 | + _git(tmp_path, "init") |
| 25 | + (tmp_path / ".gitignore").write_text("/tmp/\n") |
| 26 | + with (tmp_path / ".git" / "info" / "exclude").open("a") as exclude: |
| 27 | + exclude.write("\n.codex/\n") |
| 28 | + |
| 29 | + tracked_source = _python_file(tmp_path / "src" / "tracked.py") |
| 30 | + untracked_source = _python_file(tmp_path / "src" / "new.py") |
| 31 | + ignored_by_gitignore = _python_file(tmp_path / "tmp" / "generated.py") |
| 32 | + ignored_by_info_exclude = _python_file(tmp_path / ".codex" / "external.py") |
| 33 | + _git(tmp_path, "add", ".gitignore", "src/tracked.py") |
| 34 | + |
| 35 | + found = set(source_python_files(tmp_path)) |
| 36 | + |
| 37 | + assert tracked_source in found |
| 38 | + assert untracked_source in found |
| 39 | + assert ignored_by_gitignore not in found |
| 40 | + assert ignored_by_info_exclude not in found |
| 41 | + |
| 42 | + |
| 43 | +def test_source_files_include_tracked_files_that_match_ignore_rules(tmp_path: Path) -> None: |
| 44 | + _git(tmp_path, "init") |
| 45 | + tracked_source = _python_file(tmp_path / "generated" / "tracked.py") |
| 46 | + _git(tmp_path, "add", "generated/tracked.py") |
| 47 | + (tmp_path / ".gitignore").write_text("/generated/\n") |
| 48 | + |
| 49 | + assert tracked_source in source_python_files(tmp_path) |
0 commit comments