diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2022-05-19 20:52:29 +0000 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2022-05-19 20:52:29 +0000 |
commit | cc35918fe2995ccebec2b05a8ed4064c15d414ea (patch) | |
tree | 407b071919f2dceac3fb76b7579ed7bbe0748f75 | |
parent | b282b7b5d82892a9cf717e9e0951088035462ae4 (diff) | |
download | alpine-conf-cc35918fe2995ccebec2b05a8ed4064c15d414ea.zip |
tests: add a fake wget
so we can simulate fetching things from internet
-rwxr-xr-x | tests/bin/wget | 75 | ||||
-rwxr-xr-x | tests/fake_wget_test | 58 |
2 files changed, 133 insertions, 0 deletions
diff --git a/tests/bin/wget b/tests/bin/wget new file mode 100755 index 0000000..72306a4 --- /dev/null +++ b/tests/bin/wget @@ -0,0 +1,75 @@ +#!/bin/sh + +prog=${0##*/} +usage() { + cat <<EOF +usage: wget [-cqS] [--spider] [-O FILE] [-o LOGFILE] [--header STR] + [--post-data STR | --post-file FILE] [-Y on/off] + [-P DIR] [-U AGENT] [-T SEC] URL... + +Retrieve files via HTTP or FTP + + --spider Only check URL existence: \$? is 0 if exists + --header STR Add STR (of form 'header: value') to headers + --post-data STR Send STR using POST method + --post-file FILE Send FILE using POST method + -c Continue retrieval of aborted transfer + -q Quiet + -P DIR Save to DIR (default .) + -S Show server response + -T SEC Network read timeout is SEC seconds + -O FILE Save to FILE ('-' for stdout) + -o LOGFILE Log messages to FILE + -U STR Use STR for User-Agent header + -Y on/off Use proxy +EOF + exit $1 +} + +msg() { + if ! [ -n "$quiet" ]; then + # busybox wget sends to stderr + echo "$@" >&2 + fi +} + +OPTS=$(getopt -l quiet,help,spider -o "qhO:" -n $prog -- "$@") || usage "1" >&2 + +quiet= +eval set -- "$OPTS" +while true; do + opt="$1" + case "$opt" in + -h|--help) + usage 0 + ;; + -q|--quiet) + quiet=1 + ;; + --spider) + exit ${SPIDER_STATUS:-0} + ;; + -O) + shift + outfile="$1" + ;; + --) + shift + break + ;; + *) usage "1" >&2 + ;; + esac + shift +done + +: ${outfile:=index.html} +case "$outfile" in + -) msg "writing to stdout" + echo "$WGETCONTENT" + ;; + *) msg "saving to '$outfile'" + echo "$WGETCONTENT" > "$outfile" + ;; +esac + diff --git a/tests/fake_wget_test b/tests/fake_wget_test new file mode 100755 index 0000000..dac7681 --- /dev/null +++ b/tests/fake_wget_test @@ -0,0 +1,58 @@ +#!/usr/bin/env atf-sh + +. $(atf_get_srcdir)/test_env.sh +init_tests \ + fake_wget_usage \ + fake_wget_spider \ + fake_wget_quiet \ + fake_wget \ + fake_wget_outfile \ + fake_wget_stdout + +fake_wget_usage_body() { + test_usage wget +} + +fake_wget_spider_body() { + init_env + atf_check -s exit:0 \ + -o empty \ + -e empty \ + wget --spider https://example.com +} + +fake_wget_quiet_body() { + init_env + atf_check -s exit:0 \ + -o empty \ + -e empty \ + wget -q https://example.com +} + +fake_wget_body() { + init_env + atf_check -s exit:0 \ + -o empty \ + -e match:"saving to 'index.html'" \ + wget https://example.com + test -f index.html || atf_fail "index.html not created" +} + +fake_wget_outfile_body() { + init_env + atf_check -s exit:0 \ + -o empty \ + -e match:"saving to 'foo'" \ + wget -O foo https://example.com + test -f foo || atf_fail "foo not created" +} + +fake_wget_stdout_body() { + init_env + export WGETCONTENT="hello world" + atf_check -s exit:0 \ + -o match:"hello world" \ + -e match:"writing to stdout" \ + wget -O - https://example.com + ! test -f - || atf_fail "- was created" +} |