typos: 1.16.22 -> 1.16.23
[NixPkgs.git] / lib / tests / sources.sh
blob079c7eea56574b7e9a6662ac27f9c59272f1ba15
1 #!/usr/bin/env bash
3 # Tests lib/sources.nix
4 # Run:
5 # [nixpkgs]$ lib/tests/sources.sh
6 # or:
7 # [nixpkgs]$ nix-build lib/tests/release.nix
9 set -euo pipefail
10 shopt -s inherit_errexit
12 # Use
13 # || die
14 die() {
15 echo >&2 "test case failed: " "$@"
16 exit 1
19 if test -n "${TEST_LIB:-}"; then
20 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
21 else
22 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
24 export NIX_PATH
26 work="$(mktemp -d)"
27 clean_up() {
28 rm -rf "$work"
30 trap clean_up EXIT
31 cd "$work"
33 # Crudely unquotes a JSON string by just taking everything between the first and the second quote.
34 # We're only using this for resulting /nix/store paths, which can't contain " anyways,
35 # nor can they contain any other characters that would need to be escaped specially in JSON
36 # This way we don't need to add a dependency on e.g. jq
37 crudeUnquoteJSON() {
38 cut -d \" -f2
41 touch {README.md,module.o,foo.bar}
43 dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
44 cleanSource ./.
45 }")' | crudeUnquoteJSON)"
46 (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
48 ./foo.bar
49 ./README.md
50 EOF
51 ) || die "cleanSource 1"
54 dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
55 cleanSourceWith { src = '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
56 }")' | crudeUnquoteJSON)"
57 (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
59 ./module.o
60 ./README.md
61 EOF
62 ) || die "cleanSourceWith 1"
64 dir="$(nix-instantiate --eval --strict --read-write-mode --json --expr '(with import <nixpkgs/lib>; "${
65 cleanSourceWith { src = cleanSource '"$work"'; filter = path: type: ! hasSuffix ".bar" path; }
66 }")' | crudeUnquoteJSON)"
67 (cd "$dir"; find) | sort -f | diff -U10 - <(cat <<EOF
69 ./README.md
70 EOF
71 ) || die "cleanSourceWith + cleanSource"
73 echo >&2 tests ok