Skip to content

Commit e1897ef

Browse files
author
Steve Peak
authored
Merge pull request #1 from codecov/rust-codecov-example
Rust codecov example
2 parents 55de96e + 6447722 commit e1897ef

File tree

5 files changed

+237
-1
lines changed

5 files changed

+237
-1
lines changed

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: rust
2+
rust:
3+
- 1.9.0
4+
5+
before_install:
6+
- sudo apt-get update
7+
8+
addons:
9+
apt:
10+
packages:
11+
- libcurl4-openssl-dev
12+
- libelf-dev
13+
- libdw-dev
14+
- cmake
15+
- gcc
16+
- binutils-dev
17+
18+
after_success: |
19+
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
20+
tar xzf master.tar.gz &&
21+
cd kcov-master &&
22+
mkdir build &&
23+
cd build &&
24+
cmake .. &&
25+
make &&
26+
sudo make install &&
27+
cd ../.. &&
28+
rm -rf kcov-master &&
29+
for file in target/debug/examplerust-*; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
30+
bash <(curl -s https://codecov.io/bash) &&
31+
echo "Uploaded code coverage"
32+

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "examplerust"
3+
version = "1.0.0"
4+
authors = ["Sunjay Varma <[email protected]>"]
5+
6+
[dependencies]

README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,133 @@
1-
# example-rust
1+
# Codecov Rust Example [![travisCI](https://travis-ci.org/codecov/example-rust.svg)](https://travis-ci.org/codecov/example-rust) [![codecov.io](http://codecov.io/github/codecov/example-rust/coverage.svg?branch=master)](http://codecov.io/github/codecov/example-rust?branch=master)
2+
3+
| [https://codecov.io][1] | [@codecov][2] | [[email protected]][3] |
4+
| ----------------------- | ------------- | --------------------- |
5+
6+
This repository serves as an **example** of how to use [the Codecov global
7+
uploader][4] with Rust.
8+
9+
Note that the coverage is deliberately incomplete. That
10+
way you can [follow the badge link][5] and see how [Codecov][1]
11+
works. You can view the code there, see hits and misses for coverage, etc.
12+
13+
## Basic Usage
14+
15+
Run your tests with [kcov][6] in order to create the necessary coverage
16+
reports. For example:
17+
18+
```
19+
kcov --exclude-pattern=/.cargo,/usr/lib --verify target/cov target/debug/<PROJECT-NAME>-<hash>
20+
```
21+
22+
`<PROJECT-NAME>` and `<hash>` are the appropriate project name and hash for
23+
your executable.
24+
25+
The hash at the end may change if cargo generates different test
26+
executables with the same name. If you are building your code
27+
differently or without cargo, change the last two arguments
28+
to kcov to respectively represent where you want the coverage to
29+
be stored and which executable to run.
30+
31+
Attempting to run `kcov` with an executable argument ending in a wildcard
32+
like `<PROJECT-NAME>-*` may result in incorrect coverage results as only a
33+
single test executable will run. **For best results, run the kcov command
34+
for each test executable and store the results in separate directories.**
35+
Codecov will automatically find and upload the cobertura.xml files and
36+
merge the coverage for you.
37+
38+
After you've run the tests and created a cobertura.xml report, you can
39+
use [the Codecov global uploader][4] to push that report to Codecov.
40+
See below for further details.
41+
42+
Installing `kcov` is largely dependent on your operating system. It is
43+
demonstrated to work on Linux systems but may not be fully compatible with
44+
Windows or OS X. Please lookup the appropriate installation instructions.
45+
The Travis CI example below demonstrates installing `kcov` on a Linux
46+
computer.
47+
48+
The version of `kcov` that is distributed with your package manager may not
49+
work with Rust binaries. You usually need to manually build the latest
50+
master branch and run kcov from there. All of this is taken care of for you
51+
in the `.travis.yml` file below.
52+
53+
## [![travis-org](https://avatars2.githubusercontent.com/u/639823?v=2&s=50)](https://travis-ci.org) Travis CI
54+
55+
### Public Repos
56+
57+
Adjust the following example `.travis.yml` file to test with the versions
58+
of Rust you desire.
59+
60+
```yml
61+
language: rust
62+
rust:
63+
- 1.9.0
64+
65+
before_install:
66+
- sudo apt-get update
67+
68+
addons:
69+
apt:
70+
packages:
71+
- libcurl4-openssl-dev
72+
- libelf-dev
73+
- libdw-dev
74+
- cmake
75+
- gcc
76+
- binutils-dev
77+
78+
after_success: |
79+
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
80+
tar xzf master.tar.gz &&
81+
cd kcov-master &&
82+
mkdir build &&
83+
cd build &&
84+
cmake .. &&
85+
make &&
86+
sudo make install &&
87+
cd ../.. &&
88+
rm -rf kcov-master &&
89+
for file in target/debug/<PROJECT-NAME>-*; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
90+
bash <(curl -s https://codecov.io/bash) &&
91+
echo "Uploaded code coverage"
92+
```
93+
94+
[Codecov][1] is integrated by the following line in `after_success:`.
95+
96+
```yml
97+
bash <(curl -s https://codecov.io/bash)
98+
```
99+
100+
This will automatically run each executable and store the results in a
101+
different directory. Codecov will automatically find the `cobertura.xml`
102+
files that `kcov` generates and combine the results.
103+
104+
### Private Repos
105+
106+
Add to your `.travis.yml` file.
107+
108+
```yml
109+
env:
110+
global:
111+
- CODECOV_TOKEN=:uuid-repo-token
112+
```
113+
114+
## Other CI services
115+
116+
+ Adjust the materials in the above example as necessary for
117+
your CI.
118+
+ Add `CODECOV_TOKEN=<your repo token>` to your CI's environment variables.
119+
(Don't store the raw token in your repo.)
120+
+ Run `bash <(curl -s https://codecov.io/bash)` after tests complete.
121+
122+
## More details
123+
124+
Visit [the global uploader's repo][4] to view its source and
125+
learn more about the script.
126+
127+
[1]: https://codecov.io
128+
[2]: https://twitter.com/codecov
129+
[3]: mailto:[email protected]
130+
[4]: https://github.com/codecov/codecov-bash
131+
[5]: http://codecov.io/github/codecov/example-rust?branch=master
132+
[6]: https://simonkagstrom.github.io/kcov/
133+

src/lib.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const EYES: &'static str = ":";
2+
3+
pub fn smile() -> String {
4+
format!("{}{}", EYES, ")")
5+
}
6+
7+
pub fn frown() -> String {
8+
format!("{}{}", EYES, "(")
9+
}
10+
11+
pub fn angry() -> String {
12+
format!("{}{}{}", ">", EYES, "(")
13+
}
14+
15+
/// Provides a string representation of a face
16+
///
17+
/// # Examples
18+
///
19+
/// ```
20+
/// # use examplerust::*;
21+
/// assert_eq!(which(&frown()), "Frown");
22+
/// ```
23+
pub fn which(face: &str) -> &'static str {
24+
if face == smile() {
25+
"Smile"
26+
}
27+
else if face == frown() {
28+
"Frown"
29+
}
30+
else if face == angry() {
31+
"Angry"
32+
}
33+
else {
34+
"I don't know"
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn can_smile() {
44+
assert_eq!(smile(), ":)");
45+
}
46+
47+
#[test]
48+
fn can_frown() {
49+
assert_eq!(frown(), ":(");
50+
}
51+
52+
#[test]
53+
fn can_angry() {
54+
assert_eq!(angry(), ">:(");
55+
}
56+
57+
#[test]
58+
fn string_representation() {
59+
assert_eq!(which(&smile()), "Smile");
60+
}
61+
}
62+

0 commit comments

Comments
 (0)