287: improve handling if no NIX_PATH is set r=Mic92 a=Mic92



Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
This commit is contained in:
bors[bot] 2023-01-19 09:44:35 +00:00 committed by GitHub
commit 2c958bfdea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 80 additions and 36 deletions

View file

@ -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)"
]

View file

@ -252,11 +252,12 @@ 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
@ -274,6 +275,7 @@ use_nix() {
read -r version_prefix < "${path}/.version"
version="${version_prefix}-${path:11:16}"
fi
fi
local profile
profile="${layout_dir}/nix-profile-${version:-unknown}$(_nix_argsum_suffix "$*")"

20
pyproject.toml Normal file
View file

@ -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

View file

@ -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} .
''

View file

@ -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)

View file

@ -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
)

View file

@ -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:

View file

@ -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")

View file

@ -1,6 +1,9 @@
{ pkgs ? import <nixpkgs> { }, someArg ? null, shellHook ? ''
{ pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { }
, someArg ? null
, shellHook ? ''
echo "Executing shellHook."
'' }:
''
}:
pkgs.mkShellNoCC {
inherit shellHook;