mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
backup_control: Fix internal tests
Adapt the tests to work with the changed function interfaces. Added a convenience function to construct a `clap` application that's used to test the functions from a "user"-perspective.
This commit is contained in:
parent
6f4e43e7c6
commit
6c86957a24
1 changed files with 36 additions and 60 deletions
|
@ -438,6 +438,7 @@ mod tests {
|
|||
use super::*;
|
||||
use std::env;
|
||||
// Required to instantiate mutex in shared context
|
||||
use clap::App;
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Mutex;
|
||||
|
||||
|
@ -454,16 +455,20 @@ mod tests {
|
|||
// Environment variable for "VERSION_CONTROL"
|
||||
static ENV_VERSION_CONTROL: &str = "VERSION_CONTROL";
|
||||
|
||||
fn make_app() -> clap::App<'static, 'static> {
|
||||
App::new("app")
|
||||
.arg(arguments::backup())
|
||||
.arg(arguments::backup_no_args())
|
||||
.arg(arguments::suffix())
|
||||
}
|
||||
|
||||
// Defaults to --backup=existing
|
||||
#[test]
|
||||
fn test_backup_mode_short_only() {
|
||||
let short_opt_present = true;
|
||||
let long_opt_present = false;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "-b"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::ExistingBackup);
|
||||
}
|
||||
|
@ -471,13 +476,10 @@ mod tests {
|
|||
// --backup takes precedence over -b
|
||||
#[test]
|
||||
fn test_backup_mode_long_preferred_over_short() {
|
||||
let short_opt_present = true;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = Some("none");
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "-b", "--backup=none"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::NoBackup);
|
||||
}
|
||||
|
@ -485,13 +487,10 @@ mod tests {
|
|||
// --backup can be passed without an argument
|
||||
#[test]
|
||||
fn test_backup_mode_long_without_args_no_env() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::ExistingBackup);
|
||||
}
|
||||
|
@ -499,13 +498,10 @@ mod tests {
|
|||
// --backup can be passed with an argument only
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_args() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = Some("simple");
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup=simple"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::SimpleBackup);
|
||||
}
|
||||
|
@ -513,43 +509,36 @@ mod tests {
|
|||
// --backup errors on invalid argument
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_args_invalid() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = Some("foobar");
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup=foobar"]);
|
||||
|
||||
let result = determine_backup_mode(short_opt_present, long_opt_present, long_opt_value);
|
||||
let result = determine_backup_mode(&matches);
|
||||
|
||||
assert!(result.is_err());
|
||||
let text = result.unwrap_err();
|
||||
let text = format!("{}", result.unwrap_err());
|
||||
assert!(text.contains("invalid argument ‘foobar’ for ‘backup type’"));
|
||||
}
|
||||
|
||||
// --backup errors on ambiguous argument
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_args_ambiguous() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = Some("n");
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup=n"]);
|
||||
|
||||
let result = determine_backup_mode(short_opt_present, long_opt_present, long_opt_value);
|
||||
let result = determine_backup_mode(&matches);
|
||||
|
||||
assert!(result.is_err());
|
||||
let text = result.unwrap_err();
|
||||
let text = format!("{}", result.unwrap_err());
|
||||
assert!(text.contains("ambiguous argument ‘n’ for ‘backup type’"));
|
||||
}
|
||||
|
||||
// --backup accepts shortened arguments (si for simple)
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_arg_shortened() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = Some("si");
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup=si"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::SimpleBackup);
|
||||
}
|
||||
|
@ -557,14 +546,11 @@ mod tests {
|
|||
// -b ignores the "VERSION_CONTROL" environment variable
|
||||
#[test]
|
||||
fn test_backup_mode_short_only_ignore_env() {
|
||||
let short_opt_present = true;
|
||||
let long_opt_present = false;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
env::set_var(ENV_VERSION_CONTROL, "none");
|
||||
let matches = make_app().get_matches_from(vec!["app", "-b"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::ExistingBackup);
|
||||
env::remove_var(ENV_VERSION_CONTROL);
|
||||
|
@ -573,14 +559,11 @@ mod tests {
|
|||
// --backup can be passed without an argument, but reads env var if existent
|
||||
#[test]
|
||||
fn test_backup_mode_long_without_args_with_env() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
env::set_var(ENV_VERSION_CONTROL, "none");
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::NoBackup);
|
||||
env::remove_var(ENV_VERSION_CONTROL);
|
||||
|
@ -589,16 +572,14 @@ mod tests {
|
|||
// --backup errors on invalid VERSION_CONTROL env var
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_env_var_invalid() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
env::set_var(ENV_VERSION_CONTROL, "foobar");
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup"]);
|
||||
|
||||
let result = determine_backup_mode(short_opt_present, long_opt_present, long_opt_value);
|
||||
let result = determine_backup_mode(&matches);
|
||||
|
||||
assert!(result.is_err());
|
||||
let text = result.unwrap_err();
|
||||
let text = format!("{}", result.unwrap_err());
|
||||
assert!(text.contains("invalid argument ‘foobar’ for ‘$VERSION_CONTROL’"));
|
||||
env::remove_var(ENV_VERSION_CONTROL);
|
||||
}
|
||||
|
@ -606,16 +587,14 @@ mod tests {
|
|||
// --backup errors on ambiguous VERSION_CONTROL env var
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_env_var_ambiguous() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
env::set_var(ENV_VERSION_CONTROL, "n");
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup"]);
|
||||
|
||||
let result = determine_backup_mode(short_opt_present, long_opt_present, long_opt_value);
|
||||
let result = determine_backup_mode(&matches);
|
||||
|
||||
assert!(result.is_err());
|
||||
let text = result.unwrap_err();
|
||||
let text = format!("{}", result.unwrap_err());
|
||||
assert!(text.contains("ambiguous argument ‘n’ for ‘$VERSION_CONTROL’"));
|
||||
env::remove_var(ENV_VERSION_CONTROL);
|
||||
}
|
||||
|
@ -623,14 +602,11 @@ mod tests {
|
|||
// --backup accepts shortened env vars (si for simple)
|
||||
#[test]
|
||||
fn test_backup_mode_long_with_env_var_shortened() {
|
||||
let short_opt_present = false;
|
||||
let long_opt_present = true;
|
||||
let long_opt_value = None;
|
||||
let _dummy = TEST_MUTEX.lock().unwrap();
|
||||
env::set_var(ENV_VERSION_CONTROL, "si");
|
||||
let matches = make_app().get_matches_from(vec!["app", "--backup"]);
|
||||
|
||||
let result =
|
||||
determine_backup_mode(short_opt_present, long_opt_present, long_opt_value).unwrap();
|
||||
let result = determine_backup_mode(&matches).unwrap();
|
||||
|
||||
assert_eq!(result, BackupMode::SimpleBackup);
|
||||
env::remove_var(ENV_VERSION_CONTROL);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue