Skip to content

Commit

Permalink
No longer copy functional tests to the build dir
Browse files Browse the repository at this point in the history
This should make `_NIX_TEST_ACCEPT=1` work again, fixing NixOS#11369.

Progress on NixOS#2503
  • Loading branch information
Ericson2314 committed Nov 3, 2024
1 parent 55fe4ee commit b3e845b
Show file tree
Hide file tree
Showing 97 changed files with 238 additions and 138 deletions.
8 changes: 8 additions & 0 deletions mk/common-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ test_name=$(echo -n "${test?must be defined by caller (test runner)}" | sed \
-e "s|\.sh$||" \
)

# Layer violation, but I am not inclined to care too much, as this code
# is about to be deleted.
src_dir=$(realpath tests/functional)

# shellcheck disable=SC2016
TESTS_ENVIRONMENT=(
"TEST_NAME=$test_name"
'NIX_REMOTE='
'PS4=+(${BASH_SOURCE[0]-$0}:$LINENO) '
"_NIX_TEST_SOURCE_DIR=${src_dir}"
"_NIX_TEST_BUILD_DIR=${src_dir}"
)

unset src_dir

read -r -a bash <<< "${BASH:-/usr/bin/env bash}"

run () {
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/binary-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ clearCache
# preserve quotes variables in the single-quoted string
# shellcheck disable=SC2016
outPath=$(nix-build --no-out-link -E '
with import ./config.nix;
with import '"${config_nix}"';
mkDerivation {
name = "nar-listing";
buildCommand = "mkdir $out; echo foo > $out/bar; ln -s xyzzy $out/link";
Expand All @@ -258,7 +258,7 @@ clearCache
# preserve quotes variables in the single-quoted string
# shellcheck disable=SC2016
outPath=$(nix-build --no-out-link -E '
with import ./config.nix;
with import '"${config_nix}"';
mkDerivation {
name = "debug-info";
buildCommand = "mkdir -p $out/lib/debug/.build-id/02; echo foo > $out/lib/debug/.build-id/02/623eda209c26a59b1a8638ff7752f6b945c26b.debug";
Expand All @@ -276,7 +276,7 @@ diff -u \
# preserve quotes variables in the single-quoted string
# shellcheck disable=SC2016
expr='
with import ./config.nix;
with import '"${config_nix}"';
mkDerivation {
name = "multi-output";
buildCommand = "mkdir -p $out; echo foo > $doc; echo $doc > $out/docref";
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/build-hook-ca-fixed.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ busybox }:

with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

let

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/build-hook.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ busybox, contentAddressed ? false }:

with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

let

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ca/content-addressed.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/ca/config.nix";

let mkCADerivation = args: mkDerivation ({
__contentAddressed = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ca/derivation-json.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
#

source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ca/duplicate-realisation-in-closure.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

source ./common.sh
source common.sh

requireDaemonNewerThan "2.4pre20210625"

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/ca/import-from-derivation.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ca/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ suites += {
'substitute.sh',
'why-depends.sh',
],
'workdir': meson.current_build_dir(),
'workdir': meson.current_source_dir(),
}
2 changes: 2 additions & 0 deletions tests/functional/ca/new-build-cmd.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1
Expand Down
9 changes: 7 additions & 2 deletions tests/functional/ca/nix-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

source common.sh

FLAKE_PATH=path:$PWD
flakeDir="$TEST_HOME/flake"
mkdir -p "${flakeDir}"
cp flake.nix "${_NIX_TEST_BUILD_DIR}/ca/config.nix" content-addressed.nix "${flakeDir}"

nix run --no-write-lock-file "$FLAKE_PATH#runnable"
# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
removeBuildDirRef "$flakeDir"/*.nix

nix run --no-write-lock-file "path:${flakeDir}#runnable"
1 change: 0 additions & 1 deletion tests/functional/ca/nix-shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ source common.sh
CONTENT_ADDRESSED=true
cd ..
source ./nix-shell.sh

2 changes: 1 addition & 1 deletion tests/functional/ca/nondeterministic.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/ca/config.nix";

let mkCADerivation = args: mkDerivation ({
__contentAddressed = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/ca/racy.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# build it at once.


with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/ca/config.nix";

mkDerivation {
name = "simple";
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/ca/repl.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/ca/why-depends.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/check-refs.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

rec {

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/check-reqs.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

rec {
dep1 = mkDerivation {
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/check.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{checkBuildId ? 0}:

with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

{
nondeterministic = mkDerivation {
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/chroot-store.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ if canUseSandbox; then
}
EOF

cp simple.nix shell.nix simple.builder.sh config.nix "$flakeDir/"
cp simple.nix shell.nix simple.builder.sh "${config_nix}" "$flakeDir/"

# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
removeBuildDirRef "$flakeDir"/*.nix

TODO_NixOS

Expand Down
1 change: 1 addition & 0 deletions tests/functional/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ COMMON_SH_SOURCED=1

functionalTestsDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

source "$functionalTestsDir/common/source-either.sh"
source "$functionalTestsDir/common/vars.sh"
source "$functionalTestsDir/common/functions.sh"
source "$functionalTestsDir/common/init.sh"
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/common/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ count() {
echo $#
}

# Sometimes, e.g. due to pure eval, restricted eval, or sandboxing, we
# cannot look up `config.nix` in the build dir, and have to instead get
# it from the current directory. (In this case, the current directly
# will be somewhere in `$TEST_ROOT`.)
removeBuildDirRef() {
# shellcheck disable=SC2016 # The ${} in this is Nix, not shell
sed -i -e 's,"${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/[^ ]*config.nix",./config.nix,' "$@"
}

trap onError ERR

fi # COMMON_FUNCTIONS_SH_SOURCED
4 changes: 3 additions & 1 deletion tests/functional/common/paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ commonDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

# Since these are generated files
# shellcheck disable=SC1091
source "$commonDir/source-either.sh"
# Just for `isTestOnNixOS`
source "$commonDir/functions.sh"
# shellcheck disable=SC1091
source "$commonDir/subst-vars.sh"
sourceEither "$commonDir/subst-vars.sh"
# Make sure shellcheck knows this will be defined by the above generated snippet
: "${bash?}" "${bindir?}"

Expand Down
37 changes: 37 additions & 0 deletions tests/functional/common/source-either.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# shellcheck shell=bash

if [[ -z "${COMMON_SOURCE_EITHER_SH_SOURCED-}" ]]; then

COMMON_SOURCE_EITHER_SH_SOURCED=1

sourceEither() {
local input_path="$1"

# Convert the input path to an absolute path relative to the current working directory (CWD)
local abs_path
abs_path="$(realpath "$input_path")"

# Create a relative path suffix to _NIX_TEST_SOURCE_DIR
local suffix
suffix="${abs_path#"$(realpath "${_NIX_TEST_SOURCE_DIR}")/"}"

# Check if the file exists in _NIX_TEST_BUILD_DIR and source it
if [ -f "${_NIX_TEST_BUILD_DIR}/${suffix}" ]; then
# shellcheck disable=SC1090 # Is inherently dynamic
source "${_NIX_TEST_BUILD_DIR}/${suffix}"
return 0
fi

# Check if the file exists in _NIX_TEST_SOURCE_DIR and source it
if [ -f "${_NIX_TEST_SOURCE_DIR}/${suffix}" ]; then
# shellcheck disable=SC1090 # Is inherently dynamic
source "${_NIX_TEST_SOURCE_DIR}/${suffix}"
return 0
fi

# If neither file exists, return an error or print a message
echo "File not found in either ${_NIX_TEST_BUILD_DIR} or ${_NIX_TEST_SOURCE_DIR}"
exit 1
}

fi
10 changes: 9 additions & 1 deletion tests/functional/common/vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ if [[ -z "${COMMON_VARS_SH_SOURCED-}" ]]; then

COMMON_VARS_SH_SOURCED=1

_NIX_TEST_SOURCE_DIR=$(realpath "${_NIX_TEST_SOURCE_DIR}")
_NIX_TEST_BUILD_DIR=$(realpath "${_NIX_TEST_BUILD_DIR}")

commonDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

# Since this is a generated file
# shellcheck disable=SC1091
source "$commonDir/subst-vars.sh"
sourceEither "$commonDir/subst-vars.sh"
# Make sure shellcheck knows all these will be defined by the above generated snippet
: "${bindir?} ${coreutils?} ${dot?} ${SHELL?} ${busybox?} ${version?} ${system?}"
export coreutils dot busybox version system
Expand Down Expand Up @@ -69,4 +72,9 @@ if [[ $(uname) == Linux ]] && [[ -L /proc/self/ns/user ]] && unshare --user true
_canUseSandbox=1
fi

# Very common, shorthand helps
# Used in other files
# shellcheck disable=SC2034
config_nix="${_NIX_TEST_BUILD_DIR}/config.nix"

fi # COMMON_VARS_SH_SOURCED
2 changes: 1 addition & 1 deletion tests/functional/dependencies.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ hashInvalidator ? "" }:
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

let {

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/dyn-drv/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ suites += {
'dep-built-drv.sh',
'old-daemon-error-hack.sh',
],
'workdir': meson.current_build_dir(),
'workdir': meson.current_source_dir(),
}
2 changes: 1 addition & 1 deletion tests/functional/dyn-drv/old-daemon-error-hack.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/dyn-drv/config.nix";

# A simple content-addressed derivation.
# The derivation can be arbitrarily modified by passing a different `seed`,
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/dyn-drv/recursive-mod-json.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/dyn-drv/config.nix";

let innerName = "foo"; in

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/dyn-drv/text-hashed-output.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/dyn-drv/config.nix";

# A simple content-addressed derivation.
# The derivation can be arbitrarily modified by passing a different `seed`,
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ printf 123 > "$TEST_ROOT/xyzzy/default.nix"
[[ $(nix eval --impure --expr "import $TEST_ROOT/foo/bar") = 123 ]]

# Test --arg-from-file.
[[ "$(nix eval --raw --arg-from-file foo config.nix --expr '{ foo }: { inherit foo; }' foo)" = "$(cat config.nix)" ]]
[[ "$(nix eval --raw --arg-from-file foo "${config_nix}" --expr '{ foo }: { inherit foo; }' foo)" = "$(cat "${config_nix}")" ]]

# Check that special(-ish) files are drained.
if [[ -e /proc/version ]]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/export-graph.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

rec {

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/extra-sandbox-profile.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ destFile, seed }:

with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

mkDerivation {
name = "simple";
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/failing.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ busybox }:
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";
let

mkDerivation = args:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/filter-source.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

mkDerivation {
name = "filter";
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/fixed.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with import ./config.nix;
with import "${builtins.getEnv "_NIX_TEST_BUILD_DIR"}/config.nix";

rec {

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/flakes/build-paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ cat > "$flake1Dir"/flake.nix <<EOF
}
EOF

cp ../simple.nix ../simple.builder.sh ../config.nix "$flake1Dir/"
cp ../simple.nix ../simple.builder.sh "${config_nix}" "$flake1Dir/"

echo bar > "$flake1Dir/foo"

Expand Down
5 changes: 4 additions & 1 deletion tests/functional/flakes/bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

source common.sh

cp ../simple.nix ../simple.builder.sh ../config.nix "$TEST_HOME"
cp ../simple.nix ../simple.builder.sh "${config_nix}" "$TEST_HOME"

# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
removeBuildDirRef "$TEST_HOME"/*.nix

cd "$TEST_HOME"

Expand Down
5 changes: 4 additions & 1 deletion tests/functional/flakes/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ writeSimpleFlake() {
}
EOF

cp ../simple.nix ../shell.nix ../simple.builder.sh ../config.nix "$flakeDir/"
cp ../simple.nix ../shell.nix ../simple.builder.sh "${config_nix}" "$flakeDir/"

# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
removeBuildDirRef "$flakeDir"/*.nix
}

createSimpleGitFlake() {
Expand Down
Loading

0 comments on commit b3e845b

Please sign in to comment.