Skip to content
9 changes: 9 additions & 0 deletions .cargo/mutants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ additional_cargo_args = ["--locked"]
exclude_re = [
# src/ui/widgets.rs
"\\bpaint_button\\b",
"\\bpaint_choice_row\\b",
"\\bpaint_toggle_row\\b",
"\\bpaint_progress\\b",
# The chevron of the Back button.
"\\bback_button\\b",
# src/ui/welcome.rs
"\\bpaint_tile\\b",
# Which game has a backdrop is in has_backdrop, which is tested.
"\\bpaint_backdrops\\b",
# src/ui/theme.rs
"\\bpaint_background\\b",
# src/ui/motion.rs: the button press offset
Expand All @@ -22,4 +27,8 @@ exclude_re = [
"\\bshow_cover\\b",
"\\bspinner\\b",
"in icon$",
# Equivalent mutants: no test can tell them apart from the real code.
# src/ui/images.rs: an image exactly MAX_WIDTH wide is resized to the
# same size.
"replace > with >= in decode$",
]
8 changes: 7 additions & 1 deletion src/banner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ mod tests {

#[test]
fn print_banner_formats_each_line_when_color_is_enabled() {
// console leaves out the colors when stdout is not a terminal, as in
// tests.
let previous = console::colors_enabled();
console::set_colors_enabled(true);
let mut output = Vec::new();

print_banner(&mut output, true).unwrap();
console::set_colors_enabled(previous);

let output = String::from_utf8(output).unwrap();
assert!(!output.is_empty());
assert_eq!(output.lines().count(), SONIC_BANNER.lines().count());
assert!(output.lines().all(|line| line.starts_with('\x1b')));
}

#[test]
Expand Down
14 changes: 11 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,17 @@ fn run_setup(args: SetupArgs, out: &mut CliOutput) -> Result<()> {
}

fn use_rich_prompts(args: &SetupArgs) -> bool {
std::io::stdin().is_terminal()
&& std::io::stdout().is_terminal()
&& !setup_is_fully_specified(args)
rich_prompts_fit(
std::io::stdin().is_terminal(),
std::io::stdout().is_terminal(),
args,
)
}

/// Menus need a person at a terminal on both ends, and only when the flags
/// leave a choice open.
fn rich_prompts_fit(stdin_is_terminal: bool, stdout_is_terminal: bool, args: &SetupArgs) -> bool {
stdin_is_terminal && stdout_is_terminal && !setup_is_fully_specified(args)
}

fn setup_is_fully_specified(args: &SetupArgs) -> bool {
Expand Down
55 changes: 49 additions & 6 deletions src/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
detect_games_with_saved_libraries, not_detected_error, parse_xrandr_resolution,
persist_cli_language_selection, resolve_game_kind_rich, resolve_game_path_rich,
resolve_mods_flag, resolve_setup_languages, resolve_setup_mods, resolve_setup_mods_rich,
run_from_args_with_io, run_with_io, setup_is_fully_specified,
rich_prompts_fit, run_from_args_with_io, run_with_io, setup_is_fully_specified,
};
use crate::config::APP_ID;
use crate::settings::{Settings, Value};
Expand Down Expand Up @@ -809,6 +809,40 @@ fn setup_is_fully_specified_with_explicit_flags() {
assert!(setup_is_fully_specified(&args));
}

#[test]
fn a_preset_or_all_mods_alone_is_a_mod_choice() {
let chosen = |preset: Option<&str>, all_mods: bool| SetupArgs {
game: Some("sa2".to_string()),
game_path: Some(PathBuf::from("/tmp/sa2")),
preset: preset.map(str::to_string),
all_mods,
..empty_setup_args()
};

assert!(!setup_is_fully_specified(&chosen(None, false)));
assert!(setup_is_fully_specified(&chosen(
Some("recommended"),
false
)));
assert!(setup_is_fully_specified(&chosen(None, true)));
}

#[test]
fn rich_prompts_need_a_terminal_on_both_ends_and_an_open_choice() {
let open = empty_setup_args();
let full = SetupArgs {
game: Some("sa2".to_string()),
game_path: Some(PathBuf::from("/tmp/sa2")),
all_mods: true,
..empty_setup_args()
};

assert!(rich_prompts_fit(true, true, &open));
assert!(!rich_prompts_fit(false, true, &open));
assert!(!rich_prompts_fit(true, false, &open));
assert!(!rich_prompts_fit(true, true, &full));
}

#[test]
fn setup_is_not_fully_specified_without_mod_choice() {
let args = SetupArgs {
Expand Down Expand Up @@ -1539,7 +1573,12 @@ fn game_resolution_handles_detection_errors_and_cardinality() {
..empty_setup_args()
};
assert!(super::resolve_game_kind(&empty).is_err());
assert!(super::resolve_game_path(&empty, GameKind::SA2).is_err());
assert_eq!(
super::resolve_game_path(&empty, GameKind::SA2)
.unwrap_err()
.to_string(),
"Sonic Adventure 2 was not detected. Pass --game-path."
);

let mut output = Vec::new();
run_with_io(
Expand Down Expand Up @@ -1652,10 +1691,14 @@ fn download_progress_and_summary_cover_cli_output_paths() {
}

#[test]
fn detect_resolution_uses_a_safe_fallback() {
let (width, height) = super::detect_resolution();
assert!(width > 0);
assert!(height > 0);
fn detect_resolution_asks_the_display_server_then_xrandr() {
let mut xrandr = ProcessCommand::new("xrandr");
xrandr.arg("--current");
let expected =
super::detect_resolution_with(crate::display::resolution_from_display_server, xrandr);

assert_eq!(super::detect_resolution(), expected);
assert!(expected.0 > 1 && expected.1 > 1);
}

#[test]
Expand Down
36 changes: 35 additions & 1 deletion src/external/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async fn download_attempt(
loop {
if tokio::time::Instant::now() >= window_end {
let window_bytes = downloaded - window_start_bytes;
if may_switch_mirror && window_bytes < SLOW_WINDOW_BYTES {
if may_switch_mirror && too_slow(window_bytes) {
tracing::info!(
"{mirror} sent only {window_bytes} bytes of {url} in {} seconds, asking for another mirror",
SPEED_WINDOW.as_secs()
Expand Down Expand Up @@ -323,6 +323,12 @@ async fn download_attempt(
Ok(Attempt::Done(validator))
}

/// Whether a mirror that sent `window_bytes` in one [`SPEED_WINDOW`] is too
/// slow to keep. [`SLOW_WINDOW_BYTES`] itself is fast enough.
fn too_slow(window_bytes: u64) -> bool {
window_bytes < SLOW_WINDOW_BYTES
}

/// Whether the server says it can send part of the file.
fn accepts_ranges(response: &Response) -> bool {
response
Expand Down Expand Up @@ -568,6 +574,34 @@ mod tests {
assert_eq!(log.lock().unwrap().len(), 1);
}

#[test]
fn an_empty_partial_file_starts_over_without_asking_for_a_range() {
init();
let ranges = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let seen = ranges.clone();
let (base, _) = serve(move |request| {
seen.lock()
.unwrap()
.push(request.header("Range").map(str::to_owned));
ranged_file(request, "\"v1\"")
});
let tmp = tempfile::tempdir().unwrap();
let dest = tmp.path().join("file.7z");
seed_partial(&dest, b"", Some("\"v1\""));

download_file_resumable(&format!("{base}/file"), &dest, None).unwrap();

assert_eq!(std::fs::read(&dest).unwrap(), BODY);
assert_eq!(*ranges.lock().unwrap(), [None]);
}

#[test]
fn a_mirror_is_slow_only_below_the_window_minimum() {
assert!(too_slow(0));
assert!(too_slow(SLOW_WINDOW_BYTES - 1));
assert!(!too_slow(SLOW_WINDOW_BYTES));
}

/// Long enough to look slow, short enough not to count as a stall.
const TRICKLE_PAUSE: std::time::Duration = std::time::Duration::from_millis(700);

Expand Down
21 changes: 14 additions & 7 deletions src/external/proton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ fn proton_tag_major(text: &str) -> Option<u32> {

/// Split leading ASCII digits off `text`.
fn split_number(text: &str) -> (Option<u32>, &str) {
let end = text
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(text.len());
(text[..end].parse().ok(), &text[end..])
// The rest always starts after the digits, so callers that loop on it
// move forward.
let rest = text.trim_start_matches(|c: char| c.is_ascii_digit());
(text[..text.len() - rest.len()].parse().ok(), rest)
}

fn configured_tool_from_config(
Expand Down Expand Up @@ -456,9 +456,8 @@ fn configured_tool_from_config(
best_failure = ConfiguredToolLookup::ConfiguredToolUnavailable { name };
}
Err(other) => {
if failure_priority(&other) > failure_priority(&best_failure) {
best_failure = other;
}
// On a tie the two are the same failure.
best_failure = std::cmp::max_by_key(best_failure, other, failure_priority);
}
}
}
Expand Down Expand Up @@ -889,6 +888,14 @@ fn parse_proton_dir_name(name: &str) -> Option<ProtonVersion> {
mod tests {
use super::*;

#[test]
fn split_number_splits_off_the_leading_digits() {
assert_eq!(split_number("10.0-4b"), (Some(10), ".0-4b"));
assert_eq!(split_number("11"), (Some(11), ""));
assert_eq!(split_number("x10"), (None, "x10"));
assert_eq!(split_number(""), (None, ""));
}

#[test]
fn major_from_label_reads_dotted_versions_and_numbered_tags() {
assert_eq!(major_from_label("10.1000-105"), Some(10));
Expand Down
32 changes: 32 additions & 0 deletions src/external/proton_scenarios_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,38 @@ fn switching_to_a_wine_11_build_on_another_drive_is_caught_before_launch() {
);
}

#[test]
fn switching_to_proton_hotfix_is_caught_before_launch() {
let steam = Steam::new();
let game = steam.install_game(&steam.root);
let proton = steam.install(&steam.root, PROTON_10);
steam.install(&steam.root, PROTON_HOTFIX);
steam.launch(&game, PROTON_10, &proton);
// Steam stores `proton_hotfix`, but the folder is `Proton Hotfix`.
steam.force(PROTON_HOTFIX);

assert_eq!(
steam.state(&game),
PrefixState::UnsupportedConfiguredProton {
tool_name: "Proton Hotfix".to_owned(),
major: 11,
}
);
}

#[test]
fn a_proton_10_steam_still_has_to_download_is_not_refused() {
let steam = Steam::new();
let game = steam.install_game(&steam.root);
let ge = steam.install(&steam.root, GE_PROTON_10);
steam.launch(&game, GE_PROTON_10, &ge);
// Forced, but not installed anywhere yet. Proton 10 is supported.
steam.force(PROTON_10);

let state = steam.state(&game);
assert_eq!(state, PrefixState::Ready);
}

#[test]
fn a_custom_tool_is_found_by_its_registered_name_not_its_folder() {
let steam = Steam::new();
Expand Down
26 changes: 25 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ mod tests {
assert_eq!(options.viewport.fullscreen, Some(true));
assert_eq!(options.viewport.clamp_size_to_monitor_size, Some(false));
assert_eq!(options.viewport.app_id.as_deref(), Some(config::APP_ID));
let _ = in_game_mode();
}

#[test]
fn either_gamescope_or_the_steam_gamepad_ui_means_game_mode() {
// No other test in this binary reads these variables, and the test
// leaves both unset.
let set = |gamescope: bool, gamepad_ui: bool| {
for (name, on) in [
("GAMESCOPE_WAYLAND_DISPLAY", gamescope),
("SteamGamepadUI", gamepad_ui),
] {
if on {
unsafe { std::env::set_var(name, "1") };
} else {
unsafe { std::env::remove_var(name) };
}
}
in_game_mode()
};

assert!(!set(false, false));
assert!(set(true, false));
assert!(set(false, true));
assert!(set(true, true));
set(false, false);
}
}
11 changes: 11 additions & 0 deletions src/setup/common_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use super::*;

#[test]
fn unit_tests_cache_downloads_in_their_own_temporary_folder() {
let _guard = crate::test_env::lock();
let base = std::env::temp_dir().join("adventure-mods-tests");

assert_eq!(default_cache_dir(), Some(base.clone()));
// SAFETY: the lock keeps other tests from reading the environment now.
unsafe { std::env::remove_var("ADVENTURE_MODS_CACHE_DIR") };
assert_eq!(download_cache_dir(), base.join("downloads"));
}

#[test]
fn gamebanana_item_dl_base_override() {
use std::io::{Read, Write};
Expand Down
Loading