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