From 9a964be58baeb9510183181736533f511eb239c5 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 8 Mar 2019 10:50:12 +1100 Subject: [PATCH] Update test_harness clap args structure Prepares it for adding a new subcommand --- .../beacon_chain/test_harness/src/bin.rs | 55 ++++++------------- .../beacon_chain/test_harness/src/run_test.rs | 36 ++++++++++++ 2 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 beacon_node/beacon_chain/test_harness/src/run_test.rs diff --git a/beacon_node/beacon_chain/test_harness/src/bin.rs b/beacon_node/beacon_chain/test_harness/src/bin.rs index 283cb0dfa..3769788d8 100644 --- a/beacon_node/beacon_chain/test_harness/src/bin.rs +++ b/beacon_node/beacon_chain/test_harness/src/bin.rs @@ -1,10 +1,9 @@ -use clap::{App, Arg}; +use clap::{App, Arg, SubCommand}; use env_logger::{Builder, Env}; -use std::{fs::File, io::prelude::*}; -use test_case::TestCase; -use yaml_rust::YamlLoader; +use run_test::run_test; mod beacon_chain_harness; +mod run_test; mod test_case; mod validator_harness; @@ -15,13 +14,6 @@ fn main() { .version("0.0.1") .author("Sigma Prime ") .about("Runs `test_harness` using a YAML test_case.") - .arg( - Arg::with_name("yaml") - .long("yaml") - .value_name("FILE") - .help("YAML file test_case.") - .required(true), - ) .arg( Arg::with_name("log") .long("log-level") @@ -31,39 +23,24 @@ fn main() { .default_value("debug") .required(true), ) + .subcommand( + SubCommand::with_name("run_test") + .about("Executes a YAML test specification") + .arg( + Arg::with_name("yaml") + .long("yaml") + .value_name("FILE") + .help("YAML file test_case.") + .required(true), + ), + ) .get_matches(); if let Some(log_level) = matches.value_of("log") { Builder::from_env(Env::default().default_filter_or(log_level)).init(); } - if let Some(yaml_file) = matches.value_of("yaml") { - let docs = { - let mut file = File::open(yaml_file).unwrap(); - - let mut yaml_str = String::new(); - file.read_to_string(&mut yaml_str).unwrap(); - - YamlLoader::load_from_str(&yaml_str).unwrap() - }; - - for doc in &docs { - // For each `test_cases` YAML in the document, build a `TestCase`, execute it and - // assert that the execution result matches the test_case description. - // - // In effect, for each `test_case` a new `BeaconChainHarness` is created from genesis - // and a new `BeaconChain` is built as per the test_case. - // - // After the `BeaconChain` has been built out as per the test_case, a dump of all blocks - // and states in the chain is obtained and checked against the `results` specified in - // the `test_case`. - // - // If any of the expectations in the results are not met, the process - // panics with a message. - for test_case in doc["test_cases"].as_vec().unwrap() { - let test_case = TestCase::from_yaml(test_case); - test_case.assert_result_valid(test_case.execute()) - } - } + if let Some(matches) = matches.subcommand_matches("run_test") { + run_test(matches); } } diff --git a/beacon_node/beacon_chain/test_harness/src/run_test.rs b/beacon_node/beacon_chain/test_harness/src/run_test.rs new file mode 100644 index 000000000..1a816afe0 --- /dev/null +++ b/beacon_node/beacon_chain/test_harness/src/run_test.rs @@ -0,0 +1,36 @@ +use crate::test_case::TestCase; +use clap::ArgMatches; +use std::{fs::File, io::prelude::*}; +use yaml_rust::YamlLoader; + +pub fn run_test(matches: &ArgMatches) { + if let Some(yaml_file) = matches.value_of("yaml") { + let docs = { + let mut file = File::open(yaml_file).unwrap(); + + let mut yaml_str = String::new(); + file.read_to_string(&mut yaml_str).unwrap(); + + YamlLoader::load_from_str(&yaml_str).unwrap() + }; + + for doc in &docs { + // For each `test_cases` YAML in the document, build a `TestCase`, execute it and + // assert that the execution result matches the test_case description. + // + // In effect, for each `test_case` a new `BeaconChainHarness` is created from genesis + // and a new `BeaconChain` is built as per the test_case. + // + // After the `BeaconChain` has been built out as per the test_case, a dump of all blocks + // and states in the chain is obtained and checked against the `results` specified in + // the `test_case`. + // + // If any of the expectations in the results are not met, the process + // panics with a message. + for test_case in doc["test_cases"].as_vec().unwrap() { + let test_case = TestCase::from_yaml(test_case); + test_case.assert_result_valid(test_case.execute()) + } + } + } +}