Running CI Tests Locally - deepgram-devs/deepgram-rust-sdk Wiki

You can configure your local Git repository to run a script before you commit, and then abort the commit if the script fails. By setting this script to run the same tests as the CI, you can be more confident that your commit will pass CI before you make it.

Create the file .git/hooks/pre-commit. Copy the following into it:

#!/usr/bin/env bash

# Don't proceed on errors
# See:
set -Eeuo pipefail

# cd to the root of the repository
cd ${0%/*}/../..

# Use a different target directory so that build cache doesn't get wiped each time
export CARGO_TARGET_DIR='git-hook-targets/normal'

# Make cargo give a non-zero exit code if warnings are detected
export RUSTFLAGS='-D warnings'
export RUSTDOCFLAGS='-D warnings'

# Make sure a minimal Cargo.lock isn't lingering around
cargo update

# Keep a copy of the original Cargo.toml so it can be reset once finished
cp Cargo.toml git-hook-targets/Cargo.toml.tmp

# Run this command on exit, even if aborting
# Resets Cargo.toml
# Resets Cargo.lock
trap 'mv git-hook-targets/Cargo.toml.tmp Cargo.toml; cargo update --quiet' EXIT

##### CI TESTS BEGIN #####

# Build
cargo build --all-targets --all-features

# Clippy
cargo clippy --all-targets --all-features

# Test
cargo test --all --all-features

# Format
cargo fmt --check --all

# Documentation
cargo doc --workspace --all-features

# Audit
cargo hack --remove-dev-deps
cargo generate-lockfile
cargo audit
cp git-hook-targets/Cargo.toml.tmp Cargo.toml

# Minimal Versions
export CARGO_TARGET_DIR='git-hook-targets/minimal-versions'
rustup run nightly cargo generate-lockfile -Z minimal-versions
rustup run nightly cargo build --all-targets --all-features -Z minimal-versions
rustup run nightly cargo test --all --all-features -Z minimal-versions

# Minimal Versions Without Dev Dependencies
export CARGO_TARGET_DIR='git-hook-targets/minimal-versions-no-dev-deps'
cargo hack --remove-dev-deps
rustup run nightly cargo generate-lockfile -Z minimal-versions
rustup run nightly cargo build --all-features -Z minimal-versions

Note that the script shown above is based on the CI tests from 33a00c9. If they have been changed since then, you may have to modify the script to match it. If you do, consider updating this wiki page to help others!

When you run this, you'll notice that it creates a new directory for Cargo targets. This isn't necessary, but it prevents the entire build cache from being invalidated every single time because of the different build options.

You'll probably want Git to ignore it. To do this, you can create a local gitignore file:

  1. Run git config --global core.excludesfile .local.gitignore (--global is optional)
  2. Make a file called .local.gitignore
  3. Add .local.gitignore to this file
  4. Add git-hook-targets to this file

Local gitignore files are nice because it means you don't have to pollute the shared one with things that only pertain to your clone.