Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# Run a builder, flip exit code, save log and fix outputs
2#
3# Sub-goals:
4# - Delegate to another original builder passed via args
5# - Save the build log to output for further checks
6# - Make the derivation succeed if the original builder fails
7# - Make the derivation fail if the original builder returns exit code 0
8#
9# Requirements:
10# This runs before, without and after stdenv. Do not modify the environment;
11# especially not before invoking the original builder. For example, use
12# "@" substitutions instead of PATH.
13# Do not export any variables.
14
15# Stricter bash
16set -eu
17
18# ------------------------
19# Run the original builder
20
21echo "testBuildFailure: Expecting non-zero exit from builder and args: ${*@Q}"
22
23("$@" 2>&1) | @coreutils@/bin/tee $TMPDIR/testBuildFailure.log \
24 | while IFS= read -r ln; do
25 echo "original builder: $ln"
26 done
27
28r=${PIPESTATUS[0]}
29if [[ $r = 0 ]]; then
30 echo "testBuildFailure: The builder did not fail, but a failure was expected!"
31 exit 1
32fi
33echo "testBuildFailure: Original builder produced exit code: $r"
34
35# -----------------------------------------
36# Write the build log to the default output
37
38# Source structured attrs as per nixpkgs/pkgs/stdenv/generic/source-stdenv.sh
39#
40# We need this so that we can read $outputs when `__structuredAttrs` is enabled
41#
42# NOTE: This MUST be done after the original builder has finished!
43# Otherwise we could pollute its environment.
44if [ -e "${NIX_ATTRS_SH_FILE:-}" ]; then . "$NIX_ATTRS_SH_FILE"; fi
45
46# Variables injected by replaceVars
47#
48# `$outputs` is unordered when `__structuredAttrs` is enabled,
49# so we use `replaceVars` to pass in an ordered `$outputNames` array
50@vars@
51
52declare -a outputPaths
53for name in "${outputNames[@]}"; do
54 # Either dereference $name, or access $outputs[] associative array
55 outputPath=${!name:-${outputs[$name]}}
56 outputPaths+=( "$outputPath" )
57done
58defOutPath=${outputPaths[0]}
59
60if [[ ! -d $defOutPath ]]; then
61 if [[ -e $defOutPath ]]; then
62 @coreutils@/bin/mv $defOutPath $TMPDIR/out-node
63 @coreutils@/bin/mkdir $defOutPath
64 @coreutils@/bin/mv $TMPDIR/out-node $defOutPath/result
65 fi
66fi
67
68@coreutils@/bin/mkdir -p $defOutPath
69@coreutils@/bin/mv $TMPDIR/testBuildFailure.log $defOutPath/testBuildFailure.log
70echo $r >$defOutPath/testBuildFailure.exit
71
72# ------------------------------------------------------
73# Put empty directories in place for any missing outputs
74
75for outputPath in "${outputPaths[@]}"; do
76 if [[ ! -e "${outputPath}" ]]; then
77 @coreutils@/bin/mkdir "${outputPath}";
78 fi
79done