use super::{
ArgsErr, CheckClippyTargets, Cmd, Ignored, MetaCmd, NonZeroUsizePlus1, Opts, OsString, PathBuf,
Target, Targets, TestTargets,
};
use core::iter;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt as _;
#[expect(
clippy::cognitive_complexity,
clippy::too_many_lines,
reason = "want to test for a lot of things"
)]
#[test]
fn arg_parsing() {
assert_eq!(MetaCmd::from_args(iter::empty()), Err(ArgsErr::NoArgs));
assert_eq!(
MetaCmd::from_args(iter::once(OsString::new())),
Err(ArgsErr::NoCommand)
);
assert_eq!(
MetaCmd::from_args([OsString::new(), OsString::new()].into_iter()),
Err(ArgsErr::NoCommand)
);
#[cfg(unix)]
assert_eq!(
MetaCmd::from_args([OsString::new(), OsString::from_vec(vec![255])].into_iter()),
Err(ArgsErr::UnknownArg(OsString::from_vec(vec![255])))
);
assert_eq!(
MetaCmd::from_args([OsString::new(), " clippy".to_owned().into()].into_iter()),
Err(ArgsErr::NoCommand)
);
assert_eq!(
MetaCmd::from_args([OsString::new(), "Clippy".to_owned().into()].into_iter()),
Err(ArgsErr::NoCommand)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"--summary".to_owned().into(),
"clippy".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::NoCommand)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"help".to_owned().into(),
"--summary".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::HelpWithArgs)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"version".to_owned().into(),
"foo".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::VersionWithArgs)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--cargo-path".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::MissingCargoPath)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--cargo-home".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::MissingCargoHome)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--rustup-home".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::MissingRustupHome)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--deny-warnings".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--deny-warnings".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--deny-warnings".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--deny-warnings".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--ignored".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--ignored".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignored".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--ignored".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--include-ignored".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--include-ignored".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--include-ignored".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--include-ignored".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--ignored".to_owned().into(),
"--include-ignored".to_owned().into()
]
.into_iter()
),
Err(ArgsErr::IgnoredIncludeIgnored)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--ignore-features".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::MissingIgnoredFeatures)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--ignore-features".to_owned().into(),
",".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateIgnoredFeatures(",".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,,a".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateIgnoredFeatures("a,,a".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
",a,b,".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateIgnoredFeatures(",a,b,".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--ignore-features".to_owned().into(),
",a,,b".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateIgnoredFeatures(",a,,b".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,b,,".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateIgnoredFeatures("a,b,,".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--lib".to_owned().into(),
"--all-targets".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::AllTargets)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--lib".to_owned().into(),
"--all-targets".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::AllTargets)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--all-targets".to_owned().into(),
"test".to_owned().into(),
"--doc".to_owned().into(),
"--all-targets".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::AllTargets)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--all-targets".to_owned().into(),
"--doc".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::Doc)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--doc".to_owned().into(),
"--lib".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::Doc)
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--doc".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::UnknownArg("--doc".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--color".to_owned().into(),
"clippy".to_owned().into(),
"--color".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateOption("--color".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--ignored".to_owned().into(),
"--ignored".to_owned().into(),
]
.into_iter()
),
Err(ArgsErr::DuplicateOption("--ignored".to_owned().into()))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--all-targets".to_owned().into(),
"--allow-implied-features".to_owned().into(),
"--cargo-home".to_owned().into(),
"--ignored".to_owned().into(),
"--cargo-path".to_owned().into(),
"cargo".to_owned().into(),
"--color".to_owned().into(),
"--default-toolchain".to_owned().into(),
"--deny-warnings".to_owned().into(),
"--dir".to_owned().into(),
OsString::new(),
"--ignore-compile-errors".to_owned().into(),
"--ignore-features".to_owned().into(),
",a".to_owned().into(),
"--ignore-msrv".to_owned().into(),
"--rustup-home".to_owned().into(),
"a".to_owned().into(),
"--progress".to_owned().into(),
"--skip-msrv".to_owned().into(),
"--summary".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Clippy(CheckClippyTargets::All, true,),
Opts {
exec_dir: Some(PathBuf::new()),
rustup_home: Some("a".to_owned().into()),
cargo_home: Some("--ignored".to_owned().into()),
cargo_path: "cargo/cargo".to_owned().into(),
color: true,
default_toolchain: true,
allow_implied_features: true,
ignore_compile_errors: true,
ignore_msrv: true,
progress: true,
skip_msrv: true,
summary: true,
ignore_features: vec![String::new(), "a".to_owned()],
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--all-targets".to_owned().into(),
"--deny-warnings".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Clippy(CheckClippyTargets::All, true,),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: Vec::new(),
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--allow-implied-features".to_owned().into(),
"--cargo-home".to_owned().into(),
"--ignored".to_owned().into(),
"--cargo-path".to_owned().into(),
"cargo".to_owned().into(),
"--color".to_owned().into(),
"--default-toolchain".to_owned().into(),
"--dir".to_owned().into(),
OsString::new(),
"--ignore-compile-errors".to_owned().into(),
"--ignore-features".to_owned().into(),
OsString::new(),
"--ignore-msrv".to_owned().into(),
"--ignored".to_owned().into(),
"--rustup-home".to_owned().into(),
OsString::new(),
"--progress".to_owned().into(),
"--skip-msrv".to_owned().into(),
"--summary".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Test(TestTargets::Default, Ignored::Only),
Opts {
exec_dir: Some(PathBuf::new()),
rustup_home: Some(PathBuf::new()),
cargo_home: Some("--ignored".to_owned().into()),
cargo_path: "cargo/cargo".to_owned().into(),
color: true,
default_toolchain: true,
allow_implied_features: true,
ignore_compile_errors: true,
ignore_msrv: true,
progress: true,
skip_msrv: true,
summary: true,
ignore_features: vec![String::new()],
}
))
);
assert_eq!(
MetaCmd::from_args([OsString::new(), "test".to_owned().into(),].into_iter()),
Ok(MetaCmd::Cargo(
Cmd::Test(TestTargets::Default, Ignored::None),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: Vec::new(),
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"test".to_owned().into(),
"--include-ignored".to_owned().into()
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Test(TestTargets::Default, Ignored::Include),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: Vec::new(),
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--all-targets".to_owned().into(),
"--allow-implied-features".to_owned().into(),
"--cargo-home".to_owned().into(),
"--ignored".to_owned().into(),
"--cargo-path".to_owned().into(),
"cargo".to_owned().into(),
"--color".to_owned().into(),
"--default-toolchain".to_owned().into(),
"--dir".to_owned().into(),
OsString::new(),
"--ignore-compile-errors".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,".to_owned().into(),
"--ignore-msrv".to_owned().into(),
"--rustup-home".to_owned().into(),
OsString::new(),
"--progress".to_owned().into(),
"--skip-msrv".to_owned().into(),
"--summary".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::All),
Opts {
exec_dir: Some(PathBuf::new()),
rustup_home: Some(PathBuf::new()),
cargo_home: Some("--ignored".to_owned().into()),
cargo_path: "cargo/cargo".to_owned().into(),
color: true,
default_toolchain: true,
allow_implied_features: true,
ignore_compile_errors: true,
ignore_msrv: true,
progress: true,
skip_msrv: true,
summary: true,
ignore_features: vec!["a".to_owned(), String::new()],
}
))
);
assert_eq!(
MetaCmd::from_args([OsString::new(), "check".to_owned().into(),].into_iter()),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::Default),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: Vec::new(),
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,,b".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::Default),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: vec!["a".to_owned(), String::new(), "b".to_owned()],
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,b,".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::Default),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: vec!["a".to_owned(), "b".to_owned(), String::new()],
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
"a,b".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::Default),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: vec!["a".to_owned(), "b".to_owned()],
}
))
);
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"check".to_owned().into(),
"--ignore-features".to_owned().into(),
"a , , b, ".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::Check(CheckClippyTargets::Default),
Opts {
exec_dir: None,
rustup_home: None,
cargo_home: None,
cargo_path: "cargo".to_owned().into(),
color: false,
default_toolchain: false,
allow_implied_features: false,
ignore_compile_errors: false,
ignore_msrv: false,
progress: false,
skip_msrv: false,
summary: false,
ignore_features: vec![
"a ".to_owned(),
" ".to_owned(),
" b".to_owned(),
" ".to_owned()
],
}
))
);
assert_eq!(
MetaCmd::from_args([OsString::new(), "help".to_owned().into(),].into_iter()),
Ok(MetaCmd::Help)
);
assert_eq!(
MetaCmd::from_args([OsString::new(), "version".to_owned().into(),].into_iter()),
Ok(MetaCmd::Version)
);
let mut check_targets = Targets::new(Target::Examples);
assert!(check_targets.add(Target::Tests));
assert!(!check_targets.add(Target::Examples));
let mut test_targets = Targets::new(Target::Benches);
assert!(test_targets.add(Target::Bins));
assert!(!test_targets.add(Target::Bins));
assert!(test_targets.add(Target::Examples));
assert!(test_targets.add(Target::Lib));
assert!(test_targets.add(Target::Tests));
assert_eq!(
MetaCmd::from_args(
[
OsString::new(),
"clippy".to_owned().into(),
"--all-targets".to_owned().into(),
"--allow-implied-features".to_owned().into(),
"--cargo-home".to_owned().into(),
"--ignored".to_owned().into(),
"--cargo-path".to_owned().into(),
"cargo".to_owned().into(),
"--color".to_owned().into(),
"--default-toolchain".to_owned().into(),
"--deny-warnings".to_owned().into(),
"--dir".to_owned().into(),
OsString::new(),
"--ignore-compile-errors".to_owned().into(),
"--ignore-features".to_owned().into(),
",a".to_owned().into(),
"--ignore-msrv".to_owned().into(),
"--rustup-home".to_owned().into(),
"a".to_owned().into(),
"test".to_owned().into(),
"--benches".to_owned().into(),
"--bins".to_owned().into(),
"--examples".to_owned().into(),
"--include-ignored".to_owned().into(),
"--lib".to_owned().into(),
"--tests".to_owned().into(),
"--progress".to_owned().into(),
"--skip-msrv".to_owned().into(),
"check".to_owned().into(),
"--tests".to_owned().into(),
"--examples".to_owned().into(),
"--summary".to_owned().into(),
]
.into_iter()
),
Ok(MetaCmd::Cargo(
Cmd::CheckClippyTest(
CheckClippyTargets::Targets(check_targets),
CheckClippyTargets::All,
true,
TestTargets::Targets(test_targets),
Ignored::Include,
),
Opts {
exec_dir: Some(PathBuf::new()),
rustup_home: Some("a".to_owned().into()),
cargo_home: Some("--ignored".to_owned().into()),
cargo_path: "cargo/cargo".to_owned().into(),
color: true,
default_toolchain: true,
allow_implied_features: true,
ignore_compile_errors: true,
ignore_msrv: true,
progress: true,
skip_msrv: true,
summary: true,
ignore_features: vec![String::new(), "a".to_owned()],
}
))
);
}
#[test]
fn non_zero_usize_plus_1() {
#[cfg(target_pointer_width = "64")]
assert_eq!(NonZeroUsizePlus1(0).to_string(), "18446744073709551616");
#[cfg(target_pointer_width = "64")]
assert_eq!(
NonZeroUsizePlus1(usize::MAX).to_string(),
"18446744073709551615"
);
#[cfg(target_pointer_width = "32")]
assert_eq!(NonZeroUsizePlus1(0).to_string(), "4294967296");
#[cfg(target_pointer_width = "32")]
assert_eq!(NonZeroUsizePlus1(usize::MAX).to_string(), "4294967295");
#[cfg(target_pointer_width = "16")]
assert_eq!(NonZeroUsizePlus1(0).to_string(), "65536");
#[cfg(target_pointer_width = "16")]
assert_eq!(NonZeroUsizePlus1(usize::MAX).to_string(), "65535");
assert_eq!(NonZeroUsizePlus1(1).to_string(), "1");
assert_eq!(NonZeroUsizePlus1(2).to_string(), "2");
assert_eq!(NonZeroUsizePlus1(10).to_string(), "10");
}