refactored: DRY repetitive test fixtures with owning struct + rstest

Picks up the remaining test-only suggestions from the style audit. No
production code change.

- launch_profile/templates.rs: introduces a Fixture struct that owns
  the four PathBuf inputs (lib/nat/game/assets) and exposes a ctx()
  method. Eight tests that each rebuilt the same context inline now
  collapse into one rstest with eight cases plus two standalone
  tests for the recursive-substitution and Windows-backslash edge
  cases. Net code: ~120 lines saved.

- launch_profile/render.rs: same pattern. Fixture::new() owns the
  paths plus a FeatureSet; ctx() and rule_ctx() expose what every
  test needs. Six tests stop redeclaring four PathBufs + a five-line
  RuleContext literal apiece.

- instance/launch/mod.rs: three installer_version_dir_name tests
  (Forge / NeoForge / non-installer loaders) become one rstest with
  five cases (one per ModLoader variant). A regression that
  misorders the match arms would now fail a specific case, not just
  the catch-all "non-installer" test.

- instance/content/resource_packs.rs: ten extract_description_*
  tests become one rstest with ten cases. Each case still hits a
  distinct match arm (string, object{text}, object{no text}, array
  variants, null, number, bool); the rstest is cosmetic, not a
  consolidation that loses coverage.

Net effect: same coverage, ~250 lines less repetitive test setup,
clearer intent. Test count rises by 2 because rstest expands cases
into separate test functions.
This commit is contained in:
objz committed 2026-05-27 17:35:50 +02:00
1 parent 41d6da8008
commit 8d7a0903fd
4 files changed
+174 -350

No files matched your search

