use std::io;
use std::fs::File;
use clap::{ArgMatches, SubCommand, Arg, App};
use colored::*;
use serial_unit_testing::serial::Serial;
use serial_unit_testing::parser;
use serial_unit_testing::tests::TestCaseSettings;
use crate::commands;
pub fn run(matches: &ArgMatches) -> Result<(), String> {
let filename = matches.value_of("file").unwrap();
let mut file = match File::open(filename) {
Ok(file) => file,
Err(ref e) if e.kind() == io::ErrorKind::NotFound => return Err("File not found".to_string()),
Err(e) => return Err(format!("{}", e))
};
let (settings, port_name) = commands::get_serial_settings(matches).unwrap();
let mut serial = Serial::open_with_settings(port_name, &settings)?;
let mut default_test_settings = TestCaseSettings::default();
default_test_settings.verbose = Some(matches.is_present("verbose"));
let test_suites = match parser::parse_file_with_default_settings(&mut file, default_test_settings) {
Ok(test_suites) => test_suites,
Err(e) => return Err(format!("Unable to parse file: {}", e))
};
let mut total_tests = 0;
let mut successful_tests = 0;
let mut failed_tests = 0;
let stop_on_failure = matches.is_present("stop");
for mut test_suite in test_suites {
if stop_on_failure {
test_suite.settings.stop_on_failure = stop_on_failure;
}
let result = test_suite.run_and_print(&mut serial);
let successful = test_suite.successful();
let failed = test_suite.failed();
total_tests += successful + failed;
successful_tests += successful;
failed_tests += failed;
println!();
if result == false && test_suite.settings.stop_on_failure {
if stop_on_failure {
println!("Stopping because 'stop-on-failure' is set");
break;
} else {
println!("Stopping group because 'stop-on-failure' is set\n");
continue;
}
}
}
println!("\nRan {} tests, {} successful, {} failed", total_tests.to_string().yellow(), successful_tests.to_string().green(), failed_tests.to_string().red());
Ok(())
}
pub fn command<'a>() -> App<'a, 'a> {
SubCommand::with_name("run")
.about("Run script on serial port")
.arg(Arg::with_name("file")
.help("Script to run on the serial port")
.required(true)
.takes_value(true))
.args(commands::serial_arguments(true, false).as_slice())
.arg(Arg::with_name("stop")
.long("stop-on-failure")
.short("S")
.help("Stop on first test failing"))
.arg(Arg::with_name("verbose")
.long("verbose")
.short("v")
.help("Show verbose output"))
}