at 23.11-beta 2.8 kB view raw
1#!/usr/bin/env bash 2 3# Tests lib/filesystem.nix 4# Run: 5# [nixpkgs]$ lib/tests/filesystem.sh 6# or: 7# [nixpkgs]$ nix-build lib/tests/release.nix 8 9set -euo pipefail 10shopt -s inherit_errexit 11 12# Use 13# || die 14die() { 15 echo >&2 "test case failed: " "$@" 16 exit 1 17} 18 19if test -n "${TEST_LIB:-}"; then 20 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")" 21else 22 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)" 23fi 24export NIX_PATH 25 26work="$(mktemp -d)" 27clean_up() { 28 rm -rf "$work" 29} 30trap clean_up EXIT 31cd "$work" 32 33mkdir directory 34touch regular 35ln -s target symlink 36mkfifo fifo 37 38expectSuccess() { 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" 44 fi 45 if [[ ! "$result" =~ $expectedResultRegex ]]; then 46 die "$expr == $result, but $expectedResultRegex was expected" 47 fi 48} 49 50expectFailure() { 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" 56 fi 57 if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then 58 die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected" 59 fi 60} 61 62expectSuccess "pathType /." '"directory"' 63expectSuccess "pathType $PWD/directory" '"directory"' 64expectSuccess "pathType $PWD/regular" '"regular"' 65expectSuccess "pathType $PWD/symlink" '"symlink"' 66expectSuccess "pathType $PWD/fifo" '"unknown"' 67 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. 71if [[ "$(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.'" 74fi 75 76expectSuccess "pathIsDirectory /." "true" 77expectSuccess "pathIsDirectory $PWD/directory" "true" 78expectSuccess "pathIsDirectory $PWD/regular" "false" 79expectSuccess "pathIsDirectory $PWD/symlink" "false" 80expectSuccess "pathIsDirectory $PWD/fifo" "false" 81expectSuccess "pathIsDirectory $PWD/non-existent" "false" 82 83expectSuccess "pathIsRegularFile /." "false" 84expectSuccess "pathIsRegularFile $PWD/directory" "false" 85expectSuccess "pathIsRegularFile $PWD/regular" "true" 86expectSuccess "pathIsRegularFile $PWD/symlink" "false" 87expectSuccess "pathIsRegularFile $PWD/fifo" "false" 88expectSuccess "pathIsRegularFile $PWD/non-existent" "false" 89 90echo >&2 tests ok