From 5174c2163730c683ca5c2a01be537c38173bab33 Mon Sep 17 00:00:00 2001 From: sterni Date: Sun, 9 Oct 2022 11:22:00 +0200 Subject: fix(nix/tag): correct no match check in discr It uses discrDef internally, but passes `null` as the default tag name, causing Nix to drop the attribute and return an empty attribute set if the default case is hit. Consequently we need to check for the empty attribute set, not `null` to figure out if there was no match found. We can also test this behavior using `assertThrows` which was introduced after the tag library was originally written. Change-Id: I45adb2f9602762dfc867956323fb3f5ae4c8bd1d Reviewed-on: https://cl.tvl.fyi/c/depot/+/6904 Autosubmit: sterni Reviewed-by: Profpatsch Tested-by: BuildkiteCI --- nix/tag/default.nix | 2 +- nix/tag/tests.nix | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/nix/tag/default.nix b/nix/tag/default.nix index 0038404460..2955656323 100644 --- a/nix/tag/default.nix +++ b/nix/tag/default.nix @@ -78,7 +78,7 @@ let # Like `discrDef`, but fail if there is no match. discr = fs: v: let res = discrDef null fs v; in - assert lib.assertMsg (res != null) + assert lib.assertMsg (res != { }) "tag.discr: No predicate found that matches ${lib.generators.toPretty {} v}"; res; diff --git a/nix/tag/tests.nix b/nix/tag/tests.nix index bcc42c758a..e0085b4837 100644 --- a/nix/tag/tests.nix +++ b/nix/tag/tests.nix @@ -4,6 +4,7 @@ let inherit (depot.nix.runTestsuite) runTestsuite assertEq + assertThrows it ; @@ -50,6 +51,10 @@ let { int = lib.isInt; } ] "foo") { def = "foo"; }) + (assertThrows "throws failing to match" + (discr [ + { fish = x: x == 42; } + ] 21)) ]; match-test = it "can match things" [ -- cgit 1.4.1