diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 1c8d4d228..3cd73a469 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -97,5 +97,5 @@ Key architectural boundaries: - `pact_models` is patched in `rust/Cargo.toml` via `[patch.crates-io]` to the local path. Changes there automatically affect workspace crates without publishing. - Logging and test output are centered on `tracing`/`tracing-subscriber`; tests commonly use `test-log` so `RUST_LOG=debug` is useful when debugging failures. - Common test helpers across crates are `rstest`, `expectest`, `hamcrest2`, and `pretty_assertions`. Follow those existing choices before introducing new test utilities. -- CI exercises more than the default happy path: full workspace build/test, Linux `cargo clippy`, `cargo check --no-default-features`, WASM builds for `pact_models` and `pact_matching`, and a separate run with `PACT_MATCHING_ENGINE=v2`. +- CI (`.github/workflows/test.yml`) runs on Linux, macOS and Windows: nextest plus doctests, the compatibility suite, the FFI C examples, a musl build, `cargo hack check --each-feature`, WASM builds for `pact_models` and `pact_matching`, Linux `cargo clippy`, and an advisory run with `PACT_MATCHING_ENGINE=v2`. - Commit messages follow Conventional Changelog style (`feat:`, `fix:`, `chore:`, `refactor:`). diff --git a/.github/scripts/ffi-examples.sh b/.github/scripts/ffi-examples.sh new file mode 100755 index 000000000..c288530a4 --- /dev/null +++ b/.github/scripts/ffi-examples.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Builds pact_ffi in release through its CMake project, installs it under +# rust/pact_ffi/build/install (lib/ + include/pact.h), then builds and runs +# every example in rust/pact_ffi/examples/ against that install. +set -euo pipefail + +cd "$(dirname "$0")/../../rust/pact_ffi" +install_dir="$PWD/build/install" + +# MARK: Library + +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build --config Release +cmake --build build --config Release --target generate_header +cmake --install build --config Release --prefix "$install_dir" + +# MARK: Examples + +for example in examples/*/; do + name=$(basename "$example") + build_dir="build/examples/$name" + echo "::group::Example $name" + cmake -S "$example" -B "$build_dir" -DCMAKE_BUILD_TYPE=Release + cmake --build "$build_dir" --config Release + case "$(uname -s)" in + MINGW* | MSYS* | CYGWIN*) + PATH="$install_dir/lib:$PATH" "$build_dir/Release/example.exe" + ;; + Darwin) + DYLD_LIBRARY_PATH="$install_dir/lib" "$build_dir/example" + ;; + *) + LD_LIBRARY_PATH="$install_dir/lib" "$build_dir/example" + ;; + esac + echo "::endgroup::" +done diff --git a/.github/workflows/build-ffi.yml b/.github/workflows/build-ffi.yml deleted file mode 100644 index e31fc0d5d..000000000 --- a/.github/workflows/build-ffi.yml +++ /dev/null @@ -1,79 +0,0 @@ -name: Pact-Rust FFI Build - -on: - push: - branches: - - master - pull_request: - branches: - - master - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -env: - # Generic - FORCE_COLOR: '1' - CLICOLOR: '1' - # Rust - CARGO_TERM_COLOR: always - RUST_BACKTRACE: '1' - -jobs: - build: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - rust: [ nightly ] - env: - pact_do_not_track: true - steps: - - name: Checkout code - uses: actions/checkout@v5 - - - name: Install Conan - uses: conan-io/setup-conan@09566912d661de90608308897a4d513e850c04e8 # v1.2.0 - with: - cache_packages: true - - - name: Install Rust - uses: dtolnay/rust-toolchain@stable - with: - toolchain: ${{ matrix.rust }} - - - name: Cache Rust build artifacts - uses: Swatinem/rust-cache@v2 - with: - workspaces: rust -> target - - - name: Install cbindgen and just - uses: taiki-e/install-action@main - with: - tool: cbindgen,just - - - name: Install macOS dependencies - if: runner.os == 'macOS' - run: | - brew install \ - shared-mime-info \ - gettext - - - name: Check plugin log callback headers - if: runner.os == 'Linux' - working-directory: rust/pact_ffi - run: bash tests/check-plugin-log-callback.sh - - - name: Run C consumer examples - working-directory: c/consumer - run: just run - - - name: Run C provider verification - working-directory: c/provider - run: just run - - - name: Run mock_server_logs test - run: cargo test -p pact_ffi returns_mock_server_logs -- --nocapture --include-ignored - working-directory: rust diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 9ac77f3c3..000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,100 +0,0 @@ -name: Pact-Rust Build - -on: - push: - branches: - - '**' - pull_request: - branches: - - master - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -jobs: - build: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - env: - pact_do_not_track: true - steps: - - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: stable - components: clippy - - uses: Swatinem/rust-cache@v2 - with: - workspaces: rust -> target - - name: Install shared mime info DB - if: runner.os == 'macOS' - run: brew install shared-mime-info - - name: Tests - run: cargo test - working-directory: rust - env: - RUST_LOG: debug - RUST_BACKTRACE: 1 - - name: Build Components - run: cargo build - working-directory: rust - - name: Clippy - if: runner.os == 'Linux' - run: cargo clippy - working-directory: rust - - v2-engine: - runs-on: ubuntu-latest - continue-on-error: true - env: - pact_do_not_track: true - steps: - - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - workspaces: rust -> target - - name: Tests - run: cargo test - working-directory: rust - env: - RUST_LOG: debug - RUST_BACKTRACE: 1 - PACT_MATCHING_ENGINE: v2 - PACT_V2_MATCHING_LOG_EXECUTED_PLAN: 1 - - musl-build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - run: | - docker run --rm --user "$(id -u)":"$(id -g)" -v $(pwd):/workspace -w /workspace/rust -t -e TZ=UTC pactfoundation/rust-musl-build ./scripts/ci-musl-build.sh - - check-features: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: Swatinem/rust-cache@v2 - with: - workspaces: rust -> target - - run: cargo check --no-default-features - working-directory: rust - - check-wasm: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@stable - with: - targets: wasm32-wasip2 - - uses: Swatinem/rust-cache@v2 - with: - workspaces: rust -> target - - run: cargo build --target wasm32-wasip2 - working-directory: rust/pact_models - - run: cargo build --target wasm32-wasip2 - working-directory: rust/pact_matching diff --git a/.github/workflows/compatability-suite.yml b/.github/workflows/compatability-suite.yml deleted file mode 100644 index 00fbef144..000000000 --- a/.github/workflows/compatability-suite.yml +++ /dev/null @@ -1,78 +0,0 @@ -name: Pact-Rust Compatibility Suite - -on: - push: - branches: - - master - pull_request: - branches: - - master - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -env: - pact_do_not_track: true - -jobs: - v1: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - steps: - - uses: actions/checkout@v2 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - workspaces: compatibility-suite -> target - - name: Run Cucumber - run: cargo test --test v1* - working-directory: compatibility-suite - v2: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - steps: - - uses: actions/checkout@v2 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - workspaces: compatibility-suite -> target - - name: Run Cucumber - run: cargo test --test v2* - working-directory: compatibility-suite - v3: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - steps: - - uses: actions/checkout@v2 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - workspaces: compatibility-suite -> target - - name: Run Cucumber - run: cargo test --test v3* - working-directory: compatibility-suite - v4: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: ${{ fromJSON(github.event_name == 'pull_request' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-15"]') }} - steps: - - uses: actions/checkout@v2 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - workspaces: compatibility-suite -> target - - name: Run Cucumber - run: cargo test --test v4* - working-directory: compatibility-suite diff --git a/.github/workflows/issue-comment-created.yml b/.github/workflows/issue-comment-created.yml index 8d83e08a3..69639a685 100644 --- a/.github/workflows/issue-comment-created.yml +++ b/.github/workflows/issue-comment-created.yml @@ -5,26 +5,34 @@ on: types: - created +permissions: + issues: write + jobs: jira: runs-on: ubuntu-latest if: ${{ github.event.comment.body == '/jira ticket' }} steps: - - run: echo ${{ github.event.comment.body }} + - env: + COMMENT_BODY: ${{ github.event.comment.body }} + run: echo "$COMMENT_BODY" - name: Login - uses: atlassian/gajira-login@v3 + uses: atlassian/gajira-login@ca13f8850ea309cf44a6e4e0c49d9aa48ac3ca4c # v3 env: JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }} JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }} JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} - name: SearchParam - run: echo 'summary ~ ${{ toJSON(github.event.issue.title)}} AND project=${{ secrets.JIRA_PROJECT }}' + env: + ISSUE_TITLE: ${{ toJSON(github.event.issue.title) }} + JIRA_PROJECT: ${{ secrets.JIRA_PROJECT }} + run: echo "summary ~ ${ISSUE_TITLE} AND project=${JIRA_PROJECT}" - name: Search id: search - uses: tomhjp/gh-action-jira-search@v0.1.0 + uses: tomhjp/gh-action-jira-search@e099f122d99bf8f12707c157a98166b16498466c # v0.1.0 with: jql: 'summary ~ ${{ toJSON(github.event.issue.title)}} AND project=${{ secrets.JIRA_PROJECT }}' @@ -34,7 +42,7 @@ jobs: - name: Create id: create if: steps.search.outputs.issue == '' - uses: atlassian/gajira-create@v3 + uses: atlassian/gajira-create@1ff0b6bd115a780592b47bfbb63fc4629132e6ec # v3 with: project: ${{ secrets.JIRA_PROJECT }} issuetype: Task @@ -47,7 +55,7 @@ jobs: - name: Add Comment if: steps.search.outputs.issue == '' && steps.create.outputs.issue != '' - uses: actions/github-script@v5 + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: github-token: ${{secrets.GITHUB_TOKEN}} script: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f171ae1cd..608cbf78f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,10 +5,6 @@ on: types: - published - pull_request: - branches: - - master - workflow_dispatch: jobs: @@ -42,12 +38,14 @@ jobs: steps: - uses: actions/checkout@v4 + # With no `toolchain` input, the action installs the one pinned in + # rust-toolchain.toml and adds the matrix target to it. - name: Install release Rust toolchain - uses: dtolnay/rust-toolchain@stable + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 with: - # Rust 1.99 miscompiles C bool returns on Linux x86_64 (rust-lang/rust#163911). - toolchain: 1.98.1 - targets: ${{ matrix.targets }} + target: ${{ matrix.targets }} + build-warnings: warn + cache: false - name: Rust caching uses: Swatinem/rust-cache@v2 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..7a1cca3f9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,362 @@ +--- +name: Test + +on: + push: + branches: + - master + pull_request: + branches: + - master + types: + - opened + - synchronize + - reopened + - ready_for_review + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +env: + FORCE_COLOR: '1' + NEXTEST_PROFILE: ci + PACT_DO_NOT_TRACK: 'true' + +jobs: + # MARK: Sentinel + + complete: + name: CI complete + if: always() + needs: + - test + - compat + - ffi + - musl + - features + - wasm + - clippy + runs-on: ubuntu-latest + steps: + - name: Fail if any job failed + if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') + run: exit 1 + + # MARK: Cross-platform + + test: + name: Test (${{ matrix.os }}) + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + # Windows builds are dominated by filesystem overhead; ReFS Dev Drives + # avoid most of it. + - name: Set up Dev Drive + if: runner.os == 'Windows' + uses: samypr100/setup-dev-drive@562171fe8df7401fdf582d766cd3b6ab80d5b1a0 # v4.1.0 + with: + drive-size: 12GB + workspace-copy: true + env-mapping: | + CARGO_HOME,{{ DEV_DRIVE }}/.cargo + RUSTUP_HOME,{{ DEV_DRIVE }}/.rustup + + - name: Resolve workspace root + shell: bash + run: echo "WORKSPACE_ROOT=${DEV_DRIVE_WORKSPACE:-$GITHUB_WORKSPACE}" >> "$GITHUB_ENV" + + - name: Install macOS dependencies + if: runner.os == 'macOS' + run: brew install shared-mime-info + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + cache-workspaces: ${{ env.WORKSPACE_ROOT }}/rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + - name: Install tools + uses: jdx/mise-action@08ae2a091c038e1e05ec6f5115dc5c69d5a2a6b8 # v5.1.2 + with: + working_directory: ${{ env.WORKSPACE_ROOT }} + install_args: aqua:nextest-rs/nextest/cargo-nextest + cache_save: ${{ github.ref == 'refs/heads/master' }} + + - name: Run tests + working-directory: ${{ env.WORKSPACE_ROOT }}/rust + env: + RUST_LOG: debug + run: cargo nextest run --workspace + + # nextest does not run doctests. This step and the ones below run even + # when nextest fails, so one failure does not hide the rest. + - name: Run doctests + if: ${{ !cancelled() }} + working-directory: ${{ env.WORKSPACE_ROOT }}/rust + run: cargo test --workspace --doc --no-fail-fast + + - name: Run mock server log test + if: ${{ !cancelled() }} + working-directory: ${{ env.WORKSPACE_ROOT }}/rust + run: cargo nextest run -p pact_ffi --run-ignored only -E 'test(=returns_mock_server_logs)' + + - name: Run tests with the V2 matching engine + if: ${{ !cancelled() && runner.os == 'Linux' }} + continue-on-error: true + working-directory: ${{ env.WORKSPACE_ROOT }}/rust + env: + RUST_LOG: debug + PACT_MATCHING_ENGINE: v2 + PACT_V2_MATCHING_LOG_EXECUTED_PLAN: '1' + run: cargo nextest run --workspace + + compat: + name: Compatibility suite (${{ matrix.os }}) + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Dev Drive + if: runner.os == 'Windows' + uses: samypr100/setup-dev-drive@562171fe8df7401fdf582d766cd3b6ab80d5b1a0 # v4.1.0 + with: + drive-size: 12GB + workspace-copy: true + env-mapping: | + CARGO_HOME,{{ DEV_DRIVE }}/.cargo + RUSTUP_HOME,{{ DEV_DRIVE }}/.rustup + + - name: Resolve workspace root + shell: bash + run: echo "WORKSPACE_ROOT=${DEV_DRIVE_WORKSPACE:-$GITHUB_WORKSPACE}" >> "$GITHUB_ENV" + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + cache-workspaces: ${{ env.WORKSPACE_ROOT }}/compatibility-suite -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + # The cucumber binaries use `harness = false`, which nextest cannot drive. + - name: Run cucumber suites + working-directory: ${{ env.WORKSPACE_ROOT }}/compatibility-suite + run: cargo test --no-fail-fast + + ffi: + name: FFI (${{ matrix.os }}) + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + runs-on: ${{ matrix.os }} + defaults: + run: + shell: bash + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Dev Drive + if: runner.os == 'Windows' + uses: samypr100/setup-dev-drive@562171fe8df7401fdf582d766cd3b6ab80d5b1a0 # v4.1.0 + with: + drive-size: 12GB + workspace-copy: true + env-mapping: | + CARGO_HOME,{{ DEV_DRIVE }}/.cargo + RUSTUP_HOME,{{ DEV_DRIVE }}/.rustup + + - name: Resolve workspace root + run: echo "WORKSPACE_ROOT=${DEV_DRIVE_WORKSPACE:-$GITHUB_WORKSPACE}" >> "$GITHUB_ENV" + + - name: Install macOS dependencies + if: runner.os == 'macOS' + run: brew install shared-mime-info gettext + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + cache-workspaces: ${{ env.WORKSPACE_ROOT }}/rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + # cbindgen's macro expansion passes `-Zunpretty=expanded`, which only + # nightly accepts. The library itself builds with the pinned toolchain. + - name: Install nightly for cbindgen + run: rustup toolchain install nightly --profile minimal + + # mise's `cargo:` backend requires mise's own `rust` tool, which resolves + # to the toolchain already installed from rust-toolchain.toml. + - name: Install tools + uses: jdx/mise-action@08ae2a091c038e1e05ec6f5115dc5c69d5a2a6b8 # v5.1.2 + with: + working_directory: ${{ env.WORKSPACE_ROOT }} + install_args: rust aqua:cargo-bins/cargo-binstall cargo:cbindgen just + cache_save: ${{ github.ref == 'refs/heads/master' }} + + - name: Install Conan + uses: conan-io/setup-conan@ed3942a6883243f4af84e13e49c972f9440fa1c2 # v1.4.0 + + - name: Restore Conan packages + id: conan-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.conan2/p + key: conan-${{ runner.os }}-${{ hashFiles('c/*/conanfile.txt') }} + restore-keys: conan-${{ runner.os }}- + + - name: Check plugin log callback headers + if: runner.os == 'Linux' + working-directory: ${{ env.WORKSPACE_ROOT }}/rust/pact_ffi + run: bash tests/check-plugin-log-callback.sh + + - name: Build pact_ffi and run its examples + working-directory: ${{ env.WORKSPACE_ROOT }} + run: .github/scripts/ffi-examples.sh + + - name: Run C consumer example + working-directory: ${{ env.WORKSPACE_ROOT }}/c/consumer + run: PACT_FFI_PREBUILT_DIR="$WORKSPACE_ROOT/rust/pact_ffi/build/install" just run + + - name: Run C provider example + working-directory: ${{ env.WORKSPACE_ROOT }}/c/provider + run: PACT_FFI_PREBUILT_DIR="$WORKSPACE_ROOT/rust/pact_ffi/build/install" just run + + - name: Save Conan packages + if: github.ref == 'refs/heads/master' && steps.conan-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.conan2/p + key: conan-${{ runner.os }}-${{ hashFiles('c/*/conanfile.txt') }} + + # MARK: Linux-only + + musl: + name: Test (musl) + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + runs-on: ubuntu-latest + # The image's own toolchain is unused; rustup installs the pinned one + # from rust-toolchain.toml on first use. + container: rust:alpine + steps: + - name: Install build dependencies + run: >- + apk add --no-cache bash git musl-dev openssl-dev shared-mime-info + protobuf protobuf-dev tar tzdata zlib-dev + + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + components: rustfmt + cache-workspaces: rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + - name: Build and test + working-directory: rust + env: + TZ: UTC + run: | + cargo build + cargo test --no-fail-fast + + features: + name: Feature combinations + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + cache-workspaces: rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + - name: Install tools + uses: jdx/mise-action@08ae2a091c038e1e05ec6f5115dc5c69d5a2a6b8 # v5.1.2 + with: + install_args: github:taiki-e/cargo-hack + cache_save: ${{ github.ref == 'refs/heads/master' }} + + - name: Check each feature + working-directory: rust + run: cargo hack check --workspace --each-feature --no-dev-deps + + wasm: + name: WASM + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + target: wasm32-wasip2 + cache-workspaces: rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + - name: Build pact_models + working-directory: rust/pact_models + run: cargo build --target wasm32-wasip2 + + - name: Build pact_matching + working-directory: rust/pact_matching + run: cargo build --target wasm32-wasip2 + + clippy: + name: Clippy + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@ecabd13d1c56bd1345c230e542e9144811ad706f # v2.0.0 + with: + build-warnings: warn + components: clippy + cache-workspaces: rust -> target + cache-save-if: ${{ github.ref == 'refs/heads/master' }} + + # No `-D warnings`: only deny-by-default lints fail the job. + - name: Run clippy + working-directory: rust + run: cargo clippy diff --git a/.github/workflows/trigger_pact_docs_update.yml b/.github/workflows/trigger_pact_docs_update.yml index 8129910ae..60fe20020 100644 --- a/.github/workflows/trigger_pact_docs_update.yml +++ b/.github/workflows/trigger_pact_docs_update.yml @@ -7,6 +7,8 @@ on: paths: - '**.md' +permissions: {} + jobs: run: runs-on: ubuntu-latest diff --git a/CLAUDE.md b/CLAUDE.md index 42e8e1979..ae8f5c476 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -24,6 +24,10 @@ cargo test # Run tests for a specific crate cargo test --package pact_models +# Run tests with nextest, as CI does (install tools with `mise install`) +cargo nextest run --workspace +cargo test --workspace --doc # nextest skips doctests + # Run a single test by name (exact match) cargo test --package pact_models -- my_test_name --exact @@ -90,6 +94,7 @@ pact_verifier_cli pact_ffi ← Standalone CLI / C FFI bindings - **Rust edition**: 2024 for all crates except `pact_ffi` (2021). - **Minimum Rust version**: 1.88.0 (required for edition 2024). +- **Toolchain**: pinned in the root `rust-toolchain.toml`. mise manages the dev tooling. cbindgen alone needs a nightly toolchain, for macro expansion. - **Async runtime**: `tokio` throughout (except WASM builds). - **Logging**: `tracing` / `tracing-subscriber` (not `log`). - **Test helpers**: `rstest`, `hamcrest2`, `expectest`, `pretty_assertions` are common dev-dependencies. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8707b6e3b..8c3c4e9ca 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -48,6 +48,20 @@ cargo build This will compile all the libraries and put the generated files in `rust/target/debug`. +The root `rust-toolchain.toml` pins the Rust version. [mise](https://mise.jdx.dev) manages the rest of the dev tooling; run `mise install` from the repository root to install it. + +### Testing + +CI runs the tests with [nextest](https://nexte.st). nextest skips doctests, so run those separately: + +```shell +cd rust +cargo nextest run --workspace +cargo test --workspace --doc +``` + +Plain `cargo test` also runs the whole suite, doctests included. + ### Releasing The released libraries for each module are built by a GH action that attaches the libraries to the GH release for each diff --git a/README.md b/README.md index 377d18dfb..440ef1593 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ ![Logo of the project](https://raw.githubusercontent.com/pact-foundation/pact-reference/master/images/logo.svg) -[![Pact-Rust Build](https://github.com/pact-foundation/pact-reference/workflows/Pact-Rust%20Build/badge.svg)](https://github.com/pact-foundation/pact-reference/actions?query=workflow%3A%22Pact-Rust+Build%22) -[![Pact-Rust FFI Build](https://github.com/pact-foundation/pact-reference/actions/workflows/build-ffi.yml/badge.svg)](https://github.com/pact-foundation/pact-reference/actions/workflows/build-ffi.yml) +[![Test](https://github.com/pact-foundation/pact-reference/actions/workflows/test.yml/badge.svg)](https://github.com/pact-foundation/pact-reference/actions/workflows/test.yml) # Pact Reference Implementation diff --git a/c/consumer/CMakeLists.txt b/c/consumer/CMakeLists.txt index d1b75a4c0..969b6da96 100644 --- a/c/consumer/CMakeLists.txt +++ b/c/consumer/CMakeLists.txt @@ -11,16 +11,6 @@ if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conan_toolchain.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/conan_toolchain.cmake") endif() -# We use [corrosion](https://github.com/corrosion-rs/corrosion) to integrate -# Rust into our C project. -include(FetchContent) -FetchContent_Declare( - Corrosion - GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git - GIT_TAG v0.5 -) -FetchContent_MakeAvailable(Corrosion) - get_filename_component(PACT_FFI_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../rust/pact_ffi" ABSOLUTE) ################################################################################ @@ -56,14 +46,58 @@ find_package(ZLIB REQUIRED) # dependencies' build scripts (e.g. mlua's vendored Lua), which cargo bakes # into the cdylib at link time but does not surface to corrosion for the # staticlib, causing missing symbol errors when linking against libpact_ffi.a. -corrosion_import_crate( - MANIFEST_PATH "${PACT_FFI_ROOT}/Cargo.toml" - CRATE_TYPES cdylib -) -corrosion_experimental_cbindgen( - TARGET pact_ffi - HEADER_NAME pact.h -) +# PACT_FFI_PREBUILT_DIR names an install prefix holding lib/ and +# include/pact.h. CI sets it to reuse one release build across projects; +# without it, Corrosion builds pact_ffi from source. +if(DEFINED ENV{PACT_FFI_PREBUILT_DIR}) + file(TO_CMAKE_PATH "$ENV{PACT_FFI_PREBUILT_DIR}" PACT_FFI_PREBUILT_DIR) + add_library(pact_ffi SHARED IMPORTED) + if(WIN32) + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/pact_ffi.dll" + IMPORTED_IMPLIB "${PACT_FFI_PREBUILT_DIR}/lib/pact_ffi.dll.lib") + elseif(APPLE) + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/libpact_ffi.dylib") + else() + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/libpact_ffi.so") + endif() + target_include_directories(pact_ffi INTERFACE "${PACT_FFI_PREBUILT_DIR}/include") +else() + # We use [corrosion](https://github.com/corrosion-rs/corrosion) to + # integrate Rust into our C project. + include(FetchContent) + FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.5 + ) + FetchContent_MakeAvailable(Corrosion) + + corrosion_import_crate( + MANIFEST_PATH "${PACT_FFI_ROOT}/Cargo.toml" + CRATE_TYPES cdylib + ) + + # cbindgen's macro expansion needs nightly; the library itself builds + # with the toolchain pinned in rust-toolchain.toml. + find_program(CBINDGEN_EXECUTABLE cbindgen REQUIRED) + set(PACT_FFI_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pact_ffi/include") + file(MAKE_DIRECTORY "${PACT_FFI_INCLUDE_DIR}") + add_custom_command( + OUTPUT "${PACT_FFI_INCLUDE_DIR}/pact.h" + COMMAND rustup run nightly "${CBINDGEN_EXECUTABLE}" + --config "${PACT_FFI_ROOT}/cbindgen.toml" + --crate pact_ffi + --output "${PACT_FFI_INCLUDE_DIR}/pact.h" + WORKING_DIRECTORY "${PACT_FFI_ROOT}" + COMMENT "Generating pact.h with cbindgen" + ) + add_custom_target(pact_ffi_header DEPENDS "${PACT_FFI_INCLUDE_DIR}/pact.h") + add_dependencies(pact_ffi pact_ffi_header) + target_include_directories(pact_ffi INTERFACE "${PACT_FFI_INCLUDE_DIR}") +endif() target_link_libraries( pact_ffi @@ -106,6 +140,14 @@ if(WIN32) ) endif() +# The run recipe loads the library from beside the executable, where +# Corrosion would have built it, so copy the prebuilt one there. +if(DEFINED ENV{PACT_FFI_PREBUILT_DIR}) + add_custom_command(TARGET pact-consumer POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ $) +endif() + # Link macOS system frameworks if building on Apple platforms if(APPLE) target_link_libraries( diff --git a/c/consumer/README.md b/c/consumer/README.md index a1328ceb7..75713a71b 100644 --- a/c/consumer/README.md +++ b/c/consumer/README.md @@ -33,7 +33,8 @@ The results of the tests are printed to stdout and will exit with a non-zero sta The tests require: -- A C and Rust compiler (Rust compiler requires nightly toolchain) +- A C compiler and the Rust toolchain pinned in `rust-toolchain.toml` +- `cbindgen` and a nightly Rust toolchain, which `cbindgen` needs to generate the header - CMake (version 3.24 or higher) - Conan (version 2.0 or higher) diff --git a/c/provider/CMakeLists.txt b/c/provider/CMakeLists.txt index 0245644e8..8101433a5 100644 --- a/c/provider/CMakeLists.txt +++ b/c/provider/CMakeLists.txt @@ -11,16 +11,6 @@ if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conan_toolchain.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/conan_toolchain.cmake") endif() -# We use [corrosion](https://github.com/corrosion-rs/corrosion) to integrate -# Rust into our C project. -include(FetchContent) -FetchContent_Declare( - Corrosion - GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git - GIT_TAG v0.5 -) -FetchContent_MakeAvailable(Corrosion) - get_filename_component(PACT_FFI_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../rust/pact_ffi" ABSOLUTE) ################################################################################ @@ -55,14 +45,58 @@ find_package(ZLIB REQUIRED) # dependencies' build scripts (e.g. mlua's vendored Lua), which cargo bakes # into the cdylib at link time but does not surface to corrosion for the # staticlib, causing missing symbol errors when linking against libpact_ffi.a. -corrosion_import_crate( - MANIFEST_PATH "${PACT_FFI_ROOT}/Cargo.toml" - CRATE_TYPES cdylib -) -corrosion_experimental_cbindgen( - TARGET pact_ffi - HEADER_NAME pact.h -) +# PACT_FFI_PREBUILT_DIR names an install prefix holding lib/ and +# include/pact.h. CI sets it to reuse one release build across projects; +# without it, Corrosion builds pact_ffi from source. +if(DEFINED ENV{PACT_FFI_PREBUILT_DIR}) + file(TO_CMAKE_PATH "$ENV{PACT_FFI_PREBUILT_DIR}" PACT_FFI_PREBUILT_DIR) + add_library(pact_ffi SHARED IMPORTED) + if(WIN32) + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/pact_ffi.dll" + IMPORTED_IMPLIB "${PACT_FFI_PREBUILT_DIR}/lib/pact_ffi.dll.lib") + elseif(APPLE) + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/libpact_ffi.dylib") + else() + set_target_properties(pact_ffi PROPERTIES + IMPORTED_LOCATION "${PACT_FFI_PREBUILT_DIR}/lib/libpact_ffi.so") + endif() + target_include_directories(pact_ffi INTERFACE "${PACT_FFI_PREBUILT_DIR}/include") +else() + # We use [corrosion](https://github.com/corrosion-rs/corrosion) to + # integrate Rust into our C project. + include(FetchContent) + FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.5 + ) + FetchContent_MakeAvailable(Corrosion) + + corrosion_import_crate( + MANIFEST_PATH "${PACT_FFI_ROOT}/Cargo.toml" + CRATE_TYPES cdylib + ) + + # cbindgen's macro expansion needs nightly; the library itself builds + # with the toolchain pinned in rust-toolchain.toml. + find_program(CBINDGEN_EXECUTABLE cbindgen REQUIRED) + set(PACT_FFI_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pact_ffi/include") + file(MAKE_DIRECTORY "${PACT_FFI_INCLUDE_DIR}") + add_custom_command( + OUTPUT "${PACT_FFI_INCLUDE_DIR}/pact.h" + COMMAND rustup run nightly "${CBINDGEN_EXECUTABLE}" + --config "${PACT_FFI_ROOT}/cbindgen.toml" + --crate pact_ffi + --output "${PACT_FFI_INCLUDE_DIR}/pact.h" + WORKING_DIRECTORY "${PACT_FFI_ROOT}" + COMMENT "Generating pact.h with cbindgen" + ) + add_custom_target(pact_ffi_header DEPENDS "${PACT_FFI_INCLUDE_DIR}/pact.h") + add_dependencies(pact_ffi pact_ffi_header) + target_include_directories(pact_ffi INTERFACE "${PACT_FFI_INCLUDE_DIR}") +endif() target_link_libraries( pact_ffi @@ -121,6 +155,14 @@ if(WIN32) ) endif() +# The run recipe loads the library from beside the executable, where +# Corrosion would have built it, so copy the prebuilt one there. +if(DEFINED ENV{PACT_FFI_PREBUILT_DIR}) + add_custom_command(TARGET pact-provider POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ $) +endif() + # Link macOS system frameworks if building on Apple platforms if(APPLE) target_link_libraries( diff --git a/c/provider/README.md b/c/provider/README.md index 8afebdb38..5600547a6 100644 --- a/c/provider/README.md +++ b/c/provider/README.md @@ -34,7 +34,8 @@ It assumes that the consumer contracts have already been generated by running th The verification tests require: -- A C and Rust compiler (Rust compiler requires nightly toolchain) +- A C compiler and the Rust toolchain pinned in `rust-toolchain.toml` +- `cbindgen` and a nightly Rust toolchain, which `cbindgen` needs to generate the header - CMake (version 3.24 or higher) - Conan (version 2.0 or higher) diff --git a/mise.toml b/mise.toml new file mode 100644 index 000000000..40eab52a4 --- /dev/null +++ b/mise.toml @@ -0,0 +1,18 @@ +#:schema https://mise.jdx.dev/schema/mise.json + +# Development tooling. Rust itself is read from `rust-toolchain.toml`. +[tools] +"aqua:cargo-bins/cargo-binstall" = "1.23.0" +"aqua:nextest-rs/nextest/cargo-nextest" = "0.9.148" +"cargo:cbindgen" = "0.29.4" +"github:taiki-e/cargo-hack" = "0.6.45" +just = "1.59.0" + +[settings] +idiomatic_version_file_enable_tools = ["rust"] + +# Install cargo tools from prebuilt binaries only, falling back to +# cargo-quickinstall's builds; never compile them from source. +[settings.cargo] +binstall_only = true +binstall_quickinstall = true diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 000000000..246cb22c3 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.99.1" diff --git a/rust/.config/nextest.toml b/rust/.config/nextest.toml new file mode 100644 index 000000000..64f5ad2c3 --- /dev/null +++ b/rust/.config/nextest.toml @@ -0,0 +1,16 @@ +[profile.ci] +# Report every failure in one run. +fail-fast = false + +# Flag tests slower than 60 s; kill them after 5 periods. +slow-timeout = { period = "60s", terminate-after = 5 } + +# pact_verifier's unit tests merge their mock servers' interactions into +# shared pact files. Pact writes lock only within a process, and nextest runs +# each test in its own, so concurrent runs read each other's truncated files. +[test-groups] +pact-verifier-pact-files = { max-threads = 1 } + +[[profile.default.overrides]] +filter = 'package(pact_verifier) & kind(lib) & test(/^(pact_broker::)?tests::/)' +test-group = 'pact-verifier-pact-files' diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 794119122..fd80a28dd 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -130,7 +130,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -141,7 +141,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -661,9 +661,12 @@ checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" [[package]] name = "constant_time_eq" -version = "0.4.2" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b" +checksum = "9eb6feb175df65d0a3ccd3e73484e940d0ac664e8034f0519fd49a93db3071d9" +dependencies = [ + "rustversion", +] [[package]] name = "content_inspector" @@ -1103,7 +1106,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -1892,15 +1895,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.15.0" @@ -2204,9 +2198,9 @@ dependencies = [ [[package]] name = "lzma-rust2" -version = "0.16.5" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca93e534d1142d1d0dcca6d25fe302508a5dfb40b302802904577725ea0b695b" +checksum = "7277c9742229dc25243236d578b6471a798c554512c6f6a04365ec2e87c2a3bb" dependencies = [ "sha2 0.11.0", ] @@ -2458,7 +2452,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -2833,13 +2827,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] name = "pact-plugin-driver" version = "1.2.7" -source = "git+https://github.com/pact-foundation/pact-plugins.git#d7882b0273cffa0b963e07cd9be2fccd1dce90b1" +source = "git+https://github.com/pact-foundation/pact-plugins.git#7a290b6b165b839abf897f77fad9cce31db728c0" dependencies = [ "anyhow", "async-trait", @@ -3538,7 +3532,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" dependencies = [ "heck", - "itertools 0.14.0", + "itertools 0.13.0", "log", "multimap", "petgraph", @@ -3559,7 +3553,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" dependencies = [ "anyhow", - "itertools 0.14.0", + "itertools 0.13.0", "proc-macro2 1.0.107", "quote 1.0.47", "syn 2.0.119", @@ -4056,7 +4050,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4122,7 +4116,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4516,7 +4510,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4563,7 +4557,7 @@ dependencies = [ "cfg-if", "libc", "psm", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4750,7 +4744,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -5556,7 +5550,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -6011,9 +6005,9 @@ dependencies = [ [[package]] name = "zip" -version = "8.6.0" +version = "9.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" +checksum = "a954114b47a54df49f83d2722814e86b23d4bb1b54a1b90fb7d92b41619ddc22" dependencies = [ "aes", "bzip2", @@ -6026,6 +6020,7 @@ dependencies = [ "indexmap 2.14.2", "lzma-rust2", "memchr", + "nix", "pbkdf2", "ppmd-rust", "sha1", @@ -6062,27 +6057,27 @@ dependencies = [ [[package]] name = "zstd" -version = "0.13.3" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" +checksum = "db3ae9bf90af6e40197bef767eeefc46ad6397420fe873830bd85e5fbbfb3c70" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "7.3.0" +version = "8.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64d80649ab6db9d9f6f9c80a40becd948eda4714a0a5ac8c4d157a32231c7882" +checksum = "ded520f16b29e1a9b771c722221b2b0a0398c2316d9bb3c42b458a5414d7434b" dependencies = [ "zstd-sys", ] [[package]] name = "zstd-sys" -version = "2.1.1+zstd.1.5.7" +version = "2.1.2+zstd.1.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aeec9eaf2dffbbd09201e23bd0ffcbaa33bb8e9266a10734fd7ed90a85eca078" +checksum = "481ada9c543b2323cbb7bbdbb5249177fbf0188ed959ed2c15e4b22421c84ed3" dependencies = [ "cc", "pkg-config", diff --git a/rust/README.md b/rust/README.md index f12da1511..3d75becf9 100644 --- a/rust/README.md +++ b/rust/README.md @@ -1,7 +1,6 @@ ![Logo of the project](https://raw.githubusercontent.com/pact-foundation/pact-reference/master/images/logo.svg) -[![Pact-Rust Build](https://github.com/pact-foundation/pact-reference/workflows/Pact-Rust%20Build/badge.svg)](https://github.com/pact-foundation/pact-reference/actions?query=workflow%3A%22Pact-Rust+Build%22) -[![Windows Build status](https://ci.appveyor.com/api/projects/status/bqlb7ny924lsu6yi?svg=true)](https://ci.appveyor.com/project/pact-foundation/pact-reference) +[![Test](https://github.com/pact-foundation/pact-reference/actions/workflows/test.yml/badge.svg)](https://github.com/pact-foundation/pact-reference/actions/workflows/test.yml) # Pact-Rust > Pact implementation written in Rust. diff --git a/rust/pact_consumer/tests/tests.rs b/rust/pact_consumer/tests/tests.rs index 322a65f6c..6bbf56471 100644 --- a/rust/pact_consumer/tests/tests.rs +++ b/rust/pact_consumer/tests/tests.rs @@ -28,6 +28,11 @@ use pact_models::sync_pact::RequestResponsePact; use pact_models::v4::http_parts::{HttpRequest, HttpResponse}; use pact_models::v4::synch_http::SynchronousHttp; +/// Installs the rustls crypto provider that a test's own HTTP clients need. +fn install_crypto_provider() { + let _ = rustls::crypto::ring::default_provider().install_default(); +} + /// This is supposed to be a doctest in mod, but it's breaking there, so /// we have an executable copy here. #[test_log::test(tokio::test)] @@ -107,6 +112,7 @@ async fn mock_server_passing_validation() -> anyhow::Result<()> { #[test_log::test] fn mock_server_passing_validation_blocking() -> anyhow::Result<()> { + install_crypto_provider(); let alice_service = PactBuilder::new_v4("BlockingConsumer", "Alice Service") .interaction("a retrieve Mallory request", "", |mut i| { i.given("there is some good mallory"); @@ -331,6 +337,7 @@ async fn post_json_with_incorrect_content_type() { // Issue #300 #[test_log::test(tokio::test)] async fn multi_value_headers() { + install_crypto_provider(); let alice_service = PactBuilder::new_v4("Consumer", "Alice Service") .interaction("a retrieve Mallory request", "", |mut i| { i.given("there is some good mallory"); diff --git a/rust/pact_ffi/CMakeLists.txt b/rust/pact_ffi/CMakeLists.txt index bc751ce04..1b47d4bfd 100644 --- a/rust/pact_ffi/CMakeLists.txt +++ b/rust/pact_ffi/CMakeLists.txt @@ -146,10 +146,6 @@ endif() # Uses the finder specified in `cmake/FindCargo.cmake` find_package(Cargo) -if(NOT CARGO_CHANNEL STREQUAL "nightly") - message(FATAL_ERROR "${NAME_STRING} requires a nightly Cargo version to build.") -endif() - # Uses the finder specified in `cmake/FindCbindgen.cmake` find_package(Cbindgen) diff --git a/rust/pact_ffi/README.md b/rust/pact_ffi/README.md index ebb1fafc9..6547b7ac2 100644 --- a/rust/pact_ffi/README.md +++ b/rust/pact_ffi/README.md @@ -14,29 +14,12 @@ documentation on the integration JSON format is [IntegrationJson.md](https://git This crates requires: - `cbindgen`, a tool for automatically generating the header file needed for C users of the crate. -- A nightly-channel version of Cargo (needed for an unstable flag used by `cbindgen` to get the macro-expanded contents of the crate source). +- A nightly Rust toolchain, used only by `cbindgen` for an unstable flag that gets the macro-expanded contents of the crate source. The library itself builds with the toolchain pinned in `rust-toolchain.toml`. It will additionally attempt to find and use `Doxygen` to generate C-friendly documentation (you can of course alternatively use `cargo doc` to get Rustdoc documentation). **Note:** Linking to the generated static library on Linux requires you to also link to `pthread`, `dl` and `m`. -## Release compiler - -The [release workflow](../../.github/workflows/release.yml) pins Rust to **1.98.1** -to avoid the Linux x86_64 C boolean return regression in Rust 1.99, tracked in -[rust-lang/rust#163911](https://github.com/rust-lang/rust/issues/163911). -Optimized code can return the `Option::None` encoding (`2`) instead of -the required false byte (`0`), causing foreign-language callers to interpret -false results as true. The 0.5.10 Linux x86_64 release artifacts exhibited this -issue; the same reduced example returns `0` with Rust 1.98.1. - -Keep the release compiler pinned until a corrected compiler is verified through -a foreign-language caller. Rust-only tests can miss this regression because -the lowest bit of the invalid return byte is still zero. This pin applies to -native release builds, not the nightly toolchain used to generate C headers. -Already-published artifacts are not repaired by this change; corrected artifacts -require rebuilding and a separate release. - ## Building with CMake For convenience, this tool integrates with CMake, which is setup to: diff --git a/rust/pact_ffi/ci-build.sh b/rust/pact_ffi/ci-build.sh deleted file mode 100755 index 209e0b674..000000000 --- a/rust/pact_ffi/ci-build.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -set -e -set -x - -rustc --version - -cargo install --force cbindgen -rm -rf ./include - -rustup toolchain install nightly - -echo ------------------------------------- -echo - Build library with CMake -echo ------------------------------------- -mkdir -p build -cd build -cmake -DCMAKE_BUILD_TYPE=Debug .. -cmake --build . -v -cd .. - -echo ------------------------------------- -echo - Generate header with cbindgen -echo ------------------------------------- -rustup run nightly cbindgen \ - --config cbindgen.toml \ - --crate pact_ffi \ - --output include/pact.h -rustup run nightly cbindgen \ - --config cbindgen-c++.toml \ - --crate pact_ffi \ - --output include/pact-c++.h - -echo ------------------------------------- -echo - Make library available for examples -echo ------------------------------------- -cd build -cmake --install . --prefix ./install - -echo ------------------------------------- -echo - Running examples -echo ------------------------------------- -cd .. -for i in examples/*; do - pushd $i - mkdir -p build - cd build - cmake .. - cmake --build . - - echo "Running example $i" - if [[ "$OSTYPE" == "msys"* ]]; then - cp ../../../build/install/lib/*.dll Debug/ - ./Debug/example.exe - elif [[ "$OSTYPE" == "darwin"* ]]; then - cp ../../../build/install/lib/*.dylib . - ./example - else - ./example - fi - - popd -done diff --git a/rust/pact_ffi/cmake/FindCargo.cmake b/rust/pact_ffi/cmake/FindCargo.cmake index 23eb54762..28ce53dd2 100644 --- a/rust/pact_ffi/cmake/FindCargo.cmake +++ b/rust/pact_ffi/cmake/FindCargo.cmake @@ -4,21 +4,14 @@ find_program(CARGO_EXECUTABLE cargo PATHS "$ENV{HOME}/.cargo" PATH_SUFFIXES bin) set(CARGO_VERSION "") -set(CARGO_CHANNEL "stable") # If we found it, see if we can get its version. if(CARGO_EXECUTABLE) - execute_process(COMMAND ${CARGO_EXECUTABLE} +nightly -V OUTPUT_VARIABLE CARGO_VERSION_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${CARGO_EXECUTABLE} -V OUTPUT_VARIABLE CARGO_VERSION_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE) if(CARGO_VERSION_OUTPUT MATCHES "cargo ([0-9]+\\.[0-9]+\\.[0-9]+).*") set(CARGO_VERSION ${CMAKE_MATCH_1}) endif() - - execute_process(COMMAND ${CARGO_EXECUTABLE} +nightly -V OUTPUT_VARIABLE CARGO_CHANNEL_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE) - - if(CARGO_CHANNEL_OUTPUT MATCHES "cargo [0-9]+\\.[0-9]+\\.[0-9]+-([a-zA-Z]*).*") - set(CARGO_CHANNEL ${CMAKE_MATCH_1}) - endif() endif() # Hides the CARGO_EXECUTABLE variable unless advanced variables are requested @@ -30,7 +23,6 @@ find_package_handle_standard_args( Cargo REQUIRED_VARS CARGO_VERSION - CARGO_CHANNEL CARGO_EXECUTABLE VERSION_VAR CARGO_VERSION diff --git a/rust/pact_ffi/tests/tests.rs b/rust/pact_ffi/tests/tests.rs index d3e9e55fa..c01bbe9b9 100644 --- a/rust/pact_ffi/tests/tests.rs +++ b/rust/pact_ffi/tests/tests.rs @@ -89,6 +89,13 @@ use pact_models::matchingrules::{MatchingRule, RuleLogic}; use pact_models::matchingrules::matchers_to_json; use pact_models::path_exp::DocPath; +/// HTTP client for calling mock servers. +/// It installs the rustls crypto provider, which the library installs only on its own code paths. +fn http_client() -> Client { + init_crypto(); + Client::default() +} + #[test] fn post_to_mock_server_with_mismatches() { // Refactored to use pactffi_create_mock_server_for_transport and pact handle API @@ -112,7 +119,7 @@ fn post_to_mock_server_with_mismatches() { let port = pactffi_create_mock_server_for_transport(pact_handle, address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); client.post(format!("http://127.0.0.1:{}/path", port).as_str()) .header(CONTENT_TYPE, "application/json") .body(r#"{"foo":"no-very-bar"}"#) @@ -527,7 +534,7 @@ fn http_consumer_feature_test() { // Mock server has started, we can't now modify the pact expect!(pactffi_upon_receiving(interaction.clone(), description.as_ptr())).to(be_false()); - let client = Client::default(); + let client = http_client(); let result = client.post(format!("http://127.0.0.1:{}/request/9999?foo=baz", port).as_str()) .header("Content-Type", "application/json") .header("Authorization", "Bearer 9999") @@ -596,7 +603,7 @@ fn http_xml_consumer_feature_test() { // Mock server has started, we can't now modify the pact expect!(pactffi_upon_receiving(interaction.clone(), description.as_ptr())).to(be_false()); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/xml", port).as_str()) .header("Accept", "application/xml") .send(); @@ -716,7 +723,7 @@ fn http_xml_consumer_with_examples_and_content_test() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/items", port).as_str()) .header("Accept", "application/xml") .send(); @@ -827,7 +834,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.post(format!("http://127.0.0.1:{}/upload", port).as_str()) .header("Content-Type", "image/gif") .body(buffer) @@ -951,7 +958,7 @@ fn each_value_matcher() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.put(format!("http://127.0.0.1:{}/book", port).as_str()) .header("Content-Type", "application/json") .body(r#"{"id1": "book100", "id2": "book2"}"#) @@ -1019,7 +1026,7 @@ fn each_key_matcher() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.put(format!("http://127.0.0.1:{}/book", port).as_str()) .header("Content-Type", "application/json") .body(r#"{"1": "foo","not valid": 1,"key": "value","key2": "value"}"#) @@ -1103,7 +1110,7 @@ fn array_contains_matcher() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/book", port).as_str()) .header("Content-Type", "application/json") .send(); @@ -1167,7 +1174,7 @@ fn multiple_query_values_with_regex_matcher() { let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/request?foo=1&foo=443&foo=112", port).as_str()) .send(); @@ -1869,7 +1876,7 @@ fn combined_each_key_and_each_value_matcher() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let json_body = json!({ "results": { "KGK-9954356": { @@ -1936,7 +1943,7 @@ fn matching_definition_expressions_matcher() { expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let json_body = json!({ "results": { "KGK-9954356": { @@ -1995,11 +2002,7 @@ fn returns_mock_server_logs() { let port = pactffi_create_mock_server_for_transport(pact_handle, address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - if rustls::crypto::CryptoProvider::get_default().is_none() { - let _ = rustls::crypto::ring::default_provider().install_default(); - } - - let client = Client::default(); + let client = http_client(); client.post(format!("http://127.0.0.1:{}/path", port).as_str()) .header(CONTENT_TYPE, "application/json") .body(r#"{"foo":"no-very-bar"}"#) @@ -2095,7 +2098,7 @@ fn http_form_urlencoded_consumer_feature_test() { // Mock server has started, we can't now modify the pact expect!(pactffi_upon_receiving(interaction.clone(), description.as_ptr())).to(be_false()); - let client = Client::default(); + let client = http_client(); let result = client.post(format!("http://127.0.0.1:{}/form-urlencoded", port).as_str()) .header("Accept", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded") @@ -2144,7 +2147,7 @@ fn time_matcher_in_query_parameters() { let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/request?item=12:13", port).as_str()) .send(); @@ -2193,7 +2196,7 @@ fn numeric_matcher_passing_test_sending_string_value() { let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let _result = client.post(format!("http://127.0.0.1:{}/request", port).as_str()) .header("Content-Type", "application/json") .body(r#"{"key2":"456","key1":"321.1"}"#) @@ -2245,7 +2248,7 @@ fn include_matcher_in_query_parameters() { let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let result = client.get(format!("http://127.0.0.1:{}/request?item=subway", port).as_str()) .send(); @@ -2304,7 +2307,7 @@ fn mime_multipart() { let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); - let client = Client::default(); + let client = http_client(); let form = reqwest::blocking::multipart::Form::new().text("baz", "bat"); let result = client.post(format!("http://127.0.0.1:{}/formpost", port).as_str()) .multipart(form) @@ -2378,7 +2381,7 @@ fn date_matcher_in_response_body_with_generator_type() { )) .to(be_false()); - let client = Client::default(); + let client = http_client(); let result = client .get(format!("http://127.0.0.1:{}/request", port).as_str()) .send(); @@ -2455,7 +2458,7 @@ fn date_matcher_in_response_body_without_generator_type() { )) .to(be_false()); - let client = Client::default(); + let client = http_client(); let result = client .get(format!("http://127.0.0.1:{}/request", port).as_str()) .send(); @@ -2558,7 +2561,7 @@ fn mime_multipart_with_json_and_image() { expect!(port).to(be_greater_than(0)); // Make actual request with different JSON values and the same JPEG structure - let client = Client::default(); + let client = http_client(); let form = reqwest::blocking::multipart::Form::new() .text("metadata", r#"{"name":"different","size":200}"#) .part( diff --git a/rust/pact_verifier/src/provider_client.rs b/rust/pact_verifier/src/provider_client.rs index 9b0d56c67..fff0e45cf 100644 --- a/rust/pact_verifier/src/provider_client.rs +++ b/rust/pact_verifier/src/provider_client.rs @@ -353,7 +353,7 @@ mod tests { #[test] fn convert_request_to_native_request_test() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest::default(); let request_builder = create_native_request(&client, &base_url, &request, &hashmap!{}).unwrap().build().unwrap(); @@ -365,7 +365,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_query_parameters() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { query: Some(hashmap!{ @@ -382,7 +382,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_headers() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { headers: Some(hashmap! { @@ -406,7 +406,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_body() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { body: OptionalBody::from("body"), @@ -421,7 +421,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_null_body() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { body: OptionalBody::Null, @@ -436,7 +436,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_json_null_body() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { headers: Some(hashmap! { @@ -493,7 +493,7 @@ mod tests { #[test] fn convert_request_to_native_request_with_custom_headers() { - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let base_url = "http://example.test:8080".to_string(); let request = HttpRequest { headers: Some(hashmap! { diff --git a/rust/pact_verifier/src/tests.rs b/rust/pact_verifier/src/tests.rs index f0f61f9e4..f520190df 100644 --- a/rust/pact_verifier/src/tests.rs +++ b/rust/pact_verifier/src/tests.rs @@ -41,6 +41,13 @@ use crate::VERIFIER_VERSION; use super::{execute_state_change, filter_consumers, filter_interaction, FilterInfo}; +/// HTTP client for tests that call servers directly. +/// It installs the rustls crypto provider, which production code installs before building its own clients. +pub(crate) fn http_client() -> Client { + let _ = rustls::crypto::ring::default_provider().install_default(); + Client::new() +} + #[test] fn if_no_interaction_filter_is_defined_returns_true() { let interaction = RequestResponseInteraction::default(); diff --git a/rust/pact_verifier/src/utils.rs b/rust/pact_verifier/src/utils.rs index 4738136b2..4df6c4e30 100644 --- a/rust/pact_verifier/src/utils.rs +++ b/rust/pact_verifier/src/utils.rs @@ -268,7 +268,7 @@ mod tests { }) .await; - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let request = client.get(&url); // retries=3: attempt 1 → 429, attempt 2 → 429, attempt 3 → 200 let result = with_retries(3, request).await; @@ -281,7 +281,7 @@ mod tests { async fn with_retries_does_not_retry_404_responses() { let (url, call_count) = spawn_server(|_| axum::http::StatusCode::NOT_FOUND.into_response()).await; - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let request = client.get(&url); let result = with_retries(3, request).await; // 404 is not retryable — should get a response after exactly 1 call @@ -295,7 +295,7 @@ mod tests { let (url, call_count) = spawn_server(|_| axum::http::StatusCode::TOO_MANY_REQUESTS.into_response()).await; - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let request = client.get(&url); // retries=0 must not panic and must send the request exactly once let result = with_retries(0, request).await; @@ -321,7 +321,7 @@ mod tests { }) .await; - let client = reqwest::Client::new(); + let client = crate::tests::http_client(); let request = client.get(&url); let result = with_retries(3, request).await; assert!(result.is_ok());