Files
Strata/terminal/tools/test_source_nul_bytes.py
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

56 lines
2.4 KiB
Python

"""No raw NUL byte in a source file. A shell heredoc that loses the backslash of '\\0' leaves a literal 0x00 inside a
character constant: gcc only warns ("null character(s) preserved in literal"), MSVC stops with an error (0.1.43.1's
generate.cpp; expert_source.cpp and pinned.cu after it). No binary, no GPU.
python -m unittest tools.test_source_nul_bytes
"""
from __future__ import annotations
import subprocess
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SOURCE_SUFFIXES = {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".cu", ".cuh", ".hip", ".mm", ".metal", ".py", ".cmake",
".txt", ".sh", ".bat", ".ps1", ".js", ".html", ".css", ".json", ".jinja", ".md", ".in"}
SOURCE_DIRS = ("src", "include", "sycl", "metal", "serve", "tools", "tests", "cmake", "bench", "docs")
def source_files() -> list[Path]:
try:
out = subprocess.run(["git", "ls-files", "-z", "--", *SOURCE_DIRS, "*.py", "*.txt", "*.sh", "*.bat"],
cwd=ROOT, capture_output=True, check=True).stdout
names = [n.decode("utf-8", "surrogateescape") for n in out.split(b"\0") if n]
paths = [ROOT / n for n in names]
except (OSError, subprocess.CalledProcessError): # not a git checkout (a release zip): walk the tree instead
paths = [p for d in SOURCE_DIRS if (ROOT / d).is_dir() for p in (ROOT / d).rglob("*")]
paths += [p for p in ROOT.iterdir() if p.is_file()]
return sorted({p for p in paths if p.is_file() and p.suffix.lower() in SOURCE_SUFFIXES})
def nul_lines(path: Path) -> list[int]:
data = path.read_bytes()
if b"\0" not in data:
return []
return [i + 1 for i, line in enumerate(data.split(b"\n")) if b"\0" in line]
class SourceNulBytes(unittest.TestCase):
def test_no_source_file_holds_a_nul_byte(self):
files = source_files()
self.assertGreater(len(files), 50, "the source scan found almost nothing")
bad = {str(p.relative_to(ROOT)): nul_lines(p) for p in files}
bad = {k: v for k, v in bad.items() if v}
self.assertEqual(bad, {}, "raw NUL bytes (write '\\0' instead) at file: [lines]")
def test_the_check_sees_a_nul(self):
import tempfile
with tempfile.TemporaryDirectory() as d:
p = Path(d) / "x.cpp"
p.write_bytes(b"int a;\nchar c = '\0';\n")
self.assertEqual(nul_lines(p), [2])
if __name__ == "__main__":
unittest.main()