From ab5df2edaca2deaaf93e9ec93faa8bc5c218ca66 Mon Sep 17 00:00:00 2001 From: Jay Caines-Gooby Date: Mon, 4 Jul 2022 18:54:11 +0100 Subject: [PATCH] Tests. To run just call 'make' You need to ensure that bats-core is installed: https://github.com/bats-core/bats-core --- Makefile | 9 ++++ tests/tests.bats | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Makefile create mode 100644 tests/tests.bats diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9be6576 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +default: test + +.PHONY: check-bats-exists +check-bats-exists: + @which bats > /dev/null || (echo "Missing dependency. You need to ensure that 'bats' is installed and in your \$$PATH. See https://github.com/bats-core/bats-core" && exit 1) + +.PHONY: test +test: check-bats-exists + bats tests/ diff --git a/tests/tests.bats b/tests/tests.bats new file mode 100644 index 0000000..b414598 --- /dev/null +++ b/tests/tests.bats @@ -0,0 +1,121 @@ +#!/usr/bin/env bats +# +# To skip a test, just uncomment the # skip line + +@test "-? switch" { + # skip + run ./ttfb -? + [[ "$output" =~ "Usage: " ]] +} + +@test "Invalid switch" { + # skip + run ./ttfb -Z + + [ $status -ne 0 ] + [[ "$output" =~ "Usage: " ]] +} + +@test "-d logs requests to ./curl.log" { + # skip + rundir="$(mktemp -d)" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -d example.com + [ $status -eq 0 ] + [ -f "$rundir/curl.log" ] +} + +@test "-l logs to specific file" { + rundir="$(mktemp -d)" + logdir="$(mktemp -d)" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -l "$logdir/log.log" example.com + [ $status -eq 0 ] + [ -f "$logdir/log.log" ] +} + +@test "-l exits if the path the custom log file doesn't exist" { + rundir="$(mktemp -d)" + logdir="$(mktemp -d)" + rmdir "$logdir" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -l "$logdir/log.log" example.com + [ $status -eq 1 ] +} + +@test "-n expects an argument" { + # skip + run ./ttfb -n example.com + [ $status -ne 0 ] + [[ "$output" =~ "Usage: " ]] +} + +@test "-n runs test multiple times" { + # skip + run ./ttfb -n 3 example.com + [ $status -eq 0 ] + [[ "$output" =~ "..." ]] + [[ "$output" =~ "fastest" ]] +} + +@test "can test multiple urls" { + # skip + run ./ttfb example.com example.com/hello-world + [ $status -eq 0 ] + [[ "$output" =~ "example.com" ]] + [[ "$output" =~ "example.com/hello-world" ]] +} + +@test "can test multiple urls multiple times" { + # skip + run ./ttfb -n 3 example.com example.com/hello-world + [ $status -eq 0 ] + [[ "$output" =~ "..." ]] + [[ "$output" =~ "example.com" ]] + [[ "$output" =~ "example.com/hello-world" ]] +} + +@test "-d logs requests to multiple urls to multiple log files" { + # skip + rundir="$(mktemp -d)" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -d example.com example.com/hello-world + [ $status -eq 0 ] + [ -f "$rundir/example_com-curl.log" ] + [ -f "$rundir/example_com_hello_world-curl.log" ] +} + +@test "-l logs requests to multiple urls to multiple custom log files" { + # skip + rundir="$(mktemp -d)" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -l "$rundir/custom.log" example.com example.com/hello-world + [ $status -eq 0 ] + [ -f "$rundir/example_com-custom.log" ] + [ -f "$rundir/example_com_hello_world-custom.log" ] +} + +@test "-d logs multiple requests to multiple urls to multiple log files" { + # skip + rundir="$(mktemp -d)" + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -n2 -d example.com example.com/hello-world + [ $status -eq 0 ] + [ -f "$rundir/example_com-curl.log" ] + [ -f "$rundir/example_com_hello_world-curl.log" ] + [ $(grep " 200 OK" "$rundir/example_com-curl.log" | wc -l) -eq 2 ] + [ $(grep " 404 Not Found" "$rundir/example_com_hello_world-curl.log" | wc -l) -eq 2 ] +} + +@test "-d logs multiple requests to multiple urls to multiple custom log files" { + # skip + rundir="$(mktemp -d)" + echo $rundir + pwd="$PWD" + cd "$rundir" && run "$pwd/ttfb" -n2 -l "$rundir/custom.log" example.com example.com/hello-world + [ $status -eq 0 ] + [ -f "$rundir/example_com-custom.log" ] + [ -f "$rundir/example_com_hello_world-custom.log" ] + [ $(grep " 200 OK" "$rundir/example_com-custom.log" | wc -l) -eq 2 ] + [ $(grep " 404 Not Found" "$rundir/example_com_hello_world-custom.log" | wc -l) -eq 2 ] +}