tests can now be run on any script (not just forgebuild in $PATH)

This commit is contained in:
southerntofu 2020-09-15 22:47:03 +02:00
parent 789bbefddf
commit 0b3e7e282a
3 changed files with 53 additions and 19 deletions

View File

@ -16,13 +16,13 @@ In the future, some scripts may be added to convert those JSON files to differen
## Tests ## Tests
This project contains the tests for validating your forgebuild implementation. These tests are written with the [bats](https://github.com/bats-core/bats-core/) testing framework, which runs anywhere bash does. Currently, the forgebuild implementation you want to test needs to be `forgebuild` in the $PATH. This project contains the tests for validating your forgebuild implementation. These tests are written with the [bats](https://github.com/bats-core/bats-core/) testing framework, which runs anywhere bash does.
To run the tests: To run the tests, you can either use `test.sh` or run bats on the tests folder directly, like so:
``` ```
# Requirements on Debian-based systems: $ # Requirements on Debian-based systems:
# sudo apt install bats git $ # sudo apt install bats git
$ git clone https://tildegit.org/forge/build $ git clone https://tildegit.org/forge/build
$ bats build/tests $ bats build/tests
✓ repository is cloned ✓ repository is cloned
@ -39,9 +39,17 @@ $ bats build/tests
10 tests, 0 failures 10 tests, 0 failures
``` ```
If you want to test an implementation of forgebuild that is not `forgebuild` in the $PATH, you can either pass its path as first argument to test.sh, or feed bats with that path in the `FORGEBUILD` environment variable:
```
$ test.sh /tmp/forgebuild.rb
$ # Is the same as
$ FORGEBUILD=/tmp/forgebuild.rb bats tests
```
TODO: TODO:
- [x] basic function tests - [x] basic function tests
- [ ] configurable forgebuild path, so we can test different implementations - [x] configurable forgebuild path, so we can test different implementations
- [x] task settings (global/per-host settings) - [x] task settings (global/per-host settings)
- [ ] PGP signatures - [ ] PGP signatures

22
test.sh Executable file
View File

@ -0,0 +1,22 @@
#! /bin/bash
if ! command -v bats > /dev/null; then
echo "bats testing tool not found. Try sudo apt install bats?"
exit 1
fi
if [ $# -lt 1 ]; then
FORGEBUILD="$(command -v forgebuild)"
if [ ! $? -eq 0 ]; then
echo "forgebuild not found in \$PATH. You can pass a path as argument to this script."
exit 2
fi
else
FORGEBUILD="$(readlink -m "$1")"
if [ ! -x $FORGEBUILD ]; then
echo "File $FORGEBUILD doesn't exist or is not executable."
exit 3
fi
fi
FORGEBUILD="$FORGEBUILD" bats "$(dirname "$0")"/tests

View File

@ -7,6 +7,10 @@
function setup { function setup {
ORIGDIR="$(pwd)" ORIGDIR="$(pwd)"
# If no $FORGEBUILD is supplied in ENV, use the default
# (otherwise you can't `bats ./tests/` manually)
[ -z "$FORGEBUILD" ] && FORGEBUILD="forgebuild"
# Ensure output from previous test does not exist # Ensure output from previous test does not exist
# (in case something crashed) # (in case something crashed)
clean clean
@ -61,35 +65,35 @@ function setup_submodule {
} }
@test "repository is cloned" { @test "repository is cloned" {
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f $FORGEBUILDDIR/.time/README ] [ -f $FORGEBUILDDIR/.time/README ]
} }
@test "first run triggers task" { @test "first run triggers task" {
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
} }
@test "no update does not trigger task" { @test "no update does not trigger task" {
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)" TIME="$(cat /tmp/forgebuild-time)"
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)" NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" = "$NEWTIME" ]] [[ "$TIME" = "$NEWTIME" ]]
} }
@test "forced run triggers task" { @test "forced run triggers task" {
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)" TIME="$(cat /tmp/forgebuild-time)"
forgebuild -f -b $FORGEBUILDDIR time $FORGEBUILD -f -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)" NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" != "$NEWTIME" ]] [[ "$TIME" != "$NEWTIME" ]]
} }
@test "update triggers task" { @test "update triggers task" {
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)" TIME="$(cat /tmp/forgebuild-time)"
cd $GITREPO cd $GITREPO
@ -97,38 +101,38 @@ function setup_submodule {
git add NEWFILE git add NEWFILE
git commit -m "second commit" git commit -m "second commit"
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)" NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" != "$NEWTIME" ]] [[ "$TIME" != "$NEWTIME" ]]
} }
@test "host-based settings are loaded" { @test "host-based settings are loaded" {
forgebuild -b $FORGEBUILDDIR host $FORGEBUILD -b $FORGEBUILDDIR host
[[ "$(cat /tmp/forgebuild-host)" = "host" ]] [[ "$(cat /tmp/forgebuild-host)" = "host" ]]
} }
@test "unknown host uses default settings" { @test "unknown host uses default settings" {
HOST="UNKNOWN" forgebuild -b $FORGEBUILDDIR host HOST="UNKNOWN" $FORGEBUILD -b $FORGEBUILDDIR host
[[ "$(cat /tmp/forgebuild-host)" = "default" ]] [[ "$(cat /tmp/forgebuild-host)" = "default" ]]
} }
@test "submodule is cloned" { @test "submodule is cloned" {
setup_submodule setup_submodule
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f $FORGEBUILDDIR/.time/sub/README ] [ -f $FORGEBUILDDIR/.time/sub/README ]
[[ "$(cat $FORGEBUILDDIR/.time/sub/README)" = "SUB" ]] [[ "$(cat $FORGEBUILDDIR/.time/sub/README)" = "SUB" ]]
} }
@test "first submodule run triggers task" { @test "first submodule run triggers task" {
setup_submodule setup_submodule
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
} }
@test "submodule update does not trigger task by default" { @test "submodule update does not trigger task by default" {
setup_submodule setup_submodule
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ] [ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)" TIME="$(cat /tmp/forgebuild-time)"
@ -137,7 +141,7 @@ function setup_submodule {
git add NEWFILE git add NEWFILE
git commit -m "second commit" git commit -m "second commit"
forgebuild -b $FORGEBUILDDIR time $FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)" NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" = "$NEWTIME" ]] [[ "$TIME" = "$NEWTIME" ]]