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
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
mod error;
pub use error::Error;
pub struct Harness {
test_paths: Vec<PathBuf>,
idx: usize,
}
impl Harness {
pub fn new<P: AsRef<Path>>(test_root: P) -> Result<Self, Error> {
let test_paths = WalkDir::new(test_root)
.min_depth(1)
.into_iter()
.filter_map(|e| {
let entry = match e {
Err(e) => return Some(Err(e)),
Ok(e) => e,
};
let path = entry.path();
if path.is_dir() {
None
} else {
let ext = path.extension()?;
if ext == "js" {
let file_name = path.file_name()?;
let file_name = file_name.to_str()?;
if file_name.ends_with("_FIXTURE.js") {
None
} else {
Some(Ok(path.to_path_buf()))
}
} else {
None
}
}
})
.collect::<Result<Vec<PathBuf>, walkdir::Error>>()?;
Ok(Self { test_paths, idx: 0 })
}
fn create_test_from(&self, p: &PathBuf) -> Result<Test, Error> {
let contents = std::fs::read_to_string(p)?;
let (yaml_start, yaml_end) = Self::find_yaml(&contents, p)?;
let yaml = contents[yaml_start..yaml_end].replace("\r", "\n");
let desc = serde_yaml::from_str(&yaml)?;
Ok(Test {
desc,
path: p.clone(),
source: contents,
})
}
fn find_yaml(content: &str, path: &PathBuf) -> Result<(usize, usize), Error> {
let start = content
.find("/*---")
.ok_or_else(|| Error::DescriptionInvalid(path.clone()))?;
let end = content
.find("---*/")
.ok_or_else(|| Error::DescriptionInvalid(path.clone()))?;
Ok((start + 5, end))
}
}
impl Iterator for Harness {
type Item = Result<Test, Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.idx >= self.test_paths.len() {
None
} else {
let p = self.test_paths.get(self.idx)?;
self.idx += 1;
Some(self.create_test_from(p))
}
}
}
pub struct Test {
pub source: String,
pub path: PathBuf,
pub desc: Description,
}
#[derive(Debug, Deserialize, Clone, Serialize, PartialEq)]
pub struct Description {
pub id: Option<String>,
pub esid: Option<String>,
pub es5id: Option<String>,
pub es6id: Option<String>,
pub info: Option<String>,
pub description: Option<String>,
pub negative: Option<Negative>,
#[serde(default)]
pub includes: Vec<String>,
#[serde(default)]
pub flags: Vec<Flag>,
#[serde(default)]
pub locale: Vec<String>,
#[serde(default)]
pub features: Vec<String>,
}
#[derive(Debug, Deserialize, Clone, Serialize, PartialEq)]
pub struct Negative {
pub phase: Phase,
#[serde(alias = "type")]
pub kind: Option<String>,
}
#[derive(Debug, Deserialize, Clone, Copy, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum Phase {
Parse,
Early,
Resolution,
Runtime,
}
#[derive(Debug, Deserialize, PartialEq, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Flag {
OnlyStrict,
NoStrict,
Module,
Raw,
Async,
Generated,
#[serde(alias = "CanBlockIsFalse")]
CanBlockIsFalse,
#[serde(alias = "CanBlockIsTrue")]
CanBlockIsTrue,
#[serde(alias = "non-deterministic")]
NonDeterministic,
}