Skip to content

Commit fda92dc

Browse files
committed
test(nix): guard lockfile drift detection and catalog rewrite behavior
Runs resolve-catalog.ts against three inline fixtures: drifted trustedDependencies/patchedDependencies must fail with a diagnostic naming the drifted entries, identical-but-reordered sections must pass, and catalog: refs must be rewritten to exact versions except when they resolve to non-registry specs. The drifted fixture has no catalog: refs, pinning that the check runs for plain projects too.
1 parent 6eaba1f commit fda92dc

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Guards the hook's offline-install prep script (resolve-catalog.ts):
2+
#
3+
# 1. A bun.lock whose trustedDependencies / patchedDependencies drifted from
4+
# package.json must fail fast with a diagnostic naming the drifted
5+
# entries. Without the check, bun silently re-resolves the affected
6+
# dependencies at install time and the drift surfaces as a wall of
7+
# ConnectionRefused errors in the sandbox with no hint of the cause.
8+
# 2. Sections that merely list the same entries in a different order must
9+
# NOT trip the check.
10+
# 3. `catalog:` references resolving to registry versions are rewritten to
11+
# the exact version, but references resolving to non-registry specs
12+
# (github:, git+, tarball URLs, file:) must be left alone — rewriting
13+
# them registers as a changed spec and forces the same re-resolve.
14+
#
15+
# The drifted fixture deliberately contains no `catalog:` refs, pinning that
16+
# the drift check runs for plain projects too, not only catalog users.
17+
_: {
18+
perSystem =
19+
{ pkgs, ... }:
20+
{
21+
checks.lockfileDriftDetection = pkgs.stdenv.mkDerivation {
22+
name = "lockfile-drift-detection";
23+
24+
dontUnpack = true;
25+
26+
nativeBuildInputs = [ pkgs.bun ];
27+
28+
buildPhase = ''
29+
export HOME="$TMPDIR"
30+
script=${../mk-derivation/resolve-catalog.ts}
31+
32+
run() {
33+
rc=0
34+
bun --config=/dev/null --no-install "$script" "$1" >log.txt 2>&1 || rc=$?
35+
}
36+
37+
expect() {
38+
if ! grep -qF "$1" log.txt; then
39+
echo "expected output to contain: $1"
40+
cat log.txt
41+
exit 1
42+
fi
43+
}
44+
45+
# --- drifted: both sections disagree with package.json ---
46+
mkdir drifted
47+
cat > drifted/package.json <<'EOF'
48+
{
49+
"name": "fixture",
50+
"version": "1.0.0",
51+
"trustedDependencies": ["node-pty"],
52+
"patchedDependencies": { "left-pad@1.3.0": "patches/left-pad.patch" }
53+
}
54+
EOF
55+
cat > drifted/bun.lock <<'EOF'
56+
{
57+
"lockfileVersion": 1,
58+
"workspaces": { "": { "name": "fixture" } },
59+
"trustedDependencies": ["esbuild"],
60+
"packages": {}
61+
}
62+
EOF
63+
64+
run drifted
65+
if [ "$rc" -eq 0 ]; then
66+
echo "drifted fixture: expected failure, got exit 0"
67+
cat log.txt
68+
exit 1
69+
fi
70+
expect "bun.lock is out of sync with package.json"
71+
expect "missing from bun.lock: node-pty"
72+
expect "only in bun.lock: esbuild"
73+
expect "patchedDependencies differ for: left-pad@1.3.0"
74+
echo "drifted fixture: failed with diagnostic, as intended"
75+
76+
# --- synced: same entries, different order ---
77+
mkdir synced
78+
cat > synced/package.json <<'EOF'
79+
{
80+
"name": "fixture",
81+
"version": "1.0.0",
82+
"trustedDependencies": ["b-pkg", "a-pkg"],
83+
"patchedDependencies": { "x@1.0.0": "patches/x.patch" }
84+
}
85+
EOF
86+
cat > synced/bun.lock <<'EOF'
87+
{
88+
"lockfileVersion": 1,
89+
"workspaces": { "": { "name": "fixture" } },
90+
"trustedDependencies": ["a-pkg", "b-pkg"],
91+
"patchedDependencies": { "x@1.0.0": "patches/x.patch" },
92+
"packages": {}
93+
}
94+
EOF
95+
96+
run synced
97+
if [ "$rc" -ne 0 ]; then
98+
echo "synced fixture: expected success, got exit $rc"
99+
cat log.txt
100+
exit 1
101+
fi
102+
echo "synced fixture: passed, ordering ignored"
103+
104+
# --- catalog: registry ref rewritten, non-registry ref preserved ---
105+
mkdir catalog
106+
cat > catalog/package.json <<'EOF'
107+
{
108+
"name": "fixture",
109+
"version": "1.0.0",
110+
"dependencies": {
111+
"bar": "catalog:",
112+
"foo": "catalog:"
113+
}
114+
}
115+
EOF
116+
cat > catalog/bun.lock <<'EOF'
117+
{
118+
"lockfileVersion": 1,
119+
"workspaces": {
120+
"": {
121+
"name": "fixture",
122+
"dependencies": { "bar": "catalog:", "foo": "catalog:" }
123+
}
124+
},
125+
"catalog": {
126+
"bar": "^1.0.0",
127+
"foo": "github:user/repo#abcdef"
128+
},
129+
"packages": {
130+
"bar": ["bar@1.2.3", "", {}, "sha512-aaa"],
131+
"foo": ["foo@github:user/repo#abcdef", {}, "abcdef"]
132+
}
133+
}
134+
EOF
135+
136+
run catalog
137+
if [ "$rc" -ne 0 ]; then
138+
echo "catalog fixture: expected success, got exit $rc"
139+
cat log.txt
140+
exit 1
141+
fi
142+
if ! grep -qF '"bar": "1.2.3"' catalog/package.json; then
143+
echo "catalog fixture: registry ref not rewritten to exact version"
144+
cat catalog/package.json
145+
exit 1
146+
fi
147+
if ! grep -qF '"foo": "catalog:"' catalog/package.json; then
148+
echo "catalog fixture: non-registry ref was rewritten; must stay catalog:"
149+
cat catalog/package.json
150+
exit 1
151+
fi
152+
echo "catalog fixture: rewrite behavior correct"
153+
'';
154+
155+
installPhase = ''
156+
mkdir -p "$out"
157+
echo "lockfileDriftDetection: PASS" > "$out/result"
158+
'';
159+
};
160+
};
161+
}

0 commit comments

Comments
 (0)