about summary refs log tree commit diff
path: root/pkgs/profpatsch
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2021-02-20 18:41:33 +0100
committersternenseemann <sternenseemann@systemli.org>2021-02-25 19:52:33 +0100
commit6bb8608948484f37b6f963219d0e8371c506e01d (patch)
treedcae80070cc36669f80d40297a1b737ad6df7037 /pkgs/profpatsch
parentfff161ae7928f73fdd65dafe40e6dc7553e61e65 (diff)
pkgs/profpatsch/nman: unit test testable stuff
This is parsing and rendering of stuff currently mostly, since our main
logic is relatively interwoven with IO stuff (to avoid copying stuff a
lot). This is fine however as the application logic is tested using the
nixos vm test we also have!
Diffstat (limited to 'pkgs/profpatsch')
-rw-r--r--pkgs/profpatsch/default.nix2
-rw-r--r--pkgs/profpatsch/nman/default.nix5
-rw-r--r--pkgs/profpatsch/nman/nman.rs84
3 files changed, 87 insertions, 4 deletions
diff --git a/pkgs/profpatsch/default.nix b/pkgs/profpatsch/default.nix
index ff90a05f..0d2a593a 100644
--- a/pkgs/profpatsch/default.nix
+++ b/pkgs/profpatsch/default.nix
@@ -128,7 +128,7 @@ in rec {
   git-commit-index = callPackage ./git-commit-index { inherit script; };
   nix-http-serve = callPackage ./nix-http-serve {};
   nman = callPackage ./nman {
-    inherit writeRustSimpleBin;
+    inherit writeRustSimpleBin testRustSimple;
     inherit (sternenseemann) temp;
   };
   sfttime = callPackage ./sfttime {};
diff --git a/pkgs/profpatsch/nman/default.nix b/pkgs/profpatsch/nman/default.nix
index 31461999..f03c4502 100644
--- a/pkgs/profpatsch/nman/default.nix
+++ b/pkgs/profpatsch/nman/default.nix
@@ -1,9 +1,10 @@
 { lib
 , writeRustSimpleBin
+, testRustSimple
 , temp
 }:
 
-writeRustSimpleBin "nman" {
+testRustSimple (writeRustSimpleBin "nman" {
   meta = {
     license = lib.licenses.gpl3Only;
     description = "Open man page in a temporary nix-shell";
@@ -14,4 +15,4 @@ writeRustSimpleBin "nman" {
   postInstall = ''
     install -Dm644 ${./nman.1} "$out/share/man/man1/nman.1"
   '';
-} ./nman.rs
+} ./nman.rs)
diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs
index 234057b4..bc7810ed 100644
--- a/pkgs/profpatsch/nman/nman.rs
+++ b/pkgs/profpatsch/nman/nman.rs
@@ -83,7 +83,7 @@ impl NmanError<'_> {
 /// outputs is to order them from most
 /// likely to least likely to contain man
 /// pages to save on realizing store paths.
-#[derive(PartialEq, PartialOrd, Eq, Ord)]
+#[derive(PartialEq, PartialOrd, Eq, Ord, Debug)]
 enum DrvOutput<'a> {
     Man,
     DevMan,
@@ -118,6 +118,7 @@ impl<'a> DrvOutput<'a> {
 /// A derivation represented as a path
 /// coupled with a parsed [`DrvOutput`]
 /// for sorting purposes.
+#[derive (Debug, PartialEq, Eq)]
 struct DrvWithOutput<'a> {
     /// The original derivation path as printed
     /// by `nix-instantiate` _including_ the output
@@ -390,3 +391,84 @@ fn main() -> std::io::Result<()> {
         },
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_section_parsing() {
+        assert!(parse_man_section("1").is_ok());
+        assert!(parse_man_section("2").is_ok());
+        assert!(parse_man_section("3").is_ok());
+        assert!(parse_man_section("3p").is_ok());
+        assert!(parse_man_section("4").is_ok());
+        assert!(parse_man_section("5").is_ok());
+        assert!(parse_man_section("6").is_ok());
+        assert!(parse_man_section("7").is_ok());
+        assert!(parse_man_section("8").is_ok());
+        assert!(parse_man_section("9").is_ok());
+
+        assert!(!parse_man_section("man").is_ok());
+        assert!(!parse_man_section("ocamlPackages.sexp").is_ok());
+        assert!(!parse_man_section("lowdown").is_ok());
+    }
+
+    #[test]
+    fn test_output_preference() {
+        // lower =^= preferred
+        assert!(DrvOutput::Man    < DrvOutput::Out);
+        assert!(DrvOutput::DevMan < DrvOutput::Out);
+        assert!(DrvOutput::Out    < DrvOutput::Doc);
+        assert!(DrvOutput::Out    < DrvOutput::DevDoc);
+        assert!(DrvOutput::Out    < DrvOutput::Lib);
+        assert!(DrvOutput::Out    < DrvOutput::Bin);
+        assert!(DrvOutput::Out    < DrvOutput::Other(b"foo"));
+    }
+
+    const OUT: &[u8] = b"/nix/store/3v06l2clmzxx4pna0yd0wiggqlh7b33s-lowdown-0.8.1.drv";
+    const DEVMAN: &[u8] = b"/nix/store/1ilsvw0y81mi8rdz2jp5kng2wakg2mq8-libunwind-1.4.0.drv!devman";
+
+    #[test]
+    fn test_drv_path_parsing() {
+        assert_eq!(
+            DrvWithOutput::parse(OUT),
+            Some(DrvWithOutput {
+                path: OUT,
+                output: DrvOutput::Out,
+            })
+        );
+
+        assert_eq!(
+            DrvWithOutput::parse(DEVMAN),
+            Some(DrvWithOutput {
+                path: DEVMAN,
+                output: DrvOutput::DevMan,
+            })
+        );
+    }
+
+    #[test]
+    fn test_drv_path_rendering() {
+        let mut expected_out_path = Vec::from(OUT);
+        expected_out_path.extend(b"!out");
+
+        let out = DrvWithOutput {
+            path: OUT,
+            output: DrvOutput::Out,
+        };
+        assert_eq!(
+            out.render().as_os_str(),
+            OsStr::from_bytes(&expected_out_path[..])
+        );
+
+        let devman = DrvWithOutput {
+            path: DEVMAN,
+            output: DrvOutput::DevMan,
+        };
+        assert_eq!(
+            devman.render().as_os_str(),
+            OsStr::from_bytes(DEVMAN)
+        );
+    }
+}