name: Tests
on:
  - push
  - pull_request

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  code-tests:
    name: Code tests
    strategy:
      fail-fast: false
      matrix:
        go:
          - oldstable
          - stable
        os:
          - ubuntu-22.04
          - ubuntu-24.04
    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Dependency Review
        uses: actions/dependency-review-action@v4
        if: github.event_name == 'pull_request'
        with:
          allow-ghsas: GHSA-56mx-8g9f-5crf

      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go }}

      - name: Install dependencies
        run: |
          sudo apt-get -qq update
          sudo apt-get install -y \
            pipx \
            squashfs-tools

          # With pipx >= 1.5.0, we could use pipx --global instead.
          PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin \
            pipx install codespell

      - name: Run static analysis
        run: make static-analysis

      - name: Unit tests (all)
        run: make check

  documentation:
    name: Documentation tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          sudo apt-get install aspell aspell-en
          sudo snap install mdl

      - name: Run markdown linter
        run: |
          make doc-lint

      - name: Run spell checker
        run: |
          make doc-spellcheck

      - name: Run inclusive naming checker
        uses: get-woke/woke-action@v0
        with:
          fail-on-error: true
          woke-args: "*.md **/*.md -c https://github.com/canonical-web-and-design/Inclusive-naming/raw/main/config.yml"

      - name: Run link checker
        # This can fail intermittently due to external resources being unavailable.
        continue-on-error: true
        run: |
          make doc-linkcheck
