| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise native completion without recopying patches or widening selection.""" |
| 3 | |
| 4 | import io |
| 5 | import json |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tarfile |
| 11 | import tempfile |
| 12 | import unittest |
| 13 | |
| 14 | import client |
| 15 | |
| 16 | |
| 17 | class FinishTest(unittest.TestCase): |
| 18 | def setUp(self): |
| 19 | temporary = tempfile.TemporaryDirectory(prefix="infra-finish-", dir=client.ROOT) |
| 20 | self.addCleanup(temporary.cleanup) |
| 21 | self.workspace = Path(temporary.name) / "main" |
| 22 | self.workspace.mkdir() |
| 23 | self.native = Path.home() / ".local/bin/pnpm-real" |
| 24 | allow = {} |
| 25 | for name in ("allowed", "denied", "patched", "dev", "optional"): |
| 26 | package = dict(name="native-finish-" + name, version="1.0.0") |
| 27 | if name != "patched": |
| 28 | package["scripts"] = {"postinstall": |
| 29 | 'node -e "require(\'fs\').writeFileSync(\'built\',process.platform)"'} |
| 30 | with tarfile.open(self.workspace / (name + ".tgz"), "w:gz") as archive: |
| 31 | for filename, content in (("package.json", json.dumps(package).encode()), |
| 32 | ("index.js", b"module.exports = 'before';\n")): |
| 33 | entry = tarfile.TarInfo("package/" + filename) |
| 34 | entry.size = len(content) |
| 35 | archive.addfile(entry, io.BytesIO(content)) |
| 36 | allow[f"native-finish-{name}@file:{name}.tgz"] = name != "denied" |
| 37 | (self.workspace / "patched.patch").write_text( |
| 38 | "diff --git a/index.js b/index.js\n--- a/index.js\n+++ b/index.js\n@@ -1 +1 @@\n" |
| 39 | "-module.exports = 'before';\n+module.exports = 'after';\n") |
| 40 | (self.workspace / "pnpm-workspace.yaml").write_text(json.dumps(dict( |
| 41 | packages=["child", "other"], allowBuilds=allow, |
| 42 | patchedDependencies={"native-finish-patched@1.0.0": "patched.patch"}))) |
| 43 | root = dict(name="finish-root", private=True, packageManager="pnpm@12.14.0") |
| 44 | for directory, package in ((self.workspace, root), |
| 45 | (self.workspace / "child", dict(name="finish-child", private=True)), |
| 46 | (self.workspace / "other", dict(name="finish-other", private=True))): |
| 47 | directory.mkdir(exist_ok=True) |
| 48 | package["scripts"] = {"prepare": |
| 49 | 'node -e "require(\'fs\').writeFileSync(\'prepared\',process.platform)"'} |
| 50 | if directory.name == "child": |
| 51 | package["dependencies"] = {"native-finish-" + name: "file:../" + name + ".tgz" |
| 52 | for name in ("allowed", "denied", "patched")} |
| 53 | package["devDependencies"] = {"native-finish-dev": "file:../dev.tgz"} |
| 54 | package["optionalDependencies"] = {"native-finish-optional": "file:../optional.tgz"} |
| 55 | (directory / "package.json").write_text(json.dumps(package)) |
| 56 | |
| 57 | def finish(self, args, *, nested=False): |
| 58 | cwd = self.workspace / "child" if nested else self.workspace |
| 59 | response = client.request(dict(command="pnpm", cwd=str(cwd), engine=client.engine(), |
| 60 | args=[*args, "--ignore-scripts"])) |
| 61 | self.assertEqual(response.returncode, 0) |
| 62 | data = json.loads(response.stdout) |
| 63 | self.assertIs(data.get("installed"), True, data) |
| 64 | client.finish_install(self.workspace, None, ignore_scripts=True, modules_hash=data["modules_hash"]) |
| 65 | patched = self.workspace / "child/node_modules/native-finish-patched/index.js" |
| 66 | before = patched.stat() if patched.exists() else None |
| 67 | client.finish_install(self.workspace, self.native, modules_hash=data["modules_hash"], cwd=cwd, args=args) |
| 68 | if before: |
| 69 | self.assertEqual(patched.read_text(), "module.exports = 'after';\n") |
| 70 | self.assertEqual((patched.stat().st_ino, patched.stat().st_mtime_ns), |
| 71 | (before.st_ino, before.st_mtime_ns)) |
| 72 | self.assertFalse((self.workspace / "child/node_modules/native-finish-denied/built").exists()) |
| 73 | |
| 74 | def test_pending_dependency_and_workspace_hooks_preserve_patched_files(self): |
| 75 | self.finish(["install"]) |
| 76 | for name in ("allowed", "dev", "optional"): |
| 77 | self.assertEqual((self.workspace / f"child/node_modules/native-finish-{name}/built").read_text(), "darwin") |
| 78 | for directory in (self.workspace, self.workspace / "child", self.workspace / "other"): |
| 79 | self.assertEqual((directory / "prepared").read_text(), "darwin") |
| 80 | (directory / "prepared").unlink() |
| 81 | self.finish(["install", "--frozen-lockfile"]) |
| 82 | for directory in (self.workspace, self.workspace / "child", self.workspace / "other"): |
| 83 | self.assertEqual((directory / "prepared").read_text(), "darwin") |
| 84 | |
| 85 | def test_filtered_production_install_keeps_other_projects_and_excluded_groups_untouched(self): |
| 86 | self.finish(["--filter", "finish-child", "install", "--prod", "--no-optional"]) |
| 87 | self.assertEqual((self.workspace / "child/node_modules/native-finish-allowed/built").read_text(), "darwin") |
| 88 | for path in ("prepared", "child/prepared", "other/prepared", "child/node_modules/native-finish-dev", |
| 89 | "child/node_modules/native-finish-optional"): |
| 90 | self.assertFalse((self.workspace / path).exists(), path) |
| 91 | |
| 92 | def test_nested_add_only_finishes_the_selected_project(self): |
| 93 | self.finish(["add", "native-finish-allowed@file:../allowed.tgz"], nested=True) |
| 94 | self.assertEqual((self.workspace / "child/node_modules/native-finish-allowed/built").read_text(), "darwin") |
| 95 | self.assertEqual((self.workspace / "child/prepared").read_text(), "darwin") |
| 96 | self.assertFalse((self.workspace / "other/prepared").exists()) |
| 97 | |
| 98 | def test_development_only_install_does_not_add_production_dependencies(self): |
| 99 | self.finish(["install", "--dev", "--no-optional"]) |
| 100 | self.assertEqual((self.workspace / "child/node_modules/native-finish-dev/built").read_text(), "darwin") |
| 101 | self.assertEqual((self.workspace / "child/prepared").read_text(), "darwin") |
| 102 | self.assertFalse((self.workspace / "child/node_modules/native-finish-allowed").exists()) |
| 103 | |
| 104 | def test_inherited_agent_environment_keeps_native_pnpm_semantics(self): |
| 105 | manifest = self.workspace / "package.json" |
| 106 | package = json.loads(manifest.read_text()) |
| 107 | package["scripts"]["prepare"] += ( |
| 108 | ' && node -e "require(\'fs\').writeFileSync(\'mode\',process.env.NODE_ENV)"') |
| 109 | manifest.write_text(json.dumps(package)) |
| 110 | result = subprocess.run([str(Path(__file__).with_name("pnpm")), "install"], cwd=self.workspace, |
| 111 | env=dict(os.environ, NODE_ENV="production", NODE_OPTIONS="--max-old-space-size=8192", |
| 112 | npm_config_user_agent="pnpm/12.14.0 node/v26.12.0 darwin arm64"), |
| 113 | text=True, capture_output=True, timeout=120) |
| 114 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 115 | self.assertIn("pnpm: Running Mac build hooks.", result.stderr) |
| 116 | self.assertNotIn("Using Mac installation", result.stderr) |
| 117 | self.assertEqual((self.workspace / "mode").read_text(), "production") |
| 118 | self.assertEqual((self.workspace / "child/node_modules/native-finish-dev/built").read_text(), "darwin") |
| 119 | |
| 120 | |
| 121 | if __name__ == "__main__": |
| 122 | if sys.platform != "darwin" or not client.ROOT.is_dir(): |
| 123 | sys.exit("Run this test in the development VM with Code mounted.") |
| 124 | unittest.main() |