Skip to content

Commit

Permalink
chore(tasks): add commit sha to snapshots to make sure submodules are…
Browse files Browse the repository at this point in the history
… not outdated

closes #2819
  • Loading branch information
Boshen committed May 14, 2024
1 parent b27a905 commit 35a356e
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 16 deletions.
2 changes: 2 additions & 0 deletions tasks/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use std::path::{Path, PathBuf};
mod babel;
mod diff;
mod request;
mod snapshot;
mod test_file;

pub use crate::{
babel::{BabelOptions, TestOs},
request::agent,
snapshot::Snapshot,
test_file::*,
};
pub use diff::print_diff_in_terminal;
Expand Down
47 changes: 47 additions & 0 deletions tasks/common/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{
env,
fs::{self, File},
io::Write,
path::Path,
process::Command,
};

pub struct Snapshot {
sha: String,
}

impl Snapshot {
/// # Panics
///
/// * Git operation fails
pub fn new(git_repo_path: &Path) -> Self {
let path = git_repo_path.to_str().unwrap();
let output = Command::new("git")
.args(["-C", path, "rev-parse", "--short", "HEAD"])
.output()
.unwrap()
.stdout;
let sha = String::from_utf8(output).unwrap().trim().to_string();
Self { sha }
}

/// # Panics
///
/// * File operation fails
pub fn save(&self, path: &Path, content: &str) {
if path.exists() {
let file = fs::read_to_string(path).unwrap();
if let Some(old_sha) = file.lines().next().unwrap().strip_prefix("commit: ") {
let outdated = self.sha != old_sha && env::var("UPDATE_SNAPSHOT").is_err();
assert!(
!outdated,
"\nRepository is outdated, please run `just submodules` to update it.\n"
);
}
}

let mut file = File::create(path).unwrap();
let content = format!("commit: {}\n\n{content}", self.sha);
file.write_all(content.as_bytes()).unwrap();
}
}
2 changes: 2 additions & 0 deletions tasks/coverage/codegen_babel.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

codegen_babel Summary:
AST Parsed : 2099/2099 (100.00%)
Positive Passed: 2099/2099 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/codegen_misc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 78d1d3a7

codegen_misc Summary:
AST Parsed : 16/16 (100.00%)
Positive Passed: 16/16 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/codegen_test262.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 17ba9aea47

codegen_test262 Summary:
AST Parsed : 45836/45836 (100.00%)
Positive Passed: 45836/45836 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/codegen_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 64d2eeea7b

