Skip to content

Commit b6ef1e7

Browse files
committed
test: handle Windows global CLI shim in snap tests
1 parent ec321f7 commit b6ef1e7

1 file changed

Lines changed: 63 additions & 15 deletions

File tree

packages/tools/src/snap-test.ts

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,44 +125,72 @@ function resolveGlobalCliBinary(binDir: string): string {
125125
return fs.realpathSync(binaryPath);
126126
}
127127

128-
function resolveBuiltGlobalCliBinary(casesDir: string): string {
128+
function resolveInstalledGlobalCliTargetBinary(binDir: string): string {
129129
const binaryName = process.platform === 'win32' ? 'vp.exe' : 'vp';
130+
const binaryPath = path.join(
131+
path.resolve(expandHome(binDir)),
132+
'..',
133+
'current',
134+
'bin',
135+
binaryName,
136+
);
137+
if (!fs.existsSync(binaryPath)) {
138+
throw new Error(`Unable to find installed global snap test vp binary at ${binaryPath}`);
139+
}
140+
141+
return fs.realpathSync(binaryPath);
142+
}
143+
144+
function resolveBuiltGlobalCliArtifact(
145+
casesDir: string,
146+
binaryName: string,
147+
packageName: string,
148+
): string {
130149
const repoRoot = resolveRepoRoot(casesDir);
131-
const targetDirs = [
132-
...(process.env.CARGO_TARGET_DIR ? [process.env.CARGO_TARGET_DIR] : []),
133-
path.join(repoRoot, 'target'),
134-
];
135-
const candidates = targetDirs.flatMap((targetDir) => {
136-
const targetCandidates = [
150+
const targetDirs = [path.join(repoRoot, 'target')];
151+
if (process.env.CARGO_TARGET_DIR) {
152+
targetDirs.unshift(process.env.CARGO_TARGET_DIR);
153+
}
154+
const candidates: string[] = [];
155+
for (const targetDir of targetDirs) {
156+
candidates.push(
137157
path.join(targetDir, 'release', binaryName),
138158
path.join(targetDir, 'debug', binaryName),
139-
];
159+
);
140160
if (!fs.existsSync(targetDir)) {
141-
return targetCandidates;
161+
continue;
142162
}
143163

144164
for (const entry of fs.readdirSync(targetDir, { withFileTypes: true })) {
145165
if (entry.isDirectory()) {
146-
targetCandidates.push(
166+
candidates.push(
147167
path.join(targetDir, entry.name, 'release', binaryName),
148168
path.join(targetDir, entry.name, 'debug', binaryName),
149169
);
150170
}
151171
}
152-
return targetCandidates;
153-
});
172+
}
154173
const binaryPath = candidates.find((candidate) => fs.existsSync(candidate));
155174
if (!binaryPath) {
156175
throw new Error(
157-
`Unable to find built Vite+ global CLI binary for global snap tests. Tried:\n${candidates
176+
`Unable to find built Vite+ global CLI ${binaryName} for global snap tests. Tried:\n${candidates
158177
.map((candidate) => `- ${candidate}`)
159-
.join('\n')}\nRun \`cargo build -p vite_global_cli --release\` before snap-test-global.`,
178+
.join('\n')}\nRun \`cargo build -p ${packageName} --release\` before snap-test-global.`,
160179
);
161180
}
162181

163182
return fs.realpathSync(binaryPath);
164183
}
165184

185+
function resolveBuiltGlobalCliBinary(casesDir: string): string {
186+
const binaryName = process.platform === 'win32' ? 'vp.exe' : 'vp';
187+
return resolveBuiltGlobalCliArtifact(casesDir, binaryName, 'vite_global_cli');
188+
}
189+
190+
function resolveBuiltGlobalCliShim(casesDir: string): string {
191+
return resolveBuiltGlobalCliArtifact(casesDir, 'vp-shim.exe', 'vite_trampoline');
192+
}
193+
166194
function newestMtimeMs(filePath: string): number {
167195
const stats = fs.statSync(filePath);
168196
if (!stats.isDirectory()) {
@@ -200,10 +228,30 @@ function assertGlobalCliBinaryMatchesCheckout(binDir: string, casesDir: string):
200228
}
201229

202230
const globalBinary = resolveGlobalCliBinary(binDir);
203-
if (fileContentsEqual(globalBinary, builtBinary)) {
231+
if (process.platform !== 'win32' && fileContentsEqual(globalBinary, builtBinary)) {
204232
return;
205233
}
206234

235+
if (process.platform === 'win32') {
236+
const builtShim = resolveBuiltGlobalCliShim(casesDir);
237+
const installedTargetBinary = resolveInstalledGlobalCliTargetBinary(binDir);
238+
if (
239+
fileContentsEqual(globalBinary, builtShim) &&
240+
fileContentsEqual(installedTargetBinary, builtBinary)
241+
) {
242+
return;
243+
}
244+
245+
throw new Error(
246+
`Global snap tests would use stale Windows vp binaries.\n` +
247+
`Entrypoint: ${globalBinary}\n` +
248+
`Expected entrypoint to match the current checkout shim at ${builtShim}.\n` +
249+
`Installed target: ${installedTargetBinary}\n` +
250+
`Expected target to match the current checkout build at ${builtBinary}.\n` +
251+
'Run `pnpm bootstrap-cli` or `pnpm bootstrap-cli:ci` before snap-test-global.',
252+
);
253+
}
254+
207255
throw new Error(
208256
`Global snap tests would use a stale vp binary from ${globalBinary}.\n` +
209257
`Expected it to match the current checkout build at ${builtBinary}.\n` +

0 commit comments

Comments
 (0)