|
| 1 | +use crate::{ |
| 2 | + config::PatchedVersionMeta, |
| 3 | + download_url, run, |
| 4 | + util::{sha1, TimeFormatter}, |
| 5 | +}; |
| 6 | +use std::{env::current_dir, io::Result, path::PathBuf}; |
| 7 | +use tokio::test; |
| 8 | +use tracing_subscriber::fmt::format; |
| 9 | + |
| 10 | +fn run_dir() -> Result<PathBuf> { |
| 11 | + let run_dir = if current_dir()?.ends_with("run") { |
| 12 | + current_dir()?.join("tests") |
| 13 | + } else { |
| 14 | + current_dir()?.join("run/tests") |
| 15 | + }; |
| 16 | + |
| 17 | + Ok(run_dir) |
| 18 | +} |
| 19 | + |
| 20 | +async fn test_version(version: String) -> Result<()> { |
| 21 | + println!(); |
| 22 | + |
| 23 | + let fmt = format() |
| 24 | + .with_file(true) |
| 25 | + .with_line_number(true) |
| 26 | + .with_timer(TimeFormatter); |
| 27 | + let _ = tracing_subscriber::fmt().event_format(fmt).try_init(); |
| 28 | + |
| 29 | + let run_dir = run_dir().expect("failed retrieving run directory"); |
| 30 | + |
| 31 | + run(vec![version.clone()], run_dir.clone(), true) |
| 32 | + .await |
| 33 | + .expect("failed running patch gen"); |
| 34 | + |
| 35 | + let patched_meta = PatchedVersionMeta::read(&run_dir.join(format!("{version}.json"))) |
| 36 | + .expect("failed reading patched meta"); |
| 37 | + let patch = &run_dir.join(format!("{version}.patch")); |
| 38 | + let vanilla_jar_path = &run_dir.join(format!("{version}-vanilla.jar")); |
| 39 | + let spigot_jar_path = &run_dir.join(format!("{version}-patched.jar")); |
| 40 | + |
| 41 | + download_url(patched_meta.vanilla_download_url, vanilla_jar_path) |
| 42 | + .await |
| 43 | + .expect("failed downloading vanilla"); |
| 44 | + |
| 45 | + assert_eq!( |
| 46 | + sha1(vanilla_jar_path).expect("failed hashing vanilla jar"), |
| 47 | + patched_meta.vanilla_jar_hash |
| 48 | + ); |
| 49 | + assert_eq!( |
| 50 | + sha1(patch).expect("failed hashing patch"), |
| 51 | + patched_meta.patch_hash |
| 52 | + ); |
| 53 | + |
| 54 | + crate::patch(vanilla_jar_path, spigot_jar_path, patch) |
| 55 | + .await |
| 56 | + .expect("failed patching"); |
| 57 | + |
| 58 | + assert_eq!( |
| 59 | + sha1(spigot_jar_path).expect("failed hashing spigot jar"), |
| 60 | + patched_meta.patched_jar_hash |
| 61 | + ); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +macro_rules! tests { |
| 67 | + ($($version:literal),+) => { |
| 68 | + $( |
| 69 | + paste::paste! { |
| 70 | + #[test] |
| 71 | + async fn [<test_ $version>]() { |
| 72 | + test_version(stringify!($version).to_owned().replace("_", ".")).await.unwrap(); |
| 73 | + } |
| 74 | + } |
| 75 | + )+ |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +tests!(1_21_3, 1_10, 1_17, 1_8); |
0 commit comments