From adb7bac0ae420832e17e91c1311eaea941b49627 Mon Sep 17 00:00:00 2001 From: bwoodsend Date: Tue, 30 Mar 2021 21:41:13 +0100 Subject: [PATCH 1/3] Add gcov coverage collecting of C code (#387) --- .gitignore | 6 ++++++ scripts/coverage.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 scripts/coverage.sh diff --git a/.gitignore b/.gitignore index b7557264..baa466f1 100644 --- a/.gitignore +++ b/.gitignore @@ -141,3 +141,9 @@ python/version.h # Docker wheel build pip-cache/ temp-wheels/ + +# gcov coverage files +cov +*.gcov +*.gcda +*.gcno diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100644 index 00000000..09f231aa --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# Coverage for ultrajson's C code. +# Usage: +# CFLAGS="--coverage -O0" python setup.py -q build_ext --inplace -f +# pytest +# ./scripts/coverage.sh +# Then inspect the files in the `cov` folder. + +# The exact arguments depend on whether we're using LLVM's gcov or GNU's. +unameOut="$(uname -s)" +case "${unameOut}" in + Linux*) gcov_options=(--relative-only -k);; + Darwin*) gcov_options=(--color);; + *) echo "Unsupported OS ${unameOut}"; exit 1;; +esac + +# The actual gcov instructions: +gcov "${gcov_options[@]}" python/**.c -o build/temp.*/python +gcov "${gcov_options[@]}" lib/**.c -o build/temp.*/lib + +# gcov dumps everything in the cwd without any option to change this. +# Manually move the .gcov files to a `cov` folder. +mkdir -p cov +rm -rf cov/* +mv ./**.gcov cov || exit 1 + +echo Written gcov files to ./cov From 47b84384c5352c85aa9c7394f7df9fe3bc717ed1 Mon Sep 17 00:00:00 2001 From: bwoodsend Date: Tue, 30 Mar 2021 23:10:41 +0100 Subject: [PATCH 2/3] Add codecov upload to CI/CD. --- .github/workflows/test.yml | 12 ++++++++++++ scripts/coverage.sh | 0 2 files changed, 12 insertions(+) mode change 100644 => 100755 scripts/coverage.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c29174b8..ddce44aa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,3 +49,15 @@ jobs: - name: Tests run: | pytest + + - name: Test with coverage + if: ${{ startsWith(matrix.os, 'ubuntu') && matrix.python-version == '3.9' }} + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + run: | + pip install -e . + pip install coverage + CFLAGS="--coverage -O0" python setup.py -q build_ext --inplace -f + coverage run -m pytest + ./scripts/coverage.sh + bash <(curl -s https://codecov.io/bash) -X gcov diff --git a/scripts/coverage.sh b/scripts/coverage.sh old mode 100644 new mode 100755 From 25dad7eae14137b3697c8dde88cc11d475f21746 Mon Sep 17 00:00:00 2001 From: bwoodsend Date: Tue, 30 Mar 2021 23:26:49 +0100 Subject: [PATCH 3/3] Drop colored gcov output on Linux. It isn't supported on the older gcc versions used by CI/CD. --- scripts/coverage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 09f231aa..4390ff17 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -10,7 +10,7 @@ # The exact arguments depend on whether we're using LLVM's gcov or GNU's. unameOut="$(uname -s)" case "${unameOut}" in - Linux*) gcov_options=(--relative-only -k);; + Linux*) gcov_options=(--relative-only);; Darwin*) gcov_options=(--color);; *) echo "Unsupported OS ${unameOut}"; exit 1;; esac