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 2 : fn write_test_file(path: &Path, content: &str) {
11 2 : let mut file = File::create(path).unwrap();
12 2 : file.write_all(content.as_bytes()).unwrap();
13 2 : }
14 :
15 6 : fn check_file_content(path: &Path, expected_content: &str) {
16 6 : let mut file = File::open(path).unwrap();
17 6 : let mut content = String::new();
18 6 :
19 6 : file.read_to_string(&mut content).unwrap();
20 6 : assert_eq!(content, expected_content);
21 6 : }
22 :
23 2 : #[test]
24 2 : fn test_line_in_file() {
25 2 : let path = Path::new("./tests/tmp/config_test.txt");
26 2 : write_test_file(path, "line1\nline2.1\t line2.2\nline3");
27 2 :
28 2 : let line = "line2.1\t line2.2";
29 2 : let result = line_in_file(path, line).unwrap();
30 2 : assert!(!result);
31 2 : check_file_content(path, "line1\nline2.1\t line2.2\nline3");
32 2 :
33 2 : let line = "line4";
34 2 : let result = line_in_file(path, line).unwrap();
35 2 : assert!(result);
36 2 : check_file_content(path, "line1\nline2.1\t line2.2\nline3\nline4");
37 2 :
38 2 : remove_file(path).unwrap();
39 2 :
40 2 : let path = Path::new("./tests/tmp/new_config_test.txt");
41 2 : let line = "line4";
42 2 : let result = line_in_file(path, line).unwrap();
43 2 : assert!(result);
44 2 : check_file_content(path, "line4");
45 2 :
46 2 : remove_file(path).unwrap();
47 2 : }
48 : }
|