From 7a9845c6e27567ccc5f6831ed345f959479c6f1a Mon Sep 17 00:00:00 2001 From: Anton Iakimov Date: Thu, 31 Aug 2023 13:43:23 +0200 Subject: [PATCH] ci: hide `go test` output to the file on CI Let's hide stdout of go tests to the files. In case of failed test - it should be included in junit report anyway and shown in Jenkins. Closes #3543 --- .gitignore | 3 +++ Makefile | 17 +++++------------ _assets/scripts/colors.sh | 12 ++++++++++++ _assets/scripts/run_unit_tests.sh | 25 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 _assets/scripts/colors.sh create mode 100755 _assets/scripts/run_unit_tests.sh diff --git a/.gitignore b/.gitignore index 6336e7f61..43d074b6a 100644 --- a/.gitignore +++ b/.gitignore @@ -82,3 +82,6 @@ package-lock.json # junit reports for Jenkins integration report.xml + +# go test logs +test.log diff --git a/Makefile b/Makefile index db3a52276..06e1f85f9 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,6 @@ GIT_COMMIT = $(shell git rev-parse --short HEAD) AUTHOR ?= $(shell git config user.email || echo $$USER) ENABLE_METRICS ?= true -BUILD_TAGS ?= BUILD_FLAGS ?= $(shell echo "-ldflags='\ -X github.com/status-im/status-go/params.Version=$(RELEASE_TAG:v%=%) \ -X github.com/status-im/status-go/params.GitCommit=$(GIT_COMMIT) \ @@ -48,7 +47,6 @@ BUILD_FLAGS_MOBILE ?= $(shell echo "-ldflags='\ -X github.com/status-im/status-go/params.IpfsGatewayURL=$(IPFS_GATEWAY_URL)'") networkid ?= StatusChain -gotest_extraflags = DOCKER_IMAGE_NAME ?= statusteam/status-go BOOTNODE_IMAGE_NAME ?= statusteam/bootnode @@ -311,28 +309,23 @@ docker-test: ##@tests Run tests in a docker container with golang. test: test-unit ##@tests Run basic, short tests during development -test-unit: SHELL := /bin/bash -o pipefail -test-unit: UNIT_TEST_PACKAGES = $(shell go list ./... | \ +test-unit: export BUILD_TAGS ?= +test-unit: export UNIT_TEST_PACKAGES ?= $(shell go list ./... | \ grep -v /vendor | \ grep -v /t/e2e | \ grep -v /t/benchmarks | \ grep -v /transactions/fake ) test-unit: ##@tests Run unit and integration tests - for package in $(UNIT_TEST_PACKAGES); do \ - set -e; \ - package_dir=$$(go list -f {{.Dir}} $${package}); \ - go test -tags '$(BUILD_TAGS)' -timeout 30m -v -failfast $${package} $(gotest_extraflags) | \ - go-junit-report -iocopy -out $${package_dir}/report.xml; \ - done + ./_assets/scripts/run_unit_tests.sh -test-unit-race: gotest_extraflags=-race +test-unit-race: export GOTEST_EXTRAFLAGS=-race test-unit-race: test-unit ##@tests Run unit and integration tests with -race flag test-e2e: ##@tests Run e2e tests # order: reliability then alphabetical # TODO(tiabc): make a single command out of them adding `-p 1` flag. -test-e2e-race: gotest_extraflags=-race +test-e2e-race: export GOTEST_EXTRAFLAGS=-race test-e2e-race: test-e2e ##@tests Run e2e tests with -race flag canary-test: node-canary diff --git a/_assets/scripts/colors.sh b/_assets/scripts/colors.sh new file mode 100644 index 000000000..6b19a153f --- /dev/null +++ b/_assets/scripts/colors.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# Colors +export YLW='\033[1;33m' +export RED='\033[0;31m' +export GRN='\033[0;32m' +export BLU='\033[0;34m' +export BLD='\033[1m' +export RST='\033[0m' + +# Clear line +export CLR='\033[2K' diff --git a/_assets/scripts/run_unit_tests.sh b/_assets/scripts/run_unit_tests.sh new file mode 100755 index 000000000..27c3309ff --- /dev/null +++ b/_assets/scripts/run_unit_tests.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -o pipefail + +GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) + +source "${GIT_ROOT}/_assets/scripts/colors.sh" + +for package in ${UNIT_TEST_PACKAGES}; do + echo -e "${GRN}Testing:${RST} ${package}" + package_dir=$(go list -f "{{.Dir}}" "${package}") + output_file=${package_dir}/test.log + + go test -tags "${BUILD_TAGS}" -timeout 30m -v -failfast "${package}" ${GOTEST_EXTRAFLAGS} | \ + if [ "${CI}" = "true" ]; then cat > "${output_file}"; else tee "${output_file}"; fi + go_test_exit=$? + + if [ "${CI}" = "true" ]; then + go-junit-report -in "${output_file}" -out "${package_dir}"/report.xml + fi + + if [ ${go_test_exit} -ne 0 ]; then + echo -e "${YLW}Failed, see the log:${RST} ${BLD}${output_file}${RST}" + exit "${go_test_exit}" + fi +done