Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Add the ability to include other test files #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libexec/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ else
shift
fi

BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
export BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
BATS_TEST_NAMES=()

load() {
Expand Down
23 changes: 20 additions & 3 deletions libexec/bats-preprocess
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ encode_name() {
}

tests=()
index=0
pattern='^ *@test *([^ ].*) *\{ *(.*)$'
index=${1:-0}
test_pattern='^ *@test *([^ ].*) *\{ *(.*)$'
include_pattern='^ *include *(.+)$'

while IFS= read -r line; do
let index+=1
if [[ "$line" =~ $pattern ]]; then
if [[ "$line" =~ $include_pattern ]]; then
name="${BASH_REMATCH[1]}"

filename="$BATS_TEST_DIRNAME/${name}.bats"
if [ "${name:0:1}" = "/" ]; then
filename="${name}"
fi

[ -f "$filename" ] || {
echo "bats: $filename does not exist" >&2
exit 1
}

output="$($0 $index < "${filename}")"
let index+=$(echo "$output" | grep -c bats_test_begin)
echo "$output"
elif [[ "$line" =~ $test_pattern ]]; then
quoted_name="${BASH_REMATCH[1]}"
body="${BASH_REMATCH[2]}"
name="$(eval echo "$quoted_name")"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/suite/include/a.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include include/b

export INCLUDER=a

@test "a truth" {
true
}
7 changes: 7 additions & 0 deletions test/fixtures/suite/include/c.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include include/b

export INCLUDER=c

@test "c truth" {
true
}
3 changes: 3 additions & 0 deletions test/fixtures/suite/include/include/b.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@test "shared truth via $INCLUDER" {
true
}
10 changes: 10 additions & 0 deletions test/suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ fixtures suite
[ "${lines[5]}" = "begin 3 quasi-truth" ]
[ "${lines[6]}" = "not ok 3 quasi-truth" ]
}

@test "including shared tests" {
run bats "$FIXTURE_ROOT/include"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..4" ]
echo "$output" | grep "^ok . a truth"
echo "$output" | grep "^ok . c truth"
echo "$output" | grep "^ok . shared truth via a"
echo "$output" | grep "^ok . shared truth via c"
}