1
0
mirror of https://github.com/jaygooby/ttfb.sh synced 2025-12-10 07:57:21 +01:00

Tests. To run just call 'make'

You need to ensure that bats-core is installed:
https://github.com/bats-core/bats-core
This commit is contained in:
Jay Caines-Gooby
2022-07-04 18:54:11 +01:00
parent 91886e3d27
commit ab5df2edac
2 changed files with 130 additions and 0 deletions

9
Makefile Normal file
View File

@@ -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/

121
tests/tests.bats Normal file
View File

@@ -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 ]
}