-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
307 lines (256 loc) · 9.17 KB
/
build.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2023-2025 Sean Kelleher. All rights reserved.
// Use of this source code is governed by an MIT
// licence that can be found in the LICENCE file.
use std::env;
use std::fs;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
#[macro_use]
extern crate indoc;
extern crate lalrpop;
// TODO This is the first, functional version of this file, which hasn't been
// refactored with best practices (e.g. dependency injection, and verifyng
// abstractions). This should be done before any additional work is applied to
// the file.
fn main() {
lalrpop::process_root().unwrap();
let raw_tgt_dir = env::var("OUT_DIR").unwrap();
let tgt_dir = Path::new(&raw_tgt_dir);
// TODO Consider returning an error from `gen_tests` instead of `panic`ing.
gen_tests("tests/stdout", tgt_dir);
}
fn gen_tests(src_dir: &str, tgt_dir: &Path) {
let tgt_file = tgt_dir.join("tests.rs");
let mut test_file = File::create(&tgt_file)
.expect("couldn't create test file");
let test_dir = tgt_dir.join("tests");
// TODO Consider removing old test directories on each run.
// TODO Consider creating the test directory when running generated tests,
// as opposed to the time of generating the tests.
if let Err(e) = fs::create_dir(&test_dir) {
if e.kind() != ErrorKind::AlreadyExists {
panic!("couldn't create test directory: {e}");
}
}
write_test_file_header(&mut test_file);
let entries = fs::read_dir(src_dir)
.expect("couldn't read test directory");
for maybe_entry in entries {
let entry = maybe_entry
.expect("couldn't read test directory entry");
let file_type = entry.file_type()
.expect("couldn't get file type");
let entry_path = entry.path();
if !file_type.is_file() {
panic!("'{}' isn't a file", entry_path.display());
}
if let Some(ext) = entry_path.extension() {
if ext != "test" && ext != "xtest" {
continue;
}
let entry_stem_raw = entry_path.file_stem()
.expect("couldn't extract file stem from path");
let entry_stem = entry_stem_raw.to_str()
.expect("file stem contains invalid UTF-8");
writedoc!(
test_file,
"
mod {mod_name} {{
#[allow(clippy::wildcard_imports)]
use super::*;
",
mod_name = entry_stem,
)
.expect("couldn't write test file module start");
let is_extended_test = ext == "xtest";
for test in extract_tests(entry_path.clone(), is_extended_test) {
write_test(&mut test_file, &test_dir, entry_stem, &test);
}
write!(test_file, "\n}}\n")
.expect("couldn't write test file module end");
}
}
}
fn extract_tests(entry_path: PathBuf, extended_format: bool) -> Vec<Test> {
let f = File::open(entry_path)
.expect("couldn't open test file");
let mut tests = vec![];
let mut end_matched = false;
let mut cur_test: Option<Test> = None;
let mut test_section = 0;
for maybe_line in BufReader::new(f).lines() {
let line = maybe_line
.expect("couldn't read line from test file");
if end_matched {
panic!("extra lines discovered after closing test marker");
}
let suffix =
if let Some(suf) = line.strip_prefix(TEST_MARKER_START) {
suf
} else if line == TEST_MARKER_SECTION {
test_section += 1;
continue;
} else {
let mut test = cur_test.take()
.expect("lines discovered before first test marker");
#[allow(clippy::collapsible_else_if)]
if extended_format {
if test_section == 0 {
let value = line.strip_prefix("exit_code: ")
.expect("missing 'exit_code' key");
test.tgt_code = value.parse()
.expect("couldn't parse exit code as `i32`");
} else if test_section == 1 {
test.src += &(line + "\n");
} else if test_section == 2 {
test.tgt_stdout += &(line + "\n");
} else if test_section == 3 {
test.tgt_stderr += &(line + "\n");
} else {
panic!("too many sections defined for extended test");
}
} else {
if test_section == 0 {
test.src += &(line + "\n");
} else if test_section == 1 {
test.tgt_stdout += &(line + "\n");
} else {
panic!("too many sections defined for test");
}
}
cur_test.replace(test);
continue;
};
if suffix.is_empty() {
if let Some(t) = cur_test.take() {
tests.push(t);
} else {
panic!("no tests defined");
}
end_matched = true;
continue;
}
let test_name =
if let Some(suf) = suffix.strip_prefix(' ') {
suf
} else {
panic!("expected space before test name");
};
if let Some(t) = cur_test.take() {
if (extended_format && test_section < 3)
|| (!extended_format && test_section < 1) {
panic!("expected output not defined for test '{}'", t.name);
}
tests.push(t);
}
cur_test = Some(Test{
name: String::from(test_name),
src: String::new(),
tgt_code: 0,
tgt_stdout: String::new(),
tgt_stderr: String::new(),
});
test_section = 0;
}
if !end_matched {
panic!("test file didn't end with closing test marker");
}
tests
}
#[derive(Clone)]
struct Test {
name: String,
src: String,
tgt_code: i32,
tgt_stdout: String,
tgt_stderr: String,
}
const TEST_MARKER_START: &str =
"==================================================";
const TEST_MARKER_SECTION: &str =
"--------------------------------------------------";
fn write_test_file_header(test_file: &mut File) {
let header = indoc!{"
use std::fs;
use std::path::Path;
use crate::assert_cmd::Command;
struct Test {
src: String,
exp: TestExpectation,
}
struct TestExpectation {
code: i32,
stdout: String,
stderr: String,
}
fn run_test(test_dir: &str, test_file_path: &str, test: Test) {
let Test{src, exp} = test;
let path = Path::new(test_dir).join(test_file_path);
fs::write(&path, src)
.unwrap_or_else(|_| {
panic!(\"couldn't create test file '{}'\", path.display());
});
let mut cmd = Command::cargo_bin(env!(\"CARGO_PKG_NAME\")).unwrap();
let assert = cmd
.current_dir(test_dir)
.arg(test_file_path)
.assert();
assert
.code(exp.code)
.stdout(exp.stdout)
.stderr(exp.stderr);
}
"};
write!(test_file, "{header}")
.expect("couldn't write test file header");
}
fn write_test(
test_file: &mut File,
root_test_dir: &Path,
file_test_dir_name: &str,
test: &Test,
) {
let file_test_dir = root_test_dir.join(file_test_dir_name);
fs::create_dir_all(&file_test_dir)
.expect("couldn't create directories for file tests");
let test_file_path =
Path::new(file_test_dir_name).join(test.name.clone() + ".sd");
// TODO Indent rendered code.
write!(
test_file,
indoc!{"
#[allow(clippy::manual_string_new)]
#[allow(clippy::needless_raw_string_hashes)]
#[test]
fn {name}() {{
run_test(
\"{test_dir}\",
\"{test_file_path}\",
Test{{
src: String::from(r#\"{src}\"#),
exp: TestExpectation{{
code: {tgt_code},
stdout: String::from(r#\"{tgt_stdout}\"#),
stderr: String::from(r#\"{tgt_stderr}\"#),
}},
}}
);
}}
"},
name = test.name,
test_dir = root_test_dir.display(),
test_file_path = test_file_path.display(),
src = test.src,
tgt_code = test.tgt_code,
tgt_stdout = test.tgt_stdout,
tgt_stderr = test.tgt_stderr,
)
.unwrap_or_else(|_| panic!(
"couldn't write test to test file '{test_file_path:?}'",
));
}