Line data Source code
1 : #[cfg(test)]
2 : mod config_tests {
3 :
4 : use std::fs::{remove_file, File};
5 : use std::io::{Read, Write};
6 : use std::path::Path;
7 :
8 : use compute_tools::config::*;
9 :
10 6 : fn write_test_file(path: &Path, content: &str) {
11 6 : let mut file = File::create(path).unwrap();
12 6 : file.write_all(content.as_bytes()).unwrap();
13 6 : }
14 :
15 18 : fn check_file_content(path: &Path, expected_content: &str) {
16 18 : let mut file = File::open(path).unwrap();
17 18 : let mut content = String::new();
18 18 :
19 18 : file.read_to_string(&mut content).unwrap();
20 18 : assert_eq!(content, expected_content);
21 18 : }
22 :
23 : #[test]
24 6 : fn test_line_in_file() {
25 6 : let path = Path::new("./tests/tmp/config_test.txt");
26 6 : write_test_file(path, "line1\nline2.1\t line2.2\nline3");
27 6 :
28 6 : let line = "line2.1\t line2.2";
29 6 : let result = line_in_file(path, line).unwrap();
30 6 : assert!(!result);
31 6 : check_file_content(path, "line1\nline2.1\t line2.2\nline3");
32 6 :
33 6 : let line = "line4";
34 6 : let result = line_in_file(path, line).unwrap();
35 6 : assert!(result);
36 6 : check_file_content(path, "line1\nline2.1\t line2.2\nline3\nline4");
37 6 :
38 6 : remove_file(path).unwrap();
39 6 :
40 6 : let path = Path::new("./tests/tmp/new_config_test.txt");
41 6 : let line = "line4";
42 6 : let result = line_in_file(path, line).unwrap();
43 6 : assert!(result);
44 6 : check_file_content(path, "line4");
45 6 :
46 6 : remove_file(path).unwrap();
47 6 : }
48 : }
|