Skip to content

Commit

Permalink
Fixed multiple includes on try
Browse files Browse the repository at this point in the history
- Cleaned up the start-at to only parse once
- Fixed the multiple --include. Clap no longer allows multiple occurences, it only allows multiple passed on one occurence. See clap-rs/clap#2688 and https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#400---2022-09-28
- This does introduce a bug that if you specify the config file immediately after --include(s) it will think it's part of the --include. The user must either pass another option after -i or put the config file before the -i
  • Loading branch information
tkmcmaster committed Jan 27, 2023
1 parent ac6a3f0 commit e2311e6
Showing 1 changed file with 94 additions and 9 deletions.
103 changes: 94 additions & 9 deletions src/bin/pewpew.rs
@@ -1,5 +1,5 @@
use std::{
convert::TryInto, ffi::OsString, fs::create_dir_all, io, path::PathBuf, time::UNIX_EPOCH,
convert::TryInto, ffi::OsString, fs::create_dir_all, io, path::PathBuf, time::{UNIX_EPOCH, Duration},
};

use clap::{builder::ValueParser, crate_version, Arg, ArgMatches, Command};
Expand All @@ -18,11 +18,8 @@ fn get_filter_reg() -> Regex {
Regex::new("^(.*?)(!=|=)(.*)").expect("is a valid regex")
}

fn parse_duration(arg: &str) -> Result<String, config::Error> {
match duration_from_string(arg.into()) {
Ok(_) => Ok(arg.to_string()),
Err(error) => Err(error),
}
fn parse_duration(arg: &str) -> Result<Duration, config::Error> {
duration_from_string(arg.into())
}

fn parse_filter(arg: &str) -> Result<String, &'static str> {
Expand Down Expand Up @@ -136,7 +133,12 @@ fn get_arg_matcher() -> clap::Command {
.long("include")
.long_help(r#"Filter which endpoints are included in the try run. Filters work based on an endpoint's tags. Filters are specified in the format "key=value" where "*" is a wildcard. Any endpoint matching the filter is included in the test"#)
// .multiple_occurrences(true)
.num_args(0..)
// https://github.com/clap-rs/clap/issues/2688
// https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#400---2022-09-28
// There is no option for multiple '-i x -i y -i z' anymore. The only option is '-i x y z'
// Unfortunately this means you can no longer do 'pewpew try -i x y z test.yaml'
// since it will try to parse test.yaml as an include. -i must either be last, or be followed by another - parameter
.num_args(1..)
.value_parser(parse_filter)
.value_name("INCLUDE")
)
Expand Down Expand Up @@ -206,8 +208,8 @@ fn get_cli_config(matches: ArgMatches) -> ExecConfig {
let stats_file_format = StatsFileFormat::Json;
let watch_config_file = matches.get_flag("watch");
let start_at = matches
.get_one::<String>("start-at")
.map(|s| duration_from_string(s.to_string()).expect("start_at should match pattern"));
.get_one::<Duration>("start-at")
.map(|d| d.to_owned());
let run_config = RunConfig {
config_file,
output_format,
Expand Down Expand Up @@ -355,6 +357,7 @@ mod tests {
static RUN_COMMAND: &str = "run";
static TRY_COMMAND: &str = "try";
static YAML_FILE: &str = "./tests/integration.yaml";
static YAML_FILE2: &str = "./tests/int_on_demand.yaml";
static TEST_DIR: &str = "./tests/";
static STATS_FILE: &str = "stats-paths.json";

Expand Down Expand Up @@ -647,6 +650,88 @@ mod tests {
}
}

#[test]
fn cli_try_include() {
let matches = get_arg_matcher()
.try_get_matches_from([
"myprog",
TRY_COMMAND,
"-i",
"_id=0",
"_id=1",
"-f",
"json",
YAML_FILE2,
])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), TRY_COMMAND);

let cli_config: ExecConfig = get_cli_config(matches);
match cli_config {
ExecConfig::Try(try_config) => {
assert_eq!(try_config.config_file.to_str().unwrap(), YAML_FILE2);
assert!(try_config.filters.is_some());
let filters = try_config.filters.unwrap();
assert_eq!(filters.len(), 2);
match &filters[0] {
TryFilter::Eq(key, value) => {
assert_eq!(key, "_id");
assert_eq!(value, "0");
}
_ => panic!(),
}
match &filters[1] {
TryFilter::Eq(key, value) => {
assert_eq!(key, "_id");
assert_eq!(value, "1");
}
_ => panic!(),
}
}
_ => panic!(),
}
}

#[test]
fn cli_try_include2() {
let matches = get_arg_matcher()
.try_get_matches_from([
"myprog",
TRY_COMMAND,
YAML_FILE2,
"-i",
"_id=0",
"_id=1",
])
.unwrap();
assert_eq!(matches.subcommand_name().unwrap(), TRY_COMMAND);

let cli_config: ExecConfig = get_cli_config(matches);
match cli_config {
ExecConfig::Try(try_config) => {
assert_eq!(try_config.config_file.to_str().unwrap(), YAML_FILE2);
assert!(try_config.filters.is_some());
let filters = try_config.filters.unwrap();
assert_eq!(filters.len(), 2);
match &filters[0] {
TryFilter::Eq(key, value) => {
assert_eq!(key, "_id");
assert_eq!(value, "0");
}
_ => panic!(),
}
match &filters[1] {
TryFilter::Eq(key, value) => {
assert_eq!(key, "_id");
assert_eq!(value, "1");
}
_ => panic!(),
}
}
_ => panic!(),
}
}

#[test]
fn cli_try_format_json() {
let matches = get_arg_matcher()
Expand Down

0 comments on commit e2311e6

Please sign in to comment.