testing: migrate flakes/develop.sh

Change-Id: Ibd97c54efb72285a4b969fe11e6e6adf3a2d5511
This commit is contained in:
eldritch horrors
2026-02-22 20:46:06 +00:00
parent 01ff67595b
commit d69048e92b
4 changed files with 203 additions and 72 deletions
-71
View File
@@ -1,71 +0,0 @@
source ../common.sh
clearStore
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
# Create flake under test.
cp ../shell-hello.nix ../config.nix $TEST_HOME/
cat <<EOF >$TEST_HOME/flake.nix
{
inputs.nixpkgs.url = "$TEST_HOME/nixpkgs";
outputs = {self, nixpkgs}: {
packages.$system.hello = (import ./config.nix).mkDerivation {
name = "hello";
outputs = [ "out" "dev" ];
meta.outputsToInstall = [ "out" ];
buildCommand = "";
};
};
}
EOF
# Create fake nixpkgs flake.
mkdir -p $TEST_HOME/nixpkgs
cp ../config.nix ../nix-shell/shell.nix $TEST_HOME/nixpkgs
cat <<EOF >$TEST_HOME/nixpkgs/flake.nix
{
outputs = {self}: {
legacyPackages.$system.bashInteractive = (import ./shell.nix {}).bashInteractive;
};
}
EOF
cd $TEST_HOME
# Test whether `nix develop` passes through environment variables.
[[ "$(
ENVVAR=a nix develop --no-write-lock-file .#hello <<EOF
echo "\$ENVVAR"
EOF
)" = "a" ]]
# Test whether `nix develop --ignore-environment` does _not_ pass through environment variables.
[[ -z "$(
ENVVAR=a nix develop --ignore-environment --no-write-lock-file .#hello <<EOF
echo "\$ENVVAR"
EOF
)" ]]
# Determine the bashInteractive executable.
nix build --no-write-lock-file './nixpkgs#bashInteractive' --out-link ./bash-interactive
BASH_INTERACTIVE_EXECUTABLE="$PWD/bash-interactive/bin/bash"
# Test whether `nix develop` sets `SHELL` to nixpkgs#bashInteractive shell.
[[ "$(
SHELL=custom nix develop --no-write-lock-file .#hello <<EOF
echo "\$SHELL"
EOF
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
# Test whether `nix develop` with ignore environment sets `SHELL` to nixpkgs#bashInteractive shell.
[[ "$(
SHELL=custom nix develop --ignore-environment --no-write-lock-file .#hello <<EOF
echo "\$SHELL"
EOF
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
# Test whether `nix develop` sets `IN_NIX_SHELL`
[[ "$(nix develop --no-write-lock-file .#hello -c sh -c 'echo $IN_NIX_SHELL')" = "impure" ]]
[[ "$(nix develop --no-write-lock-file --ignore-environment .#hello -c /bin/sh -c 'echo $IN_NIX_SHELL')" = "impure" ]]
clearStore
-1
View File
@@ -33,7 +33,6 @@ functional_tests_scripts = [
'init.sh',
'test-infra.sh',
'flakes/flakes.sh',
'flakes/develop.sh',
'flakes/develop-r8854.sh',
'flakes/mercurial.sh',
'flakes/follow-paths.sh',
+109
View File
@@ -0,0 +1,109 @@
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import with_files, File, EnvTemplate, CopyFile
from testlib.environ import environ
from testlib.utils import get_global_asset
from pathlib import Path
import pytest
system = environ.get("system")
files = {
"flake.nix": EnvTemplate(f"""{{
inputs.nixpkgs.url = "@HOME@/nixpkgs";
outputs = {{self, nixpkgs}}: {{
packages.{system}.hello = (import ./config.nix).mkDerivation {{
name = "hello";
outputs = [ "out" "dev" ];
meta.outputsToInstall = [ "out" ];
buildCommand = "";
}};
}};
}}"""),
"config.nix": get_global_asset("config.nix"),
"shell-hello.nix": CopyFile("assets/shell-hello.nix"),
"nixpkgs": {
"flake.nix": File(f"""{{
outputs = {{self}}: {{
legacyPackages.{system}.bashInteractive = (import ./shell.nix {{}}).bashInteractive;
}};
}}"""),
"config.nix": get_global_asset("config.nix"),
"shell.nix": get_global_asset("shell.nix"),
},
}
@pytest.fixture(autouse=True)
def common_init(nix: Nix):
nix.settings.add_xp_feature("nix-command", "flakes")
@with_files(files)
class TestDevelop:
def test_env_passthrough(self, nix: Nix):
nix.env["ENVVAR"] = "a"
result = (
nix.nix(["develop", "--no-write-lock-file", ".#hello"])
.with_stdin(b'echo "$ENVVAR"')
.run()
.ok()
)
assert result.stdout_s == "a\n"
def test_no_env_passthrough_on_ignore_environment(self, nix: Nix):
nix.env["ENVVAR"] = "a"
result = (
nix.nix(["develop", "--ignore-environment", "--no-write-lock-file", ".#hello"])
.with_stdin(b'echo "$ENVVAR"')
.run()
.ok()
)
assert result.stdout_s == "\n"
class TestShell:
@pytest.fixture(autouse=True)
def shell_init(self, nix: Nix, files: Path): # noqa: ARG002
nix.nix(
[
"build",
"--no-write-lock-file",
"./nixpkgs#bashInteractive",
"--out-link",
"./bash-interactive",
]
).run().ok()
self.bash_interactive = nix.env.dirs.home / "bash-interactive/bin/bash"
nix.env["SHELL"] = "custom"
@pytest.mark.parametrize("extra_args", [[], ["--ignore-environment"]])
def test_shell_is_bash_interactive(self, nix: Nix, extra_args: list[str]):
result = (
nix.nix(["develop", *extra_args, "--no-write-lock-file", ".#hello"])
.with_stdin(b'echo "$SHELL"')
.run()
.ok()
)
assert Path(result.stdout_s.strip()).samefile(self.bash_interactive)
@pytest.mark.parametrize("extra_args", [[], ["--ignore-environment"]])
def test_sets_in_nix_shell(self, nix: Nix, extra_args: list[str]):
result = (
nix.nix(
[
"develop",
"--no-write-lock-file",
*extra_args,
".#hello",
"-c",
"sh",
"-c",
"echo $IN_NIX_SHELL",
]
)
.run()
.ok()
)
assert result.stdout_s == "impure\n"
@@ -0,0 +1,94 @@
{ inNixShell ? false, contentAddressed ? false, fooContents ? "foo" }:
let cfg = import ./config.nix; in
with cfg;
let
mkDerivation =
if contentAddressed then
args: cfg.mkDerivation ({
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
} // args)
else cfg.mkDerivation;
in
let pkgs = rec {
setupSh = builtins.toFile "setup" ''
export VAR_FROM_STDENV_SETUP=foo
for pkg in $buildInputs; do
export PATH=$PATH:$pkg/bin
done
# mimic behavior of stdenv for `$out` etc. for structured attrs.
if [ -n "''${NIX_ATTRS_SH_FILE}" ]; then
for o in "''${!outputs[@]}"; do
eval "''${o}=''${outputs[$o]}"
export "''${o}"
done
fi
declare -a arr1=(1 2 "3 4" 5)
declare -a arr2=(x $'\n' $'x\ny')
fun() {
echo blabla
}
'';
stdenv = mkDerivation {
name = "stdenv";
buildCommand = ''
mkdir -p $out
ln -s ${setupSh} $out/setup
'';
};
shellDrv = mkDerivation {
name = "shellDrv";
builder = "/does/not/exist";
VAR_FROM_NIX = "bar";
ASCII_PERCENT = "%";
ASCII_AT = "@";
ASCII_ESC = "";
TEST_inNixShell = if inNixShell then "true" else "false";
inherit stdenv;
outputs = ["dev" "out"];
};
# Used by nix-shell -p
runCommand = name: args: buildCommand: mkDerivation (args // {
inherit name buildCommand stdenv;
});
foo = runCommand "foo" {} ''
mkdir -p $out/bin
echo '#!${shell}' >> $out/bin/foo
echo 'echo ${fooContents}' >> $out/bin/foo
chmod a+rx $out/bin/foo
ln -s ${shell} $out/bin/bash
'';
bar = runCommand "bar" {} ''
mkdir -p $out/bin
echo '#!${shell}' > $out/bin/bar
echo 'echo bar' >> $out/bin/bar
chmod a+rx $out/bin/bar
'';
bash = shell;
bashInteractive = runCommand "bash" {} ''
mkdir -p $out/bin
ln -s ${shell} $out/bin/bash
'';
# ruby "interpreter" that outputs "$@"
ruby = runCommand "ruby" {} ''
mkdir -p $out/bin
echo '#!${shell}' > $out/bin/ruby
echo 'printf %s "$*"' >> $out/bin/ruby
chmod a+rx $out/bin/ruby
'';
inherit pkgs;
}; in pkgs