name: Rust Tests

on:
  workflow_call:
    inputs:
      safe-checkout:
        required: true
        description: "The SHA key for the commit we want to run over"
        type: string

permissions:
  contents: read

jobs:
  # This job should be run on presubmit, if any continuous-only tests are added we will need to input test-type above
  linux:
    strategy:
      fail-fast: false   # Don't cancel all jobs if one fails.
      matrix:
        config:
          - { name: "Bzlmod and Workspace", flags: --enable_bzlmod --enable_workspace }
          - { name: Optimized, flags: --config=opt }
          - { name: ASAN, flags: --config=asan }

        include:
          - targets: "//rust/... //src/google/protobuf/compiler/rust/..."
          - image: "us-docker.pkg.dev/protobuf-build/containers/common/linux/bazel:9.2.0-4d8e80ef93b0219fb907af9dd4596b92946995d8"
          - bazel_cmd: "test"

          # Override cases with custom images
          - config: { name: Cargo }
            # We need Rust 1.90 so that we can use the new workspace publishing
            # feature.
            image: "us-docker.pkg.dev/protobuf-build/containers/release/linux/rust:9.0.0-1.90.0-98863a45066747115144a8a8fb4818189d1e0761"
            bazel_cmd: "run"
            targets: "//rust/release_crates:cargo_test"

    name: Linux ${{ matrix.config.name }}
    runs-on: ubuntu-22-4core
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel-docker@v5
        with:
          image: ${{ matrix.image }}
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel-cache: rust_linux
          bazel: >-
            ${{ matrix.bazel_cmd }} --crosstool_top=//toolchain:clang_suite --@rules_rust//rust/settings:experimental_use_cc_common_link=True
            ${{ matrix.targets }} ${{ matrix.config.flags }}

  windows:
    strategy:
      fail-fast: false   # Don't cancel all jobs if one fails.
    name: Windows Cargo
    runs-on: windows-2022
    steps:
      - name: Checkout pending changes
        uses: protocolbuffers/protobuf-ci/checkout@v5
        with:
          ref: ${{ inputs.safe-checkout }}
      - name: Install Rust
        shell: bash
        # We need Rust 1.90 so that we can use the new workspace publishing
        # feature.
        run: rustup default 1.90
      - name: Run tests
        uses: protocolbuffers/protobuf-ci/bazel@v5
        with:
          credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
          bazel: run //rust/release_crates:cargo_test
          bazel-cache: windows-cargo
          version: 9.2.0
