mirror of
https://github.com/clearlinux/ok.sh.git
synced 2026-08-28 13:25:50 +00:00
bfe7676570
We can't rely on the output order here so perform a few searches instead.
143 lines
3.8 KiB
Bash
Executable File
143 lines
3.8 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
# Unit tests for the octokit.sh script.
|
|
|
|
SCRIPT="octokit.sh"
|
|
JQ="${OCTOKIT_SH_JQ_BIN:-jq}"
|
|
JQ_V="$(jq --version 2>&1 | awk '{ print $3 }')"
|
|
|
|
_main() {
|
|
local cmd ret
|
|
|
|
cmd="$1" && shift
|
|
"$cmd" "$@"
|
|
ret=$?
|
|
|
|
[ $ret -eq 0 ] || printf 'Fail: %s\n' "$cmd" 1>&2
|
|
exit $ret
|
|
}
|
|
|
|
test_format_json() {
|
|
# Test output without filtering through jq.
|
|
|
|
local output
|
|
local is_fail=0
|
|
|
|
$SCRIPT -j _format_json foo=Foo bar=123 baz=true qux=Qux=Qux quux='Multi-line
|
|
string' | {
|
|
read -r output
|
|
|
|
printf '%s\n' "$output" | grep -q -E '^{' || {
|
|
printf 'JSON does not start with a { char.\n'; is_fail=1 ;}
|
|
printf '%s\n' "$output" | grep -q -E '}$' || {
|
|
printf 'JSON does not end with a } char.\n'; is_fail=1 ;}
|
|
printf '%s\n' "$output" | grep -q -E '"foo": "Foo"' || {
|
|
printf 'JSON does not contain "foo": "Foo" text.\n'; is_fail=1 ;}
|
|
printf '%s\n' "$output" | grep -q -E '"Multi-line\\nstring"' || {
|
|
printf 'JSON does not have properly formatted multiline string.\n'; is_fail=1 ;}
|
|
|
|
if [ "$is_fail" -ne 1 ] ; then
|
|
return 0
|
|
else
|
|
printf 'Unexpected JSON output: `%s`\n' "$output"
|
|
return 1
|
|
fi
|
|
}
|
|
}
|
|
|
|
test_format_urlencode() {
|
|
# _format_urlencode
|
|
|
|
local output num_params
|
|
local is_fail=0
|
|
|
|
$SCRIPT _format_urlencode foo='Foo Foo' bar='<Bar>&/Bar/' | {
|
|
read -r output
|
|
|
|
printf '%s\n' "$output" | grep -q -E 'foo=Foo%20Foo' || {
|
|
printf 'Urlencoded output malformed foo section.\n'; is_fail=1 ;}
|
|
|
|
printf '%s\n' "$output" | grep -q -E 'bar=%3CBar%3E%26%2FBar%2F' || {
|
|
printf 'Urlencoded output malformed bar section.\n'; is_fail=1 ;}
|
|
|
|
num_params="$(printf '%s\n' "$output" | awk -F'&' '{ print NF }')"
|
|
if [ "$num_params" -ne 2 ] ; then
|
|
printf 'Urlencoded output has %s sections; expected 2.\n'\
|
|
"$num_params"
|
|
is_fail=1
|
|
fi
|
|
|
|
if [ "$is_fail" -ne 1 ] ; then
|
|
return 0
|
|
else
|
|
printf 'Unexpected urlencoded output\n' "$output"
|
|
return 1
|
|
fi
|
|
}
|
|
}
|
|
|
|
test_format_json_jq() {
|
|
# Test output after filtering through jq.
|
|
|
|
local output keys vals expected_out
|
|
|
|
$SCRIPT _format_json foo=Foo bar=123 baz=true qux=Qux=Qux quux='Multi-line
|
|
string' | {
|
|
read -r output
|
|
|
|
keys=$(printf '%s\n' "$output" | jq -r -c 'keys | .[]' | sort | paste -s -d',' -)
|
|
vals=$(printf '%s\n' "$output" | jq -r -c '.[]' | sort | paste -s -d',' -)
|
|
|
|
if [ 'bar,baz,foo,quux,qux' = "$keys" ] && [ '123,Foo,Multi-line,Qux=Qux,string,true' = "$vals" ] ; then
|
|
return 0
|
|
else
|
|
printf 'Expected output does not match output: `%s` != `%s`\n' \
|
|
"$expected_out" "$output"
|
|
return 1
|
|
fi
|
|
}
|
|
}
|
|
|
|
test_filter_json_args() {
|
|
local json out
|
|
json='["foo", "bar", true, {"qux": "Qux"}]'
|
|
|
|
printf '%s\n' "$json" | $SCRIPT _filter_json 'length' | {
|
|
read -r out
|
|
|
|
if [ 4 -eq "$out" ] ; then
|
|
return 0
|
|
else
|
|
printf 'Expected output does not match output: `%s` != `%s`\n' \
|
|
"$expected_out" "$out"
|
|
return 1
|
|
fi
|
|
}
|
|
}
|
|
|
|
test_response_headers() {
|
|
# Test that process response outputs headers in deterministic order.
|
|
|
|
local baz bar foo
|
|
|
|
printf 'HTTP/1.1 200 OK
|
|
Server: example.com
|
|
Foo: Foo!
|
|
Bar: Bar!
|
|
Baz: Baz!
|
|
|
|
Hi\n' | $SCRIPT _response Baz Bad Foo | {
|
|
read -r baz
|
|
read -r bar # Ensure unfound items are blank.
|
|
read -r foo
|
|
|
|
ret=0
|
|
[ "$baz" = 'Baz!' ] || { ret=1; printf '`Baz!` != `%s`\n' "$baz"; }
|
|
[ "$bar" = '' ] || { ret=1; printf '`` != `%s`\n' "$bar"; }
|
|
[ "$foo" = 'Foo!' ] || { ret=1; printf '`Foo!` != `%s`\n' "$foo"; }
|
|
|
|
return $ret
|
|
}
|
|
}
|
|
|
|
_main "$@"
|