Skip to content

Commit

Permalink
Merge pull request #1 from jszwedko/master
Browse files Browse the repository at this point in the history
merging include PR sstephenson#116 in my fork
sstephenson#116
  • Loading branch information
Sylvain303 authored Mar 23, 2017
2 parents 0360811 + b50cee6 commit 12c3dd7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
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"
}

0 comments on commit 12c3dd7

Please sign in to comment.