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
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:
# sudo apt install bats git
$ # Requirements on Debian-based systems:
$ # sudo apt install bats git
$ git clone https://tildegit.org/forge/build
$ bats build/tests
✓ repository is cloned
@ -39,9 +39,17 @@ $ bats build/tests
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:
- [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)
- [ ] 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 {
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
# (in case something crashed)
clean
@ -61,35 +65,35 @@ function setup_submodule {
}
@test "repository is cloned" {
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f $FORGEBUILDDIR/.time/README ]
}
@test "first run triggers task" {
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
}
@test "no update does not trigger task" {
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)"
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" = "$NEWTIME" ]]
}
@test "forced run triggers task" {
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)"
forgebuild -f -b $FORGEBUILDDIR time
$FORGEBUILD -f -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" != "$NEWTIME" ]]
}
@test "update triggers task" {
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)"
cd $GITREPO
@ -97,38 +101,38 @@ function setup_submodule {
git add NEWFILE
git commit -m "second commit"
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" != "$NEWTIME" ]]
}
@test "host-based settings are loaded" {
forgebuild -b $FORGEBUILDDIR host
$FORGEBUILD -b $FORGEBUILDDIR host
[[ "$(cat /tmp/forgebuild-host)" = "host" ]]
}
@test "unknown host uses default settings" {
HOST="UNKNOWN" forgebuild -b $FORGEBUILDDIR host
HOST="UNKNOWN" $FORGEBUILD -b $FORGEBUILDDIR host
[[ "$(cat /tmp/forgebuild-host)" = "default" ]]
}
@test "submodule is cloned" {
setup_submodule
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f $FORGEBUILDDIR/.time/sub/README ]
[[ "$(cat $FORGEBUILDDIR/.time/sub/README)" = "SUB" ]]
}
@test "first submodule run triggers task" {
setup_submodule
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
}
@test "submodule update does not trigger task by default" {
setup_submodule
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
[ -f /tmp/forgebuild-time ]
TIME="$(cat /tmp/forgebuild-time)"
@ -137,7 +141,7 @@ function setup_submodule {
git add NEWFILE
git commit -m "second commit"
forgebuild -b $FORGEBUILDDIR time
$FORGEBUILD -b $FORGEBUILDDIR time
NEWTIME="$(cat /tmp/forgebuild-time)"
[[ "$TIME" = "$NEWTIME" ]]