diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4117c21a3a7c41923aaf02139f09e3f7509d5e39..62c3b918784e1fdcff906f4d49d71fa9932a78db 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,6 +10,10 @@ image: "rust:latest" # - redis:latest # - postgres:latest +variables: + KCOV_DISCARD_CACHE: "false" + APT_CACHE_DIR: apt-cache + # Optional: Install a C compiler, cmake and git into the container. # You will often need this when you (or any of your dependencies) depends on C code. #before_script: @@ -17,7 +21,26 @@ image: "rust:latest" #- apt-get install -yqq --no-install-recommends build-essential # Use cargo to test the project +# And kcov to analyze the coverage: +# https://grauwoelfchen.at/posts/get-test-coverage-for-software-written-in-rust-using-kcov/ test:cargo: + before_script: + - mkdir -pv $APT_CACHE_DIR && apt-get -qq update && apt-get -qqy upgrade + - apt-get -qq -o dir::cache::archives="$APT_CACHE_DIR" install -y + binutils-dev libcurl4-openssl-dev zlib1g-dev libdw-dev libiberty-dev + cmake + - ./ci/build-kcov + - ./kcov/bin/kcov --version + - rustc --version && cargo --version # Print version info for debugging script: - - rustc --version && cargo --version # Print version info for debugging - cargo test --all + after_script: + - rm -f target/debug/weighted_random_list-*.d + - ./kcov/bin/kcov --include-path ./src ./target/cov target/debug/weighted_random_list-* + - COVERAGE=$(grep -Po 'covered":.*?[^\\]"' target/cov/index.js* | grep "[0-9]*\.[0-9]" -o) + - echo "Coverage:" $COVERAGE + cache: + untracked: true + paths: + - apt-cache + - kcov diff --git a/README.md b/README.md index 6c28803e8bb8e09caab5bb24a3d015e9ed91d7ce..507b35c2de5fdcc2ad27ad62030323d0bc943612 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # weighted_random_list [![CI Status](https://gitlab.com/dns2utf8/weighted_random_list/badges/master/build.svg)](https://gitlab.com/dns2utf8/weighted_random_list) +[![coverage report](https://gitlab.com/dns2utf8/weighted_random_list/badges/master/coverage.svg)](https://gitlab.com/dns2utf8/weighted_random_list/commits/master) A Vec that allows you to define the weight of each entry and randomly get entries: diff --git a/ci/build-kcov b/ci/build-kcov new file mode 100755 index 0000000000000000000000000000000000000000..15cc15c7f8d58459e118676320f18fc6e1094e89 --- /dev/null +++ b/ci/build-kcov @@ -0,0 +1,30 @@ +#!/bin/bash +set -eu + +# NOTE: +# if set KCOV_DISCARD_CACHE=true, then it will force installing kcov) + +renew="${KCOV_DISCARD_CACHE:-false}" + +kcov_dir="kcov" +kcov_bin="${kcov_dir}/bin/kcov" +kcov_url="https://github.com/SimonKagstrom/kcov/archive" +kcov_ver="v34" + +if [[ -f "${kcov_bin}" && "${renew}" != "true" ]]; then + echo "kcov already installed in ${kcov_bin}" +else + rm -fr $kcov_dir + mkdir $kcov_dir + cd $kcov_dir + curl -sLO ${kcov_url}/${kcov_ver}.tar.gz + mkdir $kcov_ver + tar zxvf ${kcov_ver}.tar.gz -C $kcov_ver --strip-components=1 + cd $kcov_ver + mkdir build + cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=/ + make + make install DESTDIR=../ +fi