1#!/usr/bin/env bash
2
3# Tests lib/sources.nix
4# Run:
5# [nixpkgs]$ lib/tests/sources.sh
6# or:
7# [nixpkgs]$ nix-build lib/tests/release.nix
8
9set -euo pipefail
10shopt -s inherit_errexit
11
12# Use
13# || die
14die() {
15 echo >&2 "test case failed: " "$@"
16 exit 1
17}
18
19if test -n "${TEST_LIB:-}"; then
20 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
21else
22 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
23fi
24export NIX_PATH
25
26work="$(mktemp -d)"
27clean_up() {
28 rm -rf "$work"
29}
30trap clean_up EXIT
31cd "$work"
32
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
37crudeUnquoteJSON() {
38 cut -d \" -f2
39}
40
41touch {README.md,module.o,foo.bar}
42
43dir="$(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
47.
48./foo.bar
49./README.md
50EOF
51) || die "cleanSource 1"
52
53
54dir="$(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
58.
59./module.o
60./README.md
61EOF
62) || die "cleanSourceWith 1"
63
64dir="$(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
68.
69./README.md
70EOF
71) || die "cleanSourceWith + cleanSource"
72
73echo >&2 tests ok