Skip to content

Commit

Permalink
Add unit test for parsing maybenot machines on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Oct 30, 2024
1 parent f73d69d commit b5266cb
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions talpid-wireguard/src/wireguard_nt/daita.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,8 @@ impl Machinist {

static MAYBENOT_MACHINES: OnceCell<Vec<maybenot::Machine>> = OnceCell::new();

let machines = MAYBENOT_MACHINES.get_or_try_init(|| {
let path = resource_dir.join("maybenot_machines_v2");
log::debug!("Reading maybenot machines from {}", path.display());

let mut machines = vec![];
let machines_str = fs::read_to_string(path).map_err(Error::EnumerateMachines)?;
for machine_str in machines_str.lines() {
let machine_str = machine_str.trim();
if matches!(machine_str.chars().next(), None | Some('#')) {
continue;
}
log::debug!("Adding maybenot machine: {machine_str}");
machines.push(
machine_str
.parse::<maybenot::Machine>()
.map_err(|_error| Error::InvalidMachine(machine_str.to_owned()))?,
);
}
Ok(machines)
})?;
let machines =
MAYBENOT_MACHINES.get_or_try_init(|| load_maybenot_machines(resource_dir))?;

let quit_event =
talpid_windows::sync::Event::new(true, false).map_err(Error::InitializeQuitEvent)?;
Expand Down Expand Up @@ -456,3 +438,41 @@ impl Machinist {
}
}
}

fn load_maybenot_machines(resource_dir: &Path) -> Result<Vec<maybenot::Machine>, Error> {
let path = resource_dir.join("maybenot_machines_v2");
log::debug!("Reading maybenot machines from {}", path.display());

let mut machines = vec![];
let machines_str = fs::read_to_string(path).map_err(Error::EnumerateMachines)?;
for machine_str in machines_str.lines() {
let machine_str = machine_str.trim();
if matches!(machine_str.chars().next(), None | Some('#')) {
continue;
}
log::debug!("Adding maybenot machine: {machine_str}");
machines.push(
machine_str
.parse::<maybenot::Machine>()
.map_err(|_error| Error::InvalidMachine(machine_str.to_owned()))?,
);
}
Ok(machines)
}

#[cfg(test)]
mod test {
use super::load_maybenot_machines;
use std::path::PathBuf;

#[test]
fn test_load_maybenot_machines() {
let dist_assets = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.expect("CARGO_MANIFEST_DIR env var not set")
.join("..")
.join("dist-assets");

load_maybenot_machines(&dist_assets).unwrap();
}
}

0 comments on commit b5266cb

Please sign in to comment.