diff --git a/tests/functional/flakes/init.sh b/tests/functional/flakes/init.sh deleted file mode 100644 index 2d4c77ba1..000000000 --- a/tests/functional/flakes/init.sh +++ /dev/null @@ -1,87 +0,0 @@ -source ./common.sh - -requireGit - -templatesDir=$TEST_ROOT/templates -flakeDir=$TEST_ROOT/flake -nixpkgsDir=$TEST_ROOT/nixpkgs - -nix registry add --registry $registry templates git+file://$templatesDir -nix registry add --registry $registry nixpkgs git+file://$nixpkgsDir - -createGitRepo $nixpkgsDir -createSimpleGitFlake $nixpkgsDir - -# Test 'nix flake init'. -createGitRepo $templatesDir - -cat > $templatesDir/flake.nix < $templatesDir/trivial/flake.nix < $templatesDir/trivial/a -echo b > $templatesDir/trivial/b - -git -C $templatesDir add flake.nix trivial/ -git -C $templatesDir commit -m 'Initial' - -nix flake check templates -nix flake show templates -nix flake show templates --json | jq - -createGitRepo $flakeDir -(cd $flakeDir && nix flake init) -(cd $flakeDir && nix flake init) # check idempotence -git -C $flakeDir add flake.nix -nix flake check $flakeDir -nix flake show $flakeDir -nix flake show $flakeDir --json | jq -git -C $flakeDir commit -a -m 'Initial' - -# Test 'nix flake init' with benign conflicts -createGitRepo "$flakeDir" -echo a > $flakeDir/a -(cd $flakeDir && nix flake init) # check idempotence - -# Test 'nix flake init' with conflicts -createGitRepo "$flakeDir" -echo b > $flakeDir/a -pushd $flakeDir -(! nix flake init) |& grep "refusing to overwrite existing file '$flakeDir/a'" -popd -git -C $flakeDir commit -a -m 'Changed' - -# Test 'nix flake new'. -rm -rf $flakeDir -nix flake new -t templates#trivial $flakeDir -nix flake new -t templates#trivial $flakeDir # check idempotence -nix flake check $flakeDir diff --git a/tests/functional/meson.build b/tests/functional/meson.build index 15471522e..12cfe3649 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -36,7 +36,6 @@ functional_tests_scripts = [ 'flakes/develop.sh', 'flakes/develop-r8854.sh', 'flakes/mercurial.sh', - 'flakes/init.sh', 'flakes/inputs.sh', 'flakes/follow-paths.sh', 'flakes/build-paths.sh', diff --git a/tests/functional2/flakes/test_init.py b/tests/functional2/flakes/test_init.py new file mode 100644 index 000000000..63daf3dea --- /dev/null +++ b/tests/functional2/flakes/test_init.py @@ -0,0 +1,176 @@ +from testlib.fixtures.nix import Nix +from testlib.fixtures.git import Git +from testlib.fixtures.file_helper import with_files, File +from testlib.environ import environ +from testlib.utils import get_global_asset_pack, get_global_asset +from pathlib import Path +import pytest + +system = environ.get("system") + +files = { + "templates": get_global_asset_pack(".git") + | { + "flake.nix": File("""{ + description = "Some templates"; + + outputs = { self }: { + templates = rec { + trivial = { + path = ./trivial; + description = "A trivial flake"; + welcomeText = '' + Welcome to my trivial flake + ''; + }; + default = trivial; + }; + }; + }"""), + "trivial": { + "flake.nix": File(f"""{{ + description = "A flake for building Hello World"; + + outputs = {{ self, nixpkgs }}: {{ + packages.{system} = rec {{ + hello = nixpkgs.legacyPackages.{system}.hello; + default = hello; + }}; + }}; + }}"""), + "a": File("a"), + "b": File("b"), + }, + }, + "flake": get_global_asset_pack(".git"), + "nixpkgs": get_global_asset_pack(".git") + | { + "config.nix": get_global_asset("config.nix"), + "flake.nix": File(f"""{{ + outputs = {{ self }}: {{ + legacyPackages.{system}.hello = + with import ./config.nix; + mkDerivation {{ + name = "hello"; + buildCommand = "echo hello > $out"; + }}; + }}; + }}"""), + }, + "registry.json": File(""), +} + + +@pytest.fixture(autouse=True) +def common_init(nix: Nix, files: Path, git: Git): + nix.settings.add_xp_feature("nix-command", "flakes") + nix.settings["flake-registry"] = str(files / "registry.json") + + git(files / "templates", "add", "flake.nix", "trivial/") + git(files / "templates", "commit", "-m", "Initial") + git(files / "nixpkgs", "add", "flake.nix", "config.nix") + + nix.nix( + [ + "registry", + "add", + "--registry", + files / "registry.json", + "templates", + f"git+file://{files}/templates", + ] + ).run().ok() + nix.nix( + [ + "registry", + "add", + "--registry", + files / "registry.json", + "nixpkgs", + f"git+file://{files}/nixpkgs", + ] + ).run().ok() + + +@with_files(files) +class TestFlakeTemplates: + def test_check(self, nix: Nix, files: Path): + assert ( + "checking template 'templates.trivial'" + in nix.nix(["flake", "check", files / "templates"]).run().ok().stderr_s + ) + + def test_show(self, nix: Nix, files: Path): + assert ( + "A trivial flake" in nix.nix(["flake", "show", files / "templates"]).run().ok().stdout_s + ) + + def test_show_json(self, nix: Nix, files: Path): + result = nix.nix(["flake", "show", files / "templates", "--json"]).run().json() + assert result["templates"]["default"]["description"] == "A trivial flake" + + def test_init(self, nix: Nix, files: Path): + stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s + assert f"wrote: {files}/flake/flake.nix" in stderr + + class TestWithInit: + @pytest.fixture(autouse=True) + def init_flake(self, nix: Nix, files: Path): + nix.nix(["flake", "init"], cwd=files / "flake").run().ok() + + def test_init_idempotent(self, nix: Nix, files: Path): + stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s + assert "skipping identical file" in stderr + + class TestInRepo: + @pytest.fixture(autouse=True) + def init_repo(self, files: Path, git: Git): + git(files / "flake", "add", "flake.nix") + + def test_check(self, nix: Nix, files: Path): + stderr = nix.nix(["flake", "check", files / "flake"]).run().ok().stderr_s + assert f"fetching git input 'git+file://{files}/flake'" in stderr + assert f"fetching git input 'git+file://{files}/nixpkgs'" in stderr + assert "evaluating flake" in stderr + + def test_show(self, nix: Nix, files: Path): + stdout = nix.nix(["flake", "show", files / "flake"]).run().ok().stdout_s + assert "hello: package 'hello'" in stdout + + def test_show_json(self, nix: Nix, files: Path): + result = nix.nix(["flake", "show", files / "flake", "--json"]).run().json() + assert result["packages"][system]["default"]["name"] == "hello" + assert result["packages"][system]["hello"]["name"] == "hello" + + def test_benign_conflict_idempotence(self, nix: Nix, files: Path): + (files / "flake/a").write_text("a") + + stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s + assert "skipping identical file" in stderr + assert f"wrote: {files}/flake/flake.nix" in stderr + + def test_init_conflict(self, nix: Nix, files: Path): + (files / "flake/a").write_text("b") + + stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().expect(1).stderr_s + assert f"refusing to overwrite existing file '{files}/flake/a'" in stderr + + def test_new(self, nix: Nix): + stderr = nix.nix(["flake", "new", "-t", "templates#trivial", "flake2"]).run().ok().stderr_s + assert f"wrote: {nix.env.dirs.home}/flake2/flake.nix" in stderr + + class TestNew: + def test_idempotent(self, nix: Nix, files: Path): + nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"]).run().ok() + stderr = ( + nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"]) + .run() + .ok() + .stderr_s + ) + assert "skipping identical file" in stderr + + def test_check(self, nix: Nix, files: Path): + nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"]).run().ok() + stderr = nix.nix(["flake", "check", files / "flake2"]).run().ok().stderr_s + assert "evaluating flake" in stderr