Skip to content

Commit 40af5a6

Browse files
committed
tree-wide: Move most tests under mod test
This is a general best practice; specifically motivated by handling test-specific imports. Signed-off-by: Colin Walters <[email protected]>
1 parent c88fcfd commit 40af5a6

File tree

8 files changed

+672
-640
lines changed

8 files changed

+672
-640
lines changed

lib/src/blockdev.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -334,28 +334,28 @@ pub(crate) fn parse_size_mib(mut s: &str) -> Result<u64> {
334334
Ok(v * mul)
335335
}
336336

337-
#[test]
338-
fn test_parse_size_mib() {
339-
let ident_cases = [0, 10, 9, 1024].into_iter().map(|k| (k.to_string(), k));
340-
let cases = [
341-
("0M", 0),
342-
("10M", 10),
343-
("10MiB", 10),
344-
("1G", 1024),
345-
("9G", 9216),
346-
("11T", 11 * 1024 * 1024),
347-
]
348-
.into_iter()
349-
.map(|(k, v)| (k.to_string(), v));
350-
for (s, v) in ident_cases.chain(cases) {
351-
assert_eq!(parse_size_mib(&s).unwrap(), v as u64, "Parsing {s}");
352-
}
353-
}
354-
355337
#[cfg(test)]
356338
mod test {
357339
use super::*;
358340

341+
#[test]
342+
fn test_parse_size_mib() {
343+
let ident_cases = [0, 10, 9, 1024].into_iter().map(|k| (k.to_string(), k));
344+
let cases = [
345+
("0M", 0),
346+
("10M", 10),
347+
("10MiB", 10),
348+
("1G", 1024),
349+
("9G", 9216),
350+
("11T", 11 * 1024 * 1024),
351+
]
352+
.into_iter()
353+
.map(|(k, v)| (k.to_string(), v));
354+
for (s, v) in ident_cases.chain(cases) {
355+
assert_eq!(parse_size_mib(&s).unwrap(), v as u64, "Parsing {s}");
356+
}
357+
}
358+
359359
#[test]
360360
fn test_parse_sfdisk() -> Result<()> {
361361
let fixture = indoc::indoc! { r#"

lib/src/cli.rs

Lines changed: 118 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,127 +1125,132 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
11251125
}
11261126
}
11271127

1128-
#[test]
1129-
fn test_callname() {
1130-
use std::os::unix::ffi::OsStrExt;
1131-
1132-
// Cases that change
1133-
let mapped_cases = [
1134-
("", "bootc"),
1135-
("/foo/bar", "bar"),
1136-
("/foo/bar/", "bar"),
1137-
("foo/bar", "bar"),
1138-
("../foo/bar", "bar"),
1139-
("usr/bin/ostree-container", "ostree-container"),
1140-
];
1141-
for (input, output) in mapped_cases {
1142-
assert_eq!(
1143-
output,
1144-
callname_from_argv0(OsStr::new(input)),
1145-
"Handling mapped case {input}"
1146-
);
1147-
}
1128+
#[cfg(test)]
1129+
mod tests {
1130+
use super::*;
1131+
1132+
#[test]
1133+
fn test_callname() {
1134+
use std::os::unix::ffi::OsStrExt;
1135+
1136+
// Cases that change
1137+
let mapped_cases = [
1138+
("", "bootc"),
1139+
("/foo/bar", "bar"),
1140+
("/foo/bar/", "bar"),
1141+
("foo/bar", "bar"),
1142+
("../foo/bar", "bar"),
1143+
("usr/bin/ostree-container", "ostree-container"),
1144+
];
1145+
for (input, output) in mapped_cases {
1146+
assert_eq!(
1147+
output,
1148+
callname_from_argv0(OsStr::new(input)),
1149+
"Handling mapped case {input}"
1150+
);
1151+
}
11481152

1149-
// Invalid UTF-8
1150-
assert_eq!("bootc", callname_from_argv0(OsStr::from_bytes(b"foo\x80")));
1153+
// Invalid UTF-8
1154+
assert_eq!("bootc", callname_from_argv0(OsStr::from_bytes(b"foo\x80")));
1155+
1156+
// Cases that are identical
1157+
let ident_cases = ["foo", "bootc"];
1158+
for case in ident_cases {
1159+
assert_eq!(
1160+
case,
1161+
callname_from_argv0(OsStr::new(case)),
1162+
"Handling ident case {case}"
1163+
);
1164+
}
1165+
}
11511166

1152-
// Cases that are identical
1153-
let ident_cases = ["foo", "bootc"];
1154-
for case in ident_cases {
1167+
#[test]
1168+
fn test_parse_install_args() {
1169+
// Verify we still process the legacy --target-no-signature-verification
1170+
let o = Opt::try_parse_from([
1171+
"bootc",
1172+
"install",
1173+
"to-filesystem",
1174+
"--target-no-signature-verification",
1175+
"/target",
1176+
])
1177+
.unwrap();
1178+
let o = match o {
1179+
Opt::Install(InstallOpts::ToFilesystem(fsopts)) => fsopts,
1180+
o => panic!("Expected filesystem opts, not {o:?}"),
1181+
};
1182+
assert!(o.target_opts.target_no_signature_verification);
1183+
assert_eq!(o.filesystem_opts.root_path.as_str(), "/target");
1184+
// Ensure we default to old bound images behavior
11551185
assert_eq!(
1156-
case,
1157-
callname_from_argv0(OsStr::new(case)),
1158-
"Handling ident case {case}"
1186+
o.config_opts.bound_images,
1187+
crate::install::BoundImagesOpt::Stored
11591188
);
11601189
}
1161-
}
1162-
1163-
#[test]
1164-
fn test_parse_install_args() {
1165-
// Verify we still process the legacy --target-no-signature-verification
1166-
let o = Opt::try_parse_from([
1167-
"bootc",
1168-
"install",
1169-
"to-filesystem",
1170-
"--target-no-signature-verification",
1171-
"/target",
1172-
])
1173-
.unwrap();
1174-
let o = match o {
1175-
Opt::Install(InstallOpts::ToFilesystem(fsopts)) => fsopts,
1176-
o => panic!("Expected filesystem opts, not {o:?}"),
1177-
};
1178-
assert!(o.target_opts.target_no_signature_verification);
1179-
assert_eq!(o.filesystem_opts.root_path.as_str(), "/target");
1180-
// Ensure we default to old bound images behavior
1181-
assert_eq!(
1182-
o.config_opts.bound_images,
1183-
crate::install::BoundImagesOpt::Stored
1184-
);
1185-
}
11861190

1187-
#[test]
1188-
fn test_parse_opts() {
1189-
assert!(matches!(
1190-
Opt::parse_including_static(["bootc", "status"]),
1191-
Opt::Status(StatusOpts {
1192-
json: false,
1193-
format: None,
1194-
format_version: None,
1195-
booted: false
1196-
})
1197-
));
1198-
assert!(matches!(
1199-
Opt::parse_including_static(["bootc", "status", "--format-version=0"]),
1200-
Opt::Status(StatusOpts {
1201-
format_version: Some(0),
1202-
..
1203-
})
1204-
));
1205-
}
1191+
#[test]
1192+
fn test_parse_opts() {
1193+
assert!(matches!(
1194+
Opt::parse_including_static(["bootc", "status"]),
1195+
Opt::Status(StatusOpts {
1196+
json: false,
1197+
format: None,
1198+
format_version: None,
1199+
booted: false
1200+
})
1201+
));
1202+
assert!(matches!(
1203+
Opt::parse_including_static(["bootc", "status", "--format-version=0"]),
1204+
Opt::Status(StatusOpts {
1205+
format_version: Some(0),
1206+
..
1207+
})
1208+
));
1209+
}
12061210

1207-
#[test]
1208-
fn test_parse_generator() {
1209-
assert!(matches!(
1210-
Opt::parse_including_static([
1211-
"/usr/lib/systemd/system/bootc-systemd-generator",
1212-
"/run/systemd/system"
1213-
]),
1214-
Opt::Internals(InternalsOpts::SystemdGenerator { normal_dir, .. }) if normal_dir == "/run/systemd/system"
1215-
));
1216-
}
1211+
#[test]
1212+
fn test_parse_generator() {
1213+
assert!(matches!(
1214+
Opt::parse_including_static([
1215+
"/usr/lib/systemd/system/bootc-systemd-generator",
1216+
"/run/systemd/system"
1217+
]),
1218+
Opt::Internals(InternalsOpts::SystemdGenerator { normal_dir, .. }) if normal_dir == "/run/systemd/system"
1219+
));
1220+
}
12171221

1218-
#[test]
1219-
fn test_parse_ostree_ext() {
1220-
assert!(matches!(
1221-
Opt::parse_including_static(["bootc", "internals", "ostree-container"]),
1222-
Opt::Internals(InternalsOpts::OstreeContainer { .. })
1223-
));
1224-
1225-
fn peel(o: Opt) -> Vec<OsString> {
1226-
match o {
1227-
Opt::Internals(InternalsOpts::OstreeExt { args }) => args,
1228-
o => panic!("unexpected {o:?}"),
1222+
#[test]
1223+
fn test_parse_ostree_ext() {
1224+
assert!(matches!(
1225+
Opt::parse_including_static(["bootc", "internals", "ostree-container"]),
1226+
Opt::Internals(InternalsOpts::OstreeContainer { .. })
1227+
));
1228+
1229+
fn peel(o: Opt) -> Vec<OsString> {
1230+
match o {
1231+
Opt::Internals(InternalsOpts::OstreeExt { args }) => args,
1232+
o => panic!("unexpected {o:?}"),
1233+
}
12291234
}
1230-
}
1231-
let args = peel(Opt::parse_including_static([
1232-
"/usr/libexec/libostree/ext/ostree-ima-sign",
1233-
"ima-sign",
1234-
"--repo=foo",
1235-
"foo",
1236-
"bar",
1237-
"baz",
1238-
]));
1239-
assert_eq!(
1240-
args.as_slice(),
1241-
["ima-sign", "--repo=foo", "foo", "bar", "baz"]
1242-
);
1235+
let args = peel(Opt::parse_including_static([
1236+
"/usr/libexec/libostree/ext/ostree-ima-sign",
1237+
"ima-sign",
1238+
"--repo=foo",
1239+
"foo",
1240+
"bar",
1241+
"baz",
1242+
]));
1243+
assert_eq!(
1244+
args.as_slice(),
1245+
["ima-sign", "--repo=foo", "foo", "bar", "baz"]
1246+
);
12431247

1244-
let args = peel(Opt::parse_including_static([
1245-
"/usr/libexec/libostree/ext/ostree-container",
1246-
"container",
1247-
"image",
1248-
"pull",
1249-
]));
1250-
assert_eq!(args.as_slice(), ["container", "image", "pull"]);
1248+
let args = peel(Opt::parse_including_static([
1249+
"/usr/libexec/libostree/ext/ostree-container",
1250+
"container",
1251+
"image",
1252+
"pull",
1253+
]));
1254+
assert_eq!(args.as_slice(), ["container", "image", "pull"]);
1255+
}
12511256
}

0 commit comments

Comments
 (0)