mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
test: Don't run "go build" each time a helper is invoked
Our tests invoke a variety of helpers, some of them are written in Go. Today, we call "go build" (directly or indirectly via "go run"), which is a bit wasteful and slows down the tests. This patch makes the tests only build our Go helpers once every 10s at most. The solution is a bit hacky but in the context of these tests, it's practical.
This commit is contained in:
@@ -44,8 +44,12 @@ GOCOVERDIR="${COVER_DIR}/sh" setsid -w ./cmd/chasquid-util/test.sh
|
|||||||
go tool covdata merge -i "${COVER_DIR}/go,${COVER_DIR}/sh" -o "${COVER_DIR}/all"
|
go tool covdata merge -i "${COVER_DIR}/go,${COVER_DIR}/sh" -o "${COVER_DIR}/all"
|
||||||
go tool covdata textfmt -i "${COVER_DIR}/all" -o "${COVER_DIR}/merged.out"
|
go tool covdata textfmt -i "${COVER_DIR}/all" -o "${COVER_DIR}/merged.out"
|
||||||
|
|
||||||
# Ignore protocol buffer-generated files, as they are not relevant.
|
# Ignore protocol buffer-generated files and test utilities, as they are not
|
||||||
grep -v ".pb.go:" < "${COVER_DIR}/merged.out" > "${COVER_DIR}/final.out"
|
# relevant.
|
||||||
|
cat "${COVER_DIR}/merged.out" \
|
||||||
|
| grep -v ".pb.go:" \
|
||||||
|
| grep -v "blitiri.com.ar/go/chasquid/test/util/" \
|
||||||
|
> "${COVER_DIR}/final.out"
|
||||||
|
|
||||||
# Generate reports based on the merged output.
|
# Generate reports based on the merged output.
|
||||||
go tool cover -func="$COVER_DIR/final.out" | sort -k 3 -n > "$COVER_DIR/func.txt"
|
go tool cover -func="$COVER_DIR/final.out" | sort -k 3 -n > "$COVER_DIR/func.txt"
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
# Run from the config directory because data_dir is relative.
|
# Run from the config directory because data_dir is relative.
|
||||||
cd config || exit 1
|
cd config || exit 1
|
||||||
go run ../../../cmd/chasquid-util/ -C=. "$@"
|
../../../cmd/chasquid-util/chasquid-util -C=. "$@"
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ fi
|
|||||||
|
|
||||||
# Test chasquid-util's ability to do alias resolution talking to chasquid.
|
# Test chasquid-util's ability to do alias resolution talking to chasquid.
|
||||||
# We use chamuyero for convenience, so we can match the output exactly.
|
# We use chamuyero for convenience, so we can match the output exactly.
|
||||||
|
# We run it once to ensure it gets built.
|
||||||
|
chasquid-util --help > /dev/null
|
||||||
for i in *.cmy; do
|
for i in *.cmy; do
|
||||||
if ! chamuyero "$i" > "$i.log" 2>&1 ; then
|
if ! chamuyero "$i" > "$i.log" 2>&1 ; then
|
||||||
echo "$i failed, log follows"
|
echo "$i failed, log follows"
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ function init() {
|
|||||||
if [ "${RACE}" == "1" ]; then
|
if [ "${RACE}" == "1" ]; then
|
||||||
GOFLAGS="$GOFLAGS -race"
|
GOFLAGS="$GOFLAGS -race"
|
||||||
fi
|
fi
|
||||||
|
if [ "${GOCOVERDIR}" != "" ]; then
|
||||||
|
GOFLAGS="$GOFLAGS -cover -covermode=count"
|
||||||
|
fi
|
||||||
|
|
||||||
# Remove the directory where test-mda will deliver mail, so previous
|
# Remove the directory where test-mda will deliver mail, so previous
|
||||||
# runs don't interfere with this one.
|
# runs don't interfere with this one.
|
||||||
@@ -27,12 +30,7 @@ function init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function chasquid() {
|
function chasquid() {
|
||||||
if [ "${GOCOVERDIR}" != "" ]; then
|
go-build-cached "${TBASE}/../../"
|
||||||
GOFLAGS="-cover -covermode=count -o chasquid $GOFLAGS"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
|
||||||
( cd "${TBASE}/../../" || exit 1; go build $GOFLAGS -tags="$GOTAGS" . )
|
|
||||||
|
|
||||||
# HOSTALIASES: so we "fake" hostnames.
|
# HOSTALIASES: so we "fake" hostnames.
|
||||||
# PATH: so chasquid can call test-mda without path issues.
|
# PATH: so chasquid can call test-mda without path issues.
|
||||||
@@ -43,12 +41,37 @@ function chasquid() {
|
|||||||
"${TBASE}/../../chasquid" "$@"
|
"${TBASE}/../../chasquid" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function go-build-cached() { (
|
||||||
|
# This runs "go build" on the given directory, but only once every
|
||||||
|
# 10s, or if the build flags/tags change.
|
||||||
|
# Because in tests we run some of the Go programs often, this speeds
|
||||||
|
# up the tests.
|
||||||
|
cd "$1" || exit 1
|
||||||
|
touch -d "10 seconds ago" .reference
|
||||||
|
echo "-tags=$GOTAGS : $GOFLAGS" > .flags-new
|
||||||
|
if
|
||||||
|
! cmp -s .flags-new .flags >/dev/null 2>&1 ||
|
||||||
|
[ "$(basename "$PWD")" -ot ".reference" ] ;
|
||||||
|
then
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
go build -tags="$GOTAGS" $GOFLAGS
|
||||||
|
|
||||||
|
# Write to .flags instead of renaming, to prevent races where
|
||||||
|
# was .flags-new is already renamed by the time we get here.
|
||||||
|
# Do this _after_ the build so worst case we build twice,
|
||||||
|
# instead of having the chance to run an old binary.
|
||||||
|
echo "-tags=$GOTAGS : $GOFLAGS" > .flags
|
||||||
|
fi
|
||||||
|
) }
|
||||||
|
|
||||||
|
|
||||||
function chasquid-util() {
|
function chasquid-util() {
|
||||||
# Run chasquid-util from inside the config dir, since in our tests
|
# Run chasquid-util from inside the config dir, since in our tests
|
||||||
# data_dir is relative to the config.
|
# data_dir is relative to the config.
|
||||||
|
go-build-cached "${TBASE}/../../cmd/chasquid-util/"
|
||||||
CONFDIR="${CONFDIR:-config}"
|
CONFDIR="${CONFDIR:-config}"
|
||||||
( cd "$CONFDIR" && \
|
( cd "$CONFDIR" && \
|
||||||
go run "${TBASE}/../../cmd/chasquid-util/" \
|
"${TBASE}/../../cmd/chasquid-util/chasquid-util" \
|
||||||
-C=. \
|
-C=. \
|
||||||
"$@" \
|
"$@" \
|
||||||
)
|
)
|
||||||
@@ -80,7 +103,8 @@ function add_user() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function dovecot-auth-cli() {
|
function dovecot-auth-cli() {
|
||||||
go run "${TBASE}/../../cmd/dovecot-auth-cli/dovecot-auth-cli.go" "$@"
|
go-build-cached "${TBASE}/../../cmd/dovecot-auth-cli/"
|
||||||
|
"${TBASE}/../../cmd/dovecot-auth-cli/dovecot-auth-cli" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function run_msmtp() {
|
function run_msmtp() {
|
||||||
@@ -109,28 +133,28 @@ function chamuyero() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function generate_cert() {
|
function generate_cert() {
|
||||||
( cd "${UTILDIR}/generate_cert/" || exit 1; go build )
|
go-build-cached "${UTILDIR}/generate_cert/"
|
||||||
"${UTILDIR}/generate_cert/generate_cert" "$@"
|
"${UTILDIR}/generate_cert/generate_cert" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadgen() {
|
function loadgen() {
|
||||||
( cd "${UTILDIR}/loadgen/" || exit 1; go build )
|
go-build-cached "${UTILDIR}/loadgen/"
|
||||||
"${UTILDIR}/loadgen/loadgen" "$@"
|
"${UTILDIR}/loadgen/loadgen" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function conngen() {
|
function conngen() {
|
||||||
( cd "${UTILDIR}/conngen/" || exit 1; go build )
|
go-build-cached "${UTILDIR}/conngen/"
|
||||||
"${UTILDIR}/conngen/conngen" "$@"
|
"${UTILDIR}/conngen/conngen" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function minidns_bg() {
|
function minidns_bg() {
|
||||||
( cd "${UTILDIR}/minidns" || exit 1; go build )
|
go-build-cached "${UTILDIR}/minidns/"
|
||||||
"${UTILDIR}/minidns/minidns" "$@" &
|
"${UTILDIR}/minidns/minidns" "$@" &
|
||||||
export MINIDNS=$!
|
export MINIDNS=$!
|
||||||
}
|
}
|
||||||
|
|
||||||
function fexp() {
|
function fexp() {
|
||||||
( cd "${UTILDIR}/fexp/" || exit 1; go build )
|
go-build-cached "${UTILDIR}/fexp/"
|
||||||
"${UTILDIR}/fexp/fexp" "$@"
|
"${UTILDIR}/fexp/fexp" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user