Make wrapper work for simple usecase

This commit is contained in:
vlad 2021-12-21 02:00:48 -08:00
parent c7e09adba2
commit bb695de69f
5 changed files with 294 additions and 3 deletions

View File

@ -52,4 +52,19 @@ _verify-prerequisites() {
fi
}
kubectlw() {
_wrapper \
-n kubectl \
-v v1.19.4 \
-l https://dl.k8s.io/release/VERSION/bin/linux/amd64/kubectl \
-d https://dl.k8s.io/release/VERSION/bin/darwin/amd64/kubectl \
-- \
"$@"
}
⚡kubectl-version() {
_help-line "Show version of kubectl wrapper" "$@"
kubectlw version
}
source ⚡

View File

@ -140,11 +140,12 @@ $ echo '
kubectlw() {
_wrapper \
-a "$@" \
-n kubectl \
-v v1.19.4 \
-l https://dl.k8s.io/release/VERSION/bin/linux/amd64/kubectl \
-d https://dl.k8s.io/release/VERSION/bin/darwin/amd64/kubectl
-d https://dl.k8s.io/release/VERSION/bin/darwin/amd64/kubectl \
-- \
"$@"
}
⚡kubectl-version() {

View File

@ -2,7 +2,7 @@
try() {
output=$(mktemp -d)
"$@" > "$output/stdout" 2> "$output/stderr"
PATH="$fake_path:$PATH" "$@" > "$output/stdout" 2> "$output/stderr"
EXIT_CODE=$?
STDOUT=$(<"$output/stdout")
STDERR=$(<"$output/stderr")
@ -14,12 +14,14 @@ setUp() {
export TMPDIR="$PWD/.generated"
export-repo-root
move-to-working-directory
create-fake-path
}
tearDown() {
[ "${_shunit_test_:=}" = '' ] && return 0
clean-up-working-directory
clean-up-fake-path
if [ "${__shunit_testSuccess:?}" != 0 ]; then
output-outputs
@ -39,11 +41,20 @@ move-to-working-directory() {
cd $working_dir
}
create-fake-path() {
fake_path=$(mktemp -d)
invokations=$(mktemp -d)
}
clean-up-working-directory() {
cd $original_dir
rm -rf $working_dir
}
clean-up-fake-path() {
rm -rf $fake_path $invokations
}
output-outputs() {
(
bold '*******Test failed********'
@ -61,6 +72,39 @@ bold() {
echo -e "\033[0m"
}
mock() {
command="$1"; shift
[[ -n "$1" ]] && output="$1"; shift
if ! [[ -f $fake_path/$command ]]; then
echo 'echo "$@" >> '$invokations/$command > $fake_path/$command
chmod +x $fake_path/$command
fi
if [[ -n "$output" ]]; then
echo "echo '$output'" >> $fake_path/$command
fi
}
assertCalledWithArguments() {
if ! [[ -f $invokations/$1 ]]; then
fail "$1 never invoked"
return 1
fi
if ! assertContains "$1 not invoked with expected arguments:" "$(<$invokations/$1)" "$2"; then
bold Actual invokations:
cat "$invokations/$1"
return 1
fi
}
assertNotCalled() {
if [[ -f $invokations/$1 ]]; then
fail "$1 has been invoked"
return 1
fi
}
SHUNIT_PATH=./.generated/shunit2-2.1.8/shunit2
if [ ! -f $SHUNIT_PATH ]; then (
mkdir -p .generated

202
test/test-wrapper Normal file
View File

@ -0,0 +1,202 @@
#!/usr/bin/env bash
testWrapperDownloadsFromLinuxUrlOnLinux() {
mock curl
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
assertCalledWithArguments curl http://something/linux
}
testWrapperHasCurlFollowLocationHeader() {
mock curl
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
assertCalledWithArguments curl --location
}
testWrapperDownloadsFromDarwinUrlOnDarwin() {
mock curl
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
assertCalledWithArguments curl http://something/darwin
}
testWrapperSubstitutesVERSIONInUrl() {
mock curl
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
assertCalledWithArguments curl http://something/1337/darwin
}
testWrapperRunsDownloadedBinary() {
mock curl '#!/usr/bin/env bash
printf "Hello "
printf "friend"
'
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
assertContains "$STDOUT" "Hello friend"
}
testWrapperPersistsBinaryInLocationBasedOnNameAndVersion() {
mock curl '#!/usr/bin/env true
potato
'
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
assertContains "$(<.generated/wrapper/something/1337/bin)" potato
}
testWrapperDoesNotRedownloadBinaryIfPresent() {
mock curl 'whatever'
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/
touch .generated/wrapper/something/1337/bin
try ./go hello
assertNotCalled curl
assertNotContains "$(<.generated/wrapper/something/1337/bin)" potato
}
testWrapperPassesArgumentsToExecutedBinary() {
mock curl '#!/usr/bin/env bash
echo "$@"
'
mock uname Darwin
echo '
#!/usr/bin/env bash
somethingw() {
_wrapper \
-n something \
-v 1337 \
-d http://something/darwin \
-- \
"$@"
}
⚡hello() { somethingw Hello friend ; }
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go hello
assertContains "$STDOUT" "Hello friend"
}
source $(dirname $0)/runner

29
View File

@ -49,6 +49,35 @@ _bad-message() {
echo -e "\e[31m$@\e[0m"
}
_wrapper() {
unset OPTIND
while getopts "l:d:n:v:" opt; do
case $opt in
l) linux_url=$OPTARG ;;
d) darwin_url=$OPTARG ;;
n) name=$OPTARG ;;
v) version=$OPTARG ;;
esac
done
while [[ -n "$1" && "$1" != "--" ]]; do
shift
done
shift
if [[ $(uname -s) == Linux ]]; then
url=$linux_url
else
url=$darwin_url
fi
wrapper_root=".generated/wrapper/$name/$version/"
mkdir -p $wrapper_root
[[ -f $wrapper_root/bin ]] || curl --location ${url//VERSION/$version} > $wrapper_root/bin
chmod +x $wrapper_root/bin
./$wrapper_root/bin "$@"
}
[[ $(type -t _verify-prerequisites) == "function" ]] && ! _verify-prerequisites && exit 1
main "$@"