3 # Tests lib/filesystem.nix
5 # [nixpkgs]$ lib/tests/filesystem.sh
7 # [nixpkgs]$ nix-build lib/tests/release.nix
10 shopt -s inherit_errexit
15 echo >&2 "test case failed: " "$@"
19 if test -n "${TEST_LIB:-}"; then
20 NIX_PATH
=nixpkgs
="$(dirname "$TEST_LIB")"
22 NIX_PATH
=nixpkgs
="$(cd "$
(dirname "${BASH_SOURCE[0]}")/..
/..
"; pwd)"
40 local expectedResultRegex
=$2
41 if ! result
=$
(nix-instantiate
--eval --strict --json \
42 --expr "with (import <nixpkgs/lib>).filesystem; $expr"); then
43 die
"$expr failed to evaluate, but it was expected to succeed"
45 if [[ ! "$result" =~
$expectedResultRegex ]]; then
46 die
"$expr == $result, but $expectedResultRegex was expected"
52 local expectedErrorRegex
=$2
53 if result
=$
(nix-instantiate
--eval --strict --json 2>"$work/stderr" \
54 --expr "with (import <nixpkgs/lib>).filesystem; $expr"); then
55 die
"$expr evaluated successfully to $result, but it was expected to fail"
57 if [[ ! "$(<"$work/stderr
")" =~
$expectedErrorRegex ]]; then
58 die
"Error was $(<"$work/stderr
"), but $expectedErrorRegex was expected"
62 expectSuccess
"pathType /." '"directory"'
63 expectSuccess
"pathType $PWD/directory" '"directory"'
64 expectSuccess
"pathType $PWD/regular" '"regular"'
65 expectSuccess
"pathType $PWD/symlink" '"symlink"'
66 expectSuccess
"pathType $PWD/fifo" '"unknown"'
68 # Only check error message when a Nixpkgs-specified error is thrown,
69 # which is only the case when `readFileType` is not available
70 # and the fallback implementation needs to be used.
71 if [[ "$(nix-instantiate --eval --expr 'builtins ? readFileType')" == false
]]; then
72 expectFailure
"pathType $PWD/non-existent" \
73 "error: evaluation aborted with the following error message: 'lib.filesystem.pathType: Path $PWD/non-existent does not exist.'"
76 expectSuccess
"pathIsDirectory /." "true"
77 expectSuccess
"pathIsDirectory $PWD/directory" "true"
78 expectSuccess
"pathIsDirectory $PWD/regular" "false"
79 expectSuccess
"pathIsDirectory $PWD/symlink" "false"
80 expectSuccess
"pathIsDirectory $PWD/fifo" "false"
81 expectSuccess
"pathIsDirectory $PWD/non-existent" "false"
83 expectSuccess
"pathIsRegularFile /." "false"
84 expectSuccess
"pathIsRegularFile $PWD/directory" "false"
85 expectSuccess
"pathIsRegularFile $PWD/regular" "true"
86 expectSuccess
"pathIsRegularFile $PWD/symlink" "false"
87 expectSuccess
"pathIsRegularFile $PWD/fifo" "false"
88 expectSuccess
"pathIsRegularFile $PWD/non-existent" "false"