From 6b469cc255c1b6de1640c03d56415b1b12a0565f Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Thu, 11 Apr 2019 15:26:24 +0200 Subject: [PATCH] Adding tests for cli --- dqcsim-cli/src/arg_parse/parse.rs | 8 --- dqcsim-cli/src/arg_parse/plugins.rs | 105 ++++++++++++++++++++++++++++ dqcsim-cli/src/main.rs | 14 +++- 3 files changed, 116 insertions(+), 11 deletions(-) diff --git a/dqcsim-cli/src/arg_parse/parse.rs b/dqcsim-cli/src/arg_parse/parse.rs index 0352830b7..5274ba164 100644 --- a/dqcsim-cli/src/arg_parse/parse.rs +++ b/dqcsim-cli/src/arg_parse/parse.rs @@ -230,14 +230,6 @@ pub struct CommandLineConfiguration { } impl CommandLineConfiguration { - /// Produces a DQCsim configuration from `std::env::args()`. - /// - /// This is just a shorthand for `parse_from(std::env::args())`, refer - /// to its docs for more info. - pub fn parse() -> Result { - CommandLineConfiguration::parse_from(std::env::args()) - } - /// Produces a DQCsim configuration from the specified command line /// argument iterable. /// diff --git a/dqcsim-cli/src/arg_parse/plugins.rs b/dqcsim-cli/src/arg_parse/plugins.rs index 8694fcf49..1f2773cde 100644 --- a/dqcsim-cli/src/arg_parse/plugins.rs +++ b/dqcsim-cli/src/arg_parse/plugins.rs @@ -110,3 +110,108 @@ impl PluginDefinition { } } } + +#[cfg(test)] +mod tests { + use super::*; + use dqcsim::{ + common::types::PluginType, host::configuration::PluginProcessNonfunctionalConfiguration, + }; + + #[test] + fn non_func_opt() { + let pnfo = PluginNonfunctionalOpts::default(); + assert_eq!( + pnfo, + PluginNonfunctionalOpts { + verbosity: None, + tee_files: vec![], + stdout_mode: None, + stderr_mode: None, + accept_timeout: None, + shutdown_timeout: None, + }, + ); + } + + #[test] + fn non_func_opt_debug() { + let pnfo = PluginNonfunctionalOpts::default(); + assert_eq!( + format!("{:?}", pnfo), + "PluginNonfunctionalOpts { verbosity: None, tee_files: [], stdout_mode: None, stderr_mode: None, accept_timeout: None, shutdown_timeout: None }", + ); + } + + #[test] + fn into_process_conf() { + let p = PluginNonfunctionalOpts::default(); + let c: PluginProcessNonfunctionalConfiguration = p.into_config(LoglevelFilter::Debug); + assert_eq!( + c, + PluginProcessNonfunctionalConfiguration { + verbosity: LoglevelFilter::Debug, + tee_files: vec![], + stdout_mode: StreamCaptureMode::Capture(Loglevel::Info), + stderr_mode: StreamCaptureMode::Capture(Loglevel::Info), + accept_timeout: Timeout::from_seconds(5), + shutdown_timeout: Timeout::from_seconds(5), + } + ); + + let p = PluginNonfunctionalOpts { + verbosity: Some(LoglevelFilter::Fatal), + tee_files: vec![TeeFileConfiguration::new( + LoglevelFilter::Error, + "/dev/null", + )], + stdout_mode: Some(StreamCaptureMode::Null), + stderr_mode: Some(StreamCaptureMode::Pass), + accept_timeout: Some(Timeout::Infinite), + shutdown_timeout: Some(Timeout::from_seconds(1)), + }; + let c: PluginProcessNonfunctionalConfiguration = p.into_config(LoglevelFilter::Debug); + assert_eq!( + c, + PluginProcessNonfunctionalConfiguration { + verbosity: LoglevelFilter::Fatal, + tee_files: vec![TeeFileConfiguration::new( + LoglevelFilter::Error, + "/dev/null", + )], + stdout_mode: StreamCaptureMode::Null, + stderr_mode: StreamCaptureMode::Pass, + accept_timeout: Timeout::Infinite, + shutdown_timeout: Timeout::from_seconds(1), + } + ); + } + + #[test] + fn into_def_conf() { + let p = PluginDefinition { + name: "name".to_string(), + specification: PluginProcessSpecification::from_sugar( + "/bin/echo", + PluginType::Operator, + ) + .unwrap(), + functional: PluginProcessFunctionalConfiguration::default(), + nonfunctional: PluginNonfunctionalOpts::default(), + }; + let c: PluginProcessConfiguration = p.into_config(LoglevelFilter::Trace); + assert_eq!( + c, + PluginProcessConfiguration { + name: "name".to_string(), + specification: PluginProcessSpecification::from_sugar( + "/bin/echo", + PluginType::Operator + ) + .unwrap(), + functional: PluginProcessFunctionalConfiguration::default(), + nonfunctional: PluginProcessNonfunctionalConfiguration::default(), + } + ); + } +} diff --git a/dqcsim-cli/src/main.rs b/dqcsim-cli/src/main.rs index ebabbf403..6efe91abc 100755 --- a/dqcsim-cli/src/main.rs +++ b/dqcsim-cli/src/main.rs @@ -3,6 +3,7 @@ use dqcsim::{ host::{accelerator::Accelerator, reproduction::HostCall, simulator::Simulator}, info, note, }; +use std::ffi::OsString; use failure::Error; @@ -58,8 +59,12 @@ fn run( Ok(()) } -fn internal_main() -> Result<(), Error> { - let mut cfg = CommandLineConfiguration::parse().or_else(|e| { +fn internal_main(args: I) -> Result<(), Error> +where + I: IntoIterator, + T: Into + Clone, +{ + let mut cfg = CommandLineConfiguration::parse_from(args).or_else(|e| { println!("{}", e); Err(e) })?; @@ -87,9 +92,12 @@ fn internal_main() -> Result<(), Error> { } fn main() { - let result = internal_main(); + let result = internal_main(std::env::args()); std::process::exit(match result { Ok(_) => 0, Err(_) => 1, }); } + +#[cfg(test)] +mod tests {}