about summary refs log tree commit diff
path: root/lib/path
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-10 21:16:25 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-26 23:29:44 +0200
commit4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3 (patch)
tree1b2e71f9b1e938afd03230f31c9d45e246ea989f /lib/path
parent4dc542d80149a284637a46df2a1f2e4b066cd219 (diff)
downloadnixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar.gz
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar.bz2
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar.lz
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar.xz
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.tar.zst
nixlib-4e14f5fee6a68ee5fb56d07e70e86fcfcebdc7d3.zip
lib.path.subpath.components: init
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Diffstat (limited to 'lib/path')
-rw-r--r--lib/path/default.nix31
-rw-r--r--lib/path/tests/unit.nix13
2 files changed, 44 insertions, 0 deletions
diff --git a/lib/path/default.nix b/lib/path/default.nix
index 936e9b030253..181124c179a9 100644
--- a/lib/path/default.nix
+++ b/lib/path/default.nix
@@ -336,6 +336,37 @@ in /* No rec! Add dependencies on this file at the top. */ {
               ${subpathInvalidReason path}''
       ) 0 subpaths;
 
+  /*
+  Split [a subpath](#function-library-lib.path.subpath.isValid) into its path component strings.
+  Throw an error if the subpath isn't valid.
+  Note that the returned path components are also valid subpath strings, though they are intentionally not [normalised](#function-library-lib.path.subpath.normalise).
+
+  Laws:
+
+  - Splitting a subpath into components and [joining](#function-library-lib.path.subpath.join) the components gives the same subpath but [normalised](#function-library-lib.path.subpath.normalise):
+
+        subpath.join (subpath.components s) == subpath.normalise s
+
+  Type:
+    subpath.components :: String -> [ String ]
+
+  Example:
+    subpath.components "."
+    => [ ]
+
+    subpath.components "./foo//bar/./baz/"
+    => [ "foo" "bar" "baz" ]
+
+    subpath.components "/foo"
+    => <error>
+  */
+  subpath.components =
+    subpath:
+    assert assertMsg (isValid subpath) ''
+      lib.path.subpath.components: Argument is not a valid subpath string:
+          ${subpathInvalidReason subpath}'';
+    splitRelPath subpath;
+
   /* Normalise a subpath. Throw an error if the subpath isn't valid, see
   `lib.path.subpath.isValid`
 
diff --git a/lib/path/tests/unit.nix b/lib/path/tests/unit.nix
index 9c5b752cf64a..fc5a84493adb 100644
--- a/lib/path/tests/unit.nix
+++ b/lib/path/tests/unit.nix
@@ -204,6 +204,19 @@ let
       expr = (builtins.tryEval (subpath.normalise "..")).success;
       expected = false;
     };
+
+    testSubpathComponentsExample1 = {
+      expr = subpath.components ".";
+      expected = [ ];
+    };
+    testSubpathComponentsExample2 = {
+      expr = subpath.components "./foo//bar/./baz/";
+      expected = [ "foo" "bar" "baz" ];
+    };
+    testSubpathComponentsExample3 = {
+      expr = (builtins.tryEval (subpath.components "/foo")).success;
+      expected = false;
+    };
   };
 in
   if cases == [] then "Unit tests successful"