codegen_typescript Summary:
AST Parsed : 5243/5243 (100.00%)
Positive Passed: 5228/5243 (99.71%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/minifier_babel.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

minifier_babel Summary:
AST Parsed : 1638/1638 (100.00%)
Positive Passed: 1638/1638 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/minifier_test262.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 17ba9aea47

minifier_test262 Summary:
AST Parsed : 45836/45836 (100.00%)
Positive Passed: 45836/45836 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

parser_babel Summary:
AST Parsed : 2093/2099 (99.71%)
Positive Passed: 2086/2099 (99.38%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 78d1d3a7

parser_misc Summary:
AST Parsed : 16/16 (100.00%)
Positive Passed: 16/16 (100.00%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/parser_test262.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 17ba9aea47

parser_test262 Summary:
AST Parsed : 45289/45289 (100.00%)
Positive Passed: 45289/45289 (100.00%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 64d2eeea7b

parser_typescript Summary:
AST Parsed : 5240/5243 (99.94%)
Positive Passed: 5233/5243 (99.81%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/prettier_babel.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

prettier_babel Summary:
AST Parsed : 2099/2099 (100.00%)
Positive Passed: 1893/2099 (90.19%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/prettier_misc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 78d1d3a7

prettier_misc Summary:
AST Parsed : 16/16 (100.00%)
Positive Passed: 9/16 (56.25%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/prettier_test262.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 17ba9aea47

prettier_test262 Summary:
AST Parsed : 45836/45836 (100.00%)
Positive Passed: 42686/45836 (93.13%)
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/prettier_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 64d2eeea7b

prettier_typescript Summary:
AST Parsed : 5243/5243 (100.00%)
Positive Passed: 2434/5243 (46.42%)
Expand Down
19 changes: 10 additions & 9 deletions tasks/coverage/src/suite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::HashSet,
fs::{self, File},
fs,
io::{stdout, Read, Write},
panic::UnwindSafe,
path::{Path, PathBuf},
Expand All @@ -22,7 +22,7 @@ use oxc_diagnostics::{GraphicalReportHandler, GraphicalTheme, NamedSource};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::{SourceType, Span};
use oxc_tasks_common::normalize_path;
use oxc_tasks_common::{normalize_path, Snapshot};

use crate::{project_root, AppArgs};

Expand Down Expand Up @@ -241,8 +241,7 @@ pub trait Suite<T: Case> {

/// # Errors
fn snapshot_errors(&self, name: &str, report: &CoverageReport<T>) -> std::io::Result<()> {
let path = project_root().join(format!("tasks/coverage/{}.snap", name.to_lowercase()));
let mut file = File::create(path).unwrap();
let snapshot = Snapshot::new(self.get_test_root());

let mut tests = self
.get_test_cases()
Expand All @@ -252,18 +251,20 @@ pub trait Suite<T: Case> {

tests.sort_by_key(|case| case.path());

let mut out: Vec<u8> = vec![];

let args = AppArgs { detail: true, ..AppArgs::default() };
self.print_coverage(name, &args, report, &mut file)?;
self.print_coverage(name, &args, report, &mut out)?;

let mut out = String::new();
for case in &tests {
if let TestResult::CorrectError(error, _) = &case.test_result() {
out.push_str(error);
out.extend(error.as_bytes());
}
}

file.write_all(out.as_bytes())?;
file.flush()?;
let path = project_root().join(format!("tasks/coverage/{}.snap", name.to_lowercase()));
let out = String::from_utf8(out).unwrap();
snapshot.save(&path, &out);
Ok(())
}
}
Expand Down
2 changes: 2 additions & 0 deletions tasks/coverage/transformer_babel.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

transformer_babel Summary:
AST Parsed : 2099/2099 (100.00%)
Positive Passed: 2099/2099 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/transformer_misc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 78d1d3a7

transformer_misc Summary:
AST Parsed : 16/16 (100.00%)
Positive Passed: 16/16 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/transformer_test262.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 17ba9aea47

transformer_test262 Summary:
AST Parsed : 45836/45836 (100.00%)
Positive Passed: 45836/45836 (100.00%)
2 changes: 2 additions & 0 deletions tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 64d2eeea7b

transformer_typescript Summary:
AST Parsed : 5243/5243 (100.00%)
Positive Passed: 5243/5243 (100.00%)
2 changes: 2 additions & 0 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

Passed: 308/362

# All Passed:
Expand Down
2 changes: 2 additions & 0 deletions tasks/transform_conformance/babel_exec.snap.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

Passed: 1/3

# All Passed:
Expand Down
2 changes: 2 additions & 0 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

Passed: 2/2

# All Passed:
Expand Down
2 changes: 2 additions & 0 deletions tasks/transform_conformance/oxc_exec.snap.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: 4bd1b2c2f1

Passed: 0/0

# All Passed:
Expand Down
15 changes: 8 additions & 7 deletions tasks/transform_conformance/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use indexmap::IndexMap;
use oxc_tasks_common::{normalize_path, project_root};
use std::{
fs::{self, File},
io::Write,
fs,
path::{Path, PathBuf},
process::Command,
};

use indexmap::IndexMap;
use test_case::TestCaseKind;
use walkdir::WalkDir;

use oxc_tasks_common::{normalize_path, project_root, Snapshot};

mod test_case;

#[test]
Expand All @@ -26,6 +27,7 @@ pub struct TestRunnerOptions {
/// The test runner which walks the babel repository and searches for transformation tests.
pub struct TestRunner {
options: TestRunnerOptions,
snapshot: Snapshot,
}

fn babel_root() -> PathBuf {
Expand Down Expand Up @@ -139,7 +141,7 @@ impl SnapshotOption {

impl TestRunner {
pub fn new(options: TestRunnerOptions) -> Self {
Self { options }
Self { options, snapshot: Snapshot::new(&babel_root()) }
}

/// # Panics
Expand Down Expand Up @@ -246,8 +248,7 @@ impl TestRunner {
let snapshot = format!(
"Passed: {all_passed_count}/{total}\n\n# All Passed:\n{all_passed}\n\n\n{snapshot}"
);
let mut file = File::create(dest).unwrap();
file.write_all(snapshot.as_bytes()).unwrap();
self.snapshot.save(&dest, &snapshot);
}
}
}
Expand Down

0 comments on commit 35a356e

Please sign in to comment.