From 57f1df294b36f92533a000d4bfcb140fa483ad81 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Tue, 25 Aug 2026 22:04:08 +0300 Subject: [PATCH] tests: add file names to TAP output by using a customized formatter --- Makefile | 12 +++++--- test/libexec/bats-format-tap-suite | 45 ++++++++++++++++++++++++++++++ test/run | 2 +- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100755 test/libexec/bats-format-tap-suite diff --git a/Makefile b/Makefile index 17175588..6a8ae7e9 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,8 @@ $(TEST_PYTHON_BUILD_DOCKER_TARGETS): $(TEST_PYTHON_BUILD_DOCKER_PREFIX)-% : $(TE $${CI+-e CI="$${CI}"} \ $(INTERACTIVE) \ $(DOCKER_IMAGE):$(DOCKER_TAG) \ - bats $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} plugins/python-build/test/$${BATS_FILE_FILTER} + bats $${CI:+-F "/code/test/libexec/bats-format-tap-suite"} \ + $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} plugins/python-build/test/$${BATS_FILE_FILTER} .PHONY: $(TEST_BINARY_DOCKER_PREFIX) $(TEST_BINARY_DOCKER_PREFIX): $(TEST_BINARY_DOCKER_TARGETS) @@ -84,7 +85,8 @@ $(TEST_BINARY_DOCKER_TARGETS): $(TEST_BINARY_DOCKER_PREFIX)-% : $(TEST_BATS_IMAG $${CI+-e CI="$${CI}"} \ $(INTERACTIVE) \ $(DOCKER_IMAGE):$(DOCKER_TAG) \ - bats $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} plugins/pyenv-binary/test/$${BATS_FILE_FILTER} + bats $${CI:+-F "/code/test/libexec/bats-format-tap-suite"} \ + $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} plugins/pyenv-binary/test/$${BATS_FILE_FILTER} # Build all images needed for bats under docker .PHONY: $(TEST_BATS_IMAGE_PREFIX) @@ -120,10 +122,12 @@ test-unit: bats PATH="./bats/bin:$$PATH" test/run test-python-build: bats - cd plugins/python-build && $(PWD)/bats/bin/bats $${CI:+--tap} $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} test/$${BATS_FILE_FILTER} + cd plugins/python-build && $(PWD)/bats/bin/bats $${CI:+-F "$(PWD)/test/libexec/bats-format-tap-suite"} \ + $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} test/$${BATS_FILE_FILTER} test-binary: bats - cd plugins/pyenv-binary && $(PWD)/bats/bin/bats $${CI:+--tap} $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} test/$${BATS_FILE_FILTER} + cd plugins/pyenv-binary && $(PWD)/bats/bin/bats $${CI:+-F "$(PWD)/test/libexec/bats-format-tap-suite"} \ + $${BATS_TEST_FILTER:+--filter "$${BATS_TEST_FILTER}"} test/$${BATS_FILE_FILTER} .SECONDARY: bats-$(TEST_BATS_VERSION) bats-$(TEST_BATS_VERSION): diff --git a/test/libexec/bats-format-tap-suite b/test/libexec/bats-format-tap-suite new file mode 100755 index 00000000..59284775 --- /dev/null +++ b/test/libexec/bats-format-tap-suite @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -e + +# Wraps Bats TAP formatter, adding file names to the output + +_BATS_FORMATTER_LIB_RELPATH="lib/bats-core/formatter.bash" +source "$BATS_ROOT/$_BATS_FORMATTER_LIB_RELPATH" + + +# `bats-format-tap` calls this fn at the end +# We must stub it to be able to source and alter it +# https://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function +eval orig_"$(declare -f bats_parse_internal_extended_tap)" +bats_parse_internal_extended_tap() { true; } + + +# stub sourcing lib from `bats-format-tap` +# as that would've reset the stubbed fn +FAKE_BATS_ROOT= +_trap_rm_fakeroot() { rm -rf "$FAKE_BATS_ROOT"; } +REAL_BATS_ROOT="$BATS_ROOT" +FAKE_BATS_ROOT=$(mktemp -d) +trap _trap_rm_fakeroot ERR EXIT +mkdir -p "$(dirname "$FAKE_BATS_ROOT/$_BATS_FORMATTER_LIB_RELPATH")" +touch "$FAKE_BATS_ROOT/$_BATS_FORMATTER_LIB_RELPATH" +BATS_ROOT="$FAKE_BATS_ROOT" + +source "$REAL_BATS_ROOT/libexec/bats-core/bats-format-tap" + +_trap_rm_fakeroot +trap - ERR EXIT +BATS_ROOT="$REAL_BATS_ROOT" +unset FAKE_BATS_ROOT REAL_BATS_ROOT _trap_rm_fakeroot + + +# Finally, alter `bats-format-tap`'s logic and invoke it + +# print as "Anything" line +bats_tap_stream_suite() { # + # bats only passes --base-name to specific built-in formatters + # so we don't have info where the test root is + printf '%s\n' "${1##*/}" +} + +orig_bats_parse_internal_extended_tap \ No newline at end of file diff --git a/test/run b/test/run index 45eb16ed..49aac72d 100755 --- a/test/run +++ b/test/run @@ -6,4 +6,4 @@ if [ -n "$PYENV_NATIVE_EXT" ]; then make -C src fi -exec bats ${CI:+--tap} ${BATS_TEST_FILTER:+--filter "${BATS_TEST_FILTER}"} test/${BATS_FILE_FILTER} +exec bats ${CI:+-F "$PWD/test/libexec/bats-format-tap-suite"} ${BATS_TEST_FILTER:+--filter "${BATS_TEST_FILTER}"} test/${BATS_FILE_FILTER}