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 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() { use_nix() {
_nix_direnv_preflight _nix_direnv_preflight
local layout_dir path local layout_dir path version
path=$(_nix_direnv_realpath "$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)")
layout_dir=$(direnv_layout_dir) 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 if [[ -f "${path}/.version-suffix" ]]; then
version=$(< "${path}/.version-suffix") version=$(< "${path}/.version-suffix")
elif [[ -f "${path}/.git/HEAD" ]]; then elif [[ -f "${path}/.git/HEAD" ]]; then
@ -274,6 +275,7 @@ use_nix() {
read -r version_prefix < "${path}/.version" read -r version_prefix < "${path}/.version"
version="${version_prefix}-${path:11:16}" version="${version_prefix}-${path:11:16}"
fi fi
fi
local profile local profile
profile="${layout_dir}/nix-profile-${version:-unknown}$(_nix_argsum_suffix "$*")" 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, direnv,
mypy, mypy,
python3, python3,
lib,
ruff
}: }:
writeScript "run-tests" '' writeScript "run-tests" ''
set -e set -e
@ -11,12 +13,12 @@ writeScript "run-tests" ''
echo run shellcheck echo run shellcheck
${shellcheck}/bin/shellcheck direnvrc ${shellcheck}/bin/shellcheck direnvrc
echo run black echo run black
LC_ALL=en_US.utf-8 ${python3.pkgs.black}/bin/black --check . LC_ALL=en_US.utf-8 ${lib.getExe python3.pkgs.black} --check .
echo run flake8 echo run ruff
${python3.pkgs.flake8}/bin/flake8 --ignore E501 tests ${lib.getExe ruff} tests
echo run mypy echo run mypy
${mypy}/bin/mypy tests ${lib.getExe mypy} tests
echo run unittest 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 from typing import Iterator
import pytest import pytest
from procs import run from procs import run
@ -40,6 +39,8 @@ def direnv_project(test_root: Path, project_root: Path) -> Iterator[DirenvProjec
with TemporaryDirectory() as _dir: with TemporaryDirectory() as _dir:
dir = Path(_dir) / "proj" dir = Path(_dir) / "proj"
shutil.copytree(test_root / "testenv", dir) 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" nix_direnv = project_root / "direnvrc"
c = DirenvProject(Path(dir), nix_direnv) c = DirenvProject(Path(dir), nix_direnv)

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess import subprocess
from typing import List, Union, IO, Any
from pathlib import Path from pathlib import Path
from typing import IO, Any, List, Optional, Union
_FILE = Union[None, int, IO[Any]] _FILE = Union[None, int, IO[Any]]
_DIR = Union[None, Path, str] _DIR = Union[None, Path, str]
@ -16,10 +15,11 @@ def run(
cwd: _DIR = None, cwd: _DIR = None,
stderr: _FILE = None, stderr: _FILE = None,
stdout: _FILE = None, stdout: _FILE = None,
env: Optional[dict[str, str]] = None,
) -> subprocess.CompletedProcess: ) -> subprocess.CompletedProcess:
if cwd is not None: if cwd is not None:
print(f"cd {cwd}") print(f"cd {cwd}")
print("$ " + " ".join(cmd)) print("$ " + " ".join(cmd))
return subprocess.run( 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 #!/usr/bin/env python2
import sys
import subprocess import subprocess
import sys
import unittest import unittest
from procs import run
from direnv_project import DirenvProject from direnv_project import DirenvProject
from procs import run
def common_test(direnv_project: DirenvProject) -> None: def common_test(direnv_project: DirenvProject) -> None:

View file

@ -1,20 +1,27 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
import sys import os
import subprocess import subprocess
import sys
import unittest import unittest
from typing import Optional
from procs import run
from direnv_project import DirenvProject 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( out = run(
["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd], args,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
check=False, check=False,
cwd=direnv_project.dir, cwd=direnv_project.dir,
env=env,
) )
sys.stdout.write(out.stdout) sys.stdout.write(out.stdout)
sys.stderr.write(out.stderr) 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") 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: def test_args(direnv_project: DirenvProject) -> None:
direnv_project.setup_envrc("use nix --argstr someArg OK") direnv_project.setup_envrc("use nix --argstr someArg OK")
direnv_exec(direnv_project, "echo $SHOULD_BE_SET") 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." echo "Executing shellHook."
'' }: ''
}:
pkgs.mkShellNoCC { pkgs.mkShellNoCC {
inherit shellHook; inherit shellHook;