diff --git a/bors.toml b/bors.toml index b750afa..c063f8e 100644 --- a/bors.toml +++ b/bors.toml @@ -1,2 +1,4 @@ cut_body_after = "" # don't include text from the PR body in the merge commit message -status = [ "tests" ] +status = [ + "tests (ubuntu-latest)" +] diff --git a/direnvrc b/direnvrc index d3dd839..b2e7d24 100644 --- a/direnvrc +++ b/direnvrc @@ -252,27 +252,29 @@ use_flake() { use_nix() { _nix_direnv_preflight - local layout_dir path - path=$(_nix_direnv_realpath "$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)") + local layout_dir path version layout_dir=$(direnv_layout_dir) + path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs 2>/dev/null) + if [[ -n "$path" ]]; then + path=$(_nix_direnv_realpath "$path") - local version - if [[ -f "${path}/.version-suffix" ]]; then - version=$(< "${path}/.version-suffix") - elif [[ -f "${path}/.git/HEAD" ]]; then - local head - read -r head < "${path}/.git/HEAD" - local regex="ref: (.*)" - if [[ "$head" =~ $regex ]]; then - read -r version < "${path}/.git/${BASH_REMATCH[1]}" - else - version="$head" + if [[ -f "${path}/.version-suffix" ]]; then + version=$(< "${path}/.version-suffix") + elif [[ -f "${path}/.git/HEAD" ]]; then + local head + read -r head < "${path}/.git/HEAD" + local regex="ref: (.*)" + if [[ "$head" =~ $regex ]]; then + read -r version < "${path}/.git/${BASH_REMATCH[1]}" + else + version="$head" + fi + elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then + # borrow some bits from the store path + local version_prefix + read -r version_prefix < "${path}/.version" + version="${version_prefix}-${path:11:16}" fi - elif [[ -f "${path}/.version" && "${path}" == "/nix/store/"* ]]; then - # borrow some bits from the store path - local version_prefix - read -r version_prefix < "${path}/.version" - version="${version_prefix}-${path:11:16}" fi local profile diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b470684 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[tool.ruff] +line-length = 88 + +select = ["E", "F", "I"] +ignore = [ "E501" ] + +[tool.mypy] +python_version = "3.10" +warn_redundant_casts = true +disallow_untyped_calls = true +disallow_untyped_defs = true +no_implicit_optional = true + +[[tool.mypy.overrides]] +module = "setuptools.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "pytest.*" +ignore_missing_imports = true diff --git a/run-tests.nix b/run-tests.nix index 189198e..ae908cc 100644 --- a/run-tests.nix +++ b/run-tests.nix @@ -4,6 +4,8 @@ direnv, mypy, python3, + lib, + ruff }: writeScript "run-tests" '' set -e @@ -11,12 +13,12 @@ writeScript "run-tests" '' echo run shellcheck ${shellcheck}/bin/shellcheck direnvrc echo run black - LC_ALL=en_US.utf-8 ${python3.pkgs.black}/bin/black --check . - echo run flake8 - ${python3.pkgs.flake8}/bin/flake8 --ignore E501 tests + LC_ALL=en_US.utf-8 ${lib.getExe python3.pkgs.black} --check . + echo run ruff + ${lib.getExe ruff} tests echo run mypy - ${mypy}/bin/mypy tests + ${lib.getExe mypy} tests echo run unittest - ${python3.pkgs.pytest}/bin/pytest . + ${lib.getExe python3.pkgs.pytest} . '' diff --git a/tests/direnv_project.py b/tests/direnv_project.py index 17c4c50..784eaaa 100644 --- a/tests/direnv_project.py +++ b/tests/direnv_project.py @@ -8,7 +8,6 @@ from tempfile import TemporaryDirectory from typing import Iterator import pytest - from procs import run @@ -40,6 +39,8 @@ def direnv_project(test_root: Path, project_root: Path) -> Iterator[DirenvProjec with TemporaryDirectory() as _dir: dir = Path(_dir) / "proj" shutil.copytree(test_root / "testenv", dir) + shutil.copyfile(project_root / "flake.nix", dir / "flake.nix") + shutil.copyfile(project_root / "flake.lock", dir / "flake.lock") nix_direnv = project_root / "direnvrc" c = DirenvProject(Path(dir), nix_direnv) diff --git a/tests/procs.py b/tests/procs.py index 81cf150..7f8cf5e 100644 --- a/tests/procs.py +++ b/tests/procs.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 import subprocess -from typing import List, Union, IO, Any from pathlib import Path - +from typing import IO, Any, List, Optional, Union _FILE = Union[None, int, IO[Any]] _DIR = Union[None, Path, str] @@ -16,10 +15,11 @@ def run( cwd: _DIR = None, stderr: _FILE = None, stdout: _FILE = None, + env: Optional[dict[str, str]] = None, ) -> subprocess.CompletedProcess: if cwd is not None: print(f"cd {cwd}") print("$ " + " ".join(cmd)) return subprocess.run( - cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout + cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout, env=env ) diff --git a/tests/test_gc.py b/tests/test_gc.py index fefda77..2982128 100644 --- a/tests/test_gc.py +++ b/tests/test_gc.py @@ -1,11 +1,11 @@ #!/usr/bin/env python2 -import sys import subprocess +import sys import unittest -from procs import run from direnv_project import DirenvProject +from procs import run def common_test(direnv_project: DirenvProject) -> None: diff --git a/tests/test_use_nix.py b/tests/test_use_nix.py index c9495f8..8144a62 100644 --- a/tests/test_use_nix.py +++ b/tests/test_use_nix.py @@ -1,20 +1,27 @@ #!/usr/bin/env python2 -import sys +import os import subprocess +import sys import unittest +from typing import Optional -from procs import run from direnv_project import DirenvProject +from procs import run -def direnv_exec(direnv_project: DirenvProject, cmd: str) -> None: +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( - ["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd], + 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) @@ -28,6 +35,13 @@ def test_attrs(direnv_project: DirenvProject) -> None: direnv_exec(direnv_project, "echo $THIS_IS_A_SUBSHELL") +def test_no_nix_path(direnv_project: DirenvProject) -> None: + direnv_project.setup_envrc("use nix --argstr someArg OK") + env = os.environ.copy() + del env["NIX_PATH"] + direnv_exec(direnv_project, "echo $SHOULD_BE_SET", env=env) + + def test_args(direnv_project: DirenvProject) -> None: direnv_project.setup_envrc("use nix --argstr someArg OK") direnv_exec(direnv_project, "echo $SHOULD_BE_SET") diff --git a/tests/testenv/shell.nix b/tests/testenv/shell.nix index 05d0a7a..a0456f6 100644 --- a/tests/testenv/shell.nix +++ b/tests/testenv/shell.nix @@ -1,6 +1,9 @@ -{ pkgs ? import { }, someArg ? null, shellHook ? '' +{ pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { } +, someArg ? null +, shellHook ? '' echo "Executing shellHook." -'' }: +'' +}: pkgs.mkShellNoCC { inherit shellHook;