Skip to content

Commit

Permalink
baby's first unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mxve committed Sep 1, 2024
1 parent bdb3a5f commit 880afb1
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/tests_tmp
109 changes: 107 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ runas = "1.2.0"
winresource = "0.1.17"
static_vcruntime = "2.0"

[dev-dependencies]
strip-ansi-escapes = "0.2.0"
serial_test = "3.1.1"

[package.metadata.winresource]
OriginalFilename = "alterware-launcher.exe"
FileDescription = "AlterWare Launcher"
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ mod misc;
mod self_update;
mod structs;

#[cfg(test)]
mod tests;

use global::*;
use structs::*;

Expand Down
6 changes: 3 additions & 3 deletions src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ pub fn fatal_error(error: &str) {
pub fn human_readable_bytes(bytes: u64) -> String {
let mut bytes = bytes as f64;
let mut i = 0;
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
while bytes > 1024.0 {
const UNITS: [&str; 9] = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
while bytes >= 1024.0 {
bytes /= 1024.0;
i += 1;
}
format!("{bytes:.2}{}", units[i])
format!("{bytes:.2}{}", UNITS[i])
}

pub fn pb_style_download(pb: &ProgressBar, state: bool) {
Expand Down
4 changes: 2 additions & 2 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> Game<'a> {
}
}

#[derive(serde::Deserialize, serde::Serialize)]
#[derive(serde::Deserialize, serde::Serialize, PartialEq, Debug, Clone)]
pub struct Config {
pub update_only: bool,
pub skip_self_update: bool,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl PrintPrefix {
}
}

#[derive(serde::Deserialize, serde::Serialize, Default)]
#[derive(serde::Deserialize, serde::Serialize, Default, PartialEq, Debug, Clone)]
pub struct Cache {
pub iw4x_revision: String,
pub hashes: HashMap<String, String>,
Expand Down
114 changes: 114 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
mod config {
use crate::{config, fs, structs};
use serial_test::serial;
use std::path::Path;

fn setup_test_path() -> std::path::PathBuf {
let path = Path::new("tests_tmp").join("config.json");
if path.exists() {
fs::remove_file(&path).unwrap();
}
path
}

#[test]
#[serial]
fn load_config() {
let path = setup_test_path();

let config = config::load(path.clone());
assert_eq!(config, structs::Config::default());

fs::remove_file(path).unwrap();
}

#[test]
#[serial]
fn save_and_load_config() {
let path = setup_test_path();

let config = structs::Config::default();
config::save(path.clone(), config.clone());
let loaded_config = config::load(path.clone());
assert_eq!(loaded_config, config);

fs::remove_file(path).unwrap();
}

#[test]
#[serial]
fn save_value() {
let path = setup_test_path();

config::save_value(path.clone(), "update_only", true);
let loaded_config = config::load(path.clone());
assert_eq!(loaded_config.update_only, true);

fs::remove_file(path).unwrap();
}
}

mod misc {
use crate::{fs, misc, structs};
use std::{fs::File, io::Write, path::Path};

#[test]
fn file_blake3() {
let path = Path::new("tests_tmp").join("blake3");
if path.exists() {
fs::remove_file(&path).unwrap();
}
fs::create_dir_all(path.parent().unwrap()).unwrap();

File::create(&path)
.unwrap()
.write_all(b"alterware")
.unwrap();
let blake3 = misc::file_blake3(&path).unwrap();
assert_eq!(
blake3,
"f18a70588a620f3a874120dbc2a41f49a0f44349c8a9c10c51f2f1c7bb678daa"
);

fs::remove_file(path).unwrap();
}

#[test]
fn human_readable_bytes() {
assert_eq!(misc::human_readable_bytes(0), "0.00B");
assert_eq!(misc::human_readable_bytes(1023), "1023.00B");
assert_eq!(misc::human_readable_bytes(1024), "1.00KB");
assert_eq!(misc::human_readable_bytes(1099511627776), "1.00TB");
}

#[test]
#[cfg(unix)]
fn is_program_in_path() {
assert!(misc::is_program_in_path("ls"));
assert!(!misc::is_program_in_path("nonexistent"));
}

#[test]
fn cache_operations() {
let path = Path::new("tests_tmp");
fs::create_dir_all(path).unwrap();
let cache_file = path.join("awcache.json");

// Test initial empty cache
let initial_cache = misc::get_cache(path);
assert_eq!(initial_cache, structs::Cache::default());

// Test saving and loading cache
let test_cache = structs::Cache {
iw4x_revision: "r1234".to_string(),
hashes: [("test".to_string(), "hash".to_string())]
.into_iter()
.collect(),
};
misc::save_cache(path, test_cache.clone());
let loaded_cache = misc::get_cache(path);
assert_eq!(loaded_cache, test_cache);

fs::remove_file(&cache_file).unwrap();
}
}

0 comments on commit 880afb1

Please sign in to comment.