Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
109 changes: 109 additions & 0 deletions tests/copyright_check_untracked_test.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 9 additions & 1 deletion tools/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading