Files
lix/tests/functional2/flakes/test_develop.py
T
eldritch horrors d69048e92b testing: migrate flakes/develop.sh
Change-Id: Ibd97c54efb72285a4b969fe11e6e6adf3a2d5511
2026-02-22 20:46:06 +00:00

110 lines
3.4 KiB
Python

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"