+18 -58
View File
@@ -141,64 +141,24 @@ fn read_pack_metadata_from_dir(dir: &Path) -> (String, String, Option<Vec<u8>>)
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn extract_description_from_string() {
let val = serde_json::json!("Simple pack");
assert_eq!(extract_description(&val), "Simple pack");
}
#[test]
fn extract_description_from_object_with_text() {
let val = serde_json::json!({"text": "Hello world"});
assert_eq!(extract_description(&val), "Hello world");
}
#[test]
fn extract_description_from_object_without_text() {
let val = serde_json::json!({"color": "red"});
assert_eq!(extract_description(&val), "");
}
#[test]
fn extract_description_from_array_of_strings() {
let val = serde_json::json!(["Hello", " ", "world"]);
assert_eq!(extract_description(&val), "Hello world");
}
#[test]
fn extract_description_from_array_of_objects() {
let val = serde_json::json!([{"text": "A"}, {"text": "B"}]);
assert_eq!(extract_description(&val), "AB");
}
#[test]
fn extract_description_from_mixed_array() {
let val = serde_json::json!(["Prefix ", {"text": "suffix"}]);
assert_eq!(extract_description(&val), "Prefix suffix");
}
#[test]
fn extract_description_from_empty_array() {
let val = serde_json::json!([]);
assert_eq!(extract_description(&val), "");
}
#[test]
fn extract_description_from_null() {
let val = serde_json::Value::Null;
assert_eq!(extract_description(&val), "");
}
#[test]
fn extract_description_from_number() {
let val = serde_json::json!(42);
assert_eq!(extract_description(&val), "");
}
#[test]
fn extract_description_from_bool() {
let val = serde_json::json!(true);
assert_eq!(extract_description(&val), "");
// every case exercises a distinct match arm in extract_description.
// string + object{text} + object{no text} + array(string) +
// array(object) + array(mixed) + array(empty) + null + number + bool.
// mutating any arm to fall through to "" would fail at least one case.
#[rstest::rstest]
#[case::string(json!("Simple pack"), "Simple pack")]
#[case::object_with_text(json!({"text": "Hello world"}), "Hello world")]
#[case::object_without_text(json!({"color": "red"}), "")]
#[case::array_of_strings(json!(["Hello", " ", "world"]), "Hello world")]
#[case::array_of_objects(json!([{"text": "A"}, {"text": "B"}]), "AB")]
#[case::mixed_array(json!(["Prefix ", {"text": "suffix"}]), "Prefix suffix")]
#[case::empty_array(json!([]), "")]
#[case::null(serde_json::Value::Null, "")]
#[case::number(json!(42), "")]
#[case::bool(json!(true), "")]
fn extract_description_handles(#[case] input: serde_json::Value, #[case] expected: &str) {
assert_eq!(extract_description(&input), expected);
}
}
+17 -19
View File
@@ -755,29 +755,27 @@ mod tests {
);
}
#[test]
fn installer_version_dir_name_for_forge() {
// each loader maps to a distinct directory-naming branch. one rstest
// exercises every variant so a regression that misorders the match
// arms in installer_version_dir_name is caught.
#[rstest::rstest]
#[case::forge(ModLoader::Forge, "1.20.1", "47.2.0", Some("1.20.1-forge-47.2.0"))]
#[case::neoforge(ModLoader::NeoForge, "1.21.1", "21.1.0", Some("neoforge-21.1.0"))]
#[case::vanilla(ModLoader::Vanilla, "1.20.1", "v", None)]
#[case::fabric(ModLoader::Fabric, "1.20.1", "0.14.21", None)]
#[case::quilt(ModLoader::Quilt, "1.20.1", "0.20.0", None)]
fn installer_version_dir_name_per_loader(
#[case] loader: ModLoader,
#[case] game_version: &str,
#[case] loader_version: &str,
#[case] expected: Option<&str>,
) {
assert_eq!(
installer_version_dir_name(ModLoader::Forge, "1.20.1", "47.2.0"),
Some("1.20.1-forge-47.2.0".to_string())
installer_version_dir_name(loader, game_version, loader_version),
expected.map(str::to_owned)
);
}
#[test]
fn installer_version_dir_name_for_neoforge() {
assert_eq!(
installer_version_dir_name(ModLoader::NeoForge, "1.21.1", "21.1.0"),
Some("neoforge-21.1.0".to_string())
);
}
#[test]
fn installer_version_dir_name_for_non_installer_loaders() {
assert!(installer_version_dir_name(ModLoader::Vanilla, "1.20.1", "v").is_none());
assert!(installer_version_dir_name(ModLoader::Fabric, "1.20.1", "0.14.21").is_none());
assert!(installer_version_dir_name(ModLoader::Quilt, "1.20.1", "0.20.0").is_none());
}
// exercises the modern-profile early-return in
// migrate_legacy_loader_profile_if_needed. any of inheritsFrom,
// arguments, minecraftArguments present (or game_arguments absent)
+67 -123
View File
@@ -85,31 +85,59 @@ mod tests {
use crate::launch_profile::rules::{FeatureSet, OsCondition, Rule, RuleAction};
use std::path::PathBuf;
fn template_fixture<'a>(
library_directory: &'a std::path::Path,
natives_directory: &'a std::path::Path,
game_directory: &'a std::path::Path,
assets_root: &'a std::path::Path,
) -> TemplateContext<'a> {
TemplateContext {
library_directory,
classpath_separator: ":",
version_name: "1.20.1",
version_type: "release",
natives_directory,
classpath: "a.jar:b.jar",
game_directory,
assets_root,
assets_index_name: "5",
auth_player_name: "Player",
auth_uuid: "00000000-0000-0000-0000-000000000000",
auth_access_token: "token",
auth_xuid: "0",
user_type: "msa",
user_properties: "{}",
launcher_name: "rmcl",
launcher_version: "0.3.0",
clientid: "0",
// owns the path buffers + FeatureSet so each test just calls
// fx.template_ctx() and fx.rule_ctx() instead of declaring four PathBufs
// and a RuleContext inline. all tests use linux/x86_64; if a test needs
// a different OS it sets it explicitly.
struct Fixture {
lib: PathBuf,
nat: PathBuf,
game: PathBuf,
assets: PathBuf,
features: FeatureSet,
}
impl Fixture {
fn new() -> Self {
Self {
lib: PathBuf::from("/m/libraries"),
nat: PathBuf::from("/m/natives"),
game: PathBuf::from("/i/.minecraft"),
assets: PathBuf::from("/m/assets"),
features: FeatureSet::default(),
}
}
fn template_ctx(&self) -> TemplateContext<'_> {
TemplateContext {
library_directory: &self.lib,
classpath_separator: ":",
version_name: "1.20.1",
version_type: "release",
natives_directory: &self.nat,
classpath: "a.jar:b.jar",
game_directory: &self.game,
assets_root: &self.assets,
assets_index_name: "5",
auth_player_name: "Player",
auth_uuid: "00000000-0000-0000-0000-000000000000",
auth_access_token: "token",
auth_xuid: "0",
user_type: "msa",
user_properties: "{}",
launcher_name: "rmcl",
launcher_version: "0.3.0",
clientid: "0",
}
}
fn rule_ctx(&self) -> RuleContext<'_> {
RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &self.features,
}
}
}
@@ -123,24 +151,12 @@ mod tests {
#[test]
fn legacy_minecraft_arguments_render_into_game() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let mut profile = minimal_profile();
profile.minecraft_arguments =
Some("--username ${auth_player_name} --version ${version_name}".into());
let rendered = render_args(&profile, &rule_ctx, &template_ctx).unwrap();
let rendered = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
assert_eq!(rendered.main_class, "net.test.Main");
assert!(rendered.jvm.is_empty());
assert_eq!(
@@ -151,19 +167,7 @@ mod tests {
#[test]
fn modern_arguments_render_with_literals_and_substitutions() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let mut profile = minimal_profile();
profile.arguments = Some(Arguments {
game: vec![
@@ -175,26 +179,14 @@ mod tests {
)],
});
let rendered = render_args(&profile, &rule_ctx, &template_ctx).unwrap();
let rendered = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
assert_eq!(rendered.game, vec!["--username", "Player"]);
assert_eq!(rendered.jvm, vec!["-Djava.library.path=/m/natives"]);
}
#[test]
fn conditional_argument_with_single_value_is_filtered_by_os_rule() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let osx_only = Argument::Conditional {
rules: vec![Rule {
action: RuleAction::Allow,
@@ -214,7 +206,7 @@ mod tests {
jvm: vec![osx_only],
});
let rendered = render_args(&profile, &rule_ctx, &template_ctx).unwrap();
let rendered = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
assert!(
rendered.jvm.is_empty(),
"osx-only arg should be skipped on linux"
@@ -223,19 +215,7 @@ mod tests {
#[test]
fn conditional_argument_with_multiple_value_pushes_all() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let linux_arg = Argument::Conditional {
rules: vec![Rule {
action: RuleAction::Allow,
@@ -258,7 +238,7 @@ mod tests {
jvm: vec![linux_arg],
});
let rendered = render_args(&profile, &rule_ctx, &template_ctx).unwrap();
let rendered = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
assert_eq!(
rendered.jvm,
vec!["--add-opens", "java.base/sun.security.util=ALL-UNNAMED"]
@@ -267,23 +247,11 @@ mod tests {
#[test]
fn missing_main_class_returns_error() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let mut profile = minimal_profile();
profile.main_class = None;
let result = render_args(&profile, &rule_ctx, &template_ctx);
let result = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx());
assert!(matches!(result, Err(RenderError::MissingMainClass)));
}
@@ -340,20 +308,8 @@ mod tests {
let merged = resolve::resolve(loader_profile, tmp.path()).await.unwrap();
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let rendered = render_args(&merged, &rule_ctx, &template_ctx).unwrap();
let fx = Fixture::new();
let rendered = render_args(&merged, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
// child main_class wins after merge
assert_eq!(
@@ -387,19 +343,7 @@ mod tests {
fn modern_arguments_takes_precedence_over_legacy_field() {
// a profile that somehow has both arguments and minecraft_arguments
// should use arguments only (legacy is fallback).
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
let features = FeatureSet::default();
let rule_ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
let fx = Fixture::new();
let mut profile = minimal_profile();
profile.arguments = Some(Arguments {
game: vec![Argument::Literal("--from-arguments".into())],
@@ -407,7 +351,7 @@ mod tests {
});
profile.minecraft_arguments = Some("--from-legacy".into());
let rendered = render_args(&profile, &rule_ctx, &template_ctx).unwrap();
let rendered = render_args(&profile, &fx.rule_ctx(), &fx.template_ctx()).unwrap();
assert_eq!(rendered.game, vec!["--from-arguments"]);
}
}
+72 -150
View File
@@ -101,158 +101,84 @@ mod tests {
use super::*;
use std::path::PathBuf;
fn ctx_fixture<'a>(
library_directory: &'a Path,
natives_directory: &'a Path,
game_directory: &'a Path,
assets_root: &'a Path,
) -> TemplateContext<'a> {
TemplateContext {
library_directory,
classpath_separator: ":",
version_name: "1.20.1",
version_type: "release",
natives_directory,
classpath: "a.jar:b.jar",
game_directory,
assets_root,
assets_index_name: "5",
auth_player_name: "Player",
auth_uuid: "00000000-0000-0000-0000-000000000000",
auth_access_token: "token",
auth_xuid: "0",
user_type: "msa",
user_properties: "{}",
launcher_name: "rmcl",
launcher_version: "0.3.0",
clientid: "0",
// owns the path buffers so tests don't have to declare them inline; the
// ctx() method borrows from self to build a TemplateContext with the
// standard set of values. windows() returns a fixture with backslash
// paths so the OS-independence test stays self-contained.
struct Fixture {
lib: PathBuf,
nat: PathBuf,
game: PathBuf,
assets: PathBuf,
user_properties: String,
}
impl Fixture {
fn unix() -> Self {
Self {
lib: PathBuf::from("/m/libraries"),
nat: PathBuf::from("/m/natives"),
game: PathBuf::from("/i/.minecraft"),
assets: PathBuf::from("/m/assets"),
user_properties: "{}".to_string(),
}
}
fn windows() -> Self {
Self {
lib: PathBuf::from(r"C:\Users\test\.minecraft\libraries"),
nat: PathBuf::from(r"C:\Users\test\.minecraft\natives"),
game: PathBuf::from(r"C:\Users\test\.minecraft"),
assets: PathBuf::from(r"C:\Users\test\.minecraft\assets"),
user_properties: "{}".to_string(),
}
}
fn ctx(&self) -> TemplateContext<'_> {
TemplateContext {
library_directory: &self.lib,
classpath_separator: ":",
version_name: "1.20.1",
version_type: "release",
natives_directory: &self.nat,
classpath: "a.jar:b.jar",
game_directory: &self.game,
assets_root: &self.assets,
assets_index_name: "5",
auth_player_name: "Player",
auth_uuid: "00000000-0000-0000-0000-000000000000",
auth_access_token: "token",
auth_xuid: "0",
user_type: "msa",
user_properties: &self.user_properties,
launcher_name: "rmcl",
launcher_version: "0.3.0",
clientid: "0",
}
}
}
#[test]
fn no_placeholders_unchanged() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(
substitute("--add-modules ALL-MODULE-PATH", &ctx),
"--add-modules ALL-MODULE-PATH"
);
}
#[test]
fn single_known_substitution() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(substitute("v=${version_name}", &ctx), "v=1.20.1");
}
#[test]
fn unknown_placeholder_left_as_is() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(
substitute("x=${not_a_real_var}y", &ctx),
"x=${not_a_real_var}y"
);
}
#[test]
fn unclosed_placeholder_left_as_is() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(
substitute("--prefix ${unclosed", &ctx),
"--prefix ${unclosed"
);
}
#[test]
fn dollar_without_brace_is_literal() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(substitute("$$ literal $5 $", &ctx), "$$ literal $5 $");
}
#[test]
fn multiple_substitutions() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(
substitute("${version_name}-${auth_player_name}", &ctx),
"1.20.1-Player"
);
}
#[test]
fn path_substitution() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(
substitute("-DlibraryDirectory=${library_directory}", &ctx),
"-DlibraryDirectory=/m/libraries"
);
#[rstest::rstest]
#[case::no_placeholders("--add-modules ALL-MODULE-PATH", "--add-modules ALL-MODULE-PATH")]
#[case::single_known("v=${version_name}", "v=1.20.1")]
#[case::unknown_placeholder("x=${not_a_real_var}y", "x=${not_a_real_var}y")]
#[case::unclosed_placeholder("--prefix ${unclosed", "--prefix ${unclosed")]
#[case::dollar_without_brace("$$ literal $5 $", "$$ literal $5 $")]
#[case::multiple("${version_name}-${auth_player_name}", "1.20.1-Player")]
#[case::path("-DlibraryDirectory=${library_directory}", "-DlibraryDirectory=/m/libraries")]
#[case::empty_input("", "")]
fn substitute_handles(#[case] input: &str, #[case] expected: &str) {
let fx = Fixture::unix();
assert_eq!(substitute(input, &fx.ctx()), expected);
}
#[test]
fn substituted_value_is_not_recursively_substituted() {
// simulate a `user_properties` value that happens to contain a `${...}`
// simulate a user_properties value that happens to contain a ${...}
// pattern. it should NOT trigger another substitution pass.
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = TemplateContext {
library_directory: &lib,
classpath_separator: ":",
version_name: "1.20.1",
natives_directory: &nat,
classpath: "a.jar:b.jar",
game_directory: &game,
assets_root: &assets,
assets_index_name: "5",
auth_player_name: "Player",
auth_uuid: "00000000-0000-0000-0000-000000000000",
auth_access_token: "token",
auth_xuid: "0",
user_type: "msa",
user_properties: "${version_name}",
launcher_name: "rmcl",
launcher_version: "0.3.0",
clientid: "0",
version_type: "release",
};
assert_eq!(substitute("${user_properties}", &ctx), "${version_name}");
}
#[test]
fn empty_input_is_empty() {
let lib = PathBuf::from("/m/libraries");
let nat = PathBuf::from("/m/natives");
let game = PathBuf::from("/i/.minecraft");
let assets = PathBuf::from("/m/assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
assert_eq!(substitute("", &ctx), "");
let mut fx = Fixture::unix();
fx.user_properties = "${version_name}".to_string();
assert_eq!(substitute("${user_properties}", &fx.ctx()), "${version_name}");
}
#[test]
@@ -261,12 +187,8 @@ mod tests {
// backslashes. the substitution must not interpret backslashes as
// escape sequences or do anything else clever - it just copies the
// value into the output.
let lib = PathBuf::from(r"C:\Users\test\.minecraft\libraries");
let nat = PathBuf::from(r"C:\Users\test\.minecraft\natives");
let game = PathBuf::from(r"C:\Users\test\.minecraft");
let assets = PathBuf::from(r"C:\Users\test\.minecraft\assets");
let ctx = ctx_fixture(&lib, &nat, &game, &assets);
let result = substitute("-Dpath=${library_directory}", &ctx);
let fx = Fixture::windows();
let result = substitute("-Dpath=${library_directory}", &fx.ctx());
assert!(
result.contains(r"C:\Users\test\.minecraft\libraries"),
"expected backslashes preserved, got: {result}"