Skip to content

Commit

Permalink
lib.path: Minor improvements
Browse files Browse the repository at this point in the history
- Use isValid when possible instead of subpathInvalidReason: NixOS/nixpkgs#209099 (comment)
- Add documentation to function arguments
- Use newlines for error messages: NixOS/nixpkgs#208887 (comment)
- Add short comments for the unit test groups: NixOS/nixpkgs#208887 (comment)
- Slight formatting improvement for laws: NixOS/nixpkgs#209099 (comment)
  • Loading branch information
infinisil committed Jan 18, 2023
1 parent dd95f73 commit d935001
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/path/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ let
assertMsg
;

inherit (lib.path.subpath)
isValid
;

# Return the reason why a subpath is invalid, or `null` if it's valid
subpathInvalidReason = value:
if ! isString value then
Expand Down Expand Up @@ -133,7 +137,9 @@ in /* No rec! Add dependencies on this file at the top. */ {
subpath.isValid "./foo//bar/"
=> true
*/
subpath.isValid = value:
subpath.isValid =
# The value to check
value:
subpathInvalidReason value == null;


Expand All @@ -150,11 +156,11 @@ in /* No rec! Add dependencies on this file at the top. */ {
Laws:
- (Idempotency) Normalising multiple times gives the same result:
- Idempotency - normalising multiple times gives the same result:
subpath.normalise (subpath.normalise p) == subpath.normalise p
- (Uniqueness) There's only a single normalisation for the paths that lead to the same file system node:
- Uniqueness - there's only a single normalisation for the paths that lead to the same file system node:
subpath.normalise p != subpath.normalise q -> $(realpath ${p}) != $(realpath ${q})
Expand Down Expand Up @@ -210,9 +216,12 @@ in /* No rec! Add dependencies on this file at the top. */ {
subpath.normalise "/foo"
=> <error>
*/
subpath.normalise = path:
assert assertMsg (subpathInvalidReason path == null)
"lib.path.subpath.normalise: Argument is not a valid subpath string: ${subpathInvalidReason path}";
joinRelPath (splitRelPath path);
subpath.normalise =
# The subpath string to normalise
subpath:
assert assertMsg (isValid subpath) ''
lib.path.subpath.normalise: Argument is not a valid subpath string:
${subpathInvalidReason subpath}'';
joinRelPath (splitRelPath subpath);

}
4 changes: 4 additions & 0 deletions lib/path/tests/unit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let
inherit (lib.path) subpath;

cases = lib.runTests {
# Test examples from the lib.path.subpath.isValid documentation
testSubpathIsValidExample1 = {
expr = subpath.isValid null;
expected = false;
Expand All @@ -30,6 +31,7 @@ let
expr = subpath.isValid "./foo//bar/";
expected = true;
};
# Some extra tests
testSubpathIsValidTwoDotsEnd = {
expr = subpath.isValid "foo/..";
expected = false;
Expand Down Expand Up @@ -71,6 +73,7 @@ let
expected = true;
};

# Test examples from the lib.path.subpath.normalise documentation
testSubpathNormaliseExample1 = {
expr = subpath.normalise "foo//bar";
expected = "./foo/bar";
Expand Down Expand Up @@ -107,6 +110,7 @@ let
expr = (builtins.tryEval (subpath.normalise "/foo")).success;
expected = false;
};
# Some extra tests
testSubpathNormaliseIsValidDots = {
expr = subpath.normalise "./foo/.bar/.../baz...qux";
expected = "./foo/.bar/.../baz...qux";
Expand Down

0 comments on commit d935001

Please sign in to comment.