summary refs log tree commit diff
path: root/lib/tests/filesystem.sh
blob: 61710da92ba2d7ef10509d7feb957bfbdc076874 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash

# Tests lib/filesystem.nix
# Run:
# [nixpkgs]$ lib/tests/filesystem.sh
# or:
# [nixpkgs]$ nix-build lib/tests/release.nix

set -euo pipefail
shopt -s inherit_errexit

# Use
#     || die
die() {
  echo >&2 "test case failed: " "$@"
  exit 1
}

if test -n "${TEST_LIB:-}"; then
  NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
else
  NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
fi
export NIX_PATH

work="$(mktemp -d)"
clean_up() {
  rm -rf "$work"
}
trap clean_up EXIT
cd "$work"

mkdir directory
touch regular
ln -s target symlink
mkfifo fifo

checkPathType() {
    local path=$1
    local expectedPathType=$2
    local actualPathType=$(nix-instantiate --eval --strict --json 2>&1 \
        -E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathType path' \
        --argstr path "$path")
    if [[ "$actualPathType" != "$expectedPathType" ]]; then
        die "lib.filesystem.pathType \"$path\" == $actualPathType, but $expectedPathType was expected"
    fi
}

checkPathType "/" '"directory"'
checkPathType "$PWD/directory" '"directory"'
checkPathType "$PWD/regular" '"regular"'
checkPathType "$PWD/symlink" '"symlink"'
checkPathType "$PWD/fifo" '"unknown"'

checkPathIsDirectory() {
    local path=$1
    local expectedIsDirectory=$2
    local actualIsDirectory=$(nix-instantiate --eval --strict --json 2>&1 \
        -E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathIsDirectory path' \
        --argstr path "$path")
    if [[ "$actualIsDirectory" != "$expectedIsDirectory" ]]; then
        die "lib.filesystem.pathIsDirectory \"$path\" == $actualIsDirectory, but $expectedIsDirectory was expected"
    fi
}

checkPathIsDirectory "/" "true"
checkPathIsDirectory "$PWD/directory" "true"
checkPathIsDirectory "$PWD/regular" "false"
checkPathIsDirectory "$PWD/symlink" "false"
checkPathIsDirectory "$PWD/fifo" "false"
checkPathIsDirectory "$PWD/non-existent" "false"

checkPathIsRegularFile() {
    local path=$1
    local expectedIsRegularFile=$2
    local actualIsRegularFile=$(nix-instantiate --eval --strict --json 2>&1 \
        -E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathIsRegularFile path' \
        --argstr path "$path")
    if [[ "$actualIsRegularFile" != "$expectedIsRegularFile" ]]; then
        die "lib.filesystem.pathIsRegularFile \"$path\" == $actualIsRegularFile, but $expectedIsRegularFile was expected"
    fi
}

checkPathIsRegularFile "/" "false"
checkPathIsRegularFile "$PWD/directory" "false"
checkPathIsRegularFile "$PWD/regular" "true"
checkPathIsRegularFile "$PWD/symlink" "false"
checkPathIsRegularFile "$PWD/fifo" "false"
checkPathIsRegularFile "$PWD/non-existent" "false"

echo >&2 tests ok