diff options
author | EuAndreh <eu@euandre.org> | 2024-08-10 21:02:25 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-08-10 21:02:25 -0300 |
commit | b03960fd93e5a1039754321a3562d48483801191 (patch) | |
tree | 65a8a41ebd07f3a46d8939863bfd4850ae4ae403 /tests | |
parent | Initial implementation (diff) | |
download | glaze-b03960fd93e5a1039754321a3562d48483801191.tar.gz glaze-b03960fd93e5a1039754321a3562d48483801191.tar.xz |
Build with "go tool" and remove uneeded dependencies
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/cli-opts.sh | 46 | ||||
-rw-r--r-- | tests/glaze.go | 36 | ||||
-rwxr-xr-x | tests/integration.sh | 73 | ||||
-rw-r--r-- | tests/lib.sh | 119 | ||||
-rw-r--r-- | tests/lib_test.go | 13 | ||||
-rw-r--r-- | tests/main.go | 7 | ||||
-rw-r--r-- | tests/resources/index.html | 4 | ||||
l--------- | tests/resources/link.txt | 1 | ||||
-rw-r--r-- | tests/resources/original.txt | 10 |
9 files changed, 295 insertions, 14 deletions
diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index fcb62ca..96d4726 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -1,4 +1,48 @@ #!/bin/sh set -eu -exit +. tests/lib.sh + + +test_needs_2_arguments() { + testing 'needs 2 arguments' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin FROM-ADDR 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./binder.bin TO-ADDR 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 2 + assert_empty_stdout + assert_usage "$ERR" + rm -f "$OUT" "$ERR" + + test_ok +} + + +exit # FIXME +test_needs_2_arguments diff --git a/tests/glaze.go b/tests/glaze.go new file mode 100644 index 0000000..366efa0 --- /dev/null +++ b/tests/glaze.go @@ -0,0 +1,36 @@ +package glaze + +import ( + g "gobang" +) + + +func test_adjustPattern() { + inputs := []string { + "", + "/", + "/*", + "/abc*", + "/abc/", + "/abc/*", + "abc/*", + } + expected := []string { + "{$}", + "/{$}", + "/", + "/abc", + "/abc/{$}", + "/abc/", + "abc/", + } + + for i, input := range inputs { + g.AssertEqual(adjustPattern(input), expected[i]) + } +} + + +func MainTest() { + test_adjustPattern() +} diff --git a/tests/integration.sh b/tests/integration.sh new file mode 100755 index 0000000..e0a92dc --- /dev/null +++ b/tests/integration.sh @@ -0,0 +1,73 @@ +#!/bin/sh +set -eu + +. tests/lib.sh + + +test_exits_when_upstream_errors() { + testing 'exits when upstream errors' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR" s1.socket client.txt' EXIT + + rm -f s1.socket + ./binder.bin localhost:1234 s1.socket 1>"$OUT" 2>"$ERR" & + pid=$! + while ! lsof -s TCP:LISTEN -i :1234 > /dev/null; do + true + done + + echo request | socat tcp-connect:localhost:1234 stdio > client.txt + kill $pid + wait + + assert_fgrep_stdout 'listen-start' + assert_fgrep_stdout 'active-connections' + assert_fgrep_stdout 'dial-connection' + assert_empty_stderr + assert_empty_stream 'client.txt' client.txt + rm -f "$OUT" "$ERR" s1.socket client.txt + + test_ok +} + +test_works_from_client_to_server() { + testing 'works from client to server' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR" s2.socket client.txt server.txt' EXIT + + rm -f s2.socket + ./binder.bin localhost:1234 s2.socket 1>"$OUT" 2>"$ERR" & + pid=$! + while ! lsof -s TCP:LISTEN -i :1234 > /dev/null; do + true + done + + echo response | socat unix-listen:s2.socket stdio > server.txt & + while [ ! -S s2.socket ]; do + true + done + + echo request | socat tcp-connect:localhost:1234 stdio > client.txt + kill $pid + wait + + assert_fgrep_stdout 'listen-start' + assert_fgrep_stdout 'active-connections' + assert_empty_stderr + assert_fgrep_stream 'client.txt' client.txt 'response' + assert_fgrep_stream 'server.txt' server.txt 'request' + rm -f "$OUT" "$ERR" s2.socket client.txt server.txt + + test_ok +} + + +exit # FIXME +test_exits_when_upstream_errors +test_works_from_client_to_server diff --git a/tests/lib.sh b/tests/lib.sh new file mode 100644 index 0000000..07ecbef --- /dev/null +++ b/tests/lib.sh @@ -0,0 +1,119 @@ +#!/bin/sh + +end="\033[0m" +red="\033[0;31m" +green="\033[0;32m" +yellow="\033[0;33m" + +N= +OUT= +ERR= +STATUS= + +ERROR() { + # shellcheck disable=2059 + printf "${red}ERROR${end}" +} + +print_debug_info() { + # shellcheck disable=2016 + printf 'LINENO: %s\n$OUT: %s\n$ERR: %s\n' \ + "$N" "$OUT" "$ERR" >&2 +} + +assert_status() { + if [ "$STATUS" != "$1" ]; then + printf '\n%s: Bad status.\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$STATUS" >&2 + print_debug_info + exit 1 + fi +} + +assert_usage() { + if ! grep -Fq 'Usage' "$1"; then + echo 'Expected to find "Usage" text, it was missing:' >&2 + cat "$1" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stream() { + if [ -s "$2" ]; then + FMT='\n%s: Expected %s (%s) to be empty, but has content:\n%s\n' + # shellcheck disable=2059 + printf "$FMT" \ + "$(ERROR)" "$1" "$2" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stdout() { + assert_empty_stream STDOUT "$OUT" +} + +assert_empty_stderr() { + assert_empty_stream STDERR "$ERR" +} + +assert_stream() { + if [ "$(cat "$2")" != "$3" ]; then + printf '\n%s: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_stdout() { + assert_stream STDOUT "$OUT" "$1" +} + +assert_stderr() { + assert_stream STDERR "$ERR" "$1" +} + +assert_grep_stream() { + if ! grep -qE "$3" "$2"; then + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_grep_stdout() { + assert_grep_stream STDOUT "$OUT" "$1" +} + +assert_grep_stderr() { + assert_grep_stream STDERR "$ERR" "$1" +} + +assert_fgrep_stream() { + if ! grep -Fq -- "$3" "$2"; then + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_fgrep_stdout() { + assert_fgrep_stream STDOUT "$OUT" "$1" +} + +assert_fgrep_stderr() { + assert_fgrep_stream STDERR "$ERR" "$1" +} + +testing() { + printf "${yellow}testing${end}: %s..." "$1" >&2 +} + +test_ok() { + # shellcheck disable=2059 + printf " ${green}OK${end}.\n" >&2 +} diff --git a/tests/lib_test.go b/tests/lib_test.go deleted file mode 100644 index 31259c6..0000000 --- a/tests/lib_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package lib_test - -import ( - "testing" - - "euandre.org/glaze/src" -) - -func TestPlaceholder(t *testing.T) { - if (&glaze.PatternPath{}).String() != "FIXME" { - t.Fail() - } -} diff --git a/tests/main.go b/tests/main.go new file mode 100644 index 0000000..835ef3a --- /dev/null +++ b/tests/main.go @@ -0,0 +1,7 @@ +package main + +import "glaze" + +func main() { + glaze.MainTest() +} diff --git a/tests/resources/index.html b/tests/resources/index.html new file mode 100644 index 0000000..4a3ce66 --- /dev/null +++ b/tests/resources/index.html @@ -0,0 +1,4 @@ +olar + +oiwjef + diff --git a/tests/resources/link.txt b/tests/resources/link.txt new file mode 120000 index 0000000..44ab045 --- /dev/null +++ b/tests/resources/link.txt @@ -0,0 +1 @@ +original.txt
\ No newline at end of file diff --git a/tests/resources/original.txt b/tests/resources/original.txt new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/tests/resources/original.txt @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 |