test: fallback on npm if pnpm is not available

This commit is contained in:
zhom
2025-08-10 04:16:13 +04:00
parent 82a0b7fdf0
commit f26f301c96
2 changed files with 54 additions and 42 deletions
+7 -24
View File
@@ -699,50 +699,33 @@ impl BundlerTestHelper {
// Build command to run the executable.
#[cfg(windows)]
let (exec_to_run, work_dir) = {
use std::time::{SystemTime, UNIX_EPOCH};
let temp_dir = std::env::temp_dir();
let (exec_to_run, work_dir, _run_guard) = {
// Create a unique temp directory per invocation to avoid filename races
let run_dir = TempDir::new().context("Failed to create temp dir for run copy")?;
let mut base = executable_path
.file_name()
.map(|s| s.to_os_string())
.unwrap_or_else(|| "app.exe".into());
// Ensure .exe extension is present
if Path::new(&base).extension().is_none() {
base.push(".exe");
}
let mut candidate = temp_dir.join(&base);
if candidate.exists() {
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let stem = Path::new(&base)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("app");
let ext = Path::new(&base)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("exe");
candidate = temp_dir.join(format!("{stem}-{stamp}.{ext}"));
}
let candidate = run_dir.path().join(&base);
std::fs::copy(executable_path, &candidate).with_context(|| {
format!(
"Failed to copy executable to temp: {} -> {}",
"Failed to copy executable to temp dir: {} -> {}",
executable_path.display(),
candidate.display()
)
})?;
// Give Windows a brief moment to finalize the new executable on disk
// Small delay to allow AV/file indexing to settle
std::thread::sleep(std::time::Duration::from_millis(50));
// Verify existence
if !candidate.exists() {
anyhow::bail!(
"Temp executable not found after copy: {}",
candidate.display()
);
}
(candidate, temp_dir)
(candidate, run_dir.path().to_path_buf(), run_dir)
};
#[cfg(not(windows))]
+47 -18
View File
@@ -973,10 +973,17 @@ packages:
// Simulate a real pnpm installation by installing the actual dependency
println!("Installing pnpm dependencies for test...");
let pnpm_install = Command::new("pnpm")
.args(["install"])
.current_dir(&test_app_path)
.output();
let pnpm_install = if cfg!(windows) {
Command::new("cmd")
.args(["/C", "pnpm", "install"])
.current_dir(&test_app_path)
.output()
} else {
Command::new("pnpm")
.args(["install"])
.current_dir(&test_app_path)
.output()
};
match pnpm_install {
Ok(output) if output.status.success() => {
@@ -990,10 +997,17 @@ packages:
println!("Falling back to npm install...");
// Fallback to npm if pnpm is not available
let npm_install = Command::new("npm")
.args(["install", "adm-zip"])
.current_dir(&test_app_path)
.output()?;
let npm_install = if cfg!(windows) {
Command::new("cmd")
.args(["/C", "npm", "install", "adm-zip"])
.current_dir(&test_app_path)
.output()?
} else {
Command::new("npm")
.args(["install", "adm-zip"])
.current_dir(&test_app_path)
.output()?
};
if !npm_install.status.success() {
return Err("Failed to install dependencies for test".into());
@@ -1003,10 +1017,17 @@ packages:
println!("pnpm not found, falling back to npm install...");
// Fallback to npm if pnpm is not available
let npm_install = Command::new("npm")
.args(["install", "adm-zip"])
.current_dir(&test_app_path)
.output()?;
let npm_install = if cfg!(windows) {
Command::new("cmd")
.args(["/C", "npm", "install", "adm-zip"])
.current_dir(&test_app_path)
.output()?
} else {
Command::new("npm")
.args(["install", "adm-zip"])
.current_dir(&test_app_path)
.output()?
};
if !npm_install.status.success() {
return Err("Failed to install dependencies for test".into());
@@ -1178,12 +1199,20 @@ async fn test_bundle_simple_project() {
fs::write(project_path.join("package.json"), package_json).unwrap();
fs::write(project_path.join("index.js"), index_js).unwrap();
// Install dependencies using npm (simpler than pnpm for this test)
let npm_install = Command::new("npm")
.arg("install")
.current_dir(&project_path)
.output()
.unwrap();
// Install dependencies using npm (use platform-appropriate invocation)
let npm_install = if cfg!(windows) {
Command::new("cmd")
.args(["/C", "npm", "install"])
.current_dir(&project_path)
.output()
.unwrap()
} else {
Command::new("npm")
.arg("install")
.current_dir(&project_path)
.output()
.unwrap()
};
assert!(npm_install.status.success(), "npm install failed");