mirror of
https://github.com/nix-community/nix-direnv.git
synced 2025-12-17 06:21:18 +01:00
refactor tests
* factor out duplicated code * make tests an importable package * idiomatic pytest usage * do not touch files outside of the tmp test tree and do not depend on external state (except for /nix/store ☹) * run coverage to root out dead code
This commit is contained in:
parent
c3c23453e4
commit
0d145c01d5
12 changed files with 252 additions and 240 deletions
|
|
@ -1,67 +1,35 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
from direnv_project import DirenvProject
|
||||
from procs import run
|
||||
|
||||
from .case import TestCase
|
||||
|
||||
|
||||
def direnv_exec(
|
||||
direnv_project: DirenvProject, cmd: str, env: Optional[dict[str, str]] = None
|
||||
) -> None:
|
||||
args = ["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd]
|
||||
print("$ " + " ".join(args))
|
||||
out = run(
|
||||
args,
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
check=False,
|
||||
cwd=direnv_project.dir,
|
||||
env=env,
|
||||
)
|
||||
sys.stdout.write(out.stdout)
|
||||
sys.stderr.write(out.stderr)
|
||||
assert out.returncode == 0
|
||||
assert "OK\n" == out.stdout
|
||||
assert "renewed cache" in out.stderr
|
||||
class TestUseNix(TestCase):
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_attrs(self, strict_env: bool) -> None:
|
||||
self.setup_envrc("use nix -A subshell", strict_env=strict_env)
|
||||
self.assert_direnv_var("THIS_IS_A_SUBSHELL")
|
||||
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_with_nix_path(self, strict_env: bool) -> None:
|
||||
if (nix_path := os.environ.get("NIX_PATH")) is None:
|
||||
pytest.skip("no parent NIX_PATH")
|
||||
else:
|
||||
self.setup_envrc(
|
||||
"use nix --argstr someArg OK", strict_env=strict_env, NIX_PATH=nix_path
|
||||
)
|
||||
self.assert_direnv_var("SHOULD_BE_SET", NIX_PATH=nix_path)
|
||||
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_attrs(direnv_project: DirenvProject, strict_env: bool) -> None:
|
||||
direnv_project.setup_envrc("use nix -A subshell", strict_env=strict_env)
|
||||
direnv_exec(direnv_project, "echo $THIS_IS_A_SUBSHELL")
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_args(self, strict_env: bool) -> None:
|
||||
self.setup_envrc("use nix --argstr someArg OK", strict_env=strict_env)
|
||||
self.assert_direnv_var("SHOULD_BE_SET")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_no_nix_path(direnv_project: DirenvProject, strict_env: bool) -> None:
|
||||
direnv_project.setup_envrc("use nix --argstr someArg OK", strict_env=strict_env)
|
||||
env = os.environ.copy()
|
||||
del env["NIX_PATH"]
|
||||
direnv_exec(direnv_project, "echo $SHOULD_BE_SET", env=env)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_args(direnv_project: DirenvProject, strict_env: bool) -> None:
|
||||
direnv_project.setup_envrc("use nix --argstr someArg OK", strict_env=strict_env)
|
||||
direnv_exec(direnv_project, "echo $SHOULD_BE_SET")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_no_files(direnv_project: DirenvProject, strict_env: bool) -> None:
|
||||
direnv_project.setup_envrc("use nix -p hello", strict_env=strict_env)
|
||||
out = run(
|
||||
["direnv", "status"],
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
check=False,
|
||||
cwd=direnv_project.dir,
|
||||
)
|
||||
assert out.returncode == 0
|
||||
assert 'Loaded watch: "."' not in out.stdout
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@pytest.mark.parametrize("strict_env", [False, True])
|
||||
def test_no_files(self, strict_env: bool) -> None:
|
||||
self.setup_envrc("use nix -p hello", strict_env=strict_env)
|
||||
result = self.direnv_run("status")
|
||||
assert 'Loaded watch: "."' not in result.stdout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue