Skip to content

Commit

Permalink
update test cli parser (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuunlimm authored Oct 28, 2024
1 parent 979d410 commit 39b7dbb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
1 change: 1 addition & 0 deletions aptos-indexer-processors-sdk/Cargo.lock

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

2 changes: 1 addition & 1 deletion aptos-indexer-processors-sdk/testing-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ diesel-async = { workspace = true }
diesel_migrations = { workspace = true }
futures = { workspace = true }
futures-util = { workspace = true }

lazy_static = { workspace = true }
# Postgres SSL support
native-tls = { workspace = true }
once_cell = { workspace = true }
Expand Down
106 changes: 97 additions & 9 deletions aptos-indexer-processors-sdk/testing-framework/src/cli_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,117 @@ pub fn get_test_config() -> (bool, Option<String>) {
pub fn parse_test_args() -> TestArgs {
let raw_args: Vec<String> = std::env::args().collect();

// Find the "--" separator (if it exists)
// Find the "--" separator, or default to include all args after the test name
let clap_args_position = raw_args.iter().position(|arg| arg == "--");

// Only pass the arguments that come after "--", if it exists
let custom_args: Vec<String> = match clap_args_position {
Some(position) => raw_args[position + 1..].to_vec(), // Slice after `--`
None => Vec::new(), // If no `--` is found, treat as no custom args
// Determine the starting position for custom arguments
let custom_args_start = match clap_args_position {
Some(position) => position + 1, // Start after the "--" if it exists
None => 1, // Start after the test name, skip the first element
};

// Manually parse the "--generate-output" flag
let generate_output_flag = custom_args.contains(&"--generate-output".to_string());
// Collect custom arguments based on determined start position
let custom_args: Vec<String> = raw_args[custom_args_start..].to_vec();

// Manually parse the "generate-output" flag
let generate_output_flag = custom_args.contains(&"generate-output".to_string());

// Manually parse the "--output-path" flag and get its associated value
let output_path = custom_args
.windows(2)
.find(|args| args[0] == "--output-path")
.find(|args| args[0] == "output-path")
.map(|args| args[1].clone());

println!("Parsed generate_output_flag: {}", generate_output_flag);
println!("Parsed output_path: {:?}", output_path);
println!(
"Parsed output_path: {}",
output_path.clone().unwrap_or_else(|| "None".to_string())
);

TestArgs {
generate_output: generate_output_flag,
output_path,
}
}

#[cfg(test)]
mod tests {
use super::*;
pub fn parse_test_args_from_vec(args: Vec<String>) -> TestArgs {
// Find the "--" separator (if it exists)
let clap_args_position = args.iter().position(|arg| arg == "--");

// Only pass the arguments that come after "--", if it exists
let custom_args: Vec<String> = match clap_args_position {
Some(position) => args[position + 1..].to_vec(), // Slice after `--`
None => Vec::new(), // If no `--` is found, treat as no custom args
};

// Manually parse the "--generate-output" flag
let generate_output_flag = custom_args.contains(&"generate-output".to_string());

// Manually parse the "--output-path" flag and get its associated value
let output_path = custom_args
.windows(2)
.find(|args| args[0] == "output-path")
.map(|args| args[1].clone());

println!("Parsed generate_output_flag: {}", generate_output_flag);
println!(
"Parsed output_path: {}",
output_path.clone().unwrap_or_else(|| "None".to_string())
);

TestArgs {
generate_output: generate_output_flag,
output_path,
}
}

#[test]
fn test_parse_generate_output_flag() {
let args = vec![
"test_binary".to_string(),
"--".to_string(),
"generate-output".to_string(),
];
let parsed = parse_test_args_from_vec(args);
assert!(parsed.generate_output);
assert_eq!(parsed.output_path, None);
}

#[test]
fn test_parse_output_path() {
let args = vec![
"test_binary".to_string(),
"--".to_string(),
"output-path".to_string(),
"/some/path".to_string(),
];
let parsed = parse_test_args_from_vec(args);
assert!(!parsed.generate_output);
assert_eq!(parsed.output_path, Some("/some/path".to_string()));
}

#[test]
fn test_parse_both_arguments() {
let args = vec![
"test_binary".to_string(),
"--".to_string(),
"generate-output".to_string(),
"output-path".to_string(),
"/some/other/path".to_string(),
];
let parsed = parse_test_args_from_vec(args);
assert!(parsed.generate_output);
assert_eq!(parsed.output_path, Some("/some/other/path".to_string()));
}

#[test]
fn test_parse_no_arguments() {
let args = vec!["test_binary".to_string()];
let parsed = parse_test_args_from_vec(args);
assert!(!parsed.generate_output);
assert_eq!(parsed.output_path, None);
}
}

0 comments on commit 39b7dbb

Please sign in to comment.