diff --git a/Makefile b/Makefile index 88cfd8b70..733064d93 100644 --- a/Makefile +++ b/Makefile @@ -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 | \ diff --git a/_assets/ci/Jenkinsfile.tests b/_assets/ci/Jenkinsfile.tests index e3e1ed1bc..9621453d2 100644 --- a/_assets/ci/Jenkinsfile.tests +++ b/_assets/ci/Jenkinsfile.tests @@ -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 } diff --git a/_assets/scripts/run_unit_tests.sh b/_assets/scripts/run_unit_tests.sh index 63bc13dbf..7843af469 100755 --- a/_assets/scripts/run_unit_tests.sh +++ b/_assets/scripts/run_unit_tests.sh @@ -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 \ No newline at end of file diff --git a/nix/overlay.nix b/nix/overlay.nix index 4b528356d..818f8db65 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -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 = [ diff --git a/nix/pkgs/cc-test-reporter/default.nix b/nix/pkgs/cc-test-reporter/default.nix new file mode 100644 index 000000000..485f9388a --- /dev/null +++ b/nix/pkgs/cc-test-reporter/default.nix @@ -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"]; + }; +} diff --git a/nix/shell.nix b/nix/shell.nix index b8f8e2697..cb3b1d469 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -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) ''