about summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-12-08 22:59:40 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-12-08 23:15:13 +0100
commit72bd4bbb58f6867a00af2a0ddaae2d601c1ee2c7 (patch)
tree963c7cc24a9c02e49450428aea9b6a0bc588d832 /lib/tests/misc.nix
parent50793752a71b35bc0dab203db7d6ad18281e2e1e (diff)
lib.attrsets.longestValidPathPrefix: init
Allows finding the most specific path that exists.
This is useful for error messages relating to attribute paths.
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 608af656d02c0..b97f080cca3a8 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -697,6 +697,46 @@ runTests {
     expected = false;
   };
 
+  testLongestValidPathPrefix_empty_empty = {
+    expr = attrsets.longestValidPathPrefix [ ] { };
+    expected = [ ];
+  };
+
+  testLongestValidPathPrefix_empty_nonStrict = {
+    expr = attrsets.longestValidPathPrefix [ ] (throw "do not use");
+    expected = [ ];
+  };
+
+  testLongestValidPathPrefix_zero = {
+    expr = attrsets.longestValidPathPrefix [ "a" (throw "do not use") ] { d = null; };
+    expected = [ ];
+  };
+
+  testLongestValidPathPrefix_zero_b = {
+    expr = attrsets.longestValidPathPrefix [ "z" "z" ] "remarkably harmonious";
+    expected = [ ];
+  };
+
+  testLongestValidPathPrefix_one = {
+    expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a = null; };
+    expected = [ "a" ];
+  };
+
+  testLongestValidPathPrefix_two = {
+    expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b = null; };
+    expected = [ "a" "b" ];
+  };
+
+  testLongestValidPathPrefix_three = {
+    expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b.c = null; };
+    expected = [ "a" "b" "c" ];
+  };
+
+  testLongestValidPathPrefix_three_extra = {
+    expr = attrsets.longestValidPathPrefix [ "a" "b" "c" ] { a.b.c.d = throw "nope"; };
+    expected = [ "a" "b" "c" ];
+  };
+
   testFindFirstIndexExample1 = {
     expr = lists.findFirstIndex (x: x > 3) (abort "index found, so a default must not be evaluated") [ 1 6 4 ];
     expected = 1;