Skip to content

Commit

Permalink
Improve installer tests code quality and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen1 committed Jan 17, 2025
1 parent 7de625a commit 146c699
Showing 1 changed file with 60 additions and 94 deletions.
154 changes: 60 additions & 94 deletions tests/installer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@ 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() {
let temp_dir = TempDir::new().unwrap();
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="{}"
Expand All @@ -40,48 +31,32 @@ 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() {
let temp_dir = TempDir::new().unwrap();
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"))
Expand All @@ -90,62 +65,25 @@ 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::<Vec<_>>()
.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")
.arg("-File")
.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() {
Expand All @@ -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"))
Expand All @@ -166,34 +103,25 @@ 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()),
);

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() {
Expand All @@ -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"))
Expand All @@ -214,7 +141,6 @@ fn test_windows_installer_download() {
)
.unwrap();

// Modify install directory
let modified_script = original_script.replace(
"$InstallDir = \"$HOME\\.local\\bin\"",
&format!(
Expand All @@ -225,25 +151,65 @@ fn test_windows_installer_download() {

fs::write(&installer_script, modified_script).unwrap();

// Run the installer
let status = Command::new("powershell")
.arg("-ExecutionPolicy")
.arg("Bypass")
.arg("-File")
.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::<Vec<_>>()
.join("\n")
}

0 comments on commit 146c699

Please sign in to comment.