Commit 1ea01785 authored by Signum Temporis's avatar Signum Temporis
Browse files

Add Bats tests

parent 6798d6f4
......@@ -13,7 +13,7 @@ trim_trailing_whitespace = true
indent_size = 2
indent_style = tab
[*.sh]
[*.{sh,bash,bats}]
indent_style = tab
[Makefile]
......
......@@ -4,6 +4,7 @@ MAINTAINER := Signum Temporis <7721809-SignumTemporis@users.noreply.gitlab.com>
PROJECT_URL := https://gitlab.com/SignumTemporis/docker-fpc
TITLE := Free Pascal Compiler (FPC)
BATS := bats
.DEFAULT_GOAL := builds
......@@ -30,6 +31,7 @@ intermediates := fpc-base fpc-main-install fpc-cross-win64-install
targets := fpc-main-full fpc-main-slim fpc-cross-win64-full fpc-cross-win64-slim
cleanTargets := $(addsuffix .clean,$(targets) $(intermediates))
tagTargets := $(addsuffix .tag,$(targets))
testTargets := $(addsuffix .test,$(targets))
pushTargets := $(addsuffix .push,$(targets))
cleantagTargets := $(addsuffix .cleantag,$(targets))
......@@ -50,6 +52,10 @@ clean: $(cleanTargets)
.PHONY: tags
tags: $(tagTargets)
# Test all target images
.PHONY: tests
tests: $(testTargets)
# Push all public tags of target images
.PHONY: push
push: $(pushTargets)
......@@ -113,6 +119,11 @@ $(tagTargets): %.tag:
docker tag $*:latest $(PUBLIC_NAME):$$t; \
done
# Test a target image
.PHONY: $(testTargets)
$(testTargets): %.test:
$(BATS) test/$*.bats
# Push public tags of a target image
.PHONY: $(pushTargets)
$(pushTargets): %.push:
......
......@@ -26,6 +26,16 @@ make builds
make tags
```
### Test tagged images
This needs [`bats`](https://github.com/bats-core/bats-core) to be on path.
```console
make tests
```
Alternatively, override `BATS` variable of `make`, i.e.:
```console
make tests BATS=/opt/bats-core/bin/bats
```
### Remove images
```console
make clean
......
hello
hello.exe
hello.o
#!/usr/bin/env bash
source "$BATS_TEST_DIRNAME"/settings.sh
_common_setup() {
DOCKER_RUN_OPTS="--pull never --rm"
IMG=${BATS_TEST_DESCRIPTION%% *}
IMG=${IMG:1:-1}
}
_common_teardown() {
rm -f "$BATS_TEST_DIRNAME"/hello{,.exe,.o}
}
_test_reports_version() {
run docker run $DOCKER_RUN_OPTS $IMG:latest -iV
[ "$output" = "$FPC_VERSION" ]
}
_test_builds_executable() {
run docker run $DOCKER_RUN_OPTS -v "$BATS_TEST_DIRNAME":/workspace $IMG:latest hello.pas
file -b "$BATS_TEST_DIRNAME"/hello$1 | grep -q "$2"
}
_test_builds_linux_executable() {
_test_builds_executable "" \
"^ELF 64-bit LSB executable, x86-64,.*, for GNU/Linux\b"
}
_test_builds_windows_executable() {
_test_builds_executable ".exe" \
"^PE32+ executable (console) x86-64 .*, for MS Windows$"
}
_test_has_tag() {
local IMG2=${BATS_TEST_DESCRIPTION##* }
local ID=$(docker image inspect --format={{.Id}} $IMG:latest)
local ID2=$(docker image inspect --format={{.Id}} $IMG2)
[ "$ID" == "$ID2" ]
}
# vim: ft=bash noet
source "$BATS_TEST_DIRNAME/settings.sh"
setup() {
load 'common-setup'
_common_setup
}
teardown() {
_common_teardown
}
@test "[fpc-cross-win64-full] reports version $FPC_VERSION" {
_test_reports_version
}
@test "[fpc-cross-win64-full] builds x86-64 Windows executable" {
_test_builds_windows_executable
}
@test "[fpc-cross-win64-full] has tag $PUBLIC_NAME:cross.x86_64-win64" {
_test_has_tag
}
@test "[fpc-cross-win64-full] has tag $PUBLIC_NAME:$FPC_VERSION-cross.x86_64-win64" {
_test_has_tag
}
@test "[fpc-cross-win64-full] has tag $PUBLIC_NAME:cross.x86_64-win64.full" {
_test_has_tag
}
@test "[fpc-cross-win64-full] has tag $PUBLIC_NAME:$FPC_VERSION-cross.x86_64-win64.full" {
_test_has_tag
}
# vim: ft=bash noet
source "$BATS_TEST_DIRNAME/settings.sh"
setup() {
load 'common-setup'
_common_setup
}
teardown() {
_common_teardown
}
@test "[fpc-cross-win64-slim] reports version $FPC_VERSION" {
_test_reports_version
}
@test "[fpc-cross-win64-slim] builds x86-64 Windows executable" {
_test_builds_windows_executable
}
@test "[fpc-cross-win64-slim] has tag $PUBLIC_NAME:cross.x86_64-win64.slim" {
_test_has_tag
}
@test "[fpc-cross-win64-slim] has tag $PUBLIC_NAME:$FPC_VERSION-cross.x86_64-win64.slim" {
_test_has_tag
}
# vim: ft=bash noet
source "$BATS_TEST_DIRNAME/settings.sh"
setup() {
load 'common-setup'
_common_setup
}
teardown() {
_common_teardown
}
@test "[fpc-main-full] reports version $FPC_VERSION" {
_test_reports_version
}
@test "[fpc-main-full] builds x86-64 Linux executable" {
_test_builds_linux_executable
}
@test "[fpc-main-full] has tag $PUBLIC_NAME:latest" {
_test_has_tag
}
@test "[fpc-main-full] has tag $PUBLIC_NAME:$FPC_VERSION" {
_test_has_tag
}
@test "[fpc-main-full] has tag $PUBLIC_NAME:full" {
_test_has_tag
}
@test "[fpc-main-full] has tag $PUBLIC_NAME:$FPC_VERSION-full" {
_test_has_tag
}
# vim: ft=bash noet
source "$BATS_TEST_DIRNAME/settings.sh"
setup() {
load 'common-setup'
_common_setup
}
teardown() {
_common_teardown
}
@test "[fpc-main-slim] reports version $FPC_VERSION" {
_test_reports_version
}
@test "[fpc-main-slim] builds x86-64 Linux executable" {
_test_builds_linux_executable
}
@test "[fpc-main-slim] has tag $PUBLIC_NAME:slim" {
_test_has_tag
}
@test "[fpc-main-slim] has tag $PUBLIC_NAME:$FPC_VERSION-slim" {
_test_has_tag
}
begin
writeln('Hello world!');
end.
FPC_VERSION=3.2.2
PUBLIC_NAME=signumtemporis/fpc
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment