Skip to content

Commit

Permalink
Add tests for temp_make and temp_del for setup_file and teardown_file
Browse files Browse the repository at this point in the history
  • Loading branch information
NickLarsenNZ committed Nov 19, 2020
1 parent cc4b614 commit 8d93763
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/temp.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ temp_del() {
if ! ( batslib_is_caller --indirect 'teardown' \
|| batslib_is_caller --indirect 'teardown_file' )
then
echo "Must be called from \`teardown' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" \
echo "Must be called from \`teardown' or \`teardown_file' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" \
| batslib_decorate 'ERROR: temp_del' \
| fail
return $?
Expand Down
8 changes: 8 additions & 0 deletions test/70-temp-10-temp_make.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ fixtures 'temp'
bats "${TEST_FIXTURE_ROOT}/temp_make-setup.bats"
}

@test "temp_make() <var>: works when called from \`setup_file'" {
bats "${TEST_FIXTURE_ROOT}/temp_make-setup_file.bats"
}

@test "temp_make() <var>: works when called from \`@test'" {
bats "${TEST_FIXTURE_ROOT}/temp_make-test.bats"
}
Expand All @@ -39,6 +43,10 @@ fixtures 'temp'
bats "${TEST_FIXTURE_ROOT}/temp_make-teardown.bats"
}

@test "temp_make() <var>: works when called from \`teardown_file'" {
bats "${TEST_FIXTURE_ROOT}/temp_make-teardown_file.bats"
}

@test "temp_make() <var>: does not work when called from \`main'" {
run bats "${TEST_FIXTURE_ROOT}/temp_make-main.bats"

Expand Down
31 changes: 28 additions & 3 deletions test/70-temp-11-temp_del.bats
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ fixtures 'temp'
[ -e "$TEST_TEMP_DIR" ]
}

@test "temp_del() <path>: \`BATSLIB_TEMP_PRESERVE_ON_FAILURE' works when called from \`teardown_file'" {
teardown() { rm -r -- "$TEST_TEMP_DIR"; }

TEST_TEMP_DIR="$(temp_make)"
export TEST_TEMP_DIR
run bats "${TEST_FIXTURE_ROOT}/temp_del-teardown_file.bats"

[ "$status" -eq 1 ]
[ -e "$TEST_TEMP_DIR" ]
}

@test "temp_del() <path>: \`BATSLIB_TEMP_PRESERVE_ON_FAILURE' does not work when called from \`main'" {
teardown() { rm -r -- "$TEST_TEMP_DIR"; }

Expand All @@ -99,7 +110,7 @@ fixtures 'temp'
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- ERROR: temp_del --' ]
[ "${lines[1]}" == "Must be called from \`teardown' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]
[ "${lines[1]}" == "Must be called from \`teardown' or \`teardown_file' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]
[ "${lines[2]}" == '--' ]
}

Expand All @@ -113,7 +124,21 @@ fixtures 'temp'
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 10 ]
[[ ${lines[6]} == *'-- ERROR: temp_del --' ]] || false
[[ ${lines[7]} == *"Must be called from \`teardown' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]] || false
[[ ${lines[7]} == *"Must be called from \`teardown' or \`teardown_file' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]] || false
[[ ${lines[8]} == *'--' ]] || false
}

@test "temp_del() <path>: \`BATSLIB_TEMP_PRESERVE_ON_FAILURE' does not work when called from \`setup_file'" {
teardown() { rm -r -- "$TEST_TEMP_DIR"; }

TEST_TEMP_DIR="$(temp_make)"
export TEST_TEMP_DIR
run bats "${TEST_FIXTURE_ROOT}/temp_del-setup_file.bats"

[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 10 ]
[[ ${lines[6]} == *'-- ERROR: temp_del --' ]] || false
[[ ${lines[7]} == *"Must be called from \`teardown' or \`teardown_file' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]] || false
[[ ${lines[8]} == *'--' ]] || false
}

Expand All @@ -127,6 +152,6 @@ fixtures 'temp'
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 10 ]
[[ ${lines[6]} == *'-- ERROR: temp_del --' ]] || false
[[ ${lines[7]} == *"Must be called from \`teardown' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]] || false
[[ ${lines[7]} == *"Must be called from \`teardown' or \`teardown_file' when using \`BATSLIB_TEMP_PRESERVE_ON_FAILURE'" ]] || false
[[ ${lines[8]} == *'--' ]] || false
}
12 changes: 12 additions & 0 deletions test/fixtures/temp/temp_del-setup_file.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats

load 'test_helper'

setup_file() {
local -ir BATSLIB_TEMP_PRESERVE_ON_FAILURE=1
temp_del "$TEST_TEMP_DIR"
}

@test "temp_del() <path>: \`BATSLIB_TEMP_PRESERVE_ON_FAILURE' does not work when called from \`setup_file'" {
true
}
12 changes: 12 additions & 0 deletions test/fixtures/temp/temp_del-teardown_file.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats

load 'test_helper'

@test "temp_del() <path>: \`BATSLIB_TEMP_PRESERVE_ON_FAILURE' works when called from \`teardown_file'" {
false
}

teardown_file() {
local -ir BATSLIB_TEMP_PRESERVE_ON_FAILURE=1
temp_del "$TEST_TEMP_DIR"
}
15 changes: 15 additions & 0 deletions test/fixtures/temp/temp_make-setup_file.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bats

load 'test_helper'

setup_file() {
TEST_TEMP_DIR="$(temp_make)"
}

@test "temp_make() <var>: works when called from \`setup_file'" {
true
}

teardown_file() {
rm -r -- "$TEST_TEMP_DIR"
}
12 changes: 12 additions & 0 deletions test/fixtures/temp/temp_make-teardown_file.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bats

load 'test_helper'

@test "temp_make() <var>: works when called from \`teardown_file'" {
true
}

teardown_file() {
TEST_TEMP_DIR="$(temp_make)"
rm -r -- "$TEST_TEMP_DIR"
}

0 comments on commit 8d93763

Please sign in to comment.