1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use chrono::{DateTime, FixedOffset};
use pretty_assertions::assert_eq;
use std::collections::BTreeSet;
use std::fs;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path;
use std::path::Path;
use tempfile::tempdir;
pub fn get_file_content<P: AsRef<Path>>(path: P) -> Vec<String> {
let path = path.as_ref();
let file = File::open(path).unwrap_or_else(|_| panic!("file {:?} not found", path));
let reader = BufReader::new(file);
let mut vec = Vec::new();
for result_line in reader.lines() {
let line =
result_line.unwrap_or_else(|_| panic!("Cannot parse as a line in file {:?}", path));
vec.push(line);
}
vec
}
pub fn get_lines_content<P: AsRef<Path>>(path: P) -> Vec<String> {
let path = path.as_ref();
let file = File::open(path).unwrap_or_else(|_| panic!("file {:?} not found", path));
let reader = BufReader::new(file);
let mut lines_vec: Vec<String> = reader
.lines()
.into_iter()
.map(|result_line| {
result_line.unwrap_or_else(|_| panic!("Cannot parse as a line in file {:?}", path))
})
.collect();
lines_vec.sort();
lines_vec
}
fn get_files_to_compare<P>(dir: P, files_to_check: Option<&Vec<&str>>) -> BTreeSet<String>
where
P: AsRef<Path>,
{
match files_to_check {
None => fs::read_dir(dir.as_ref())
.unwrap()
.map(|file| file.unwrap())
.filter(|file| file.path().is_file())
.map(|file| file.file_name().into_string().unwrap())
.filter(|file_name| !file_name.starts_with('.')) .collect(),
Some(v) => v.iter().map(|&f| f.to_string()).collect(),
}
}
pub fn compare_output_dir_with_expected<P: AsRef<Path>, Q: AsRef<Path>>(
output_dir: P,
files_to_check: Option<Vec<&str>>,
work_dir_expected: Q,
) {
compare_output_dir_with_expected_lines(output_dir, files_to_check, work_dir_expected);
}
pub fn compare_output_dir_with_expected_lines<P: AsRef<Path>, Q: AsRef<Path>>(
output_dir: P,
files_to_check: Option<Vec<&str>>,
work_dir_expected: Q,
) {
let files = get_files_to_compare(&output_dir, files_to_check.as_ref());
let expected_files = get_files_to_compare(&work_dir_expected, files_to_check.as_ref());
assert_eq!(
files, expected_files,
"Different number of produced and expected files"
);
for filename in files {
let output_file_path = output_dir.as_ref().join(filename.clone());
let output_contents = get_lines_content(output_file_path);
let expected_file_path = format!(
"{}/{}",
work_dir_expected.as_ref().to_string_lossy(),
filename
);
let expected_contents = get_lines_content(expected_file_path);
assert_eq!(expected_contents, output_contents);
}
}
pub fn compare_output_dir_with_expected_content<P: AsRef<Path>, Q: AsRef<Path>>(
output_dir: P,
files_to_check: Option<Vec<&str>>,
work_dir_expected: Q,
) {
let files = get_files_to_compare(&output_dir, files_to_check.as_ref());
let expected_files = get_files_to_compare(&work_dir_expected, files_to_check.as_ref());
assert_eq!(
files.len(),
expected_files.len(),
"Different number of produced and expected files"
);
for filename in files {
let output_file_path = output_dir.as_ref().join(filename.clone());
let output_contents = get_file_content(output_file_path);
let expected_file_path = format!(
"{}/{}",
work_dir_expected.as_ref().to_string_lossy(),
filename
);
let expected_contents = get_file_content(expected_file_path);
assert_eq!(expected_contents, output_contents);
}
}
pub fn create_file_with_content(path: &path::Path, file_name: &str, content: &str) {
let file_path = path.join(file_name);
let mut f = File::create(file_path).unwrap();
f.write_all(content.as_bytes()).unwrap();
}
pub fn test_in_tmp_dir<F>(func: F)
where
F: FnOnce(&path::Path),
{
let tmp_dir = tempdir().expect("create temp dir");
{
let path = tmp_dir.as_ref();
func(path);
}
tmp_dir.close().expect("delete temp dir");
}
pub fn get_test_datetime() -> DateTime<FixedOffset> {
DateTime::parse_from_rfc3339("2019-04-03T17:19:00Z").unwrap()
}