Skip to content

Commit

Permalink
refactor(transform_conformance): clean up some code (#7354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 19, 2024
1 parent 4c367b6 commit 65f1e82
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
6 changes: 3 additions & 3 deletions tasks/transform_conformance/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) const PLUGINS: &[&str] = &[
pub const PLUGINS: &[&str] = &[
"babel-preset-env",
// // ES2024
// "babel-plugin-transform-unicode-sets-regex",
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(crate) const PLUGINS: &[&str] = &[
"regexp",
];

pub(crate) const PLUGINS_NOT_SUPPORTED_YET: &[&str] = &[
pub const PLUGINS_NOT_SUPPORTED_YET: &[&str] = &[
"proposal-decorators",
"transform-class-properties",
"transform-classes",
Expand All @@ -74,7 +74,7 @@ pub(crate) const PLUGINS_NOT_SUPPORTED_YET: &[&str] = &[
"transform-react-constant-elements",
];

pub(crate) const SKIP_TESTS: &[&str] = &[
pub const SKIP_TESTS: &[&str] = &[
// Shouldn't report in transformer
"babel-plugin-transform-typescript/test/fixtures/node-extensions/type-assertion-in-cts",
"babel-plugin-transform-typescript/test/fixtures/node-extensions/type-assertion-in-mts",
Expand Down
30 changes: 30 additions & 0 deletions tasks/transform_conformance/src/exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::{path::Path, process::Command};

use crate::{conformance_root, TestRunner};

impl TestRunner {
pub(crate) fn run_vitest(&self, dest: &Path) {
let version = String::from("node: ")
+ &String::from_utf8(Command::new("node").arg("--version").output().unwrap().stdout)
.unwrap();
let output = Command::new("node")
.current_dir(conformance_root())
.env("NO_COLOR", "1")
.args([
"--run",
"vitest",
"--",
"run",
"--reporter=basic",
"--exclude=\"\"",
"--no-color",
"./fixtures",
])
.output()
.unwrap();
let content = if output.stderr.is_empty() { output.stdout } else { output.stderr };
let output = String::from_utf8(content).unwrap();
let output = version + &output;
self.snapshot.save(dest, &output);
}
}
61 changes: 15 additions & 46 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#![allow(clippy::print_stdout, clippy::print_stderr)]

mod constants;
mod driver;
mod exec;
mod test_case;

use std::{
fs,
path::{Path, PathBuf},
process::Command,
};

use constants::PLUGINS;
Expand All @@ -13,9 +16,6 @@ use oxc_tasks_common::{normalize_path, project_root, Snapshot};
use test_case::TestCaseKind;
use walkdir::WalkDir;

mod driver;
mod test_case;

#[test]
#[cfg(any(coverage, coverage_nightly))]
fn test() {
Expand Down Expand Up @@ -63,17 +63,6 @@ const OXC_CONFORMANCE_SNAPSHOT: &str = "oxc.snap.md";
const EXEC_SNAPSHOT: &str = "babel_exec.snap.md";
const OXC_EXEC_SNAPSHOT: &str = "oxc_exec.snap.md";

struct SnapshotOption {
paths: IndexMap<String, Vec<TestCaseKind>>,
dest: PathBuf,
}

impl SnapshotOption {
fn new(paths: IndexMap<String, Vec<TestCaseKind>>, file_name: &'static str) -> Self {
Self { paths, dest: snap_root().join(file_name) }
}
}

impl TestRunner {
pub fn new(options: TestRunnerOptions) -> Self {
let snapshot = Snapshot::new(&babel_root(), /* show_commit */ true);
Expand All @@ -88,14 +77,15 @@ impl TestRunner {
] {
let (transform_paths, exec_files) =
Self::glob_files(root, self.options.filter.as_ref());
self.generate_snapshot(root, SnapshotOption::new(transform_paths, snapshot));
self.generate_snapshot(root, &snap_root().join(snapshot), transform_paths);

if self.options.exec && !exec_files.is_empty() {
let fixture_root = fixture_root();
let _ = fs::remove_dir_all(&fixture_root);
let _ = fs::create_dir(&fixture_root);
self.generate_snapshot(root, SnapshotOption::new(exec_files, exec_snapshot));
self.run_vitest(&SnapshotOption::new(IndexMap::default(), exec_snapshot));
let dest = snap_root().join(exec_snapshot);
self.generate_snapshot(root, &dest, exec_files);
self.run_vitest(&dest);
}
}
}
Expand Down Expand Up @@ -140,8 +130,12 @@ impl TestRunner {
(transform_files, exec_files)
}

fn generate_snapshot(&self, root: &Path, option: SnapshotOption) {
let SnapshotOption { paths, dest } = option;
fn generate_snapshot(
&self,
root: &Path,
dest: &Path,
paths: IndexMap<String, Vec<TestCaseKind>>,
) {
let mut snapshot = String::new();
let mut total = 0;
let mut all_passed = vec![];
Expand Down Expand Up @@ -194,32 +188,7 @@ impl TestRunner {
let snapshot = format!(
"Passed: {all_passed_count}/{total}\n\n# All Passed:\n{all_passed}\n\n\n{snapshot}"
);
self.snapshot.save(&dest, &snapshot);
self.snapshot.save(dest, &snapshot);
}
}

fn run_vitest(&self, option: &SnapshotOption) {
let version = String::from("node: ")
+ &String::from_utf8(Command::new("node").arg("--version").output().unwrap().stdout)
.unwrap();
let output = Command::new("node")
.current_dir(conformance_root())
.env("NO_COLOR", "1")
.args([
"--run",
"vitest",
"--",
"run",
"--reporter=basic",
"--exclude=\"\"",
"--no-color",
"./fixtures",
])
.output()
.unwrap();
let content = if output.stderr.is_empty() { output.stdout } else { output.stderr };
let output = String::from_utf8(content).unwrap();
let output = version + &output;
self.snapshot.save(&option.dest, &output);
}
}

0 comments on commit 65f1e82

Please sign in to comment.