Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree-wide: Move most tests under mod test #990

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,28 +334,28 @@ pub(crate) fn parse_size_mib(mut s: &str) -> Result<u64> {
Ok(v * mul)
}

#[test]
fn test_parse_size_mib() {
let ident_cases = [0, 10, 9, 1024].into_iter().map(|k| (k.to_string(), k));
let cases = [
("0M", 0),
("10M", 10),
("10MiB", 10),
("1G", 1024),
("9G", 9216),
("11T", 11 * 1024 * 1024),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v));
for (s, v) in ident_cases.chain(cases) {
assert_eq!(parse_size_mib(&s).unwrap(), v as u64, "Parsing {s}");
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_parse_size_mib() {
let ident_cases = [0, 10, 9, 1024].into_iter().map(|k| (k.to_string(), k));
let cases = [
("0M", 0),
("10M", 10),
("10MiB", 10),
("1G", 1024),
("9G", 9216),
("11T", 11 * 1024 * 1024),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v));
for (s, v) in ident_cases.chain(cases) {
assert_eq!(parse_size_mib(&s).unwrap(), v as u64, "Parsing {s}");
}
}

#[test]
fn test_parse_sfdisk() -> Result<()> {
let fixture = indoc::indoc! { r#"
Expand Down
231 changes: 118 additions & 113 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,127 +1125,132 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
}
}

