From 20bef88e64c6a7ebc7fd9f89e5b7e371f883eca6 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 24 Jan 2025 15:54:14 -0500 Subject: [PATCH] simplify ci, add tests --- .github/workflows/rust-ci.yaml | 50 ++++---------------------- rustutils/justfile | 64 ++++++++++++++++++++++++++++++++++ rustutils/src/color.rs | 42 ++++++++++++++++++++++ 3 files changed, 113 insertions(+), 43 deletions(-) create mode 100644 rustutils/justfile diff --git a/.github/workflows/rust-ci.yaml b/.github/workflows/rust-ci.yaml index abdf2636fb7..f750de407d5 100644 --- a/.github/workflows/rust-ci.yaml +++ b/.github/workflows/rust-ci.yaml @@ -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 @@ -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 diff --git a/rustutils/justfile b/rustutils/justfile new file mode 100644 index 00000000000..f9e5604d1eb --- /dev/null +++ b/rustutils/justfile @@ -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 diff --git a/rustutils/src/color.rs b/rustutils/src/color.rs index af951cecafd..12ba926add1 100644 --- a/rustutils/src/color.rs +++ b/rustutils/src/color.rs @@ -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, @@ -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, + } + ); + } +}