Simplify test with setUp

This commit is contained in:
vlad 2021-12-21 02:35:10 -08:00
parent 9b11316463
commit d2322c0e5e
2 changed files with 42 additions and 106 deletions

View File

@ -1,5 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
preserve-suite-bookend-functions() {
if type setUp &>/dev/null; then
# shellcheck disable=SC2034
eval "_suite_$(typeset -f setUp)"
else
_suite_setUp() { :; }
fi
if type tearDown &>/dev/null; then
# shellcheck disable=SC2034
eval "_suite_$(typeset -f tearDown)"
else
_suite_tearDown() { :; }
fi
}
preserve-suite-bookend-functions
try() { try() {
output=$(mktemp -d) output=$(mktemp -d)
PATH="$fake_path:$PATH" "$@" > "$output/stdout" 2> "$output/stderr" PATH="$fake_path:$PATH" "$@" > "$output/stdout" 2> "$output/stderr"
@ -15,9 +32,11 @@ setUp() {
export-repo-root export-repo-root
move-to-working-directory move-to-working-directory
create-fake-path create-fake-path
_suite_setUp
} }
tearDown() { tearDown() {
_suite_tearDown
[ "${_shunit_test_:=}" = '' ] && return 0 [ "${_shunit_test_:=}" = '' ] && return 0
clean-up-working-directory clean-up-working-directory

View File

@ -1,46 +1,39 @@
#!/usr/bin/env bash #!/usr/bin/env bash
setUp() {
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-n something \
-v 1337 \
-l http://something/VERSION/linux \
-d http://something/VERSION/darwin \
-- \
"$@"
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
}
testWrapperDownloadsFromLinuxUrlOnLinux() { testWrapperDownloadsFromLinuxUrlOnLinux() {
mock curl mock curl
mock uname Linux mock uname Linux
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-l http://something/linux
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertCalledWithArguments curl http://something/linux assertCalledWithArguments curl linux
} }
testWrapperHasCurlFollowLocationHeader() { testWrapperHasCurlFollowLocationHeader() {
mock curl mock curl
mock uname Linux mock uname Linux
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-l http://something/linux
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertCalledWithArguments curl --location assertCalledWithArguments curl --location
@ -51,43 +44,14 @@ testWrapperDownloadsFromDarwinUrlOnDarwin() {
mock curl mock curl
mock uname Darwin mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-d http://something/darwin
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertCalledWithArguments curl http://something/darwin assertCalledWithArguments curl darwin
} }
testWrapperSubstitutesVERSIONInUrl() { testWrapperSubstitutesVERSIONInUrl() {
mock curl mock curl
mock uname Darwin mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-v 1337 \
-d http://something/VERSION/darwin
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertCalledWithArguments curl http://something/1337/darwin assertCalledWithArguments curl http://something/1337/darwin
} }
@ -99,21 +63,6 @@ testWrapperRunsDownloadedBinary() {
' '
mock uname Darwin mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-d http://something/darwin
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertContains "$STDOUT" "Hello friend" assertContains "$STDOUT" "Hello friend"
} }
@ -124,22 +73,6 @@ testWrapperPersistsBinaryInLocationBasedOnNameAndVersion() {
' '
mock uname Darwin mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-n something \
-v 1337 \
-d http://something/darwin
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello try ./go hello
assertContains "$(<.generated/wrapper/something/1337/bin)" potato assertContains "$(<.generated/wrapper/something/1337/bin)" potato
} }
@ -148,22 +81,6 @@ testWrapperDoesNotRedownloadBinaryIfPresent() {
mock curl 'whatever' mock curl 'whatever'
mock uname Darwin mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-n something \
-v 1337 \
-d http://something/darwin
}
⚡hello() { somethingw ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
mkdir -p .generated/wrapper/something/1337/ mkdir -p .generated/wrapper/something/1337/
touch .generated/wrapper/something/1337/bin touch .generated/wrapper/something/1337/bin
try ./go hello try ./go hello