Skip to content

Commit

Permalink
chore(tasks/transform-conformance): support --override flag (#7774)
Browse files Browse the repository at this point in the history
The `--override` flag used to write the output which is generated by the transformer to the `overrides` folder according to the test path. The acting is similar to the previous `takeover` mode
  • Loading branch information
Dunqing committed Dec 11, 2024
1 parent b089e8b commit 4d33ffb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct TestRunnerOptions {
pub debug: bool,
pub filter: Option<String>,
pub exec: bool,
/// If it's true, will override the output of dismatch test cases,
/// and write it down to `overrides` folder
pub r#override: bool,
}

/// The test runner which walks the babel repository and searches for transformation tests.
Expand Down Expand Up @@ -155,6 +158,9 @@ impl TestRunner {
snapshot.push_str(&case);
snapshot.push_str(&format!(" ({}/{})\n", passed.len(), num_of_tests));
for test_case in failed {
if self.options.r#override {
test_case.write_override_output();
}
snapshot.push_str("* ");
snapshot.push_str(&normalize_path(
test_case.path.strip_prefix(&case_root).unwrap(),
Expand Down
9 changes: 9 additions & 0 deletions tasks/transform_conformance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ fn main() {
let options = TestRunnerOptions {
debug: args.contains("--debug"),
filter: args.opt_value_from_str("--filter").unwrap(),
r#override: args.contains("--override"),
exec: args.contains("--exec"),
};

if options.r#override {
debug_assert!(
options.filter.is_some(),
"Cannot use `--override` without a specific `--filter`, because there's no
doubt about it you do not want to override all Babel's tests"
);
}

TestRunner::new(options.clone()).run();
}
34 changes: 29 additions & 5 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct TestCase {
source_type: SourceType,
transform_options: Result<TransformOptions, Vec<String>>,
pub errors: Vec<OxcDiagnostic>,
pub transformed_code: String,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -73,7 +74,15 @@ impl TestCase {

let source_type = Self::source_type(&path, &options);

Some(Self { kind, path, options, source_type, transform_options, errors })
Some(Self {
kind,
path,
options,
source_type,
transform_options,
errors,
transformed_code: String::new(),
})
}

fn source_type(path: &Path, options: &BabelOptions) -> SourceType {
Expand Down Expand Up @@ -118,6 +127,22 @@ impl TestCase {
Some(babel_output_path)
}

pub fn write_override_output(&self) {
let Some(output_path) = self.get_output_path() else {
return;
};

let override_output_path = if output_path.starts_with(override_root()) {
output_path
} else if let Some(output_path) = Self::convert_to_override_path(&output_path) {
output_path
} else {
return;
};
fs::create_dir_all(override_output_path.parent().unwrap()).unwrap();
fs::write(&override_output_path, self.transformed_code.clone()).unwrap();
}

pub fn skip_test_case(&self) -> bool {
let options = &self.options;

Expand Down Expand Up @@ -240,7 +265,6 @@ impl TestCase {
println!("output_path: {output_path:?}");
}

let mut transformed_code = String::new();
let mut actual_errors = None;
let mut transform_options = None;

Expand All @@ -250,7 +274,7 @@ impl TestCase {
}
Ok(code) => {
transform_options.replace(self.transform_options.as_ref().unwrap().clone());
transformed_code = code;
self.transformed_code = code;
}
}

Expand Down Expand Up @@ -285,7 +309,7 @@ impl TestCase {
},
);

if transformed_code == output {
if self.transformed_code == output {
actual_errors.is_none()
} else {
if actual_errors.is_none() {
Expand Down Expand Up @@ -316,7 +340,7 @@ impl TestCase {
let output = output.cow_replace("\t", " ");
println!("{output}\n");
println!("Transformed:\n");
let transformed_code = transformed_code.cow_replace("\t", " ");
let transformed_code = self.transformed_code.cow_replace("\t", " ");
println!("{transformed_code}");
println!("Errors:\n");
if let Some(actual_errors) = &actual_errors {
Expand Down

0 comments on commit 4d33ffb

Please sign in to comment.