vuls: init at 0.27.0
[NixPkgs.git] / lib / tests / filesystem.sh
blob7e7e03bc667d213f4e9ed1c3da99aba85c3db384
1 #!/usr/bin/env bash
3 # Tests lib/filesystem.nix
4 # Run:
5 # [nixpkgs]$ lib/tests/filesystem.sh
6 # or:
7 # [nixpkgs]$ nix-build lib/tests/release.nix
9 set -euo pipefail
10 shopt -s inherit_errexit
12 # Use
13 # || die
14 die() {
15 echo >&2 "test case failed: " "$@"
16 exit 1
19 if test -n "${TEST_LIB:-}"; then
20 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
21 else
22 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
24 export NIX_PATH
26 work="$(mktemp -d)"
27 clean_up() {
28 rm -rf "$work"
30 trap clean_up EXIT
31 cd "$work"
33 mkdir directory
34 touch regular
35 ln -s target symlink
36 mkfifo fifo
38 expectSuccess() {
39 local expr=$1
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"
50 expectFailure() {
51 local expr=$1
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"
90 echo >&2 tests ok