tests: prepare common test infrastructure for CLI args

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-06-11 18:19:32 +02:00
committed by Rob Bradford
parent 12493db144
commit 1f13165fae
3 changed files with 69 additions and 11 deletions

View File

@@ -3,6 +3,10 @@
// SPDX-License-Identifier: Apache-2.0
//
#[cfg(test)]
#[path = "../test_util.rs"]
mod test_util;
use std::io::Read;
use std::marker::PhantomData;
use std::os::unix::net::UnixStream;
@@ -1132,3 +1136,39 @@ fn main() {
process::exit(1)
};
}
#[cfg(test)]
mod tests {
use std::cmp::Ordering;
use super::*;
use crate::test_util::tests::assert_args_sorted;
#[test]
fn test_cli_args_sorted() {
let args = get_cli_args();
assert_args_sorted(|| args.iter());
}
#[test]
fn test_cli_commands_sorted() {
let commands = get_cli_commands_sorted();
// check commands itself are sorted
let iter = commands.iter().zip(commands.iter().skip(1));
for (command, next) in iter {
assert_ne!(
command.get_name().cmp(next.get_name()),
Ordering::Greater,
"commands not alphabetically sorted: command={}, next={}",
command.get_name(),
next.get_name()
);
}
// check args of commands sorted
for command in commands {
assert_args_sorted(|| command.get_arguments());
}
}
}

View File

@@ -3,6 +3,9 @@
// SPDX-License-Identifier: Apache-2.0
//
#[cfg(test)]
mod test_util;
use std::fs::File;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::sync::mpsc::channel;
@@ -894,7 +897,6 @@ fn main() {
#[cfg(test)]
mod unit_tests {
use std::cmp::Ordering;
use std::path::PathBuf;
use vmm::config::VmParams;
@@ -905,6 +907,7 @@ mod unit_tests {
PayloadConfig, RngConfig, VmConfig,
};
use crate::test_util::tests::assert_args_sorted;
use crate::{create_app, get_cli_options_sorted, prepare_default_values};
fn get_vm_config_from_vec(args: &[&str]) -> VmConfig {
@@ -2014,15 +2017,6 @@ mod unit_tests {
let (default_vcpus, default_memory, default_rng) = prepare_default_values();
let args = get_cli_options_sorted(default_vcpus, default_memory, default_rng);
let iter = args.iter().zip(args.iter().skip(1));
for (elem, next) in iter {
assert_ne!(
elem.get_id().cmp(next.get_id()),
Ordering::Greater,
"items not alphabetically sorted: elem={}, next={}",
elem.get_id(),
next.get_id()
);
}
assert_args_sorted(|| args.iter())
}
}

24
src/test_util.rs Normal file
View File

@@ -0,0 +1,24 @@
// Copyright © 2025 Cyberus Technology GmbH
//
// SPDX-License-Identifier: Apache-2.0
//
#[cfg(test)]
pub mod tests {
use std::cmp::Ordering;
use clap::Arg;
pub fn assert_args_sorted<'a, F: Fn() -> R, R: Iterator<Item = &'a Arg>>(get_base_iter: F) {
let iter = get_base_iter().zip(get_base_iter().skip(1));
for (arg, next) in iter {
assert_ne!(
arg.get_id().cmp(next.get_id()),
Ordering::Greater,
"args not alphabetically sorted: arg={}, next={}",
arg.get_id(),
next.get_id()
);
}
}
}