-
-
Notifications
You must be signed in to change notification settings - Fork 971
Expand file tree
/
Copy pathparser_tests.rs
More file actions
261 lines (229 loc) · 7.62 KB
/
parser_tests.rs
File metadata and controls
261 lines (229 loc) · 7.62 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
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
//! Takes comments from biome_js_parser and turns them into test data.
//! This code is derived from rust_analyzer/xtask/codegen/gen_parser_tests
use std::{
collections::HashMap,
fs, mem,
path::{Path, PathBuf},
};
use crate::{update, Mode};
use xtask::{project_root, Result};
fn extract_comment_blocks(
text: &str,
allow_blocks_with_empty_lines: bool,
) -> Vec<(usize, Vec<String>)> {
let mut res = Vec::new();
let prefix = "// ";
let lines = text.lines().map(str::trim_start);
let mut block = (0, vec![]);
for (line_num, line) in lines.enumerate() {
if line == "//" && allow_blocks_with_empty_lines {
block.1.push(String::new());
continue;
}
let is_comment = line.starts_with(prefix);
if is_comment {
block.1.push(line[prefix.len()..].to_string());
} else {
if !block.1.is_empty() {
res.push(mem::take(&mut block));
}
block.0 = line_num + 2;
}
}
if !block.1.is_empty() {
res.push(block)
}
res
}
pub fn generate_parser_tests(mode: Mode) -> Result<()> {
let tests = tests_from_dir(&project_root().join(Path::new("crates/biome_js_parser/src")))?;
fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<bool> {
let tests_dir = project_root().join(into);
if !tests_dir.is_dir() {
fs::create_dir_all(&tests_dir)?;
}
// ok is never actually read, but it needs to be specified to create a Test in existing_tests
let existing = existing_tests(&tests_dir, true)?;
if let Some(t) = existing.keys().find(|&t| !tests.contains_key(t)) {
panic!("Test is deleted: '{}'", t);
}
let mut some_file_was_updated = false;
for (name, test) in tests {
let path = match existing.get(name) {
Some((path, _test)) => path.clone(),
None => tests_dir
.join(name)
.with_extension(test.language.extension()),
};
if let crate::UpdateResult::Updated = update(&path, &test.text, &mode)? {
some_file_was_updated = true;
}
if let Some(options) = &test.options {
let path = tests_dir.join(name).with_extension("options.json");
if let crate::UpdateResult::Updated = update(&path, options, &mode)? {
some_file_was_updated = true;
}
}
}
Ok(some_file_was_updated)
}
let mut some_file_was_updated = false;
some_file_was_updated |= install_tests(
&tests.ok,
"crates/biome_js_parser/test_data/inline/ok",
mode,
)?;
some_file_was_updated |= install_tests(
&tests.err,
"crates/biome_js_parser/test_data/inline/err",
mode,
)?;
if some_file_was_updated {
let _ = filetime::set_file_mtime(
"crates/biome_js_parser/src/tests.rs",
filetime::FileTime::now(),
);
}
Ok(())
}
#[derive(Debug)]
struct Test {
pub name: String,
pub text: String,
pub ok: bool,
pub language: Language,
pub options: Option<String>,
}
#[derive(Debug)]
enum Language {
JavaScript,
TypeScript,
TypeScriptDefinition,
Jsx,
Tsx,
}
impl Language {
const fn extension(&self) -> &'static str {
match self {
Language::JavaScript => "js",
Language::TypeScript => "ts",
Language::TypeScriptDefinition => "d.ts",
Language::Jsx => "jsx",
Language::Tsx => "tsx",
}
}
fn from_file_name(name: &str) -> Option<Language> {
let language = match name.rsplit_once('.')? {
(_, "js") => Language::JavaScript,
(rest, "ts") => match rest.rsplit_once('.') {
Some((_, "d")) => Language::TypeScriptDefinition,
_ => Language::TypeScript,
},
(_, "jsx") => Language::Jsx,
(_, "tsx") => Language::Tsx,
_ => {
return None;
}
};
Some(language)
}
}
#[derive(Default, Debug)]
struct Tests {
pub ok: HashMap<String, Test>,
pub err: HashMap<String, Test>,
}
fn collect_tests(s: &str) -> Vec<Test> {
let mut res = Vec::new();
for comment_block in extract_comment_blocks(s, false).into_iter().map(|(_, x)| x) {
let first_line = &comment_block[0];
let (ok, suffix) = match first_line.split_once(' ') {
Some(("test", suffix)) => (true, suffix),
Some(("test_err", suffix)) => (false, suffix),
_ => continue,
};
let (language, suffix) = match suffix.split_once(' ') {
Some(("jsx", suffix)) => (Language::Jsx, suffix),
Some(("js", suffix)) => (Language::JavaScript, suffix),
Some(("ts", suffix)) => (Language::TypeScript, suffix),
Some(("d.ts", suffix)) => (Language::TypeScriptDefinition, suffix),
Some(("tsx", suffix)) => (Language::Tsx, suffix),
Some((_, suffix)) => (Language::JavaScript, suffix),
_ => panic!("wrong test configuration: {:?}", suffix),
};
let (name, options) = match suffix.split_once(' ') {
Some((name, options)) => (name, Some(options.to_string())),
_ => (suffix, None),
};
let text: String = comment_block[1..]
.iter()
.cloned()
.chain([String::new()])
.collect::<Vec<_>>()
.join("\n");
assert!(!text.trim().is_empty() && text.ends_with('\n'));
res.push(Test {
name: name.to_string(),
options,
text,
ok,
language,
})
}
res
}
fn tests_from_dir(dir: &Path) -> Result<Tests> {
let mut res = Tests::default();
for entry in ::walkdir::WalkDir::new(dir) {
let entry = entry.unwrap();
if !entry.file_type().is_file() {
continue;
}
if entry.path().extension().unwrap_or_default() != "rs" {
continue;
}
process_file(&mut res, entry.path())?;
}
return Ok(res);
fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
let text = fs::read_to_string(path)?;
for test in collect_tests(&text) {
if test.ok {
if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
anyhow::bail!("Duplicate test: {}", old_test.name);
}
} else if let Some(old_test) = res.err.insert(test.name.clone(), test) {
anyhow::bail!("Duplicate test: {}", old_test.name);
}
}
Ok(())
}
}
fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> {
let mut res = HashMap::new();
for file in fs::read_dir(dir)? {
let path = file?.path();
let language = path
.extension()
.and_then(|ext| ext.to_str())
.and_then(Language::from_file_name);
if let Some(language) = language {
let name = path
.file_stem()
.map(|x| x.to_string_lossy().to_string())
.unwrap();
let text = fs::read_to_string(&path)?;
let test = Test {
name: name.clone(),
options: None,
text,
ok,
language,
};
if let Some(old) = res.insert(name, (path, test)) {
println!("Duplicate test: {:?}", old);
}
}
}
Ok(res)
}