2021-06-28 23:13:55 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-08-04 22:07:22 +00:00
|
|
|
|
|
|
|
# Tests lib/sources.nix
|
|
|
|
# Run:
|
|
|
|
# [nixpkgs]$ lib/tests/sources.sh
|
|
|
|
# or:
|
|
|
|
# [nixpkgs]$ nix-build lib/tests/release.nix
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
set -euo pipefail
|
2021-12-06 16:07:01 +00:00
|
|
|
shopt -s inherit_errexit
|
2021-06-28 23:13:55 +00:00
|
|
|
|
|
|
|
# Use
|
|
|
|
# || die
|
|
|
|
die() {
|
|
|
|
echo >&2 "test case failed: " "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if test -n "${TEST_LIB:-}"; then
|
2021-12-06 16:07:01 +00:00
|
|
|
NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
|
2021-06-28 23:13:55 +00:00
|
|
|
else
|
2021-12-06 16:07:01 +00:00
|
|
|
NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
|
2021-06-28 23:13:55 +00:00
|
|
|
fi
|
2021-12-06 16:07:01 +00:00
|
|
|
export NIX_PATH
|
2021-06-28 23:13:55 +00:00
|
|
|
|
|
|
|
work="$(mktemp -d)"
|
|
|
|
clean_up() {
|
|
|
|
rm -rf "$work"
|
|
|
|
}
|
|
|
|
trap clean_up EXIT
|
2021-12-06 16:07:01 +00:00
|
|
|
cd "$work"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
# 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
|
|
|
|
}
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
touch {README.md,module.o,foo.bar}
|
2021-06-28 23:13:55 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
|
2021-06-28 23:13:55 +00:00
|
|
|
cleanSource ./.
|
2023-07-15 17:15:38 +00:00
|
|
|
}")' | crudeUnquoteJSON)"
|
2021-12-06 16:07:01 +00:00
|
|
|
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
|
2021-06-28 23:13:55 +00:00
|
|
|
.
|
|
|
|
./foo.bar
|
|
|
|
./README.md
|
|
|
|
EOF
|
|
|
|
) || die "cleanSource 1"
|
|
|
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
|
2021-06-28 23:13:55 +00:00
|
|
|
cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
|
2023-07-15 17:15:38 +00:00
|
|
|
}")' | crudeUnquoteJSON)"
|
2021-12-06 16:07:01 +00:00
|
|
|
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
|
2021-06-28 23:13:55 +00:00
|
|
|
.
|
|
|
|
./module.o
|
|
|
|
./README.md
|
|
|
|
EOF
|
|
|
|
) || die "cleanSourceWith 1"
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
|
2021-06-28 23:13:55 +00:00
|
|
|
cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
|
2023-07-15 17:15:38 +00:00
|
|
|
}")' | crudeUnquoteJSON)"
|
2021-12-06 16:07:01 +00:00
|
|
|
(cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
|
2021-06-28 23:13:55 +00:00
|
|
|
.
|
|
|
|
./README.md
|
|
|
|
EOF
|
|
|
|
) || die "cleanSourceWith + cleanSource"
|
|
|
|
|
|
|
|
echo >&2 tests ok
|