From 203a4fc20c3743fed6be5b7218c9c7658f8ca1df Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 20 Dec 2024 10:49:15 -0500 Subject: [PATCH] tree-wide: Move everything else under #[cfg(test)] Followup to 40af5a67b1c392edc4b61648c142483938a1e651 now that some other PRs have merged this one would be less conflict-fest. This is a general best practice; specifically motivated by handling test-specific imports. Signed-off-by: Colin Walters --- lib/src/deploy.rs | 164 ++++++++++++------------ lib/src/install.rs | 100 ++++++++------- lib/src/install/osconfig.rs | 47 ++++--- ostree-ext/src/container/encapsulate.rs | 27 ++-- ostree-ext/src/ostree_prepareroot.rs | 99 +++++++------- ostree-ext/src/statistics.rs | 94 +++++++------- utils/src/command.rs | 111 ++++++++-------- 7 files changed, 338 insertions(+), 304 deletions(-) diff --git a/lib/src/deploy.rs b/lib/src/deploy.rs index 59e82e3be..88d44d9a4 100644 --- a/lib/src/deploy.rs +++ b/lib/src/deploy.rs @@ -817,44 +817,6 @@ pub(crate) fn switch_origin_inplace(root: &Dir, imgref: &ImageReference) -> Resu Ok(newest_deployment) } -#[test] -fn test_switch_inplace() -> Result<()> { - use cap_std::fs::DirBuilderExt; - - let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; - let mut builder = cap_std::fs::DirBuilder::new(); - let builder = builder.recursive(true).mode(0o755); - let deploydir = "sysroot/ostree/deploy/default/deploy"; - let target_deployment = "af36eb0086bb55ac601600478c6168f834288013d60f8870b7851f44bf86c3c5.0"; - td.ensure_dir_with( - format!("sysroot/ostree/deploy/default/deploy/{target_deployment}"), - builder, - )?; - let deploydir = &td.open_dir(deploydir)?; - let orig_imgref = ImageReference { - image: "quay.io/exampleos/original:sometag".into(), - transport: "registry".into(), - signature: None, - }; - { - let origin = origin_from_imageref(&orig_imgref)?; - deploydir.atomic_write( - format!("{target_deployment}.origin"), - origin.to_data().as_bytes(), - )?; - } - - let target_imgref = ImageReference { - image: "quay.io/someother/otherimage:latest".into(), - transport: "registry".into(), - signature: None, - }; - - let replaced = switch_origin_inplace(&td, &target_imgref).unwrap(); - assert_eq!(replaced, target_deployment); - Ok(()) -} - /// A workaround for https://github.com/ostreedev/ostree/issues/3193 /// as generated by anaconda. #[context("Updating /etc/fstab for anaconda+composefs")] @@ -935,56 +897,100 @@ pub(crate) fn fixup_etc_fstab(root: &Dir) -> Result<()> { Ok(()) } -#[test] -fn test_fixup_etc_fstab_default() -> Result<()> { - let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; - let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n"; - tempdir.create_dir_all("etc")?; - tempdir.atomic_write("etc/fstab", default)?; - fixup_etc_fstab(&tempdir).unwrap(); - assert_eq!(tempdir.read_to_string("etc/fstab")?, default); - Ok(()) -} +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_switch_inplace() -> Result<()> { + use cap_std::fs::DirBuilderExt; + + let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; + let mut builder = cap_std::fs::DirBuilder::new(); + let builder = builder.recursive(true).mode(0o755); + let deploydir = "sysroot/ostree/deploy/default/deploy"; + let target_deployment = + "af36eb0086bb55ac601600478c6168f834288013d60f8870b7851f44bf86c3c5.0"; + td.ensure_dir_with( + format!("sysroot/ostree/deploy/default/deploy/{target_deployment}"), + builder, + )?; + let deploydir = &td.open_dir(deploydir)?; + let orig_imgref = ImageReference { + image: "quay.io/exampleos/original:sometag".into(), + transport: "registry".into(), + signature: None, + }; + { + let origin = origin_from_imageref(&orig_imgref)?; + deploydir.atomic_write( + format!("{target_deployment}.origin"), + origin.to_data().as_bytes(), + )?; + } + + let target_imgref = ImageReference { + image: "quay.io/someother/otherimage:latest".into(), + transport: "registry".into(), + signature: None, + }; -#[test] -fn test_fixup_etc_fstab_multi() -> Result<()> { - let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; - let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ + let replaced = switch_origin_inplace(&td, &target_imgref).unwrap(); + assert_eq!(replaced, target_deployment); + Ok(()) + } + + #[test] + fn test_fixup_etc_fstab_default() -> Result<()> { + let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; + let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n"; + tempdir.create_dir_all("etc")?; + tempdir.atomic_write("etc/fstab", default)?; + fixup_etc_fstab(&tempdir).unwrap(); + assert_eq!(tempdir.read_to_string("etc/fstab")?, default); + Ok(()) + } + + #[test] + fn test_fixup_etc_fstab_multi() -> Result<()> { + let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; + let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n"; - tempdir.create_dir_all("etc")?; - tempdir.atomic_write("etc/fstab", default)?; - fixup_etc_fstab(&tempdir).unwrap(); - assert_eq!(tempdir.read_to_string("etc/fstab")?, default); - Ok(()) -} + tempdir.create_dir_all("etc")?; + tempdir.atomic_write("etc/fstab", default)?; + fixup_etc_fstab(&tempdir).unwrap(); + assert_eq!(tempdir.read_to_string("etc/fstab")?, default); + Ok(()) + } -#[test] -fn test_fixup_etc_fstab_ro() -> Result<()> { - let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; - let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ + #[test] + fn test_fixup_etc_fstab_ro() -> Result<()> { + let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; + let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs ro 0 0\n\ UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n"; - tempdir.create_dir_all("etc")?; - tempdir.atomic_write("etc/fstab", default)?; - fixup_etc_fstab(&tempdir).unwrap(); - assert_eq!(tempdir.read_to_string("etc/fstab")?, default); - Ok(()) -} + tempdir.create_dir_all("etc")?; + tempdir.atomic_write("etc/fstab", default)?; + fixup_etc_fstab(&tempdir).unwrap(); + assert_eq!(tempdir.read_to_string("etc/fstab")?, default); + Ok(()) + } -#[test] -fn test_fixup_etc_fstab_rw() -> Result<()> { - let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; - // This case uses `defaults` - let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ + #[test] + fn test_fixup_etc_fstab_rw() -> Result<()> { + let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; + // This case uses `defaults` + let default = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs defaults 0 0\n\ UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n"; - let modified = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ + let modified = "UUID=f7436547-20ac-43cb-aa2f-eac9632183f6 /boot auto ro 0 0\n\ # Updated by bootc-fstab-edit.service\n\ UUID=1eef9f42-40e3-4bd8-ae20-e9f2325f8b52 / xfs defaults,ro 0 0\n\ UUID=6907-17CA /boot/efi vfat umask=0077,shortname=winnt 0 2\n"; - tempdir.create_dir_all("etc")?; - tempdir.atomic_write("etc/fstab", default)?; - fixup_etc_fstab(&tempdir).unwrap(); - assert_eq!(tempdir.read_to_string("etc/fstab")?, modified); - Ok(()) + tempdir.create_dir_all("etc")?; + tempdir.atomic_write("etc/fstab", default)?; + fixup_etc_fstab(&tempdir).unwrap(); + assert_eq!(tempdir.read_to_string("etc/fstab")?, modified); + Ok(()) + } } diff --git a/lib/src/install.rs b/lib/src/install.rs index d591672b2..1eab73bad 100644 --- a/lib/src/install.rs +++ b/lib/src/install.rs @@ -1882,54 +1882,58 @@ pub(crate) async fn install_to_existing_root(opts: InstallToExistingRootOpts) -> install_to_filesystem(opts, true).await } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn install_opts_serializable() { + let c: InstallToDiskOpts = serde_json::from_value(serde_json::json!({ + "device": "/dev/vda" + })) + .unwrap(); + assert_eq!(c.block_opts.device, "/dev/vda"); + } -#[test] -fn install_opts_serializable() { - let c: InstallToDiskOpts = serde_json::from_value(serde_json::json!({ - "device": "/dev/vda" - })) - .unwrap(); - assert_eq!(c.block_opts.device, "/dev/vda"); -} - -#[test] -fn test_mountspec() { - let mut ms = MountSpec::new("/dev/vda4", "/boot"); - assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto defaults 0 0"); - ms.push_option("ro"); - assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro 0 0"); - ms.push_option("relatime"); - assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro,relatime 0 0"); -} + #[test] + fn test_mountspec() { + let mut ms = MountSpec::new("/dev/vda4", "/boot"); + assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto defaults 0 0"); + ms.push_option("ro"); + assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro 0 0"); + ms.push_option("relatime"); + assert_eq!(ms.to_fstab(), "/dev/vda4 /boot auto ro,relatime 0 0"); + } -#[test] -fn test_gather_root_args() { - // A basic filesystem using a UUID - let inspect = Filesystem { - source: "/dev/vda4".into(), - target: "/".into(), - fstype: "xfs".into(), - maj_min: "252:4".into(), - options: "rw".into(), - uuid: Some("965eb3c7-5a3f-470d-aaa2-1bcf04334bc6".into()), - children: None, - }; - let r = find_root_args_to_inherit(&[], &inspect).unwrap(); - assert_eq!(r.mount_spec, "UUID=965eb3c7-5a3f-470d-aaa2-1bcf04334bc6"); - - // In this case we take the root= from the kernel cmdline - let r = find_root_args_to_inherit( - &[ - "root=/dev/mapper/root", - "rw", - "someother=karg", - "rd.lvm.lv=root", - "systemd.debug=1", - ], - &inspect, - ) - .unwrap(); - assert_eq!(r.mount_spec, "/dev/mapper/root"); - assert_eq!(r.kargs.len(), 1); - assert_eq!(r.kargs[0], "rd.lvm.lv=root"); + #[test] + fn test_gather_root_args() { + // A basic filesystem using a UUID + let inspect = Filesystem { + source: "/dev/vda4".into(), + target: "/".into(), + fstype: "xfs".into(), + maj_min: "252:4".into(), + options: "rw".into(), + uuid: Some("965eb3c7-5a3f-470d-aaa2-1bcf04334bc6".into()), + children: None, + }; + let r = find_root_args_to_inherit(&[], &inspect).unwrap(); + assert_eq!(r.mount_spec, "UUID=965eb3c7-5a3f-470d-aaa2-1bcf04334bc6"); + + // In this case we take the root= from the kernel cmdline + let r = find_root_args_to_inherit( + &[ + "root=/dev/mapper/root", + "rw", + "someother=karg", + "rd.lvm.lv=root", + "systemd.debug=1", + ], + &inspect, + ) + .unwrap(); + assert_eq!(r.mount_spec, "/dev/mapper/root"); + assert_eq!(r.kargs.len(), 1); + assert_eq!(r.kargs[0], "rd.lvm.lv=root"); + } } diff --git a/lib/src/install/osconfig.rs b/lib/src/install/osconfig.rs index 24bca4911..f282905d4 100644 --- a/lib/src/install/osconfig.rs +++ b/lib/src/install/osconfig.rs @@ -50,37 +50,42 @@ pub(crate) fn inject_root_ssh_authorized_keys( Ok(()) } -#[test] -fn test_inject_root_ssh_symlinked() -> Result<()> { - let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; +#[cfg(test)] +mod tests { + use super::*; - // The code expects this to exist, reasonably so - root.create_dir("etc")?; - // Test with a symlink - root.symlink("var/roothome", "root")?; - inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap(); + #[test] + fn test_inject_root_ssh_symlinked() -> Result<()> { + let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; - let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; - assert_eq!( + // The code expects this to exist, reasonably so + root.create_dir("etc")?; + // Test with a symlink + root.symlink("var/roothome", "root")?; + inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap(); + + let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; + assert_eq!( content, "f~ /var/roothome/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n" ); - Ok(()) -} + Ok(()) + } -#[test] -fn test_inject_root_ssh_dir() -> Result<()> { - let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; + #[test] + fn test_inject_root_ssh_dir() -> Result<()> { + let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; - root.create_dir("etc")?; - root.create_dir("root")?; - inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap(); + root.create_dir("etc")?; + root.create_dir("root")?; + inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap(); - let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; - assert_eq!( + let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; + assert_eq!( content, "f~ /root/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n" ); - Ok(()) + Ok(()) + } } diff --git a/ostree-ext/src/container/encapsulate.rs b/ostree-ext/src/container/encapsulate.rs index 915bb7d6d..f0301667c 100644 --- a/ostree-ext/src/container/encapsulate.rs +++ b/ostree-ext/src/container/encapsulate.rs @@ -416,15 +416,20 @@ pub async fn encapsulate>( build_impl(repo, ostree_ref.as_ref(), config, opts, dest).await } -#[test] -fn test_parse_ocipath() { - let default = "/foo/bar"; - let untagged = "/foo/bar:baz"; - let tagged = "/foo/bar:baz:latest"; - assert_eq!(parse_oci_path_and_tag(default), ("/foo/bar", None)); - assert_eq!( - parse_oci_path_and_tag(tagged), - ("/foo/bar", Some("baz:latest")) - ); - assert_eq!(parse_oci_path_and_tag(untagged), ("/foo/bar", Some("baz"))); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_ocipath() { + let default = "/foo/bar"; + let untagged = "/foo/bar:baz"; + let tagged = "/foo/bar:baz:latest"; + assert_eq!(parse_oci_path_and_tag(default), ("/foo/bar", None)); + assert_eq!( + parse_oci_path_and_tag(tagged), + ("/foo/bar", Some("baz:latest")) + ); + assert_eq!(parse_oci_path_and_tag(untagged), ("/foo/bar", Some("baz"))); + } } diff --git a/ostree-ext/src/ostree_prepareroot.rs b/ostree-ext/src/ostree_prepareroot.rs index bd2dc3f1b..e922213ca 100644 --- a/ostree-ext/src/ostree_prepareroot.rs +++ b/ostree-ext/src/ostree_prepareroot.rs @@ -131,69 +131,74 @@ pub fn overlayfs_enabled_in_config(config: &glib::KeyFile) -> Result { Ok(root_transient || composefs.maybe_enabled()) } -#[test] -fn test_tristate() { - for v in ["yes", "true", "1"] { - assert_eq!(Tristate::from_str(v).unwrap(), Tristate::Enabled); - } - assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe); - for v in ["no", "false", "0"] { - assert_eq!(Tristate::from_str(v).unwrap(), Tristate::Disabled); - } - for v in ["", "junk", "fal", "tr1"] { - assert!(Tristate::from_str(v).is_err()); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_tristate() { + for v in ["yes", "true", "1"] { + assert_eq!(Tristate::from_str(v).unwrap(), Tristate::Enabled); + } + assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe); + for v in ["no", "false", "0"] { + assert_eq!(Tristate::from_str(v).unwrap(), Tristate::Disabled); + } + for v in ["", "junk", "fal", "tr1"] { + assert!(Tristate::from_str(v).is_err()); + } } -} -#[test] -fn test_composefs_state() { - assert_eq!( - ComposefsState::from_str("signed").unwrap(), - ComposefsState::Signed - ); - for v in ["yes", "true", "1"] { + #[test] + fn test_composefs_state() { assert_eq!( - ComposefsState::from_str(v).unwrap(), - ComposefsState::Tristate(Tristate::Enabled) - ); - } - assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe); - for v in ["no", "false", "0"] { - assert_eq!( - ComposefsState::from_str(v).unwrap(), - ComposefsState::Tristate(Tristate::Disabled) + ComposefsState::from_str("signed").unwrap(), + ComposefsState::Signed ); + for v in ["yes", "true", "1"] { + assert_eq!( + ComposefsState::from_str(v).unwrap(), + ComposefsState::Tristate(Tristate::Enabled) + ); + } + assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe); + for v in ["no", "false", "0"] { + assert_eq!( + ComposefsState::from_str(v).unwrap(), + ComposefsState::Tristate(Tristate::Disabled) + ); + } } -} -#[test] -fn test_overlayfs_enabled() { - let d0 = indoc::indoc! { r#" + #[test] + fn test_overlayfs_enabled() { + let d0 = indoc::indoc! { r#" [foo] bar = baz [root] "# }; - let d1 = indoc::indoc! { r#" + let d1 = indoc::indoc! { r#" [root] transient = false "# }; - let d2 = indoc::indoc! { r#" + let d2 = indoc::indoc! { r#" [composefs] enabled = false "# }; - for v in ["", d0, d1, d2] { - let kf = glib::KeyFile::new(); - kf.load_from_data(v, glib::KeyFileFlags::empty()).unwrap(); - assert!(!overlayfs_enabled_in_config(&kf).unwrap()); - } + for v in ["", d0, d1, d2] { + let kf = glib::KeyFile::new(); + kf.load_from_data(v, glib::KeyFileFlags::empty()).unwrap(); + assert!(!overlayfs_enabled_in_config(&kf).unwrap()); + } - let e0 = format!("{d0}\n[root]\ntransient = true"); - let e1 = format!("{d1}\n[composefs]\nenabled = true\n[other]\nsomekey = someval"); - let e2 = format!("{d1}\n[composefs]\nenabled = yes"); - let e3 = format!("{d1}\n[composefs]\nenabled = signed"); - for v in [e0, e1, e2, e3] { - let kf = glib::KeyFile::new(); - kf.load_from_data(&v, glib::KeyFileFlags::empty()).unwrap(); - assert!(overlayfs_enabled_in_config(&kf).unwrap()); + let e0 = format!("{d0}\n[root]\ntransient = true"); + let e1 = format!("{d1}\n[composefs]\nenabled = true\n[other]\nsomekey = someval"); + let e2 = format!("{d1}\n[composefs]\nenabled = yes"); + let e3 = format!("{d1}\n[composefs]\nenabled = signed"); + for v in [e0, e1, e2, e3] { + let kf = glib::KeyFile::new(); + kf.load_from_data(&v, glib::KeyFileFlags::empty()).unwrap(); + assert!(overlayfs_enabled_in_config(&kf).unwrap()); + } } } diff --git a/ostree-ext/src/statistics.rs b/ostree-ext/src/statistics.rs index 7b0102fb6..f527b5a87 100644 --- a/ostree-ext/src/statistics.rs +++ b/ostree-ext/src/statistics.rs @@ -55,55 +55,59 @@ pub(crate) fn median_absolute_deviation(data: &mut [u64]) -> Option<(f64, f64)> Some((median_data, mad)) } } +#[cfg(test)] +mod tests { + use super::*; -#[test] -fn test_mean() { - assert_eq!(mean(&[]), None); - for v in [0u64, 1, 5, 100] { - assert_eq!(mean(&[v]), Some(v as f64)); + #[test] + fn test_mean() { + assert_eq!(mean(&[]), None); + for v in [0u64, 1, 5, 100] { + assert_eq!(mean(&[v]), Some(v as f64)); + } + assert_eq!(mean(&[0, 1]), Some(0.5)); + assert_eq!(mean(&[0, 5, 100]), Some(35.0)); + assert_eq!(mean(&[7, 4, 30, 14]), Some(13.75)); } - assert_eq!(mean(&[0, 1]), Some(0.5)); - assert_eq!(mean(&[0, 5, 100]), Some(35.0)); - assert_eq!(mean(&[7, 4, 30, 14]), Some(13.75)); -} -#[test] -fn test_std_deviation() { - assert_eq!(std_deviation(&[]), None); - for v in [0u64, 1, 5, 100] { - assert_eq!(std_deviation(&[v]), Some(0 as f64)); + #[test] + fn test_std_deviation() { + assert_eq!(std_deviation(&[]), None); + for v in [0u64, 1, 5, 100] { + assert_eq!(std_deviation(&[v]), Some(0 as f64)); + } + assert_eq!(std_deviation(&[1, 4]), Some(1.5)); + assert_eq!(std_deviation(&[2, 2, 2, 2]), Some(0.0)); + assert_eq!( + std_deviation(&[1, 20, 300, 4000, 50000, 600000, 7000000, 80000000]), + Some(26193874.56387471) + ); } - assert_eq!(std_deviation(&[1, 4]), Some(1.5)); - assert_eq!(std_deviation(&[2, 2, 2, 2]), Some(0.0)); - assert_eq!( - std_deviation(&[1, 20, 300, 4000, 50000, 600000, 7000000, 80000000]), - Some(26193874.56387471) - ); -} -#[test] -fn test_median_absolute_deviation() { - //Assumes sorted - assert_eq!(median_absolute_deviation(&mut []), None); - for v in [0u64, 1, 5, 100] { - assert_eq!(median_absolute_deviation(&mut [v]), Some((v as f64, 0.0))); - } - assert_eq!(median_absolute_deviation(&mut [1, 4]), Some((2.5, 1.5))); - assert_eq!( - median_absolute_deviation(&mut [2, 2, 2, 2]), - Some((2.0, 0.0)) - ); - assert_eq!( - median_absolute_deviation(&mut [ - 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 9, 12, 52, 90 - ]), - Some((6.0, 2.0)) - ); + #[test] + fn test_median_absolute_deviation() { + //Assumes sorted + assert_eq!(median_absolute_deviation(&mut []), None); + for v in [0u64, 1, 5, 100] { + assert_eq!(median_absolute_deviation(&mut [v]), Some((v as f64, 0.0))); + } + assert_eq!(median_absolute_deviation(&mut [1, 4]), Some((2.5, 1.5))); + assert_eq!( + median_absolute_deviation(&mut [2, 2, 2, 2]), + Some((2.0, 0.0)) + ); + assert_eq!( + median_absolute_deviation(&mut [ + 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 9, 12, 52, 90 + ]), + Some((6.0, 2.0)) + ); - //if more than half of the data has the same value, MAD = 0, thus any - //value different from the residual median is classified as an outlier - assert_eq!( - median_absolute_deviation(&mut [0, 1, 1, 1, 1, 1, 1, 1, 0]), - Some((1.0, 0.0)) - ); + //if more than half of the data has the same value, MAD = 0, thus any + //value different from the residual median is classified as an outlier + assert_eq!( + median_absolute_deviation(&mut [0, 1, 1, 1, 1, 1, 1, 1, 0]), + Some((1.0, 0.0)) + ); + } } diff --git a/utils/src/command.rs b/utils/src/command.rs index 3db7d86fd..94caf2f08 100644 --- a/utils/src/command.rs +++ b/utils/src/command.rs @@ -140,60 +140,65 @@ impl AsyncCommandRunExt for tokio::process::Command { } } -#[test] -fn command_run_ext() { - // The basics - Command::new("true").run().unwrap(); - assert!(Command::new("false").run().is_err()); - - // Verify we capture stderr - let e = Command::new("/bin/sh") - .args(["-c", "echo expected-this-oops-message 1>&2; exit 1"]) - .run() - .err() - .unwrap(); - similar_asserts::assert_eq!( - e.to_string(), - "Subprocess failed: ExitStatus(unix_wait_status(256))\nexpected-this-oops-message\n" - ); - - // Ignoring invalid UTF-8 - let e = Command::new("/bin/sh") - .args([ - "-c", - r"echo -e 'expected\xf5\x80\x80\x80\x80-foo\xc0bar\xc0\xc0' 1>&2; exit 1", - ]) - .run() - .err() - .unwrap(); - similar_asserts::assert_eq!( - e.to_string(), - "Subprocess failed: ExitStatus(unix_wait_status(256))\nexpected�����-foo�bar��\n" - ); -} +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn command_run_ext() { + // The basics + Command::new("true").run().unwrap(); + assert!(Command::new("false").run().is_err()); + + // Verify we capture stderr + let e = Command::new("/bin/sh") + .args(["-c", "echo expected-this-oops-message 1>&2; exit 1"]) + .run() + .err() + .unwrap(); + similar_asserts::assert_eq!( + e.to_string(), + "Subprocess failed: ExitStatus(unix_wait_status(256))\nexpected-this-oops-message\n" + ); + + // Ignoring invalid UTF-8 + let e = Command::new("/bin/sh") + .args([ + "-c", + r"echo -e 'expected\xf5\x80\x80\x80\x80-foo\xc0bar\xc0\xc0' 1>&2; exit 1", + ]) + .run() + .err() + .unwrap(); + similar_asserts::assert_eq!( + e.to_string(), + "Subprocess failed: ExitStatus(unix_wait_status(256))\nexpected�����-foo�bar��\n" + ); + } -#[test] -fn command_run_ext_json() { - #[derive(serde::Deserialize)] - struct Foo { - a: String, - b: u32, + #[test] + fn command_run_ext_json() { + #[derive(serde::Deserialize)] + struct Foo { + a: String, + b: u32, + } + let v: Foo = Command::new("echo") + .arg(r##"{"a": "somevalue", "b": 42}"##) + .run_and_parse_json() + .unwrap(); + assert_eq!(v.a, "somevalue"); + assert_eq!(v.b, 42); } - let v: Foo = Command::new("echo") - .arg(r##"{"a": "somevalue", "b": 42}"##) - .run_and_parse_json() - .unwrap(); - assert_eq!(v.a, "somevalue"); - assert_eq!(v.b, 42); -} -#[tokio::test] -async fn async_command_run_ext() { - use tokio::process::Command as AsyncCommand; - let mut success = AsyncCommand::new("true"); - let mut fail = AsyncCommand::new("false"); - // Run these in parallel just because we can - let (success, fail) = tokio::join!(success.run(), fail.run(),); - success.unwrap(); - assert!(fail.is_err()); + #[tokio::test] + async fn async_command_run_ext() { + use tokio::process::Command as AsyncCommand; + let mut success = AsyncCommand::new("true"); + let mut fail = AsyncCommand::new("false"); + // Run these in parallel just because we can + let (success, fail) = tokio::join!(success.run(), fail.run(),); + success.unwrap(); + assert!(fail.is_err()); + } }