-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.rs
More file actions
148 lines (138 loc) · 5.23 KB
/
build.rs
File metadata and controls
148 lines (138 loc) · 5.23 KB
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
//! build.rs build step for assertables
//!
//! This build step does proofreading, and doesn't do any transformation.
//!
//! To see print output:
//!
//! ```sh
//! cargo -vv build
//! ```
use regex::Regex;
use std::path::Path;
use walkdir::WalkDir;
const SKIP_FILE_NAME_LIST: [&str; 2] = ["mod.rs", "lib.rs"];
const HYBRID_FILE_NAME_LIST: [&str; 3] = ["assert.rs", "assert_eq.rs", "assert_ne.rs"];
const DEPRECATED_FILE_NAME_LIST: [&str; 28] = [
"assert_abs_diff_eq.rs",
"assert_abs_diff_ge.rs",
"assert_abs_diff_gt.rs",
"assert_abs_diff_le.rs",
"assert_abs_diff_lt.rs",
"assert_abs_diff_ne.rs",
"assert_command_stderr_contains.rs",
"assert_command_stderr_is_match.rs",
"assert_command_stdout_contains.rs",
"assert_command_stdout_is_match.rs",
"assert_fs_read_to_string_matches.rs",
"assert_io_read_to_string_matches.rs",
"assert_option_none.rs",
"assert_option_some_eq.rs",
"assert_option_some_ne.rs",
"assert_option_some.rs",
"assert_poll_pending.rs",
"assert_poll_ready_eq.rs",
"assert_poll_ready_ne.rs",
"assert_poll_ready.rs",
"assert_program_args_stderr_contains.rs",
"assert_program_args_stderr_is_match.rs",
"assert_program_args_stdout_contains.rs",
"assert_program_args_stdout_is_match.rs",
"assert_result_err.rs",
"assert_result_ok_eq.rs",
"assert_result_ok_ne.rs",
"assert_result_ok.rs",
];
fn do_assert_file_entry(e: walkdir::DirEntry) {
let path = Path::new(e.path());
let stem = e
.path()
.file_stem()
.expect("file_stem")
.to_str()
.expect("to_str");
let text =
std::fs::read_to_string(path).unwrap_or_else(|path| panic!("read_to_string {}", path));
// println!("path:{}, stem:{}, len:{}", path.display(), stem, text.len());
vet_test_assert_x_as_result(&path, &stem, &text);
vet_test_assert_x(&path, &stem, &text);
vet_test_debug_assert_x(&path, &stem, &text);
}
fn vet_test_assert_x_as_result(path: &Path, stem: &str, text: &str) {
let regex_string = format!(
r"(?m)(?s)^\#\[cfg\(test\)\]\nmod (?P<mod>test_{}_as_result) ",
stem
);
match Regex::new(®ex_string).unwrap().captures(&text) {
Some(_captures) => {
// let x = String::from(captures.name("mod").unwrap().as_str());
// println!("{}", x);
}
None => panic!(
"vet_test_assert_foo_as_result path:{}, stem:{}, no match.",
path.display(),
stem
),
}
}
fn vet_test_assert_x(path: &Path, stem: &str, text: &str) {
let regex_string = format!(r"(?m)(?s)^\#\[cfg\(test\)\]\nmod (?P<mod>test_{}) ", stem);
match Regex::new(®ex_string).unwrap().captures(&text) {
Some(_captures) => {
// let x = String::from(captures.name("mod").unwrap().as_str());
// println!("{}", x);
}
None => panic!(
"vet_test_assert_x path:{}, stem:{}, no match.",
path.display(),
stem
),
}
}
// fn fix_test_debug_assert_x(path: &Path, stem: &str, text: &str) {
// let regex_string = format!(r"(?m)(?s)^(?P<block>\#\[cfg\(test\)\]\nmod test_{} .\n.*?\n}})", stem);
// match Regex::new(®ex_string).unwrap().captures(&text) {
// Some(captures) => {
// let test_assert_x = String::from(captures.name("block").unwrap().as_str());
// let test_debug_assert_x = test_assert_x
// .replace(&format!("mod test_{} ", stem), &format!("mod test_debug_{} ", stem))
// .replace(&format!(" = {}", stem), &format!(" = debug_{}", stem))
// .replace("let actual = ", "let _actual = ")
// .replace("let expect = ", "let _expect = ")
// .replace(" assert_eq!(actual, expect)", " // assert_eq!(actual, expect)");
// println!("{}", test_debug_assert_x);
// let output = format!("{}\n\n{}", text.trim(), test_debug_assert_x);
// std::fs::write(path, output).expect(&format!("fix_test_debug_assert_x std::fs::write path:{}, stem:{}", path.display(), stem));
// },
// None => panic!("fix_test_debug_assert_x path:{}, stem:{}, no match.", path.display(), stem)
// }
// }
fn vet_test_debug_assert_x(path: &Path, stem: &str, text: &str) {
let regex_string = format!(
r"(?m)(?s)^\#\[cfg\(test\)\]\nmod (?P<mod>test_debug_{}) ",
stem
);
match Regex::new(®ex_string).unwrap().captures(&text) {
Some(_captures) => {
// let x = String::from(captures.name("mod").unwrap().as_str());
// println!("{}", x);
}
None => panic!(
"vet_test_debug_assert_x path:{}, stem:{}, no match.",
path.display(),
stem
),
}
}
fn main() {
WalkDir::new("src")
.into_iter()
.map(|e| e.unwrap_or_else(|e| panic!("{:?}", e)))
.filter(|e| e.file_type().is_file())
.filter(|e| {
let file_name = e.file_name().to_str().expect("file_name().to_str()");
!SKIP_FILE_NAME_LIST.contains(&file_name)
&& !HYBRID_FILE_NAME_LIST.contains(&file_name)
&& !DEPRECATED_FILE_NAME_LIST.contains(&file_name)
})
.for_each(|e| do_assert_file_entry(e))
}