#[test]
fn test_callname() {
use std::os::unix::ffi::OsStrExt;

// Cases that change
let mapped_cases = [
("", "bootc"),
("/foo/bar", "bar"),
("/foo/bar/", "bar"),
("foo/bar", "bar"),
("../foo/bar", "bar"),
("usr/bin/ostree-container", "ostree-container"),
];
for (input, output) in mapped_cases {
assert_eq!(
output,
callname_from_argv0(OsStr::new(input)),
"Handling mapped case {input}"
);
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_callname() {
use std::os::unix::ffi::OsStrExt;

// Cases that change
let mapped_cases = [
("", "bootc"),
("/foo/bar", "bar"),
("/foo/bar/", "bar"),
("foo/bar", "bar"),
("../foo/bar", "bar"),
("usr/bin/ostree-container", "ostree-container"),
];
for (input, output) in mapped_cases {
assert_eq!(
output,
callname_from_argv0(OsStr::new(input)),
"Handling mapped case {input}"
);
}

// Invalid UTF-8
assert_eq!("bootc", callname_from_argv0(OsStr::from_bytes(b"foo\x80")));
// Invalid UTF-8
assert_eq!("bootc", callname_from_argv0(OsStr::from_bytes(b"foo\x80")));

// Cases that are identical
let ident_cases = ["foo", "bootc"];
for case in ident_cases {
assert_eq!(
case,
callname_from_argv0(OsStr::new(case)),
"Handling ident case {case}"
);
}
}

// Cases that are identical
let ident_cases = ["foo", "bootc"];
for case in ident_cases {
#[test]
fn test_parse_install_args() {
// Verify we still process the legacy --target-no-signature-verification
let o = Opt::try_parse_from([
"bootc",
"install",
"to-filesystem",
"--target-no-signature-verification",
"/target",
])
.unwrap();
let o = match o {
Opt::Install(InstallOpts::ToFilesystem(fsopts)) => fsopts,
o => panic!("Expected filesystem opts, not {o:?}"),
};
assert!(o.target_opts.target_no_signature_verification);
assert_eq!(o.filesystem_opts.root_path.as_str(), "/target");
// Ensure we default to old bound images behavior
assert_eq!(
case,
callname_from_argv0(OsStr::new(case)),
"Handling ident case {case}"
o.config_opts.bound_images,
crate::install::BoundImagesOpt::Stored
);
}
}

#[test]
fn test_parse_install_args() {
// Verify we still process the legacy --target-no-signature-verification
let o = Opt::try_parse_from([
"bootc",
"install",
"to-filesystem",
"--target-no-signature-verification",
"/target",
])
.unwrap();
let o = match o {
Opt::Install(InstallOpts::ToFilesystem(fsopts)) => fsopts,
o => panic!("Expected filesystem opts, not {o:?}"),
};
assert!(o.target_opts.target_no_signature_verification);
assert_eq!(o.filesystem_opts.root_path.as_str(), "/target");
// Ensure we default to old bound images behavior
assert_eq!(
o.config_opts.bound_images,
crate::install::BoundImagesOpt::Stored
);
}

#[test]
fn test_parse_opts() {
assert!(matches!(
Opt::parse_including_static(["bootc", "status"]),
Opt::Status(StatusOpts {
json: false,
format: None,
format_version: None,
booted: false
})
));
assert!(matches!(
Opt::parse_including_static(["bootc", "status", "--format-version=0"]),
Opt::Status(StatusOpts {
format_version: Some(0),
..
})
));
}
#[test]
fn test_parse_opts() {
assert!(matches!(
Opt::parse_including_static(["bootc", "status"]),
Opt::Status(StatusOpts {
json: false,
format: None,
format_version: None,
booted: false
})
));
assert!(matches!(
Opt::parse_including_static(["bootc", "status", "--format-version=0"]),
Opt::Status(StatusOpts {
format_version: Some(0),
..
})
));
}

#[test]
fn test_parse_generator() {
assert!(matches!(
Opt::parse_including_static([
"/usr/lib/systemd/system/bootc-systemd-generator",
"/run/systemd/system"
]),
Opt::Internals(InternalsOpts::SystemdGenerator { normal_dir, .. }) if normal_dir == "/run/systemd/system"
));
}
#[test]
fn test_parse_generator() {
assert!(matches!(
Opt::parse_including_static([
"/usr/lib/systemd/system/bootc-systemd-generator",
"/run/systemd/system"
]),
Opt::Internals(InternalsOpts::SystemdGenerator { normal_dir, .. }) if normal_dir == "/run/systemd/system"
));
}

#[test]
fn test_parse_ostree_ext() {
assert!(matches!(
Opt::parse_including_static(["bootc", "internals", "ostree-container"]),
Opt::Internals(InternalsOpts::OstreeContainer { .. })
));

fn peel(o: Opt) -> Vec<OsString> {
match o {
Opt::Internals(InternalsOpts::OstreeExt { args }) => args,
o => panic!("unexpected {o:?}"),
#[test]
fn test_parse_ostree_ext() {
assert!(matches!(
Opt::parse_including_static(["bootc", "internals", "ostree-container"]),
Opt::Internals(InternalsOpts::OstreeContainer { .. })
));

fn peel(o: Opt) -> Vec<OsString> {
match o {
Opt::Internals(InternalsOpts::OstreeExt { args }) => args,
o => panic!("unexpected {o:?}"),
}
}
}
let args = peel(Opt::parse_including_static([
"/usr/libexec/libostree/ext/ostree-ima-sign",
"ima-sign",
"--repo=foo",
"foo",
"bar",
"baz",
]));
assert_eq!(
args.as_slice(),
["ima-sign", "--repo=foo", "foo", "bar", "baz"]
);
let args = peel(Opt::parse_including_static([
"/usr/libexec/libostree/ext/ostree-ima-sign",
"ima-sign",
"--repo=foo",
"foo",
"bar",
"baz",
]));
assert_eq!(
args.as_slice(),
["ima-sign", "--repo=foo", "foo", "bar", "baz"]
);

let args = peel(Opt::parse_including_static([
"/usr/libexec/libostree/ext/ostree-container",
"container",
"image",
"pull",
]));
assert_eq!(args.as_slice(), ["container", "image", "pull"]);
let args = peel(Opt::parse_including_static([
"/usr/libexec/libostree/ext/ostree-container",
"container",
"image",
"pull",
]));
assert_eq!(args.as_slice(), ["container", "image", "pull"]);
}
}
Loading
Loading