Skip to content

Commit 2bb4e74

Browse files
committed
Switch to bats over pytest
This removes python entirely from the stack. Yet to run the tests on macOS, but they're buildable by way of nix wrapping. Worst case scenario, you cannot run them from the devshell but `nix run .#test-runner-<latest||stable>` should work okay.
1 parent 445dc9f commit 2bb4e74

23 files changed

+338
-385
lines changed

.envrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# shellcheck shell=bash
22
strict_env
33
source ./direnvrc
4-
watch_file direnvrc ./*.nix
4+
5+
watch_file direnvrc
6+
# shellcheck disable=SC2046
7+
watch_file $(find . -name "*.nix")
8+
59
use flake

flake.nix

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,18 @@
3333
self',
3434
...
3535
}:
36+
let
37+
nix-direnv = pkgs.callPackage ./default.nix { };
38+
test_pkgs = pkgs.lib.callPackagesWith pkgs ./tests { inherit nix-direnv; };
39+
in
3640
{
37-
packages = rec {
38-
nix-direnv = pkgs.callPackage ./default.nix { };
39-
default = config.packages.nix-direnv;
40-
test-runner-stable = pkgs.callPackage ./test-runner.nix {
41-
nixVersion = "stable";
42-
inherit nix-direnv;
43-
};
44-
test-runner-latest = pkgs.callPackage ./test-runner.nix {
45-
nixVersion = "latest";
46-
inherit nix-direnv;
47-
};
41+
packages = test_pkgs // {
42+
inherit nix-direnv;
43+
default = nix-direnv;
4844
};
4945

5046
devShells.default = pkgs.callPackage ./shell.nix {
51-
packages = [
52-
config.treefmt.build.wrapper
53-
pkgs.shellcheck
54-
];
47+
treefmt = config.treefmt.build.wrapper;
5548
};
5649

5750
checks =

shell.nix

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
{
22
pkgs ? import <nixpkgs> { },
3-
packages ? [ ],
3+
treefmt ? null,
4+
nix-direnv ? (pkgs.callPackage ./default.nix { }),
5+
test_pkgs ? (pkgs.lib.callPackagesWith pkgs ./tests { inherit nix-direnv; }),
46
}:
5-
6-
with pkgs;
7-
mkShell {
8-
packages = packages ++ [
9-
python3.pkgs.pytest
10-
python3.pkgs.mypy
11-
ruff
12-
direnv
13-
];
7+
let
8+
inherit (pkgs) lib;
9+
in
10+
pkgs.mkShell {
11+
DIRENV_STDLIB = "${test_pkgs.direnv-stdlib}";
12+
DIRENVRC = "${nix-direnv}/share/nix-direnv/direnvrc";
13+
BATS_LIB_PATH = lib.strings.makeSearchPath "" ([
14+
test_pkgs.bats-support
15+
test_pkgs.bats-assert
16+
]);
17+
packages =
18+
(builtins.attrValues {
19+
inherit (pkgs)
20+
bats
21+
direnv
22+
shellcheck
23+
;
24+
})
25+
++ (builtins.attrValues (lib.attrsets.filterAttrs (name: _val: name != "direnv-stdlib") test_pkgs))
26+
++ lib.optionals (treefmt != null) [ treefmt ];
1427
}

test-runner.nix

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/bash/test_versions.bats

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/default.nix

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
bash,
3+
bats,
4+
callPackage,
5+
coreutils,
6+
direnv,
7+
fetchurl,
8+
findutils,
9+
gnugrep,
10+
lib,
11+
nix-direnv,
12+
nixVersions,
13+
writeShellScriptBin,
14+
}:
15+
let
16+
direnv-stdlib = fetchurl {
17+
url = "https://raw.githubusercontent.com/direnv/direnv/refs/tags/v2.37.1/stdlib.sh";
18+
hash = "sha256-MMM04OXhqS/rRSuv8uh7CD70Z7CaGT63EtL/3LC08qM=";
19+
};
20+
bats-support = callPackage ./nix/bats-support.nix { };
21+
bats-assert = callPackage ./nix/bats-assert.nix { };
22+
mkTestRunner =
23+
nixVersion:
24+
writeShellScriptBin "test-runner-${nixVersion}" ''
25+
set -e
26+
export PATH=${
27+
lib.makeBinPath [
28+
bash
29+
direnv
30+
nixVersions.${nixVersion}
31+
coreutils
32+
findutils
33+
gnugrep
34+
]
35+
}
36+
export DIRENV_STDLIB=${direnv-stdlib}
37+
export DIRENVRC="${nix-direnv}/share/nix-direnv/direnvrc"
38+
export BATS_LIB_PATH="${bats-support}:${bats-assert}"
39+
40+
echo run unittest
41+
${lib.getExe' bats "bats"} -x --verbose-run tests/
42+
'';
43+
test-runner-stable = mkTestRunner "stable";
44+
test-runner-latest = mkTestRunner "latest";
45+
in
46+
{
47+
inherit
48+
bats-support
49+
bats-assert
50+
direnv-stdlib
51+
test-runner-stable
52+
test-runner-latest
53+
;
54+
}

tests/nix/bats-assert.nix

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{ fetchFromGitHub, stdenv }:
2+
stdenv.mkDerivation {
3+
name = "bats-assert";
4+
version = "2.1.0+";
5+
src = fetchFromGitHub {
6+
owner = "bats-core";
7+
repo = "bats-assert";
8+
rev = "912a98804efd34f24d5eae1bf97ee622ca770e9"; # master 8/7/2025
9+
hash = "sha256-gp52V4mAiT+Lod2rvEMLhi0Y7AdQQTFCHcNgb8JEKXE=";
10+
};
11+
12+
dontBuild = true;
13+
installPhase = ''
14+
15+
# This looks funny
16+
# but it means that you can use bats' built-in `bats_load_library` easily
17+
# when setting $BATS_LIB_PATH to the string of the derivation.
18+
19+
mkdir -p $out/bats-assert
20+
cp -r src $out/bats-assert/
21+
cp load.bash $out/bats-assert
22+
'';
23+
}

tests/nix/bats-support.nix

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{ stdenv, fetchFromGitHub }:
2+
3+
stdenv.mkDerivation {
4+
name = "bats-support";
5+
version = "3.0+";
6+
src = fetchFromGitHub {
7+
owner = "bats-core";
8+
repo = "bats-support";
9+
rev = "0ad082d4590108684c68975ca517a90459f05cd0";
10+
hash = "sha256-hkPAn12gQudboL9pDpQZhtaMhqyyj885tti4Gx/aun4=";
11+
};
12+
13+
dontBuild = true;
14+
installPhase = ''
15+
16+
# This looks funny
17+
# but it means that you can use bats' built-in `bats_load_library` easily
18+
# when setting $BATS_LIB_PATH to the string of the derivation.
19+
20+
mkdir -p $out/bats-support
21+
cp -r src $out/bats-support/
22+
cp load.bash $out/bats-support/
23+
'';
24+
}

tests/python/tests/__init__.py

Whitespace-only changes.

tests/python/tests/conftest.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)