//! Mandatory test 4: the same input compiles byte-identically — twice, with every input list shuffled, with //! CRLF road files, and against committed golden plans (`tests/fixtures/home/expected/`, regenerated with //! `VIA_BLESS=1` only on purpose). mod common; use common::{Parts, home_dir, plans_bytes}; #[test] fn the_same_input_gives_the_same_bytes_twice() { let a = plans_bytes(&Parts::home().compile()); let b = plans_bytes(&Parts::home().compile()); assert!(!a.is_empty()); assert_eq!(a, b); } fn reversed_json_lists(text: &str, open: &str) -> String { // Reverses the one-line items of the JSON list that starts at `open` (the fixtures keep one item per // line, multi-line items are joined first). let start = text.find(open).unwrap() + open.len(); let end = start + text[start..].find("\n ]").unwrap(); let body = &text[start..end]; let mut items: Vec = Vec::new(); let mut cur = String::new(); let mut depth = 0i32; for ch in body.chars() { match ch { '{' | '[' => depth += 1, '}' | ']' => depth -= 1, _ => {} } if ch == ',' && depth == 0 { items.push(cur.trim().to_string()); cur.clear(); } else { cur.push(ch); } } items.push(cur.trim().to_string()); items.reverse(); format!( "{}\n {}{}", &text[..start], items.join(",\n "), &text[end..] ) } #[test] fn shuffled_inputs_give_the_same_bytes() { // QA R-53: file order, people, devices and group members reversed. let base = plans_bytes(&Parts::home().compile()); let mut parts = Parts::home(); parts.roads.reverse(); let parts = parts .edit_devices(|d| reversed_json_lists(d, "\"devices\": [")) .edit_directory(|d| { reversed_json_lists(d, "\"people\": [") .replace(r#"["mark", "jana"]"#, r#"["jana", "mark"]"#) }); assert_ne!( parts.devices, Parts::home().devices, "the device list really was reordered" ); assert_eq!(plans_bytes(&parts.compile()), base); } #[test] fn crlf_road_files_compile_like_lf_ones() { // QA R-39. The custom road's raw block is kept byte for byte, CR included, so it stays LF here. let base = plans_bytes(&Parts::home().compile()); let mut parts = Parts::home(); for (name, bytes) in parts.roads.iter_mut() { if !name.starts_with("x1d7qa") { *bytes = String::from_utf8(bytes.clone()) .unwrap() .replace('\n', "\r\n") .into_bytes(); } } let c = parts.compile(); assert_eq!(c.errors().count(), 0, "{:?}", c.problems); assert_eq!(plans_bytes(&c), base); } #[test] fn plans_match_the_committed_golden_files() { // QA R-54: no paths, no clock, no locale in the output; the same bytes on every platform. let c = Parts::home().compile(); let dir = home_dir().join("expected"); let bless = std::env::var_os("VIA_BLESS").is_some(); if bless { std::fs::create_dir_all(&dir).unwrap(); } for p in c.plans.values() { let path = dir.join(format!("{}.json", p.name)); if bless { std::fs::write(&path, p.to_json()).unwrap(); continue; } let want = std::fs::read_to_string(&path) .unwrap_or_else(|_| panic!("missing golden {}", path.display())); assert_eq!(p.to_json(), want, "{} differs from its golden file", p.name); } assert_eq!( std::fs::read_dir(&dir).unwrap().count(), c.plans.len(), "a golden file without a device" ); }