Skip to content

Commit

Permalink
simplify ci, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jan 24, 2025
1 parent 1660c83 commit 20bef88
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 43 deletions.
50 changes: 7 additions & 43 deletions .github/workflows/rust-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,17 @@ jobs:
name: Rust tests
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- name: Test rustutils
run: |
# Eventually this script should be moved to a justfile as a "ci-test" recipe
# rust-info:
rustc --version
cargo --version
#
# Test code formatting
# test-fmt:
cargo fmt --all -- --check
#
# Quick compile
# check:
RUSTFLAGS='-D warnings' cargo check --workspace --all-targets
#
# Run all tests
# test:
cargo test --workspace --all-targets
#
# Run cargo clippy
# clippy:
cargo clippy --workspace --all-targets -- -D warnings
#
# Test documentation
# test-doc:
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
- run: just -v ci-test

msrv:
name: Rust tests MSRV
name: Rust tests MSRV (Minimal Supported Rust Version)
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- name: Read crate metadata
id: metadata
Expand All @@ -57,19 +36,4 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ steps.metadata.outputs.rust-version }}
components: rustfmt
- name: Test rustutils
working-directory: rustutils/
run: |
# Eventually this script should be moved to a justfile as a "ci-test-msrv" recipe
# rust-info:
rustc --version
cargo --version
#
# Quick compile
# check:
RUSTFLAGS='-D warnings' cargo check --workspace --all-targets
#
# Run all tests
# test:
cargo test --workspace --all-targets
- run: just ci-test-msrv
64 changes: 64 additions & 0 deletions rustutils/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env just --justfile

@_default:
just --list

# Clean all build artifacts
clean:
cargo clean

# Update all dependencies, including the breaking changes. Requires nightly toolchain (install with `rustup install nightly`)
update:
cargo +nightly -Z unstable-options update --breaking
cargo update

# Quick compile without building a binary
check:
RUSTFLAGS='-D warnings' cargo check --workspace --all-targets

# Build the library
build:
cargo build --workspace

# Run cargo clippy to lint the code
clippy:
cargo clippy --all-targets --workspace -- -D warnings

# Test code formatting
test-fmt:
cargo fmt --all -- --check

# Reformat all code `cargo fmt`. If nightly is available, use it for better results
fmt:
#!/usr/bin/env bash
set -euo pipefail
if command -v cargo +nightly &> /dev/null; then
echo 'Reformatting Rust code using nightly Rust fmt to sort imports'
cargo +nightly fmt --all -- --config imports_granularity=Module,group_imports=StdExternalCrate
else
echo 'Reformatting Rust with the stable cargo fmt. Install nightly with `rustup install nightly` for better results'
cargo fmt --all
fi
# Run all tests
test:
cargo test --all-targets --workspace

# Test documentation
test-doc:
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps

# Build and open code documentation
docs:
cargo doc --no-deps --open

# Print Rust version information
rust-info:
rustc --version
cargo --version

# Run all tests as expected by CI
ci-test: rust-info test-fmt clippy build test test-doc

# Run minimal subset of tests to ensure compatibility with MSRV (Minimum Supported Rust Version). This assumes the default toolchain is already set to MSRV.
ci-test-msrv: rust-info build test
42 changes: 42 additions & 0 deletions rustutils/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use csscolorparser::Color;

#[cxx::bridge(namespace = "rustutils")]
mod ffi {
// TODO: Use #[cfg_attr(test, derive(...))] once supported
// See https://github.com/dtolnay/cxx/issues/1022
#[derive(Debug, PartialEq)]
struct ParsedColor {
pub success: bool,
pub r: f32,
Expand Down Expand Up @@ -33,3 +36,42 @@ pub fn parse_css_color(css_str: &str) -> ffi::ParsedColor {
a: 0.0,
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_css_color() {
assert_eq!(
parse_css_color("rgb(255, 0, 0)"),
ffi::ParsedColor {
success: true,
r: 1.0,
g: 0.0,
b: 0.0,
a: 1.0,
}
);
assert_eq!(
parse_css_color("rgba(255, 0, 0, 0.5)"),
ffi::ParsedColor {
success: true,
r: 1.0,
g: 0.0,
b: 0.0,
a: 0.5,
}
);
assert_eq!(
parse_css_color("invalid"),
ffi::ParsedColor {
success: false,
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
}
);
}
}

0 comments on commit 20bef88

Please sign in to comment.