From a44f325e031e44cf0a2eca3b1ce044f7dafe3f0d Mon Sep 17 00:00:00 2001 From: Alex Garel Date: Wed, 16 Aug 2023 18:36:50 +0200 Subject: [PATCH] ci: integrate codecov and tests run (#8867) This avoids running tests twice --- .github/workflows/codecov.yml | 32 ------------------------------ .github/workflows/pull_request.yml | 21 ++++++++++++++++---- .gitignore | 3 +++ Makefile | 25 +++++++++++++---------- 4 files changed, 34 insertions(+), 47 deletions(-) delete mode 100644 .github/workflows/codecov.yml diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml deleted file mode 100644 index da2f1cec8ceb7..0000000000000 --- a/.github/workflows/codecov.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Unit Test Coverage - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -on: [push] -jobs: - coverage: - name: unit test coverage - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - # Before rebuilding the taxonomies, we do this to restore dates so we don't have to build all taxonomies - # Prior usage in .github/workflows/pull_request.yml - - name: rebuild taxonomies - run: | - git ls-files taxonomies/ | xargs -I{} git log -1 --date=format:%Y%m%d%H%M.%S --format='touch -t %ad "{}"' "{}" | bash - make build_taxonomies GITHUB_TOKEN="${{ secrets.TAXONOMY_CACHE_GITHUB_TOKEN }}" - - uses: actions/checkout@master - - name: generate coverage results - run: make cover - - name: generate text file with results - if: always() - run: make coverage_txt - - name: use codecov - if: always() - run: make codecov - - uses: codecov/codecov-action@v3 - if: always() - with: - files: cover_db/codecov.json diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index f657209f273d6..1043c3752d5dc 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -9,11 +9,11 @@ on: # - ".github/CODEOWNERS" # - ".github/PULL_REQUEST_TEMPLATE.md" # - ".editorconfig" - + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true - + jobs: # those are just until we de-activate their mandatory @@ -101,8 +101,21 @@ jobs: uses: ishworkh/docker-image-artifact-download@v1 with: image: "openfoodfacts-server/backend:dev" - - name: test - run: make tests + - name: tests + run: | + make codecov_prepare + make COVER_OPTS='-e HARNESS_PERL_SWITCHES="-MDevel::Cover=+ignore,tests/"' tests + - name: generate coverage results + # even if tests failed + if: always() + run: | + make coverage_txt + make codecov + - uses: codecov/codecov-action@v3 + if: always() + with: + files: cover_db/codecov.json + tests_dev: name: Test make dev diff --git a/.gitignore b/.gitignore index e291b1a650ae2..a95a54f10eb21 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,9 @@ html/js/jquery* html/js/*nivo-zoom* html/js/sigma* +# coverage data +/cover_db + # Env files .env* diff --git a/Makefile b/Makefile index ce75c3f12546c..28b7f570080ef 100644 --- a/Makefile +++ b/Makefile @@ -151,18 +151,18 @@ tail: @echo "🥫 Reading logs (Apache2, Nginx) …" tail -f logs/**/* -cover: - @echo "🥫 running …" - ${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb - ${DOCKER_COMPOSE_TEST} run --rm backend perl -I/opt/product-opener/lib -I/opt/perl/local/lib/perl5 /opt/product-opener/scripts/build_lang.pl - ${DOCKER_COMPOSE_TEST} run --rm -e HARNESS_PERL_SWITCHES="-MDevel::Cover" backend prove -l tests/unit - ${DOCKER_COMPOSE_TEST} stop +codecov_prepare: + @echo "🥫 Preparing to run code coverage…" + mkdir -p cover_db + ${DOCKER_COMPOSE_TEST} run --rm backend cover -delete + mkdir -p cover_db codecov: - @echo "🥫 running …" + @echo "🥫 running cover to generate a report usable by codecov …" ${DOCKER_COMPOSE_TEST} run --rm backend cover -report codecovbash coverage_txt: + @echo "🥫 running cover to generate text report …" ${DOCKER_COMPOSE_TEST} run --rm backend cover #----------# @@ -239,10 +239,11 @@ lint: lint_perltidy tests: build_lang_test unit_test integration_test +# add COVER_OPTS='-e HARNESS_PERL_SWITCHES="-MDevel::Cover"' if you want to trigger code coverage report generation unit_test: @echo "🥫 Running unit tests …" ${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb - ${DOCKER_COMPOSE_TEST} run -T --rm backend prove -l --jobs ${CPU_COUNT} -r tests/unit + ${DOCKER_COMPOSE_TEST} run ${COVER_OPTS} -T --rm backend prove -l --jobs ${CPU_COUNT} -r tests/unit ${DOCKER_COMPOSE_TEST} stop @echo "🥫 unit tests success" @@ -253,7 +254,7 @@ integration_test: # this is the place where variables are important ${DOCKER_COMPOSE_TEST} up -d memcached postgres mongodb backend dynamicfront incron # note: we need the -T option for ci (non tty environment) - ${DOCKER_COMPOSE_TEST} exec -T backend prove -l -r tests/integration + ${DOCKER_COMPOSE_TEST} exec ${COVER_OPTS} -T backend prove -l -r tests/integration ${DOCKER_COMPOSE_TEST} stop @echo "🥫 integration tests success" @@ -299,13 +300,15 @@ bash: # check perl compiles, (pattern rule) / but only for newer files %.pm %.pl: _FORCE - if [ -f $@ ]; then perl -c -CS -Ilib $@; else true; fi + @if [[ -f $@ ]]; then perl -c -CS -Ilib $@; else true; fi # TO_CHECK look at changed files (compared to main) with extensions .pl, .pm, .t +# filter out obsolete scripts # the ls at the end is to avoid removed files. +# the first commad is to check we have git (to avoid trying to run this line inside the container on check_perl*) # We have to finally filter out "." as this will the output if we have no file -TO_CHECK=$(shell git diff origin/main --name-only | grep '.*\.\(pl\|pm\|t\)$$' | xargs ls -d 2>/dev/null | grep -v "^.$$" ) +TO_CHECK=$(shell [ -x "`which git 2>/dev/null`" ] && git diff origin/main --name-only | grep '.*\.\(pl\|pm\|t\)$$' | grep -v "scripts/obsolete" | xargs ls -d 2>/dev/null | grep -v "^.$$" ) check_perl_fast: @echo "🥫 Checking ${TO_CHECK}"