Skip to content

Commit c0270d9

Browse files
authored
Merge pull request #598 from nix-community/bats_testing
Switch to bats over pytest
2 parents 9087701 + c59b60e commit c0270d9

20 files changed

+347
-336
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

direnvrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ _nix() {
3737
}
3838

3939
_require_version() {
40-
local cmd=$1 version=$2 required=$3
40+
local cmd=$1 raw_version=$2 version=${2%%[^0-9.]*} required=$3
4141
if ! printf "%s\n" "$required" "$version" | LC_ALL=C sort -c -V 2>/dev/null; then
4242
_nix_direnv_error \
43-
"minimum required $(basename "$cmd") version is $required (installed: $version)"
43+
"minimum required $(basename "$cmd") version is $required (installed: $raw_version)"
4444
return 1
4545
fi
4646
}
@@ -52,7 +52,7 @@ _require_cmd_version() {
5252
return 1
5353
fi
5454
version=$($cmd --version)
55-
[[ $version =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]
55+
[[ $version =~ ([0-9]+\.[0-9]+(\.[0-9]+)?) ]]
5656
_require_version "$cmd" "${BASH_REMATCH[1]}" "$required"
5757
}
5858

flake.nix

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +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 = {
38-
nix-direnv = pkgs.callPackage ./default.nix { };
39-
default = config.packages.nix-direnv;
40-
test-runner-stable = pkgs.callPackage ./test-runner.nix { nixVersion = "stable"; };
41-
test-runner-latest = pkgs.callPackage ./test-runner.nix { nixVersion = "latest"; };
41+
packages = test_pkgs // {
42+
inherit nix-direnv;
43+
default = nix-direnv;
4244
};
4345

4446
devShells.default = pkgs.callPackage ./shell.nix {
45-
packages = [
46-
config.treefmt.build.wrapper
47-
pkgs.shellcheck
48-
];
47+
treefmt = config.treefmt.build.wrapper;
4948
};
5049

5150
checks =

shell.nix

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
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+
with test_pkgs;
15+
[
16+
bats-support
17+
bats-assert
18+
]
19+
);
20+
packages =
21+
(builtins.attrValues {
22+
inherit (pkgs)
23+
bats
24+
direnv
25+
shellcheck
26+
;
27+
})
28+
++ (builtins.attrValues (lib.attrsets.filterAttrs (name: _val: name != "direnv-stdlib") test_pkgs))
29+
++ lib.optionals (treefmt != null) [ treefmt ];
1430
}

test-runner.nix

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

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 0 additions & 18 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/direnv_project.py

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

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 they mean 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+
}

0 commit comments

Comments
 (0)