Skip to content

Commit

Permalink
Polish temp file management in tests
Browse files Browse the repository at this point in the history
This improves the temp file usage for the unit tests so all temp files
are created per test and not per session.  This also removes our need to
cleanup from previous runs before each test.
  • Loading branch information
Justin Griffin committed Dec 1, 2021
1 parent c098aa6 commit 27e6a32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions test/in.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ load '/opt/bats/addons/bats-support/load.bash'
load '/opt/bats/addons/bats-assert/load.bash'
load '/opt/bats/addons/bats-mock/stub.bash'

setup() {
rm -f "$BATS_TMPDIR/resource.json"
}
#setup() {
# do any general setup
#}

source_in() {
stdin_payload=${1:-"stdin-source-with-version"}
Expand Down Expand Up @@ -46,14 +46,14 @@ teardown() {
@test "[in] fetches the resource matching the requested version" {
source_in

target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR
uid="8fca7c5f-c513-11e9-a16f-1831bfd00891"
resourceVersion=22577654

fetchResource

# then a 'resource.json' file contains the retrieved resource
retrieved_resource="$BATS_TMPDIR/resource.json"
retrieved_resource="$BATS_TEST_TMPDIR/resource.json"
assert [ -e "$retrieved_resource" ]

# and it contains the full resource content
Expand All @@ -65,35 +65,35 @@ teardown() {
@test "[in] fetched resource file will be empty if the requested uid is not found" {
source_in

target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR
uid="uid-that-does-not-exist"
resourceVersion=22577654

fetchResource

# then the 'resource.json' file will exist
retrieved_resource="$BATS_TMPDIR/resource.json"
retrieved_resource="$BATS_TEST_TMPDIR/resource.json"
assert [ -e "$retrieved_resource" ]

# but it should be empty
refute [ -s "$retrieved_resource/resource.json" ]
refute [ -s "$retrieved_resource" ]
}

@test "[in] fetched resource file will be empty if the requested resourceVersion does not match" {
source_in

target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR
uid="8fca7c5f-c513-11e9-a16f-1831bfd00891"
resourceVersion=42

fetchResource

# then the 'resource.json' file will exist
retrieved_resource="$BATS_TMPDIR/resource.json"
retrieved_resource="$BATS_TEST_TMPDIR/resource.json"
assert [ -e "$retrieved_resource" ]

# but it should be empty
refute [ -s "$retrieved_resource/resource.json" ]
refute [ -s "$retrieved_resource" ]
}

@test "[in] e2e in" {
Expand All @@ -118,7 +118,7 @@ teardown() {
@test "[in] GH-1: echos fetched resource content by default" {
source_in

target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR

# write out a dummy resource file that we expect our sut to log (publicly)
jq -n "{
Expand All @@ -142,7 +142,7 @@ teardown() {
}

gh1SensitiveFalse() {
target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR

# write out a dummy resource file to use as input
jq -n "{
Expand All @@ -166,7 +166,7 @@ gh1SensitiveFalse() {
}

gh1SensitiveTrue() {
target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR

# write out a dummy resource file to use as input
jq -n "{
Expand Down Expand Up @@ -245,15 +245,15 @@ gh1SensitiveTrue() {
source "$SUT_ASSETS_DIR/in"

# mock the vars that would be set during 'extractVersion()'
target_dir=$BATS_TMPDIR
target_dir=$BATS_TEST_TMPDIR
uid="8fca7c5f-c513-11e9-a16f-1831bfd00891"
resourceVersion=22577654

# run the test
fetchResource

# then a 'resource.json' file contains the retrieved resource
retrieved_resource="$BATS_TMPDIR/resource.json"
retrieved_resource="$BATS_TEST_TMPDIR/resource.json"
assert [ -e "$retrieved_resource" ]

# and it contains the full resource content
Expand Down
4 changes: 2 additions & 2 deletions test/out.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ source_out() {
@test "[out] GH-7 changes into sources directory given" {
source_out

sourcesDirectory "$BATS_TMPDIR"
sourcesDirectory "$BATS_TEST_TMPDIR"

# assert that we changed into the directory
assert_equal $BATS_TMPDIR $PWD
assert_equal $(readlink -m $BATS_TEST_TMPDIR) $(readlink -m $PWD)
}

@test "[out] GH-7 invoke kubectl with args provided by 'params.kubectl'" {
Expand Down

0 comments on commit 27e6a32

Please sign in to comment.