Skip to content

Commit

Permalink
WIP: rely on extracted bats-require library
Browse files Browse the repository at this point in the history
Some time back I turned some bats assertions developed for resholve
into their own library, bats-require. In theory resholve can start to
depend on that now.

That said, I'm stopping here since it's smelling like a yak:
- resholve's tests are attached in nixpkgs, so any test dependencies
  either have to be vendored or included in nixpkgs
- I haven't released bats-require yet, and I probably shouldn't put it
  in nixpkgs until I cut a release
- I should add tests before I release it, but I'm not sure how much
  effort getting all of its affordances under explicit test would be.
  • Loading branch information
abathur committed Sep 16, 2023
1 parent 71f1e48 commit 310f556
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 160 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
resolveTimeDeps = [ pkgs.bash pkgs.coreutils pkgs.file pkgs.findutils pkgs.gettext ];
in {
default = pkgs.mkShell {
buildInputs = [ pkgs.bash pkgs.resholve pkgs.bats ];
buildInputs = [ pkgs.bash pkgs.resholve (pkgs.bats.withLibraries (p: [ bats-require ])) ];
RESHOLVE_PATH = "${pkgs.lib.makeBinPath resolveTimeDeps}";
RESHOLVE_LORE = "${pkgs.binlore.collect { drvs = resolveTimeDeps; } }";
INTERP = "${pkgs.bash}/bin/bash";
Expand Down
3 changes: 2 additions & 1 deletion nixpkgs/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
, jq
, bash
, bats
, bats-require
, libressl
, openssl
, python27
Expand Down Expand Up @@ -127,7 +128,7 @@ rec {
'';
doCheck = true;
buildInputs = [ resholve ];
nativeCheckInputs = [ coreutils bats ];
nativeCheckInputs = [ coreutils (bats.withLibraries (p: [ bats-require ])) ];
# LOGLEVEL="DEBUG";

# default path
Expand Down
1 change: 1 addition & 0 deletions tests/behavior.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# that try to stake out/exercise specific behavior that demonstrate and
# codify where the boundaries of what resholve knows how to handle are.

bats_load_library bats-require
load helpers

# TODO: replace w/ real test once issue is sorted
Expand Down
1 change: 1 addition & 0 deletions tests/cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# it is also covered here. But not the inverse--the demo doesn't need to cover the same
# exact ground as thoroughly as this suite.

bats_load_library bats-require
load helpers

@test "invoking resholve without --interpreter prints an error" {
Expand Down
1 change: 1 addition & 0 deletions tests/demo.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bats_load_library bats-require
load helpers

_cols="${COLUMNS:-80}"
Expand Down
158 changes: 0 additions & 158 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,164 +11,6 @@ teardown() {
} > /dev/null
}

# status <num>
_expect_status() {
if [[ $status != "$1" ]]; then
return 1
fi
}

# line (-)<num> equals|contains|begins|ends|!equals|!contains|!begins|!ends "value"
# CAUTION: one gotcha; blank lines not included; you have to
# adjust down for each one
_expect_line() {
if [[ $1 -lt 0 ]]; then
let lineno=$1
else
# adjust to 0-index
let lineno=$1-1 || true # 1-0 causes let to return 1
fi

local line=${lines[$lineno]} kind=$2
case $kind in
equals)
if [[ $line == "$3" ]]; then
return 0
else
echo " expected line $1:"
echo " '$3'"
echo " actual:"
echo " '$line'"
return 1
fi
;;
contains)
if [[ $line == *"$3"* ]]; then
return 0
else
echo " expected line $1:"
echo " '$3'"
echo " actual:"
echo " '$line'"
return 1
fi
;;
begins)
if [[ $line == "$3"* ]]; then
return 0
else
echo " expected line $1 to begin with:"
echo " '$3'"
echo " actual line:"
echo " '$line'"
return 1
fi
;;
ends)
if [[ $line == *"$3" ]]; then
return 0
else
echo " expected line $1 to end with:"
echo " '$3'"
echo " actual line:"
echo " '$line'"
return 1
fi
;;
!equals)
if [[ $line != "$3" ]]; then
return 0
else
echo " expected line $1:"
echo " '$3'"
echo " actual:"
echo " '$line'"
return 1
fi
;;
!contains)
if [[ $line != *"$3"* ]]; then
return 0
else
echo " expected line $1:"
echo " '$3'"
echo " actual:"
echo " '$line'"
return 1
fi
;;
!begins)
if [[ $line != "$3"* ]]; then
return 0
else
echo " expected line $1 to begin with:"
echo " '$3'"
echo " actual line:"
echo " '$line'"
return 1
fi
;;
!ends)
if [[ $line != *"$3" ]]; then
return 0
else
echo " expected line $1 to end with:"
echo " '$3'"
echo " actual line:"
echo " '$line'"
return 1
fi
;;
esac
# shouldn't get here
echo "unexpected input: $@"
return 2
}

status() {
echo "_expect_status ${@@Q}"
}

line() {
echo "_expect_line ${@@Q}"
}

# cases are on STDIN
# expectations in fd passed as arg 1
require() {
mapfile _cases
mapfile _expectations < "$1"

# TODO: I'd like to print numbers by these in the TAP output, but contrary to the docs they're leaking into the pretty-print output. Worth trying after the next bats version bump.
# casenum=0
for case in "${_cases[@]}"; do
# ((casenum = casenum + 1))#
run eval "${case@E}"
# echo "# ${BATS_TEST_NUMBER}-${casenum}: ${case%$'\n'}" >&3
printf "status: %s\n" $status
printf "output:\n%s" "$output"

echo ""
echo "expectations:"
echo "${_expectations[@]}"
for expected in "${_expectations[@]}"; do
echo "expected=${expected%$'\n'}"
eval "$expected"
done

# TODO: this is probably a little faster
# but it was only respecting the last line
# and my efforts to shim in errexit weren't
# working for some reason and it's hard to debug down under bats.
# if ! source "$1"; then

# # eval "$case"
# false
# fi

done
}

parsers() {
cat parse_*.sh > parsed.sh
resholve --interpreter none --path "${PKG_PARSED}:${PKG_COREUTILS}" < parsed.sh > resolved.sh
Expand Down
1 change: 1 addition & 0 deletions tests/in-file-directives.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# older version of resholve against this suite to learn how compatible it is
# with newer versions of the format.

bats_load_library bats-require
load helpers

@test "modern resholve versions reject v1 files" {
Expand Down

0 comments on commit 310f556

Please sign in to comment.