Skip to content

Commit 37aa64b

Browse files
committed
refactor(test): deduplicate shim tests and un-ignore path_env test
Extract clear_shim_env_vars() and assert_detect_shim_tool_from_argv0() helpers to eliminate copy-paste between vpx/vpr tests — the duplication caused the original missing env var cleanup bug. Remove #[ignore] from test_format_path_prepended_always_prepends now that #[serial] makes it safe to run.
1 parent a015465 commit 37aa64b

2 files changed

Lines changed: 24 additions & 36 deletions

File tree

crates/vite_global_cli/src/shim/mod.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,18 @@ mod tests {
217217
assert!(!is_potential_package_binary("another-fake-tool"));
218218
}
219219

220+
/// Clear both shim env vars to isolate tests.
221+
/// SAFETY: caller must be `#[serial]` since this mutates process-global state.
222+
unsafe fn clear_shim_env_vars() {
223+
unsafe {
224+
std::env::remove_var(SHIM_TOOL_ENV_VAR);
225+
std::env::remove_var(LEGACY_SHIM_TOOL_ENV_VAR);
226+
}
227+
}
228+
220229
#[test]
221230
#[serial]
222231
fn test_detect_shim_tool_from_env_var() {
223-
// SAFETY: We're in a test at startup, no other threads
224232
unsafe {
225233
std::env::set_var(SHIM_TOOL_ENV_VAR, "node");
226234
std::env::remove_var(LEGACY_SHIM_TOOL_ENV_VAR);
@@ -236,7 +244,6 @@ mod tests {
236244
fn test_detect_shim_tool_from_legacy_env_var() {
237245
// When only VITE_PLUS_SHIM_TOOL is set (older trampoline), it should
238246
// fall back to reading the legacy env var.
239-
// SAFETY: We're in a test at startup, no other threads
240247
unsafe {
241248
std::env::remove_var(SHIM_TOOL_ENV_VAR);
242249
std::env::set_var(LEGACY_SHIM_TOOL_ENV_VAR, "npm");
@@ -247,46 +254,28 @@ mod tests {
247254
assert!(std::env::var(LEGACY_SHIM_TOOL_ENV_VAR).is_err());
248255
}
249256

257+
/// Tests that argv0-based tool detection works for a given tool name,
258+
/// including full path and .exe extension variants.
259+
fn assert_detect_shim_tool_from_argv0(tool: &str) {
260+
unsafe { clear_shim_env_vars() };
261+
262+
assert_eq!(detect_shim_tool(tool), Some(tool.to_string()));
263+
assert_eq!(
264+
detect_shim_tool(&format!("/home/user/.vite-plus/bin/{tool}")),
265+
Some(tool.to_string()),
266+
);
267+
assert_eq!(detect_shim_tool(&format!("{tool}.exe")), Some(tool.to_string()),);
268+
}
269+
250270
#[test]
251271
#[serial]
252272
fn test_detect_shim_tool_vpx() {
253-
// vpx should be detected via the argv0 check, before the env var check
254-
// and before is_shim_tool (which would incorrectly match it as a package binary)
255-
// SAFETY: We're in a test
256-
unsafe {
257-
std::env::remove_var(SHIM_TOOL_ENV_VAR);
258-
std::env::remove_var(LEGACY_SHIM_TOOL_ENV_VAR);
259-
}
260-
let result = detect_shim_tool("vpx");
261-
assert_eq!(result, Some("vpx".to_string()));
262-
263-
// Also works with full path
264-
let result = detect_shim_tool("/home/user/.vite-plus/bin/vpx");
265-
assert_eq!(result, Some("vpx".to_string()));
266-
267-
// Also works with .exe extension (Windows)
268-
let result = detect_shim_tool("vpx.exe");
269-
assert_eq!(result, Some("vpx".to_string()));
273+
assert_detect_shim_tool_from_argv0("vpx");
270274
}
271275

272276
#[test]
273277
#[serial]
274278
fn test_detect_shim_tool_vpr() {
275-
// vpr should be detected via the argv0 check, same pattern as vpx
276-
// SAFETY: We're in a test
277-
unsafe {
278-
std::env::remove_var(SHIM_TOOL_ENV_VAR);
279-
std::env::remove_var(LEGACY_SHIM_TOOL_ENV_VAR);
280-
}
281-
let result = detect_shim_tool("vpr");
282-
assert_eq!(result, Some("vpr".to_string()));
283-
284-
// Also works with full path
285-
let result = detect_shim_tool("/home/user/.vite-plus/bin/vpr");
286-
assert_eq!(result, Some("vpr".to_string()));
287-
288-
// Also works with .exe extension (Windows)
289-
let result = detect_shim_tool("vpr.exe");
290-
assert_eq!(result, Some("vpr".to_string()));
279+
assert_detect_shim_tool_from_argv0("vpr");
291280
}
292281
}

crates/vite_shared/src/path_env.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ mod tests {
142142
}
143143

144144
#[test]
145-
#[ignore]
146145
#[serial_test::serial]
147146
fn test_format_path_prepended_always_prepends() {
148147
// Even if the directory exists somewhere in PATH, it should be prepended

0 commit comments

Comments
 (0)