mirror of
https://github.com/nix-community/nix-direnv.git
synced 2025-11-08 19:46:11 +01:00
Improve version testing logic and add relevant tests
This commit is contained in:
parent
add4b907c0
commit
445dc9ffc6
14 changed files with 62 additions and 10 deletions
6
direnvrc
6
direnvrc
|
|
@ -37,10 +37,10 @@ _nix() {
|
|||
}
|
||||
|
||||
_require_version() {
|
||||
local cmd=$1 version=$2 required=$3
|
||||
local cmd=$1 raw_version=$2 version=${2%%[^0-9.]*} required=$3
|
||||
if ! printf "%s\n" "$required" "$version" | LC_ALL=C sort -c -V 2>/dev/null; then
|
||||
_nix_direnv_error \
|
||||
"minimum required $(basename "$cmd") version is $required (installed: $version)"
|
||||
"minimum required $(basename "$cmd") version is $required (installed: $raw_version)"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ _require_cmd_version() {
|
|||
return 1
|
||||
fi
|
||||
version=$($cmd --version)
|
||||
[[ $version =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]
|
||||
[[ $version =~ ([0-9]+\.[0-9]+(\.[0-9]+)?) ]]
|
||||
_require_version "$cmd" "${BASH_REMATCH[1]}" "$required"
|
||||
}
|
||||
|
||||
|
|
|
|||
12
flake.nix
12
flake.nix
|
|
@ -34,11 +34,17 @@
|
|||
...
|
||||
}:
|
||||
{
|
||||
packages = {
|
||||
packages = rec {
|
||||
nix-direnv = pkgs.callPackage ./default.nix { };
|
||||
default = config.packages.nix-direnv;
|
||||
test-runner-stable = pkgs.callPackage ./test-runner.nix { nixVersion = "stable"; };
|
||||
test-runner-latest = pkgs.callPackage ./test-runner.nix { nixVersion = "latest"; };
|
||||
test-runner-stable = pkgs.callPackage ./test-runner.nix {
|
||||
nixVersion = "stable";
|
||||
inherit nix-direnv;
|
||||
};
|
||||
test-runner-latest = pkgs.callPackage ./test-runner.nix {
|
||||
nixVersion = "latest";
|
||||
inherit nix-direnv;
|
||||
};
|
||||
};
|
||||
|
||||
devShells.default = pkgs.callPackage ./shell.nix {
|
||||
|
|
|
|||
|
|
@ -5,9 +5,18 @@
|
|||
lib,
|
||||
coreutils,
|
||||
gnugrep,
|
||||
bats,
|
||||
nixVersions,
|
||||
nixVersion,
|
||||
nix-direnv,
|
||||
fetchurl,
|
||||
}:
|
||||
let
|
||||
direnv-stdlib = fetchurl {
|
||||
url = "https://raw.githubusercontent.com/direnv/direnv/refs/tags/v2.37.0/stdlib.sh";
|
||||
hash = "sha256-MMM04OXhqS/rRSuv8uh7CD70Z7CaGT63EtL/3LC08qM=";
|
||||
};
|
||||
in
|
||||
writeShellScriptBin "test-runner-${nixVersion}" ''
|
||||
set -e
|
||||
export PATH=${
|
||||
|
|
@ -18,7 +27,12 @@ writeShellScriptBin "test-runner-${nixVersion}" ''
|
|||
gnugrep
|
||||
]
|
||||
}
|
||||
export DIRENV_STDLIB=${direnv-stdlib}
|
||||
export DIRENVRC="${nix-direnv}/share/nix-direnv/direnvrc"
|
||||
|
||||
echo run unittest
|
||||
${lib.getExe' python3.pkgs.pytest "pytest"} .
|
||||
echo run python unittest
|
||||
${lib.getExe' python3.pkgs.pytest "pytest"} tests/python/
|
||||
|
||||
echo run bash unittest
|
||||
${lib.getExe' bats "bats"} -x --verbose-run tests/bash/
|
||||
''
|
||||
|
|
|
|||
32
tests/bash/test_versions.bats
Normal file
32
tests/bash/test_versions.bats
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
load "$DIRENV_STDLIB"
|
||||
load "$DIRENVRC"
|
||||
|
||||
@test "test _require_version with valid versions" {
|
||||
# args: cmd version minimum_required
|
||||
run _require_version "test-cmd" "2.5" "2.4"
|
||||
[ "$status" -eq 0 ]
|
||||
run _require_version "test-cmd" "2.5" "2.4.1"
|
||||
[ "$status" -eq 0 ]
|
||||
run _require_version "test-cmd" "2.4.1" "2.4"
|
||||
[ "$status" -eq 0 ]
|
||||
run _require_version "test-cmd" "2.4" "2.4.1"
|
||||
[ "$status" -eq 1 ]
|
||||
run _require_version "test-cmd" "2.31pre20250712_b1245123" "2.4"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
|
||||
test_cmd1() {
|
||||
echo "1.2"
|
||||
}
|
||||
|
||||
test_cmd2() {
|
||||
echo "1.2.3"
|
||||
}
|
||||
|
||||
@test "test _require_cmd_version with valid versions" {
|
||||
run _require_cmd_version "test_cmd1" "1.1"
|
||||
[ "$status" -eq 0 ]
|
||||
run _require_cmd_version "test_cmd2" "1.1.1"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ from pathlib import Path
|
|||
import pytest
|
||||
|
||||
TEST_ROOT = Path(__file__).parent.resolve()
|
||||
PROJECT_ROOT = TEST_ROOT.parent
|
||||
PROJECT_ROOT = TEST_ROOT.parents[2]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -17,6 +17,6 @@ def test_root() -> Path:
|
|||
@pytest.fixture
|
||||
def project_root() -> Path:
|
||||
"""
|
||||
Root directory of the tests
|
||||
Root directory of the project
|
||||
"""
|
||||
return PROJECT_ROOT
|
||||
Loading…
Add table
Add a link
Reference in a new issue