diff --git a/README.md b/README.md index a8edc1e..a99abe3 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,10 @@ re-vendor run by the vendored-tree check on every pull request. [Mutator](https://github.com/unclebob/mutator) are vendored under `src/ub_cli/vendor` at the commits in `src/ub_cli/vendor/VENDORED`, with their imports rewritten to `ub_cli.vendor`. Never edit that directory by hand. +A fix not yet upstream goes in `scripts/vendor-patches/NAME-*.patch`, which +`scripts/vendor.py` applies after vendoring; delete the patch once the pin +includes the fix. Current patch: `mutator` decodes test output with +`errors="replace"`, so output that is not valid UTF-8 no longer crashes a run. `make check-upstreams` compares the pins with upstream and prints the update command, `python scripts/vendor.py NAME COMMIT`. The CI and Release workflows fail when the directory differs from what `scripts/vendor.py` produces. diff --git a/scripts/vendor-patches/mutator-runner-undecodable-output.patch b/scripts/vendor-patches/mutator-runner-undecodable-output.patch new file mode 100644 index 0000000..9124e61 --- /dev/null +++ b/scripts/vendor-patches/mutator-runner-undecodable-output.patch @@ -0,0 +1,12 @@ +diff --git a/src/ub_cli/vendor/mutator/runner.py b/src/ub_cli/vendor/mutator/runner.py +index e554c76..fb73927 100644 +--- a/src/ub_cli/vendor/mutator/runner.py ++++ b/src/ub_cli/vendor/mutator/runner.py +@@ -97,6 +97,7 @@ class CommandRunner: + stderr=subprocess.STDOUT, + start_new_session=True, + text=True, ++ errors="replace", + env=environment, + ) + except OSError as exc: diff --git a/scripts/vendor.py b/scripts/vendor.py index 7f51544..4c7bf83 100644 --- a/scripts/vendor.py +++ b/scripts/vendor.py @@ -7,7 +7,9 @@ installs top-level packages that collide with unrelated PyPI projects of the same name. The directory is named `vendor` because the engines skip directories of that name, so `ub crap`, `ub dry`, `ub mutate`, and `ub check` never analyze the engines themselves. -Never edit vendored code by hand; change the pin and rerun this script. +After the rewrite, each scripts/vendor-patches/NAME-*.patch is applied with `git apply`, +for fixes not yet upstream; delete a patch once the pin includes its fix. +Never edit vendored code by hand; change the pin or add a patch and rerun this script. """ import re @@ -20,6 +22,7 @@ ROOT = Path(__file__).resolve().parents[1] VENDOR = ROOT / "src" / "ub_cli" / "vendor" PINS = VENDOR / "VENDORED" +PATCHES = ROOT / "scripts" / "vendor-patches" ENGINES = ("crapper", "dryer", "mutator") NAMES = "|".join(ENGINES) FROM_IMPORT = re.compile(rf"^(\s*)from ({NAMES})(\.[\w.]+)? import ", re.M) @@ -65,6 +68,9 @@ def vendor(name: str, url: str, commit: str) -> None: shutil.copyfile(checkout / "LICENSE", target / "LICENSE") for path in sorted(target.rglob("*.py")): path.write_text(rewrite(path.read_text(), path)) + for patch in sorted(PATCHES.glob(f"{name}-*.patch")): + subprocess.run(["git", "apply", str(patch)], cwd=ROOT, check=True) + print(f"{name}: applied {patch.name}") print(f"{name}: vendored {commit[:12]}") diff --git a/src/ub_cli/vendor/mutator/runner.py b/src/ub_cli/vendor/mutator/runner.py index e554c76..fb73927 100644 --- a/src/ub_cli/vendor/mutator/runner.py +++ b/src/ub_cli/vendor/mutator/runner.py @@ -97,6 +97,7 @@ def run(self, command: Command, cwd: Path, timeout: float | None) -> CommandResu stderr=subprocess.STDOUT, start_new_session=True, text=True, + errors="replace", env=environment, ) except OSError as exc: diff --git a/tests/test_vendor.py b/tests/test_vendor.py index 3045f5a..e717fd3 100644 --- a/tests/test_vendor.py +++ b/tests/test_vendor.py @@ -1,9 +1,11 @@ import importlib.util +import sys import unittest from pathlib import Path from ub_cli.vendor.crapper.discover import iter_source_files as crap_files from ub_cli.vendor.dryer.discover import iter_source_files as dry_files +from ub_cli.vendor.mutator.runner import CommandRunner SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "vendor.py" spec = importlib.util.spec_from_file_location("vendor", SCRIPT) @@ -53,5 +55,13 @@ def test_engines_skip_the_vendored_tree(self): self.assertFalse([path for path in files if vendor.VENDOR in path.parents]) +class PatchTests(unittest.TestCase): + def test_test_output_that_is_not_utf8_does_not_crash_mutator(self): + # scripts/vendor-patches/mutator-runner-undecodable-output.patch + emit = "import sys; sys.stdout.buffer.write(bytes([111, 107, 255])); sys.exit(3)" + result = CommandRunner().run([sys.executable, "-c", emit], Path.cwd(), 30) + self.assertEqual((result.code, result.output), (3, "ok\ufffd")) + + if __name__ == "__main__": unittest.main()