Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add statistics info for jsontests #258

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions jsontests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::collections::BTreeMap;
use std::fs::{self, File};
use std::io::BufReader;

const BASIC_FILE_PATH: &str = "jsontests/res/ethtests/GeneralStateTests/";

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
Expand All @@ -20,62 +22,76 @@ struct Cli {
debug: bool,
}

fn run_file(filename: &str, debug: bool) -> Result<(), Error> {
fn run_file(filename: &str, debug: bool) -> Result<TestCompletionStatus, Error> {
let test_multi: BTreeMap<String, TestMulti> =
serde_json::from_reader(BufReader::new(File::open(filename)?))?;
let mut tests_status = TestCompletionStatus::default();

for (test_name, test_multi) in test_multi {
let tests = test_multi.tests();

for test in tests {
let short_file_name = filename.replace(BASIC_FILE_PATH, "");
for test in &tests {
if debug {
println!(
"{}/{}/{:?}/{} ===>",
filename, test_name, test.fork, test.index
print!(
"[{:?}] {} | {}/{} DEBUG: ",
test.fork, short_file_name, test_name, test.index
);
} else {
print!(
"{}/{}/{:?}/{}: ",
filename, test_name, test.fork, test.index
"[{:?}] {} | {}/{}: ",
test.fork, short_file_name, test_name, test.index
);
}
match crate::run::run_test(filename, &test_name, test, debug) {
Ok(()) => println!("okay"),
Err(Error::UnsupportedFork) => println!("skipped"),
match run::run_test(filename, &test_name, test.clone(), debug) {
Ok(()) => {
tests_status.inc_completed();
println!("ok")
}
Err(Error::UnsupportedFork) => {
tests_status.inc_skipped();
println!("skipped")
}
Err(err) => {
println!("err {:?}", err);
println!("ERROR: {:?}", err);
return Err(err);
}
}
if debug {
println!();
}
}

tests_status.print_completion();
}

Ok(())
Ok(tests_status)
}

fn run_single(filename: &str, debug: bool) -> Result<(), Error> {
fn run_single(filename: &str, debug: bool) -> Result<TestCompletionStatus, Error> {
if fs::metadata(filename)?.is_dir() {
let mut tests_status = TestCompletionStatus::default();

for filename in fs::read_dir(filename)? {
let filepath = filename?.path();
let filename = filepath.to_str().ok_or(Error::NonUtf8Filename)?;
run_file(filename, debug)?;
println!("RUM for: {filename}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println!("RUM for: {filename}");
println!("RUN for: {filename}");

tests_status += run_file(filename, debug)?;
}
tests_status.print_total_for_dir(filename);
Ok(tests_status)
} else {
run_file(filename, debug)?;
run_file(filename, debug)
}

Ok(())
}

fn main() -> Result<(), Error> {
let cli = Cli::parse();

let mut tests_status = TestCompletionStatus::default();
for filename in cli.filenames {
run_single(&filename, cli.debug)?;
tests_status += run_single(&filename, cli.debug)?;
}
tests_status.print_total();

Ok(())
}
56 changes: 56 additions & 0 deletions jsontests/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,62 @@ use serde::{
use std::collections::BTreeMap;
use std::fmt;

/// Statistic type to gather tests pass completion status
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub(crate) struct TestCompletionStatus {
pub completed: usize,
pub skipped: usize,
}

impl std::ops::AddAssign for TestCompletionStatus {
fn add_assign(&mut self, rhs: Self) {
self.completed += rhs.completed;
self.skipped += rhs.skipped;
}
}

impl TestCompletionStatus {
/// Increment `completed` statistic field
pub fn inc_completed(&mut self) {
self.completed += 1
}

/// Increment `skipped` statistic field
pub fn inc_skipped(&mut self) {
self.skipped += 1
}

/// Get total passed tests
pub fn get_total(&self) -> usize {
self.completed + self.skipped
}

/// Print completion status.
/// Most useful for single file completion statistic info
pub fn print_completion(&self) {
println!("COMPLETED: {:?} tests", self.completed);
println!("SKIPPED: {:?} tests\n", self.skipped);
}

/// Print tests pass total statistic info for directory
pub fn print_total_for_dir(&self, filename: &str) {
println!(
"TOTAL tests for: {filename}\n\tCOMPLETED: {:?}\n\tSKIPPED: {:?}",
self.completed, self.skipped
);
}

// Print total statistics info
pub fn print_total(&self) {
println!(
"\nTOTAL: {:?} tests\n\tCOMPLETED: {:?}\n\tSKIPPED: {:?}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\nTOTAL: {:?} tests\n\tCOMPLETED: {:?}\n\tSKIPPED: {:?}",
"\nTOTAL: {} tests\n\tCOMPLETED: {}\n\tSKIPPED: {}",

self.get_total(),
self.completed,
self.skipped
);
}
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
pub struct TestMulti {
#[serde(rename = "_info")]
Expand Down