From 146c69982a57efdd8e1bd1f57c40b3cd43b5cc2f Mon Sep 17 00:00:00 2001 From: Mohsen Azimi Date: Fri, 17 Jan 2025 15:15:40 +0700 Subject: [PATCH] Improve installer tests code quality and documentation --- tests/installer_test.rs | 154 ++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 94 deletions(-) diff --git a/tests/installer_test.rs b/tests/installer_test.rs index 9c7358e..6fee841 100644 --- a/tests/installer_test.rs +++ b/tests/installer_test.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use std::process::Command; use tempfile::TempDir; +/// Tests the Unix installer using a locally built binary #[cfg(target_family = "unix")] #[test] fn test_unix_installer_with_local_binary() { @@ -13,22 +14,12 @@ fn test_unix_installer_with_local_binary() { let install_dir = temp_dir.path().join("bin"); fs::create_dir_all(&install_dir).unwrap(); - // Get the path to the built binary - let cargo_target_dir = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()); - let binary_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join(cargo_target_dir) - .join("debug") - .join("stop-nagging"); - - // Copy binary to a temp location + let binary_path = get_debug_binary_path("stop-nagging"); let temp_binary = temp_dir.path().join("stop-nagging"); fs::copy(&binary_path, &temp_binary).unwrap(); - // Create a modified installer script that uses the local binary let installer_script = temp_dir.path().join("install.sh"); - - // Create a simplified installer script that just copies the binary - let modified_script = format!( + let script_content = format!( r#"#!/bin/bash set -e INSTALL_DIR="{}" @@ -40,29 +31,21 @@ chmod +x "$INSTALL_DIR/stop-nagging" temp_binary.to_str().unwrap() ); - fs::write(&installer_script, modified_script).unwrap(); + fs::write(&installer_script, script_content).unwrap(); fs::set_permissions(&installer_script, fs::Permissions::from_mode(0o755)).unwrap(); - // Run the installer let status = Command::new("bash") .arg(&installer_script) .status() .unwrap(); - assert!(status.success()); - // Verify installation let installed_binary = install_dir.join("stop-nagging"); assert!(installed_binary.exists()); - - // Test the installed binary - let output = Command::new(&installed_binary) - .arg("--help") - .output() - .unwrap(); - assert!(output.status.success()); + verify_binary_works(&installed_binary); } +/// Tests the Windows installer using a locally built binary #[cfg(target_family = "windows")] #[test] fn test_windows_installer_with_local_binary() { @@ -70,18 +53,10 @@ fn test_windows_installer_with_local_binary() { let install_dir = temp_dir.path().join("bin"); fs::create_dir_all(&install_dir).unwrap(); - // Get the path to the built binary - let cargo_target_dir = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()); - let binary_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join(cargo_target_dir) - .join("debug") - .join("stop-nagging.exe"); - - // Copy binary to a temp location + let binary_path = get_debug_binary_path("stop-nagging.exe"); let temp_binary = temp_dir.path().join("stop-nagging.exe"); fs::copy(&binary_path, &temp_binary).unwrap(); - // Create a modified installer script that uses the local binary let installer_script = temp_dir.path().join("install.ps1"); let original_script = fs::read_to_string( PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -90,37 +65,10 @@ fn test_windows_installer_with_local_binary() { ) .unwrap(); - // Modify the script to use local binary instead of downloading - let modified_script = original_script - .replace( - "Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing", - &format!( - "Copy-Item -Path \"{}\" -Destination \"$InstallDir\\stop-nagging.exe\" -Force", - temp_binary.to_str().unwrap().replace('\\', "\\\\") - ), - ) - .replace( - "$InstallDir = \"$HOME\\.local\\bin\"", - &format!( - "$InstallDir = \"{}\"", - install_dir.to_str().unwrap().replace('\\', "\\\\") - ), - ); - - // Remove the extraction part since we're not dealing with a zip - let modified_script = modified_script - .replace( - "Write-Host \"Extracting archive...\"", - "Write-Host \"Copying binary...\"", - ) - .lines() - .filter(|line| !line.contains("Expand-Archive") && !line.contains("extractDir")) - .collect::>() - .join("\n"); + let modified_script = modify_windows_script(&original_script, &temp_binary, &install_dir); fs::write(&installer_script, modified_script).unwrap(); - // Run the installer let status = Command::new("powershell") .arg("-ExecutionPolicy") .arg("Bypass") @@ -128,24 +76,14 @@ fn test_windows_installer_with_local_binary() { .arg(&installer_script) .status() .unwrap(); - assert!(status.success()); - // Verify installation let installed_binary = install_dir.join("stop-nagging.exe"); assert!(installed_binary.exists()); - - // Test the installed binary - let output = Command::new(&installed_binary) - .arg("--help") - .output() - .unwrap(); - assert!(output.status.success()); + verify_binary_works(&installed_binary); } -// Integration tests that test the actual download process -// These tests are ignored by default as they require internet connection -// and depend on GitHub releases +/// Tests the Unix installer by downloading from GitHub releases #[test] #[ignore] fn test_unix_installer_download() { @@ -157,7 +95,6 @@ fn test_unix_installer_download() { let install_dir = temp_dir.path().join("bin"); fs::create_dir_all(&install_dir).unwrap(); - // Get the installer script let installer_script = temp_dir.path().join("install.sh"); let original_script = fs::read_to_string( PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -166,7 +103,6 @@ fn test_unix_installer_download() { ) .unwrap(); - // Modify install directory let modified_script = original_script.replace( "INSTALL_DIR=\"$HOME/.local/bin\"", &format!("INSTALL_DIR=\"{}\"", install_dir.to_str().unwrap()), @@ -174,26 +110,18 @@ fn test_unix_installer_download() { fs::write(&installer_script, modified_script).unwrap(); - // Run the installer let status = Command::new("bash") .arg(&installer_script) .status() .unwrap(); - assert!(status.success()); - // Verify installation let installed_binary = install_dir.join("stop-nagging"); assert!(installed_binary.exists()); - - // Test the installed binary - let output = Command::new(&installed_binary) - .arg("--help") - .output() - .unwrap(); - assert!(output.status.success()); + verify_binary_works(&installed_binary); } +/// Tests the Windows installer by downloading from GitHub releases #[test] #[ignore] fn test_windows_installer_download() { @@ -205,7 +133,6 @@ fn test_windows_installer_download() { let install_dir = temp_dir.path().join("bin"); fs::create_dir_all(&install_dir).unwrap(); - // Get the installer script let installer_script = temp_dir.path().join("install.ps1"); let original_script = fs::read_to_string( PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -214,7 +141,6 @@ fn test_windows_installer_download() { ) .unwrap(); - // Modify install directory let modified_script = original_script.replace( "$InstallDir = \"$HOME\\.local\\bin\"", &format!( @@ -225,7 +151,6 @@ fn test_windows_installer_download() { fs::write(&installer_script, modified_script).unwrap(); - // Run the installer let status = Command::new("powershell") .arg("-ExecutionPolicy") .arg("Bypass") @@ -233,17 +158,58 @@ fn test_windows_installer_download() { .arg(&installer_script) .status() .unwrap(); - assert!(status.success()); - // Verify installation let installed_binary = install_dir.join("stop-nagging.exe"); assert!(installed_binary.exists()); + verify_binary_works(&installed_binary); +} - // Test the installed binary - let output = Command::new(&installed_binary) - .arg("--help") - .output() - .unwrap(); +// Helper functions + +fn get_debug_binary_path(binary_name: &str) -> PathBuf { + let cargo_target_dir = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()); + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join(cargo_target_dir) + .join("debug") + .join(binary_name) +} + +fn verify_binary_works(binary_path: &PathBuf) { + let output = Command::new(binary_path).arg("--help").output().unwrap(); assert!(output.status.success()); } + +#[cfg(target_family = "windows")] +fn modify_windows_script( + original_script: &str, + temp_binary: &PathBuf, + install_dir: &PathBuf, +) -> String { + let script = original_script + .replace( + "Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing", + &format!( + "Copy-Item -Path \"{}\" -Destination \"$InstallDir\\stop-nagging.exe\" -Force", + temp_binary.to_str().unwrap().replace('\\', "\\\\") + ), + ) + .replace( + "$InstallDir = \"$HOME\\.local\\bin\"", + &format!( + "$InstallDir = \"{}\"", + install_dir.to_str().unwrap().replace('\\', "\\\\") + ), + ); + + // Remove the extraction part since we're not dealing with a zip + script + .replace( + "Write-Host \"Extracting archive...\"", + "Write-Host \"Copying binary...\"", + ) + .lines() + .filter(|line| !line.contains("Expand-Archive") && !line.contains("extractDir")) + .collect::>() + .join("\n") +}