]> git.proxmox.com Git - pmg-log-tracker.git/blob - tests/utils.rs
1d14c88254e2ffeca9a60570ff98590c8860b1e3
[pmg-log-tracker.git] / tests / utils.rs
1 use std::io::BufRead;
2
3 use std::env;
4 use std::path::PathBuf;
5
6 fn get_target_dir() -> PathBuf {
7 let bin = env::current_exe().expect("exe path");
8 let mut target_dir = PathBuf::from(bin.parent().expect("bin parent"));
9 target_dir.pop();
10 target_dir
11 }
12
13 pub fn log_tracker_path() -> String {
14 let mut target_dir = get_target_dir();
15 target_dir.push("pmg-log-tracker");
16 target_dir.to_str().unwrap().to_string()
17 }
18
19
20 pub fn compare_output<R: BufRead, R2: BufRead>(command: R, expected: R2) {
21 let expected_lines: Vec<String> = expected.lines().map(|l| l.unwrap()).collect();
22 let command_lines: Vec<String> = command.lines().map(|l| l.unwrap()).collect();
23 assert_eq!(
24 expected_lines.len(),
25 command_lines.len(),
26 "expected: {}, command: {}",
27 expected_lines.len(),
28 command_lines.len()
29 );
30 for (old, new) in expected_lines.iter().zip(command_lines.iter())
31 {
32 if new.starts_with("# ") && old.starts_with("# ") {
33 continue;
34 } else if new.starts_with("# ") {
35 assert!(
36 false,
37 "comment line found in command output, but not in expected output"
38 );
39 } else if old.starts_with("# ") {
40 assert!(
41 false,
42 "comment line found in expected output, but not in command output"
43 );
44 }
45
46 assert_eq!(new, old);
47 }
48 }