├── .github ├── actions-rs │ └── grcov.yml └── workflows │ ├── enforce-license-compliance.yml │ └── rust.yml ├── .gitignore ├── .travis.yml ├── Cargo.lock ├── Cargo.toml ├── LICENSE.md ├── README.md └── src └── lib.rs /.github/actions-rs/grcov.yml: -------------------------------------------------------------------------------- 1 | output-type: lcov 2 | output-file: ./lcov.info 3 | -------------------------------------------------------------------------------- /.github/workflows/enforce-license-compliance.yml: -------------------------------------------------------------------------------- 1 | name: Enforce License Compliance 2 | 3 | on: 4 | pull_request: 5 | branches: [main, master] 6 | 7 | jobs: 8 | enforce-license-compliance: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: 'Enforce License Compliance' 12 | uses: getsentry/action-enforce-license-compliance@57ba820387a1a9315a46115ee276b2968da51f3d # main 13 | with: 14 | fossa_api_key: ${{ secrets.FOSSA_API_KEY }} 15 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - uses: actions-rs/toolchain@v1 20 | with: 21 | toolchain: nightly 22 | override: true 23 | - name: Build 24 | run: cargo build --verbose 25 | - name: Run tests 26 | run: cargo test --verbose 27 | env: 28 | CARGO_INCREMENTAL: '0' 29 | RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' 30 | RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' 31 | - name: rust-grcov 32 | # You may pin to the exact commit or the version. 33 | # uses: actions-rs/grcov@bb47b1ed7883a1502fa6875d562727ace2511248 34 | uses: actions-rs/grcov@v0.1 35 | - name: Codecov 36 | # You may pin to the exact commit or the version. 37 | # uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 38 | uses: codecov/codecov-action@v5 39 | env: 40 | CODECOV_TOKEN: ${{ secrets.CODECOV_ORG_TOKEN }} 41 | with: 42 | # Repository upload token - get it from codecov.io. Required only for private repositories 43 | # token: # optional 44 | # Specify whether the Codecov output should be verbose 45 | verbose: true 46 | fail_ci_if_error: true 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | sudo: required 4 | 5 | rust: 6 | - stable 7 | - beta 8 | - nightly 9 | 10 | matrix: 11 | allow_failures: 12 | - rust: nightly 13 | 14 | addons: 15 | apt: 16 | packages: 17 | - libcurl4-openssl-dev 18 | - libelf-dev 19 | - libdw-dev 20 | - cmake 21 | - gcc 22 | - binutils-dev 23 | - libiberty-dev 24 | 25 | after_success: | 26 | wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && 27 | tar xzf master.tar.gz && 28 | cd kcov-master && 29 | mkdir build && 30 | cd build && 31 | cmake .. && 32 | make && 33 | make install DESTDIR=../../kcov-build && 34 | cd ../.. && 35 | rm -rf kcov-master && 36 | for file in target/debug/examplerust-*; 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 && 37 | bash <(curl -s https://codecov.io/bash) && 38 | echo "Uploaded code coverage" 39 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "examplerust" 7 | version = "1.0.0" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "examplerust" 3 | version = "1.0.0" 4 | authors = ["Sunjay Varma "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Codecov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Codecov][1] Rust Example 2 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-rust.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-rust?ref=badge_shield) 3 | 4 | 5 | **As of July 2, 2016, there is [no option to make rustdoc generate a runnable test executable][7]. That means that documentation tests will not show in your coverage data. If you discover a way to run the doctest executable with kcov, please open an issue in our [feedback repository](https://github.com/codecov/feedback) and we will add that to these instructions.** 6 | 7 | ## Guide 8 | ### Travis Setup 9 | 10 | Add to your `.travis.yml` file. 11 | 12 | **NOTE: You should change `examplerust` in `for file in target/debug/examplerust-* ...` (under `after_success`) to match your package name.** For example, if your package name is `minigrep`, then the lines should be changed to `for file in target/debug/minigrep-* ...`. If you are not sure what your package name is, it can also be found in `Cargo.toml` and it is usually the same name as when you had initialised the project. 13 | ```yml 14 | language: rust 15 | 16 | sudo: required 17 | 18 | rust: 19 | - stable 20 | - beta 21 | - nightly 22 | 23 | matrix: 24 | allow_failures: 25 | - rust: nightly 26 | 27 | addons: 28 | apt: 29 | packages: 30 | - libcurl4-openssl-dev 31 | - libelf-dev 32 | - libdw-dev 33 | - cmake 34 | - gcc 35 | - binutils-dev 36 | - libiberty-dev 37 | 38 | after_success: | 39 | wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && 40 | tar xzf master.tar.gz && 41 | cd kcov-master && 42 | mkdir build && 43 | cd build && 44 | cmake .. && 45 | make && 46 | make install DESTDIR=../../kcov-build && 47 | cd ../.. && 48 | rm -rf kcov-master && 49 | for file in target/debug/examplerust-*; 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 && 50 | bash <(curl -s https://codecov.io/bash) && 51 | echo "Uploaded code coverage" 52 | ``` 53 | This will automatically run each executable and store the results in a 54 | different directory. Codecov will automatically find the `cobertura.xml` 55 | files that `kcov` generates and combine the results. 56 | 57 | #### Other CI services 58 | - Adjust the materials in the above example as necessary for your CI. 59 | - Add CODECOV_TOKEN= to your CI's environment variables. (Don't store the raw token in your repo.) 60 | - Run bash <(curl -s https://codecov.io/bash) after tests complete. 61 | 62 | ### Produce Coverage Reports 63 | 64 | Run your tests with [kcov][6] in order to create the necessary coverage 65 | reports. For example: 66 | 67 | ``` 68 | kcov --exclude-pattern=/.cargo,/usr/lib --verify target/cov target/debug/- 69 | ``` 70 | 71 | `` and `` are the appropriate project name and hash for 72 | your executable. 73 | 74 | The hash at the end may change if cargo generates different test 75 | executables with the same name. If you are building your code 76 | differently or without cargo, change the last two arguments 77 | to kcov to respectively represent where you want the coverage to 78 | be stored and which executable to run. 79 | 80 | Attempting to run `kcov` with an executable argument ending in a wildcard 81 | like `-*` may result in incorrect coverage results as only a 82 | single test executable will run. **For best results, run the kcov command 83 | for each test executable and store the results in separate directories.** 84 | Codecov will automatically find and upload the cobertura.xml files and 85 | merge the coverage for you. 86 | 87 | Note that setting the environment variable `RUSTFLAGS="-C link-dead-code"` 88 | during tests build may improve coverage accuracy by preventing dead-code elimination. 89 | Do not set this variable when creating release builds since it will increase 90 | binary size. 91 | 92 | After you've run the tests and created a cobertura.xml report, you can 93 | use [the Codecov global uploader][4] to push that report to Codecov. 94 | See below for further details. 95 | 96 | Installing `kcov` is largely dependent on your operating system. It is 97 | demonstrated to work on Linux systems but may not be fully compatible with 98 | Windows or OS X. Please lookup the appropriate installation instructions. 99 | The Travis CI example below demonstrates installing `kcov` on a Linux 100 | computer. 101 | 102 | ## Caveats 103 | ### Private Repos 104 | Add to your `.travis.yml` file. 105 | ```yml 106 | after_success: 107 | - bash <(curl -s https://codecov.io/bash) -t uuid-repo-token 108 | ``` 109 | ### `kcov` Version 110 | 111 | The version of `kcov` that is distributed with your package manager may not 112 | work with Rust binaries. You usually need to manually build the latest 113 | master branch and run kcov from there. All of this is taken care of for you 114 | in the `.travis.yml` file. 115 | 116 | 1. More documentation at https://docs.codecov.io 117 | 2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml 118 | 119 | We are happy to help if you have any questions. Please contact email our Support at [support@codecov.io](mailto:support@codecov.io) 120 | 121 | [1]: https://codecov.io/ 122 | [5]: http://codecov.io/github/codecov/example-rust?branch=master 123 | [6]: https://simonkagstrom.github.io/kcov/ 124 | [7]: http://stackoverflow.com/questions/35547710/does-rustdoc-generate-runnable-binaries 125 | [8]: https://github.com/codecov/example-rust/issues 126 | 127 | 128 | ## License 129 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-rust.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-rust?ref=badge_large) 130 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | const EYES: &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 | } else if face == frown() { 27 | "Frown" 28 | } else if face == angry() { 29 | "Angry" 30 | } else { 31 | "I don't know" 32 | } 33 | } 34 | 35 | /// This function is not called during tests, so it will be considered dead code. 36 | /// By default, and because of dead-code elimination it won't be reported as uncovered 37 | /// since the function will be removed from executable. 38 | /// This is accounted for in the Travis configuration by passing the compiler flag 39 | /// `-C link-dead-code` when building the tests. This flag disables dead code 40 | /// elimination and allows this function to be reported correctly. 41 | pub fn not_called() { 42 | println!("This is dead code"); 43 | unreachable!(); 44 | } 45 | 46 | #[cfg(test)] 47 | mod tests { 48 | use super::*; 49 | 50 | #[test] 51 | fn can_smile() { 52 | assert_eq!(smile(), ":)"); 53 | } 54 | 55 | #[test] 56 | fn can_frown() { 57 | assert_eq!(frown(), ":("); 58 | } 59 | 60 | #[test] 61 | fn can_angry() { 62 | assert_eq!(angry(), ">:("); 63 | } 64 | 65 | #[test] 66 | fn string_representation() { 67 | assert_eq!(which(&smile()), "Smile"); 68 | } 69 | } 70 | --------------------------------------------------------------------------------