Skip to content

Commit

Permalink
Add rstest crate; parameterize tests for sample-freq option
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarler committed Mar 23, 2024
1 parent 71afe9f commit 233b11f
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 42 deletions.
149 changes: 149 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ primal = "0.3.2"
[dev-dependencies]
assert_cmd = { version = "2.0.14" }
expect-test = { version = "1.4.1" }
rstest = "0.18.2"

[build-dependencies]
bindgen = "0.69.4"
Expand Down
75 changes: 33 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ fn main() -> Result<(), Box<dyn Error>> {
#[cfg(test)]
mod tests {
use super::Cli;
use super::*;
use assert_cmd::Command;
use clap::Parser;
use expect_test::expect;
use rstest::rstest;

#[test]
fn verify_cli() {
Expand All @@ -93,60 +94,50 @@ mod tests {

#[test]
fn cli_help() {
let mut cmd = Command::cargo_bin("claptests").unwrap();
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

cmd.arg("--help");
let expected = expect![[r#"
"Usage: claptests [OPTIONS]\n\nOptions:\n --pids <PIDS>\n Specific PIDs to profile\n --tid <TID>\n Specific TIDs to profile (these can be outside the PIDs selected above)\n --show-unwind-info <SHOW_UNWIND_INFO>\n \n --show-info <SHOW_INFO>\n \n -D, --duration <DURATION>\n How long this agent will run in seconds [default: 18446744073709551615]\n --filter-logs\n \n --sample-freq <SAMPLE_FREQ>\n Per-CPU Sampling Frequency in Hz [default: 19]\n -o, --output-file <OUTPUT_FILE>\n Output file for Flame Graph in SVG format [default: flame.svg]\n -h, --help\n Print help\n"
"Usage: lightswitch [OPTIONS]\n\nOptions:\n --pids <PIDS>\n Specific PIDs to profile\n --tids <TIDS>\n Specific TIDs to profile (these can be outside the PIDs selected above)\n --show-unwind-info <PATH_TO_BINARY>\n Show unwind info for given binary\n --show-info <PATH_TO_BINARY>\n Show build ID for given binary\n -D, --duration <DURATION>\n How long this agent will run in seconds [default: 18446744073709551615]\n --filter-logs\n Enable TRACE (max) level logging - defaults to INFO level otherwise\n --sample-freq <SAMPLE_FREQ_IN_HZ>\n Per-CPU Sampling Frequency in Hz [default: 19]\n --flamegraph-file <FLAMEGRAPH_FILE>\n Output file for Flame Graph in SVG format [default: flame.svg]\n -h, --help\n Print help\n"
"#]];
cmd.assert().success();
let actual = String::from_utf8(cmd.unwrap().stdout).unwrap();
expected.assert_debug_eq(&actual);
// cmd.assert().stdout(predicate::str::contains("junk"));
}

#[test]
fn sample_freq_test() {
let _expected = expect![""];
#[rstest]
// The case tuples are: (string frequency to try, error string - if expected )
// Test a frequency below the range, which is also prime
// Interestingly, this one panics, and thinks you sent -1, not -101
#[should_panic]
#[case::neg101("-101", "sample frequency not in allowed range")]
#[case::prime_19("19", "")]
#[case::non_prime_20("20", "sample frequency is not prime")]
#[case::prime_47("47", "")]
#[case::non_prime_49("49", "sample frequency is not prime")]
#[case::prime_101("101", "")]
#[case::prime_1009("1009", "")]
#[case::non_prime_out_of_range1010("1010", "sample frequency not in allowed range")]
#[case::prime_out_of_range_1013("1013", "sample frequency not in allowed range")]
fn sample_freq_successes(#[case] desired_freq: String, #[case] expected_msg: String) {
let execname = env!("CARGO_PKG_NAME");
let argname = "--sample-freq";
let myargs = vec!["claptests", "--sample-freq", "19"];
let baseargs = vec![execname, argname];

let result = Cli::try_parse_from(myargs);
let args = match result {
Ok(args) => args,
Err(error) => {
println!("Error: {:?}", error);
return;
let mut myargs = baseargs.clone();
myargs.push(desired_freq.as_str());
let result = Cli::try_parse_from(myargs.iter());
match result {
Ok(config) => {
// println!("{:?}", config);
assert_eq!(config.sample_freq, desired_freq.parse::<u16>().unwrap());
}
Err(err) => {
let actual_message = err.to_string();
// println!("Errored with: {}", actual_message);
assert!(actual_message.contains(expected_msg.as_str()));
}
};
// let args = result.unwrap();
// assert_eq!(val.sample_freq, 19);
// assert!(result.is_ok());

// Test a frequency below the range, which is also prime
if Cli::try_parse_from(vec![argname, "-101"].iter()).is_ok() {
panic!("sample frequency below range should fail");
}
if Cli::try_parse_from([argname, "19"].iter()).is_err() {
panic!("sample frequency in range and prime should succeed");
}
if Cli::try_parse_from(vec![argname, "20"].iter()).is_ok() {
panic!("sample frequency in range but not prime should fail");
}
if Cli::try_parse_from(vec![argname, "49"].iter()).is_ok() {
panic!("sample frequency in range but not prime should fail");
}
if Cli::try_parse_from(vec![argname, "101"].iter()).is_err() {
panic!("sample frequency in range and prime should succeed");
}
if Cli::try_parse_from(vec![argname, "1009"].iter()).is_err() {
panic!("sample frequency in range and prime should succeed");
}
if Cli::try_parse_from(vec![argname, "1010"].iter()).is_ok() {
panic!("sample frequency out of range and not prime should fail");
}
if Cli::try_parse_from(vec![argname, "1013"].iter()).is_ok() {
panic!("sample frequency out of range and prime should fail");
}
// Expected output/results for various inputs
}
Expand Down

0 comments on commit 233b11f

Please sign in to comment.