Skip to content

Instantly share code, notes, and snippets.

@welf
Last active May 17, 2024 00:46
Show Gist options
  • Save welf/60e4715b3bdf8dc47da6831f4d770960 to your computer and use it in GitHub Desktop.
Save welf/60e4715b3bdf8dc47da6831f4d770960 to your computer and use it in GitHub Desktop.
GitHub Actions - Rust code check
# Check PRs for formatting issues, linting issues, run tests, and check code coverage
name: Code Check
on:
push:
branches:
- main
# Perform check if Rust files or the current file are modified only
paths:
- src/**
- Cargo.toml
- Cargo.lock
- .github/workflows/code_check.yml
pull_request:
branches:
- main
paths:
- src/**
- Cargo.toml
- Cargo.lock
- .github/workflows/code_check.yml
env:
CARGO_TERM_COLOR: always
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --all-features
fmt:
name: Format
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Run cargo check
run: cargo check
- name: Check formatting
run: |
if ! cargo fmt --check ; then
echo "Formatting errors detected, please run `cargo fmt` to fix it";
exit 1
fi
lint:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Check linting
run: cargo clippy --all-features -- -D warnings # handle clippy warnings as errors
coverage:
name: Code coverage
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
# Install cargo-tarpaulin for code coverage
- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Check code coverage
run: cargo tarpaulin --verbose --workspace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment