tlafmt 0.4.1

A formatter for TLA+ specs
name: fuzz
on:
  workflow_call:
  pull_request:
  push:
    branches: [master]

permissions: {}

jobs:
  # Run each fuzz target for 60s.
  fuzz:
    runs-on: ubuntu-latest
    container:
      image: rustlang/rust:nightly
      env:
        CARGO_HOME: "/cargo/"
        FUZZ_TARGET: "${{ matrix.fuzz_target }}"
    strategy:
      matrix:
        fuzz_target: ["e2e", "module_body"]

    steps:
      - name: Ensure node is installed for act checkout
        if: ${{ env.ACT }}
        run: command -v node || (apt update && apt install -y nodejs zstd)

      - name: Checkout source
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Cache cargo dir
        id: cargo-dir
        uses: actions/cache@v4
        with:
          key: ${{ runner.os }}-cargo-nightly-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-nightly
          path: /cargo

      - name: Install cargo-fuzz
        if: steps.cargo-dir.outputs.cache-hit != 'true'
        run: cargo install cargo-fuzz --all-features

      - name: Cache corpus
        id: cache-fuzz-corpus
        uses: actions/cache@v4
        with:
          enableCrossOsArchive: true
          key: fuzz-corpus-${{ matrix.fuzz_target }}
          path: libtlafmt/fuzz/corpus/${{ matrix.fuzz_target }}

      - name: Seed fuzzing corpus from test corpus
        run: >
          mkdir -p libtlafmt/fuzz/corpus/$FUZZ_TARGET;
          cp libtlafmt/tests/corpus/* libtlafmt/fuzz/corpus/$FUZZ_TARGET

      - name: Cache fuzz target dir
        id: target-dir
        uses: actions/cache@v4
        with:
          key: ${{ runner.os }}-fuzz-target-dir-${{ matrix.fuzz_target }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-fuzz-target-dir-${{ matrix.fuzz_target }}
          path: libtlafmt/fuzz/target

      - name: Run fuzzer
        id: fuzz
        run: >
          cd libtlafmt &&
          cargo +nightly fuzz run $FUZZ_TARGET --
          -max_total_time=120
          -max_len=1022

      - name: Upload crash
        uses: actions/upload-artifact@v4
        if: failure() && steps.fuzz.outcome == 'failure'
        with:
          name: fuzz-crash
          path: libtlafmt/fuzz/artifacts/${{ matrix.fuzz_target }}