lib/tests/filesystem.sh: Check success and failure separately
This commit is contained in:
parent
29124b85cf
commit
de0c11241f
|
@ -35,59 +35,50 @@ touch regular
|
||||||
ln -s target symlink
|
ln -s target symlink
|
||||||
mkfifo fifo
|
mkfifo fifo
|
||||||
|
|
||||||
checkPathType() {
|
expectSuccess() {
|
||||||
local path=$1
|
local expr=$1
|
||||||
local expectedPathType=$2
|
local expectedResultRegex=$2
|
||||||
local actualPathType=$(nix-instantiate --eval --strict --json 2>&1 \
|
if ! result=$(nix-instantiate --eval --strict --json \
|
||||||
-E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathType path' \
|
--expr "with (import <nixpkgs/lib>).filesystem; $expr"); then
|
||||||
--argstr path "$path")
|
die "$expr failed to evaluate, but it was expected to succeed"
|
||||||
if [[ ! "$actualPathType" =~ $expectedPathType ]]; then
|
fi
|
||||||
die "lib.filesystem.pathType \"$path\" == $actualPathType, but $expectedPathType was expected"
|
if [[ ! "$result" =~ $expectedResultRegex ]]; then
|
||||||
|
die "$expr == $result, but $expectedResultRegex was expected"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
checkPathType "/" '"directory"'
|
expectFailure() {
|
||||||
checkPathType "$PWD/directory" '"directory"'
|
local expr=$1
|
||||||
checkPathType "$PWD/regular" '"regular"'
|
local expectedErrorRegex=$2
|
||||||
checkPathType "$PWD/symlink" '"symlink"'
|
if result=$(nix-instantiate --eval --strict --json 2>"$work/stderr" \
|
||||||
checkPathType "$PWD/fifo" '"unknown"'
|
--expr "with (import <nixpkgs/lib>).filesystem; $expr"); then
|
||||||
|
die "$expr evaluated successfully to $result, but it was expected to fail"
|
||||||
|
fi
|
||||||
|
if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
|
||||||
|
die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
expectSuccess "pathType /." '"directory"'
|
||||||
|
expectSuccess "pathType $PWD/directory" '"directory"'
|
||||||
|
expectSuccess "pathType $PWD/regular" '"regular"'
|
||||||
|
expectSuccess "pathType $PWD/symlink" '"symlink"'
|
||||||
|
expectSuccess "pathType $PWD/fifo" '"unknown"'
|
||||||
# Different errors depending on whether the builtins.readFilePath primop is available or not
|
# Different errors depending on whether the builtins.readFilePath primop is available or not
|
||||||
checkPathType "$PWD/non-existent" "error: (evaluation aborted with the following error message: 'lib.filesystem.pathType: Path $PWD/non-existent does not exist.'|getting status of '$PWD/non-existent': No such file or directory)"
|
expectFailure "pathType $PWD/non-existent" "error: (evaluation aborted with the following error message: 'lib.filesystem.pathType: Path $PWD/non-existent does not exist.'|getting status of '$PWD/non-existent': No such file or directory)"
|
||||||
|
|
||||||
checkPathIsDirectory() {
|
expectSuccess "pathIsDirectory /." "true"
|
||||||
local path=$1
|
expectSuccess "pathIsDirectory $PWD/directory" "true"
|
||||||
local expectedIsDirectory=$2
|
expectSuccess "pathIsDirectory $PWD/regular" "false"
|
||||||
local actualIsDirectory=$(nix-instantiate --eval --strict --json 2>&1 \
|
expectSuccess "pathIsDirectory $PWD/symlink" "false"
|
||||||
-E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathIsDirectory path' \
|
expectSuccess "pathIsDirectory $PWD/fifo" "false"
|
||||||
--argstr path "$path")
|
expectSuccess "pathIsDirectory $PWD/non-existent" "false"
|
||||||
if [[ "$actualIsDirectory" != "$expectedIsDirectory" ]]; then
|
|
||||||
die "lib.filesystem.pathIsDirectory \"$path\" == $actualIsDirectory, but $expectedIsDirectory was expected"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
checkPathIsDirectory "/" "true"
|
expectSuccess "pathIsRegularFile /." "false"
|
||||||
checkPathIsDirectory "$PWD/directory" "true"
|
expectSuccess "pathIsRegularFile $PWD/directory" "false"
|
||||||
checkPathIsDirectory "$PWD/regular" "false"
|
expectSuccess "pathIsRegularFile $PWD/regular" "true"
|
||||||
checkPathIsDirectory "$PWD/symlink" "false"
|
expectSuccess "pathIsRegularFile $PWD/symlink" "false"
|
||||||
checkPathIsDirectory "$PWD/fifo" "false"
|
expectSuccess "pathIsRegularFile $PWD/fifo" "false"
|
||||||
checkPathIsDirectory "$PWD/non-existent" "false"
|
expectSuccess "pathIsRegularFile $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
|
echo >&2 tests ok
|
||||||
|
|
Loading…
Reference in a new issue