Building and Testing Rust projects on CircleCI

Configure CircleCI for Rust projects.

Building and Testing Rust projects on CircleCI

Steps

  • Setup build environment (Use docker(image: ubuntu 18.04) and rustup)
  • Check code format (Use rustfmt)
  • Check a collection of lints to catch common mistakes and improve your code (Use rust-clippy)
  • Build and Test
  • Code coverage measurement (Use kcov and Codecov)

Full CircleCI configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
version: 2.1
jobs:
build:
docker:
- image: circleci/rust:latest

working_directory: ~/rust-demo-ci

steps:
- checkout
- run:
name: Version information
command: |
rustc --version
cargo --version
rustup --version
- run:
name: Setup build environment
command: |
sudo apt-get update
sudo apt-get install -y cmake binutils-dev libiberty-dev libelf-dev libdw-dev
no_output_timeout: 1800s
- run:
name: Format
command: |
rustup component add rustfmt
cargo fmt -- --check
- run:
name: Clippy
command: |
rustup component add clippy
cargo clippy --all
- run:
name: Test
command: RUST_BACKTRACE=1 cargo test
- run:
name: Coverage
command: |
# install kcov
export KCOV_VERSION=36
wget https://github.com/SimonKagstrom/kcov/archive/v$KCOV_VERSION.tar.gz
tar xzf v$KCOV_VERSION.tar.gz && rm v$KCOV_VERSION.tar.gz
cd kcov-$KCOV_VERSION
mkdir build && cd build
cmake .. && make && make install DESTDIR=../../kcov-build
cd ../.. && rm -rf kcov-$KCOV_VERSION

# generate and upload code coverage
for file in $(find target/debug -maxdepth 1 -name '*-*' -a ! -name '*.d' ! -name '.*'); do
[ -x "${file}" ] || continue
mkdir -p "target/cov/$(basename $file)"
./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"
done
bash <(curl -s https://codecov.io/bash)
echo "Uploaded code coverage"

workflows:
version: 2.1
build:
jobs:
- build

Update (2019-05-07)

The personality syscall required by kcov is DISABLED by Docker, so test coverage cannot be measured… (Not fix yet, may be you can use a Machine executor instead of a Docker executor)

kcov issue #151
circleci discuss link

Reference

Circle CI Document