Merge pull request #234070 from tweag/pathType-tests

Init `nixVersions.minimum` and fix `lib` tests for all Nix versions
This commit is contained in:
Robert Hensing 2023-06-01 20:00:36 +02:00 committed by GitHub
commit fb21e6d7dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 97 deletions

View File

@ -35,58 +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
checkPathType "$PWD/non-existent" "error: evaluation aborted with the following error message: 'lib.filesystem.pathType: Path $PWD/non-existent does not exist.'" die "$expr evaluated successfully to $result, but it was expected to fail"
fi
checkPathIsDirectory() { if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
local path=$1 die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
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 fi
} }
checkPathIsDirectory "/" "true" expectSuccess "pathType /." '"directory"'
checkPathIsDirectory "$PWD/directory" "true" expectSuccess "pathType $PWD/directory" '"directory"'
checkPathIsDirectory "$PWD/regular" "false" expectSuccess "pathType $PWD/regular" '"regular"'
checkPathIsDirectory "$PWD/symlink" "false" expectSuccess "pathType $PWD/symlink" '"symlink"'
checkPathIsDirectory "$PWD/fifo" "false" expectSuccess "pathType $PWD/fifo" '"unknown"'
checkPathIsDirectory "$PWD/non-existent" "false" # Different errors depending on whether the builtins.readFilePath primop is available or not
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)"
checkPathIsRegularFile() { expectSuccess "pathIsDirectory /." "true"
local path=$1 expectSuccess "pathIsDirectory $PWD/directory" "true"
local expectedIsRegularFile=$2 expectSuccess "pathIsDirectory $PWD/regular" "false"
local actualIsRegularFile=$(nix-instantiate --eval --strict --json 2>&1 \ expectSuccess "pathIsDirectory $PWD/symlink" "false"
-E '{ path }: let lib = import <nixpkgs/lib>; in lib.filesystem.pathIsRegularFile path' \ expectSuccess "pathIsDirectory $PWD/fifo" "false"
--argstr path "$path") expectSuccess "pathIsDirectory $PWD/non-existent" "false"
if [[ "$actualIsRegularFile" != "$expectedIsRegularFile" ]]; then
die "lib.filesystem.pathIsRegularFile \"$path\" == $actualIsRegularFile, but $expectedIsRegularFile was expected"
fi
}
checkPathIsRegularFile "/" "false" expectSuccess "pathIsRegularFile /." "false"
checkPathIsRegularFile "$PWD/directory" "false" expectSuccess "pathIsRegularFile $PWD/directory" "false"
checkPathIsRegularFile "$PWD/regular" "true" expectSuccess "pathIsRegularFile $PWD/regular" "true"
checkPathIsRegularFile "$PWD/symlink" "false" expectSuccess "pathIsRegularFile $PWD/symlink" "false"
checkPathIsRegularFile "$PWD/fifo" "false" expectSuccess "pathIsRegularFile $PWD/fifo" "false"
checkPathIsRegularFile "$PWD/non-existent" "false" expectSuccess "pathIsRegularFile $PWD/non-existent" "false"
echo >&2 tests ok echo >&2 tests ok

View File

@ -378,7 +378,7 @@ checkConfigOutput '^{ }$' config.sub.nixosOk ./class-check.nix
checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.sub.nixosFail.config ./class-check.nix checkConfigError 'The module .*/module-class-is-darwin.nix was imported into nixos instead of darwin.' config.sub.nixosFail.config ./class-check.nix
# submoduleWith type merge with different class # submoduleWith type merge with different class
checkConfigError 'error: A submoduleWith option is declared multiple times with conflicting class values "darwin" and "nixos".' config.sub.mergeFail.config ./class-check.nix checkConfigError 'A submoduleWith option is declared multiple times with conflicting class values "darwin" and "nixos".' config.sub.mergeFail.config ./class-check.nix
# _type check # _type check
checkConfigError 'Could not load a value as a module, because it is of type "flake", in file .*/module-imports-_type-check.nix' config.ok.config ./module-imports-_type-check.nix checkConfigError 'Could not load a value as a module, because it is of type "flake", in file .*/module-imports-_type-check.nix' config.ok.config ./module-imports-_type-check.nix

View File

