forked from biomejs/biome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.rs
293 lines (245 loc) · 9.17 KB
/
spec.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
use crate::check_reformat::CheckReformat;
use crate::snapshot_builder::{SnapshotBuilder, SnapshotOutput};
use crate::utils::strip_rome_placeholders;
use crate::TestFormatLanguage;
use biome_configuration::PartialConfiguration;
use biome_console::EnvConsole;
use biome_deserialize::json::deserialize_from_str;
use biome_diagnostics::print_diagnostic_to_string;
use biome_formatter::{FormatLanguage, FormatOptions, Printed};
use biome_fs::BiomePath;
use biome_parser::AnyParse;
use biome_rowan::{TextRange, TextSize};
use biome_service::settings::Settings;
use biome_service::workspace::{
DocumentFileSource, FeaturesBuilder, RegisterProjectFolderParams, SupportsFeatureParams,
UpdateSettingsParams,
};
use biome_service::App;
use std::ops::Range;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct SpecTestFile<'a> {
input_file: BiomePath,
root_path: &'a Path,
input_code: String,
range_start_index: Option<usize>,
range_end_index: Option<usize>,
}
impl<'a> SpecTestFile<'a> {
pub fn try_from_file(
input_file: &'a str,
root_path: &'a Path,
settings: Option<UpdateSettingsParams>,
) -> Option<SpecTestFile<'a>> {
let mut console = EnvConsole::default();
let app = App::with_console(&mut console);
let file_path = &input_file;
let spec_input_file = Path::new(input_file);
assert!(
spec_input_file.is_file(),
"The input '{}' must exist and be a file.",
spec_input_file.display()
);
app.workspace
.register_project_folder(RegisterProjectFolderParams {
set_as_current_workspace: true,
path: None,
})
.unwrap();
if let Some(settings) = settings {
app.workspace.update_settings(settings).unwrap();
}
let mut input_file = BiomePath::new(file_path);
let can_format = app
.workspace
.file_features(SupportsFeatureParams {
path: input_file.clone(),
features: FeaturesBuilder::new().with_formatter().build(),
})
.unwrap();
if can_format.supports_format() {
let mut input_code = input_file.get_buffer_from_file();
let (_, range_start_index, range_end_index) = strip_rome_placeholders(&mut input_code);
Some(SpecTestFile {
input_file,
root_path,
input_code,
range_start_index,
range_end_index,
})
} else {
None
}
}
pub fn input_code(&self) -> &str {
&self.input_code
}
pub fn file_name(&self) -> &str {
self.input_file.file_name().unwrap().to_str().unwrap()
}
pub fn input_file(&self) -> &BiomePath {
&self.input_file
}
pub fn relative_file_name(&self) -> &str {
self.input_file
.strip_prefix(self.root_path)
.unwrap_or_else(|_| {
panic!(
"failed to strip prefix {:?} from {:?}",
self.root_path, self.input_file
)
})
.to_str()
.expect("failed to get relative file name")
}
fn range(&self) -> (Option<usize>, Option<usize>) {
(self.range_start_index, self.range_end_index)
}
}
pub struct SpecSnapshot<'a, L>
where
L: TestFormatLanguage,
{
test_file: SpecTestFile<'a>,
test_directory: PathBuf,
language: L,
format_language: L::FormatLanguage,
}
impl<'a, L> SpecSnapshot<'a, L>
where
L: TestFormatLanguage,
{
pub fn new(
test_file: SpecTestFile<'a>,
test_directory: &str,
language: L,
format_language: L::FormatLanguage,
) -> Self {
let test_directory = PathBuf::from(test_directory);
SpecSnapshot {
test_file,
test_directory,
language,
format_language,
}
}
fn formatted(
&self,
parsed: &AnyParse,
format_language: L::FormatLanguage,
) -> (String, Printed) {
let has_errors = parsed.has_errors();
let syntax = parsed.syntax();
let range = self.test_file.range();
let result = match range {
(Some(start), Some(end)) => self.language.format_range(
format_language.clone(),
&syntax,
TextRange::new(
TextSize::try_from(start).unwrap(),
TextSize::try_from(end).unwrap(),
),
),
_ => self
.language
.format_node(format_language.clone(), &syntax)
.map(|formatted| formatted.print().unwrap()),
};
let formatted = result.expect("formatting failed");
let output_code = match range {
(Some(_), Some(_)) => {
let range = formatted
.range()
.expect("the result of format_range should have a range");
let mut output_code = self.test_file.input_code.clone();
output_code.replace_range(Range::<usize>::from(range), formatted.as_code());
// Check if output code is a valid syntax
let parsed = self.language.parse(&output_code);
if parsed.has_errors() {
panic!(
"{:?} format range produced an invalid syntax tree: {:?}",
self.test_file.input_file, output_code
)
}
output_code
}
_ => {
let output_code = formatted.as_code();
if !has_errors {
let check_reformat = CheckReformat::new(
&syntax,
output_code,
self.test_file.file_name(),
&self.language,
format_language,
);
check_reformat.check_reformat();
}
output_code.to_string()
}
};
(output_code, formatted)
}
pub fn test(self) {
let input_file = self.test_file().input_file().as_path();
let mut snapshot_builder = SnapshotBuilder::new(input_file)
.with_input(self.test_file.input_code())
.with_separator()
.with_multiple_outputs();
let parsed = self.language.parse(self.test_file.input_code());
let (output_code, printed) = self.formatted(&parsed, self.format_language.clone());
let max_width = self.format_language.options().line_width().value() as usize;
snapshot_builder = snapshot_builder
.with_output_and_options(
SnapshotOutput::new(&output_code).with_index(1),
self.format_language.options().clone(),
)
.with_unimplemented(&printed)
.with_lines_exceeding_max_width(&output_code, max_width);
let options_path = self.test_directory.join("options.json");
if options_path.exists() {
let mut options_path = BiomePath::new(&options_path);
let mut settings = Settings::default();
// SAFETY: we checked its existence already, we assume we have rights to read it
let (test_options, diagnostics) = deserialize_from_str::<PartialConfiguration>(
options_path.get_buffer_from_file().as_str(),
)
.consume();
settings
.merge_with_configuration(test_options.unwrap_or_default(), None, None, &[])
.unwrap();
if !diagnostics.is_empty() {
for diagnostic in diagnostics {
println!("{:?}", print_diagnostic_to_string(&diagnostic));
}
panic!("Configuration is invalid");
}
let format_language = self
.language
.to_format_language(&settings, &DocumentFileSource::from_path(input_file));
let (mut output_code, printed) = self.formatted(&parsed, format_language.clone());
let max_width = format_language.options().line_width().value() as usize;
// There are some logs that print different line endings, and we can't snapshot those
// otherwise we risk automatically having them replaced with LF by git.
//
// This is a workaround, and it might not work for all cases.
const CRLF_PATTERN: &str = "\r\n";
const CR_PATTERN: &str = "\r";
output_code = output_code
.replace(CRLF_PATTERN, "<CRLF>\n")
.replace(CR_PATTERN, "<CR>\n");
snapshot_builder = snapshot_builder
.with_output_and_options(
SnapshotOutput::new(&output_code).with_index(1),
format_language.options(),
)
.with_unimplemented(&printed)
.with_lines_exceeding_max_width(&output_code, max_width);
}
snapshot_builder.finish(self.test_file.relative_file_name());
}
fn test_file(&self) -> &SpecTestFile {
&self.test_file
}
}