lib.path: Minor improvements
- Use isValid when possible instead of subpathInvalidReason: https://github.com/NixOS/nixpkgs/pull/209099#discussion_r1068714681 - Add documentation to function arguments - Use newlines for error messages: https://github.com/NixOS/nixpkgs/pull/208887#discussion_r1069737602 - Add short comments for the unit test groups: https://github.com/NixOS/nixpkgs/pull/208887#discussion_r1072913051 - Slight formatting improvement for laws: https://github.com/NixOS/nixpkgs/pull/209099#discussion_r1068707955
This commit is contained in:
@@ -25,6 +25,10 @@ let
|
|||||||
assertMsg
|
assertMsg
|
||||||
;
|
;
|
||||||
|
|
||||||
|
inherit (lib.path.subpath)
|
||||||
|
isValid
|
||||||
|
;
|
||||||
|
|
||||||
# Return the reason why a subpath is invalid, or `null` if it's valid
|
# Return the reason why a subpath is invalid, or `null` if it's valid
|
||||||
subpathInvalidReason = value:
|
subpathInvalidReason = value:
|
||||||
if ! isString value then
|
if ! isString value then
|
||||||
@@ -133,7 +137,9 @@ in /* No rec! Add dependencies on this file at the top. */ {
|
|||||||
subpath.isValid "./foo//bar/"
|
subpath.isValid "./foo//bar/"
|
||||||
=> true
|
=> true
|
||||||
*/
|
*/
|
||||||
subpath.isValid = value:
|
subpath.isValid =
|
||||||
|
# The value to check
|
||||||
|
value:
|
||||||
subpathInvalidReason value == null;
|
subpathInvalidReason value == null;
|
||||||
|
|
||||||
|
|
||||||
@@ -150,11 +156,11 @@ in /* No rec! Add dependencies on this file at the top. */ {
|
|||||||
|
|
||||||
Laws:
|
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
|
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})
|
subpath.normalise p != subpath.normalise q -> $(realpath ${p}) != $(realpath ${q})
|
||||||
|
|
||||||
@@ -210,9 +216,12 @@ in /* No rec! Add dependencies on this file at the top. */ {
|
|||||||
subpath.normalise "/foo"
|
subpath.normalise "/foo"
|
||||||
=> <error>
|
=> <error>
|
||||||
*/
|
*/
|
||||||
subpath.normalise = path:
|
subpath.normalise =
|
||||||
assert assertMsg (subpathInvalidReason path == null)
|
# The subpath string to normalise
|
||||||
"lib.path.subpath.normalise: Argument is not a valid subpath string: ${subpathInvalidReason path}";
|
subpath:
|
||||||
joinRelPath (splitRelPath path);
|
assert assertMsg (isValid subpath) ''
|
||||||
|
lib.path.subpath.normalise: Argument is not a valid subpath string:
|
||||||
|
${subpathInvalidReason subpath}'';
|
||||||
|
joinRelPath (splitRelPath subpath);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ let
|
|||||||
inherit (lib.path) subpath;
|
inherit (lib.path) subpath;
|
||||||
|
|
||||||
cases = lib.runTests {
|
cases = lib.runTests {
|
||||||
|
# Test examples from the lib.path.subpath.isValid documentation
|
||||||
testSubpathIsValidExample1 = {
|
testSubpathIsValidExample1 = {
|
||||||
expr = subpath.isValid null;
|
expr = subpath.isValid null;
|
||||||
expected = false;
|
expected = false;
|
||||||
@@ -30,6 +31,7 @@ let
|
|||||||
expr = subpath.isValid "./foo//bar/";
|
expr = subpath.isValid "./foo//bar/";
|
||||||
expected = true;
|
expected = true;
|
||||||
};
|
};
|
||||||
|
# Some extra tests
|
||||||
testSubpathIsValidTwoDotsEnd = {
|
testSubpathIsValidTwoDotsEnd = {
|
||||||
expr = subpath.isValid "foo/..";
|
expr = subpath.isValid "foo/..";
|
||||||
expected = false;
|
expected = false;
|
||||||
@@ -71,6 +73,7 @@ let
|
|||||||
expected = true;
|
expected = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Test examples from the lib.path.subpath.normalise documentation
|
||||||
testSubpathNormaliseExample1 = {
|
testSubpathNormaliseExample1 = {
|
||||||
expr = subpath.normalise "foo//bar";
|
expr = subpath.normalise "foo//bar";
|
||||||
expected = "./foo/bar";
|
expected = "./foo/bar";
|
||||||
@@ -107,6 +110,7 @@ let
|
|||||||
expr = (builtins.tryEval (subpath.normalise "/foo")).success;
|
expr = (builtins.tryEval (subpath.normalise "/foo")).success;
|
||||||
expected = false;
|
expected = false;
|
||||||
};
|
};
|
||||||
|
# Some extra tests
|
||||||
testSubpathNormaliseIsValidDots = {
|
testSubpathNormaliseIsValidDots = {
|
||||||
expr = subpath.normalise "./foo/.bar/.../baz...qux";
|
expr = subpath.normalise "./foo/.bar/.../baz...qux";
|
||||||
expected = "./foo/.bar/.../baz...qux";
|
expected = "./foo/.bar/.../baz...qux";
|
||||||
|
|||||||
Reference in New Issue
Block a user