@ -2,9 +2,12 @@
# Don't test properties of pkgs.lib, but rather the lib in the parent directory # Don't test properties of pkgs.lib, but rather the lib in the parent directory
pkgs ? import ../.. {} // { lib = throw "pkgs.lib accessed, but the lib tests should use nixpkgs' lib path directly!"; }, pkgs ? import ../.. {} // { lib = throw "pkgs.lib accessed, but the lib tests should use nixpkgs' lib path directly!"; },
nix ? pkgs.nix, nix ? pkgs.nix,
nixVersions ? [ pkgs.nixVersions.minimum nix pkgs.nixVersions.unstable ],
}: }:
pkgs.runCommand "nixpkgs-lib-tests" { let
testWithNix = nix:
pkgs.runCommand "nixpkgs-lib-tests-nix-${nix.version}" {
buildInputs = [ buildInputs = [
(import ./check-eval.nix) (import ./check-eval.nix)
(import ./maintainers.nix { (import ./maintainers.nix {
@ -23,7 +26,7 @@ pkgs.runCommand "nixpkgs-lib-tests" {
nix nix
]; ];
strictDeps = true; strictDeps = true;
} '' } ''
datadir="${nix}/share" datadir="${nix}/share"
export TEST_ROOT=$(pwd)/test-tmp export TEST_ROOT=$(pwd)/test-tmp
export NIX_BUILD_HOOK= export NIX_BUILD_HOOK=
@ -50,5 +53,12 @@ pkgs.runCommand "nixpkgs-lib-tests" {
echo "Running lib/tests/sources.sh" echo "Running lib/tests/sources.sh"
TEST_LIB=$PWD/lib bash lib/tests/sources.sh TEST_LIB=$PWD/lib bash lib/tests/sources.sh
touch $out mkdir $out
'' echo success > $out/${nix.version}
'';
in
pkgs.symlinkJoin {
name = "nixpkgs-lib-tests";
paths = map testWithNix nixVersions;
}

View File

@ -23,14 +23,19 @@ clean_up() {
trap clean_up EXIT trap clean_up EXIT
cd "$work" cd "$work"
# Crudely unquotes a JSON string by just taking everything between the first and the second quote.
# We're only using this for resulting /nix/store paths, which can't contain " anyways,
# nor can they contain any other characters that would need to be escaped specially in JSON
# This way we don't need to add a dependency on e.g. jq
crudeUnquoteJSON() {
cut -d \" -f2
}
touch {README.md,module.o,foo.bar} touch {README.md,module.o,foo.bar}
# nix-instantiate doesn't write out the source, only computing the hash, so dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
# this uses the experimental nix command instead.
dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${
cleanSource ./. cleanSource ./.
}")')" }")' | crudeUnquoteJSON)"
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
. .
./foo.bar ./foo.bar
@ -39,9 +44,9 @@ EOF
) || die "cleanSource 1" ) || die "cleanSource 1"
dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${ dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
}")')" }")' | crudeUnquoteJSON)"
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
. .
./module.o ./module.o
@ -49,9 +54,9 @@ dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${
EOF EOF
) || die "cleanSourceWith 1" ) || die "cleanSourceWith 1"
dir="$(nix eval --impure --raw --expr '(with import <nixpkgs/lib>; "${ dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; } cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
}")')" }")' | crudeUnquoteJSON)"
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
. .
./README.md ./README.md

View File

@ -187,6 +187,23 @@ in lib.makeExtensible (self: {
hash = "sha256-KjcQkI2HgbP7KOlHxb2DvyHISQXo2OExvvjqTyK7P0o="; hash = "sha256-KjcQkI2HgbP7KOlHxb2DvyHISQXo2OExvvjqTyK7P0o=";
}; };
# The minimum Nix version supported by Nixpkgs
# Note that some functionality *might* have been backported into this Nix version,
# making this package an inaccurate representation of what features are available
# in the actual lowest minver.nix *patch* version.
minimum =
let
minver = import ../../../../lib/minver.nix;
major = lib.versions.major minver;
minor = lib.versions.minor minver;
attribute = "nix_${major}_${minor}";
nix = self.${attribute};
in
if ! self ? ${attribute} then
throw "The minimum supported Nix version is ${minver} (declared in lib/minver.nix), but pkgs.nixVersions.${attribute} does not exist."
else
nix;
stable = self.nix_2_13; stable = self.nix_2_13;
unstable = self.nix_2_16; unstable = self.nix_2_16;