From 00270fd54c69ffad31fdba65fb3d914b2760899b Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sun, 8 Nov 2026 00:25:28 -0400 Subject: [PATCH] checks: copyright --check sees untracked files, so a new file cannot pass on a green it never earned `tools/copyright.py` enumerated `git ls-files`, so an untracked file was invisible to it -- and a brand-new file is untracked exactly when a lane runs the check before `git add`. PR #1406's own test script passed `--check` locally and failed CI's copyright step on the same head; the check was green on the one file most likely to be wrong. The list is now tracked files plus `--others --exclude-standard` (ignored files stay out: a build artifact is not source), and `--fix` reaches untracked files too. tests/copyright_check_untracked_test.py (ctest `copyright_check_untracked`) runs the real tool in a scratch git repo: a tracked file with its notice passes; an untracked file without one FAILS and is named; `--fix` adds the full Apache block to it and the check goes green; an ignored file is left out. Tested the test: with the enumeration reverted to `git ls-files` alone, the untracked case fails and the rest still pass. --- CMakeLists.txt | 5 ++ tests/copyright_check_untracked_test.py | 109 ++++++++++++++++++++++++ tools/copyright.py | 10 ++- 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100755 tests/copyright_check_untracked_test.py diff --git a/CMakeLists.txt b/CMakeLists.txt index b52159a21..e2ac32153 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -579,6 +579,11 @@ add_executable(route_log_test tests/route_log_test.cpp) target_include_directories(route_log_test PRIVATE app) target_link_libraries(route_log_test PRIVATE nlohmann_json::nlohmann_json) add_test(NAME route_log COMMAND route_log_test) +# `tools/copyright.py --check` must see a file that has not been staged yet (the check used `git +# ls-files`, so a brand-new file was invisible to it -- PR #1406's test script passed --check locally +# and failed CI's copyright step on the same head). Runs the real tool in a scratch git repo. +add_test(NAME copyright_check_untracked + COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tests/copyright_check_untracked_test.py) add_dependencies(onebit route_log_test) # CI builds `--target onebit` plus a fixed list in .github/workflows/ci.yml; building this check with onebit # keeps the laya_models ctest from reporting "Not Run" without a workflow change. diff --git a/tests/copyright_check_untracked_test.py b/tests/copyright_check_untracked_test.py new file mode 100755 index 000000000..ff615ee24 --- /dev/null +++ b/tests/copyright_check_untracked_test.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""`tools/copyright.py --check` must see a file that is not staged yet. + +The tool enumerated `git ls-files`, so an untracked file was invisible to it — and a brand-new file +is untracked exactly when a lane runs the check before `git add`. That is how +`onnx/tests/ort_run_generic.sh` passed `--check` locally and failed CI's copyright step on the same +head (PR #1406): a green from a check that could not see the file most likely to be wrong. + +Each case runs the real tool in a scratch git repository, so it tests the enumeration and not a copy +of it. Ignored files must stay out: a build artifact needs no notice. +""" +import os +import subprocess +import sys +import tempfile + +HERE = os.path.dirname(os.path.abspath(__file__)) +TOOL = os.path.join(HERE, os.pardir, "tools", "copyright.py") +NOTICE = ("# Copyright 2026 bong-water-water-bong\n" + "# SPDX-License-Identifier: Apache-2.0\n" + "#\n" + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n" + "# you may not use this file except in compliance with the License.\n" + "# You may obtain a copy of the License at\n" + "#\n" + "# http://www.apache.org/licenses/LICENSE-2.0\n" + "#\n" + "# Unless required by applicable law or agreed to in writing, software\n" + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "# See the License for the specific language governing permissions and\n" + "# limitations under the License.\n") +FAIL = [] + + +def check(ok, what, why=""): + print(("ok " if ok else "FAIL ") + what + ((" -- " + why) if not ok and why else "")) + if not ok: + FAIL.append(what) + + +def run(repo, *args): + return subprocess.run([sys.executable, os.path.abspath(TOOL), *args], cwd=repo, + capture_output=True, text=True) + + +def git(repo, *args): + subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True) + + +with tempfile.TemporaryDirectory() as repo: + git(repo, "init", "-q") + git(repo, "config", "user.email", "t@example.invalid") + git(repo, "config", "user.name", "test") + # a tracked file WITH its notice: the baseline must stay green + with open(os.path.join(repo, "ok.py"), "w") as f: + f.write(NOTICE + "print(1)\n") + git(repo, "add", "ok.py") + git(repo, "commit", "-q", "-m", "baseline") + rc = run(repo, "--check") + check(rc.returncode == 0, "a tracked file with its notice passes", rc.stdout + rc.stderr) + + # THE CASE: a new file without a notice, NOT staged — this used to pass + with open(os.path.join(repo, "new.sh"), "w") as f: + f.write("#!/usr/bin/env bash\necho hi\n") + rc = run(repo, "--check") + check(rc.returncode != 0, + "an UNTRACKED file without a notice FAILS the check (the bug this fixes)", + f"rc {rc.returncode}: {rc.stdout.strip()}") + check("new.sh" in (rc.stdout + rc.stderr), + "and the failure names it", rc.stdout + rc.stderr) + + # --fix must reach it too, and then the check is green + rc = run(repo, "--fix") + check(rc.returncode == 0 and "new.sh" in rc.stdout, "--fix adds the notice to the untracked file", + rc.stdout + rc.stderr) + check(NOTICE.splitlines()[3] in open(os.path.join(repo, "new.sh")).read(), + "the notice it added is the full Apache block, not just SPDX") + rc = run(repo, "--check") + check(rc.returncode == 0, "and the check is green afterwards", rc.stdout + rc.stderr) + + # an ignored file is not source: it must not need a notice. The .gitignore itself is source (the + # repo's own carries the notice and is tracked), so it gets one here too. + with open(os.path.join(repo, ".gitignore"), "w") as f: + f.write(NOTICE + "build/\n") + git(repo, "add", ".gitignore") + git(repo, "commit", "-q", "-m", "ignore build/") + os.mkdir(os.path.join(repo, "build")) + with open(os.path.join(repo, "build", "artifact.txt"), "w") as f: + f.write("no notice here\n") + rc = run(repo, "--check") + check(rc.returncode == 0, "an ignored file is left out", rc.stdout + rc.stderr) + +print(f"copyright_check_untracked_test: {len(FAIL)} failure(s)") +sys.exit(1 if FAIL else 0) diff --git a/tools/copyright.py b/tools/copyright.py index 29260ab12..bbe742caf 100644 --- a/tools/copyright.py +++ b/tools/copyright.py @@ -139,7 +139,15 @@ def main(): mode.add_argument("--fix", action="store_true") args = ap.parse_args() - files = subprocess.run(["git", "ls-files"], capture_output=True, text=True, check=True).stdout.split("\n") + # Tracked AND untracked-but-not-ignored files. `git ls-files` alone cannot see a file that has + # not been staged yet -- which is exactly the state a new file is in when a lane runs this check + # before `git add`, so the check would report green on the file most likely to be missing its + # notice (this happened: onnx/tests/ort_run_generic.sh passed --check and failed CI on the same + # head). Ignored files stay out: a build artifact is not source. + def listing(*args): + return subprocess.run(["git", "ls-files", *args], capture_output=True, text=True, + check=True).stdout.split("\n") + files = sorted(set(filter(None, listing())) | set(filter(None, listing("--others", "--exclude-standard")))) missing, unknown = [], [] for path in filter(None, files): if exempt(path):