name: "generic template cargo tests"

on:
  push:
    branches:
      - main
    paths-ignore:
      - "evm-template/**"
      - "**.md"
      - "**.adoc"
  pull_request:
    paths-ignore:
      - "evm-template/**"
      - "**.md"
      - "**.adoc"

# If new code is pushed to a PR branch, then cancel in progress workflows for
# that PR. Ensures that we don't waste CI time, and returns results quicker.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  # Not needed in CI, should make things a bit faster
  CARGO_INCREMENTAL: 0
  CARGO_TERM_COLOR: always
  # Remove unnecessary WASM build artifacts
  WASM_BUILD_CLEAN_TARGET: 1
  # stripping symbols and optimizing for binary size
  RUSTFLAGS: -C strip=symbols -C opt-level=s
  # Disable sscache until migrate backend from legacy service
  # RUSTC_WRAPPER: "sccache"
  # SCCACHE_CACHE_SIZE: "50GB"
  SCCACHE_GHA_ENABLED: "false"

jobs:
  clippy-fmt-test:
    runs-on: ubuntu-22.04
    defaults:
      run:
        working-directory: ./generic-template

    steps:
      - name: git checkout
        uses: actions/checkout@v4

      - name: Run sccache
        uses: mozilla-actions/sccache-action@v0.0.4

      - name: Install Protoc
        uses: arduino/setup-protoc@9b1ee5b22b0a3f1feb8c2ff99b32c89b3c3191e9 # v2.0.0
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: install rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: clippy, rustfmt

      - name: Install toml-sort
        run: cargo install --git https://github.com/4meta5/toml_sort

      - name: Add file permissions for toml-sort
        run: chmod +x ./scripts/toml-sort.sh

      - name: Add file permissions for check-toml-sorted
        run: chmod +x ./scripts/check-toml-sorted.sh

      - name: Check Cargo.toml files are formatted using toml_sort
        run: ./scripts/check-toml-sorted.sh

      - name: "Install nextest"
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest

      - name: Check format
        run: cargo fmt --all -- --check

      - name: Run tests
        run: cargo nextest run --release

      - name: Check benchmarking compilation
        run: cargo check --release --features runtime-benchmarks

      - name: Run tests with async backing
        run: cargo nextest run --release --features="async-backing"

      - name: Check clippy
        run: cargo clippy --release --locked --all-targets -- -D warnings

      - name: Check Documentation
        run: cargo doc --release --locked --all --no-deps
        env:
          RUSTDOCFLAGS: "-D rustdoc::broken-intra-doc-links -D rustdoc::private_intra_doc_links"
