|
1 |
| -# example-rust |
| 1 | +# Codecov Rust Example [](https://travis-ci.org/codecov/example-rust) [](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 | +## [](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 | + |
| 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 | + |
0 commit comments