diff options
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/Cargo.toml | 8 | ||||
-rw-r--r-- | tests/testutils/src/lib.rs | 64 |
2 files changed, 72 insertions, 0 deletions
diff --git a/tests/testutils/Cargo.toml b/tests/testutils/Cargo.toml new file mode 100644 index 000000000..9003822ee --- /dev/null +++ b/tests/testutils/Cargo.toml | |||
@@ -0,0 +1,8 @@ | |||
1 | [package] | ||
2 | name = "testutils" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Aleksey Kladov <[email protected]>"] | ||
5 | |||
6 | [dependencies] | ||
7 | file = "1.0" | ||
8 | difference = "1.0.0" | ||
diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs new file mode 100644 index 000000000..9fc85cc24 --- /dev/null +++ b/tests/testutils/src/lib.rs | |||
@@ -0,0 +1,64 @@ | |||
1 | extern crate difference; | ||
2 | extern crate file; | ||
3 | |||
4 | use std::path::{PathBuf, Path}; | ||
5 | use std::fs::read_dir; | ||
6 | |||
7 | use difference::Changeset; | ||
8 | |||
9 | pub fn assert_equal_text( | ||
10 | expected: &str, | ||
11 | actual: &str, | ||
12 | path: &Path | ||
13 | ) { | ||
14 | if expected != actual { | ||
15 | print_difference(expected, actual, path) | ||
16 | } | ||
17 | } | ||
18 | |||
19 | pub fn collect_tests(paths: &[&str]) -> Vec<PathBuf> { | ||
20 | paths.iter().flat_map(|path| { | ||
21 | let path = test_data_dir().join(path); | ||
22 | test_from_dir(&path).into_iter() | ||
23 | }).collect() | ||
24 | } | ||
25 | |||
26 | fn test_from_dir(dir: &Path) -> Vec<PathBuf> { | ||
27 | let mut acc = Vec::new(); | ||
28 | for file in read_dir(&dir).unwrap() { | ||
29 | let file = file.unwrap(); | ||
30 | let path = file.path(); | ||
31 | if path.extension().unwrap_or_default() == "rs" { | ||
32 | acc.push(path); | ||
33 | } | ||
34 | } | ||
35 | acc.sort(); | ||
36 | acc | ||
37 | } | ||
38 | |||
39 | fn print_difference(expected: &str, actual: &str, path: &Path) { | ||
40 | let dir = project_dir(); | ||
41 | let path = path.strip_prefix(&dir).unwrap_or_else(|_| path); | ||
42 | println!("\nfile: {}", path.display()); | ||
43 | if expected.trim() == actual.trim() { | ||
44 | println!("whitespace difference"); | ||
45 | println!("rewriting the file"); | ||
46 | file::put_text(path, actual).unwrap(); | ||
47 | } else { | ||
48 | let changeset = Changeset::new(actual, expected, "\n"); | ||
49 | println!("{}", changeset); | ||
50 | } | ||
51 | panic!("Comparison failed") | ||
52 | } | ||
53 | |||
54 | fn project_dir() -> PathBuf { | ||
55 | let dir = env!("CARGO_MANIFEST_DIR"); | ||
56 | PathBuf::from(dir) | ||
57 | .parent().unwrap() | ||
58 | .parent().unwrap() | ||
59 | .to_owned() | ||
60 | } | ||
61 | |||
62 | fn test_data_dir() -> PathBuf { | ||
63 | project_dir().join("tests/data") | ||
64 | } \ No newline at end of file | ||