Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Adding tests for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Apr 11, 2019
1 parent 28ad5ef commit 6b469cc
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 11 deletions.
8 changes: 0 additions & 8 deletions dqcsim-cli/src/arg_parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Error> {
CommandLineConfiguration::parse_from(std::env::args())
}

/// Produces a DQCsim configuration from the specified command line
/// argument iterable.
///
Expand Down
105 changes: 105 additions & 0 deletions dqcsim-cli/src/arg_parse/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
);
}
}
14 changes: 11 additions & 3 deletions dqcsim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use dqcsim::{
host::{accelerator::Accelerator, reproduction::HostCall, simulator::Simulator},
info, note,
};
use std::ffi::OsString;

use failure::Error;

Expand Down Expand Up @@ -58,8 +59,12 @@ fn run(
Ok(())
}

fn internal_main() -> Result<(), Error> {
let mut cfg = CommandLineConfiguration::parse().or_else(|e| {
fn internal_main<I, T>(args: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let mut cfg = CommandLineConfiguration::parse_from(args).or_else(|e| {
println!("{}", e);
Err(e)
})?;
Expand Down Expand Up @@ -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 {}

0 comments on commit 6b469cc

Please sign in to comment.