Skip to content

Commit

Permalink
feat(tasks/transforme_conformance): support for testing oxc's test ca…
Browse files Browse the repository at this point in the history
…ses (#2835)

Related to:
#2822 (comment)

Although `babel` has a lot of test cases, we still need to add edge
cases that `babel` doesn't have.

This PR will allow us to add out test cases to
`/root/oxc/tasks/transform_conformance/tests`. The directory structure
is consistent with `babel`

For example
```shell
# cd /root/oxc/tasks/transform_conformance/tests
- babel-transform-plugin–optional-catch-binding
   - test
       - fixtures
           - your tests # add test cases here
```
  • Loading branch information
Dunqing committed Mar 27, 2024
1 parent 947a9f0 commit d671007
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
9 changes: 9 additions & 0 deletions tasks/transform_conformance/oxc.snap.md
@@ -0,0 +1,9 @@
Passed: 0/1

# All Passed:



# babel-plugin-transform-optional-catch-binding (0/1)
* try-catch-shadow/input.js

6 changes: 6 additions & 0 deletions tasks/transform_conformance/oxc_exec.snap.md
@@ -0,0 +1,6 @@
Passed: 0/0

# All Passed:



35 changes: 23 additions & 12 deletions tasks/transform_conformance/src/lib.rs
Expand Up @@ -32,6 +32,10 @@ fn root() -> PathBuf {
project_root().join("tasks/coverage/babel/packages")
}

fn oxc_test_root() -> PathBuf {
project_root().join("tasks/transform_conformance/tests")
}

fn snap_root() -> PathBuf {
project_root().join("tasks/transform_conformance")
}
Expand Down Expand Up @@ -96,7 +100,9 @@ const CASES: &[&str] = &[
const EXCLUDE_TESTS: &[&str] = &["babel-plugin-transform-typescript/test/fixtures/enum"];

const CONFORMANCE_SNAPSHOT: &str = "babel.snap.md";
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>>,
Expand All @@ -116,17 +122,22 @@ impl TestRunner {

/// # Panics
pub fn run(self) {
let root = root();
let (transform_paths, exec_files) = Self::glob_files(&root, self.options.filter.as_ref());
self.generate_snapshot(SnapshotOption::new(transform_paths, CONFORMANCE_SNAPSHOT));

if self.options.exec {
let fixture_root = fixture_root();
if !fixture_root.exists() {
fs::create_dir(&fixture_root).unwrap();
for (root, snapshot, exec_snapshot) in &[
(root(), CONFORMANCE_SNAPSHOT, EXEC_SNAPSHOT),
(oxc_test_root(), OXC_CONFORMANCE_SNAPSHOT, OXC_EXEC_SNAPSHOT),
] {
let (transform_paths, exec_files) =
Self::glob_files(root, self.options.filter.as_ref());
self.generate_snapshot(root, SnapshotOption::new(transform_paths, snapshot));

if self.options.exec {
let fixture_root = fixture_root();
if !fixture_root.exists() {
fs::create_dir(&fixture_root).unwrap();
}
self.generate_snapshot(root, SnapshotOption::new(exec_files, exec_snapshot));
let _ = fs::remove_dir_all(fixture_root);
}
self.generate_snapshot(SnapshotOption::new(exec_files, EXEC_SNAPSHOT));
let _ = fs::remove_dir_all(fixture_root);
}
}

Expand Down Expand Up @@ -178,7 +189,7 @@ impl TestRunner {
(transform_files, exec_files)
}

fn generate_snapshot(&self, option: SnapshotOption) {
fn generate_snapshot(&self, root: &Path, option: SnapshotOption) {
let SnapshotOption { paths, dest } = option;
let mut snapshot = String::new();
let mut total = 0;
Expand All @@ -191,7 +202,7 @@ impl TestRunner {
continue;
}

let case_root = root().join(&case).join("test/fixtures");
let case_root = root.join(&case).join("test/fixtures");
let num_of_tests = test_cases.len();
total += num_of_tests;

Expand Down
@@ -0,0 +1,3 @@
{
"plugins": ["transform-optional-catch-binding"]
}
@@ -0,0 +1,6 @@
const _unused = "It's a lie, They gonna use me:(";
try {
throw 0;
} catch {
console.log(_unused);
}
@@ -0,0 +1,6 @@
const _unused = "It's a lie, They gonna use me:(";
try {
throw 0;
} catch (_unused2) {
console.log(_unused);
}

0 comments on commit d671007

Please sign in to comment.