chore_: setup ci codeclimate coverage reports (#5101)

* chore_: nix derivation with cc-test-reporter
This commit is contained in:
Igor Sirotin 2024-05-08 09:25:01 +01:00 committed by GitHub
parent 59c6ee5ffe
commit 068ae3b67c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 76 additions and 4 deletions

View File

@ -353,6 +353,7 @@ test-unit: export UNIT_TEST_COUNT ?= 1
test-unit: export UNIT_TEST_FAILFAST ?= true
test-unit: export UNIT_TEST_RERUN_FAILS ?= true
test-unit: export UNIT_TEST_USE_DEVELOPMENT_LOGGER ?= true
test-unit: export UNIT_TEST_REPORT_CODECLIMATE ?= false
test-unit: export UNIT_TEST_PACKAGES ?= $(call sh, go list ./... | \
grep -v /vendor | \
grep -v /t/e2e | \

View File

@ -30,6 +30,11 @@ pipeline {
defaultValue: getDefaultUnitTestUseDevelopmentLogger(),
description: 'Should the job use detailed logging for tests, potentially generating large logs?'
)
booleanParam(
name: 'UNIT_TEST_REPORT_CODECLIMATE',
defaultValue: getDefaultUnitTestReportCodeClimate(),
description: 'Should the job report test coverage to CodeClimate?'
)
}
options {
@ -90,7 +95,6 @@ pipeline {
} }
}
stage('Lint') {
steps { script {
nix.shell('make lint', pure: true)
@ -114,7 +118,14 @@ pipeline {
"--publish=${DB_PORT}:${DB_PORT}",
].join(' '), "-p ${DB_PORT}") { c ->
nix.shell('make generate-handlers', pure: true)
nix.shell('make test-unit V=1', pure: false)
withCredentials([
string(
credentialsId: 'codeclimate-test-reporter-id',
variable: 'CC_TEST_REPORTER_ID'
),
]) {
nix.shell('make test-unit V=1', pure: false)
}
}
} }
post { cleanup { /* Leftover DB containers. */
@ -171,6 +182,8 @@ def getDefaultUnitTestRerunFails() { isTestNightlyJob() ? false : true }
def getDefaultUnitTestUseDevelopmentLogger() { isTestNightlyJob() ? false : true }
def getDefaultUnitTestReportCodeClimate() { isTestNightlyJob() ? false : true }
def getDefaultUnitTestFailfast() { isTestNightlyJob() ? false : true }
def getDefaultTimeout() { isTestNightlyJob() ? 5*60 : 50 }

View File

@ -62,6 +62,7 @@ run_test_for_package() {
echo -e "${GRN}Testing:${RST} ${package} Iteration:${iteration}"
package_dir=$(go list -f "{{.Dir}}" "${package}")
output_file="${package_dir}/test_${iteration}.log"
coverage_file="${package_dir}/test_${iteration}.coverage.out"
if has_extended_timeout "${package}"; then
package_timeout="${UNIT_TEST_PACKAGE_TIMEOUT_EXTENDED}"
@ -82,7 +83,9 @@ run_test_for_package() {
-v ${GOTEST_EXTRAFLAGS} \
-timeout "${package_timeout}" \
-count 1 \
-tags "${BUILD_TAGS}" | \
-tags "${BUILD_TAGS}" \
-covermode=atomic \
-coverprofile="${coverage_file}" | \
redirect_stdout "${output_file}"
local go_test_exit=$?
@ -96,6 +99,10 @@ run_test_for_package() {
return ${go_test_exit}
}
if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then
cc-test-reporter before-build
fi
for package in ${UNIT_TEST_PACKAGES}; do
for ((i=1; i<=UNIT_TEST_COUNT; i++)); do
if ! is_parallelizable "${package}" || [[ "$UNIT_TEST_FAILFAST" == 'true' ]]; then
@ -124,3 +131,12 @@ if [[ "${UNIT_TEST_COUNT}" -gt 1 ]]; then
fi
done
fi
# Gather test coverage results
echo "mode: atomic" > c.out
grep -h -v "^mode:" ./**/*.coverage.out >> c.out
rm -rf ./**/*.coverage.out
if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then
cc-test-reporter after-build --prefix=github.com/status-im/status-go
fi

View File

@ -51,6 +51,7 @@ in rec {
# Custom packages
go-modvendor = callPackage ./pkgs/go-modvendor { };
cc-test-reporter = callPackage ./pkgs/cc-test-reporter { };
gomobile = (prev.gomobile.overrideAttrs (old: {
patches = [

View File

@ -0,0 +1,41 @@
{ lib, stdenv, fetchurl }:
let
platform = lib.getAttr builtins.currentSystem {
aarch64-linux = "linux-arm64";
x86_64-linux = "linux-amd64";
aarch64-darwin = "darwin-amd64"; # There's no arm64 build for macOS, amd64 works on both
x86_64-darwin = "darwin-amd64";
};
in stdenv.mkDerivation rec {
pname = "cc-test-reporter";
version = "0.11.1";
src = fetchurl {
url = "https://codeclimate.com/downloads/test-reporter/test-reporter-${version}-${platform}";
hash = lib.getAttr builtins.currentSystem {
aarch64-linux = "sha256-b6rTiiKZiVxoR/aQaxlqG6Ftt7sqyAKXgO9EG6/sKck=";
x86_64-linux = "sha256-ne79mW3w9tHJ+3lAWzluuRp6yjWsy4lpdV/KpmjaTa0=";
aarch64-darwin = "sha256-uO9aRL3cJe+KCoC+uN6cBQy8xGQHim6h5Qzw36QO7EY=";
x86_64-darwin = "sha256-uO9aRL3cJe+KCoC+uN6cBQy8xGQHim6h5Qzw36QO7EY=";
};
};
dontUnpack = true;
installPhase = ''
runHook preInstall
install -D $src $out/bin/cc-test-reporter
chmod +x $out/bin/cc-test-reporter
runHook postInstall
'';
meta = with lib; {
description = "Code Climate test reporter for sending coverage data";
homepage = "https://docs.codeclimate.com/docs/configuring-test-coverage";
license = licenses.mit;
mainProgram = "cc-test-reporter";
platforms = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
};
}

View File

@ -21,7 +21,7 @@ in pkgs.mkShell {
buildInputs = with pkgs; [
git jq which
go golangci-lint go-junit-report gopls go-bindata gomobileMod
mockgen protobuf3_20 protoc-gen-go gotestsum go-modvendor openjdk
mockgen protobuf3_20 protoc-gen-go gotestsum go-modvendor openjdk cc-test-reporter
] ++ lib.optionals (stdenv.isDarwin) [ xcodeWrapper ];
shellHook = lib.optionalString (!isMacM1) ''