name: Test

on:
  push:
    branches:
      - main

  pull_request:

jobs:
  test:
    strategy:
      fail-fast: false

      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest

    runs-on: ${{ matrix.os }}

    steps:
      # Without this, tests will fail on Windows because Coqfmt uses LF as a
      # newline while the cloned source code contains CRLF.
      - name: Modify the git setting not to convert LF to CRLF
        run: git config --global core.autocrlf input

      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      # See https://github.com/ocaml-opam/opam-repository-mingw#updates for `opam-repositories`.
      - uses: ocaml/setup-ocaml@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10
        with:
          ocaml-compiler: "4.14.1"
          opam-repositories: |
            opam-repository-mingw: https://github.com/ocaml-opam/opam-repository-mingw.git#sunset
            default: https://github.com/ocaml/opam-repository.git
          dune-cache: true

      - name: Install dependencies
        run: opam install --deps-only --with-test .

      # Without `opam exec -- `, these commands will fail due to "dune not found"
      # error.
      - name: Build
        run: opam exec -- dune build

      - name: Run tests with dune
        run: opam exec -- dune test

      - name: Run `Require` test
        run: ./test.sh
        working-directory: ./test/require-test

  prettier:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Run Prettier
        run: npx --yes prettier --check .

  ocamlformat:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: ocaml/setup-ocaml@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10
        with:
          ocaml-compiler: "4.14.1"
          dune-cache: true

      - uses: ocaml/setup-ocaml/lint-fmt@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10

  lint_docs:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: ocaml/setup-ocaml@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10
        with:
          ocaml-compiler: "4.14.1"
          dune-cache: true

      - uses: ocaml/setup-ocaml/lint-doc@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10

  test_coq_files:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: ocaml/setup-ocaml@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10
        with:
          ocaml-compiler: "4.14.1"
          dune-cache: true

      - name: Install Coq
        run: opam install coq

      - name: List Coq files which did not compile
        run: |
          shopt -s globstar
          find ./test/coq_files/**/*.v -print0|xargs -0 -I{} sh -c 'echo -n "{}: "; opam exec -- coqc -q -vio {} > /dev/null && echo "OK" || exit 255'

  opamlint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: ocaml/setup-ocaml@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10
        with:
          ocaml-compiler: "4.14.1"
          dune-cache: true

      - uses: ocaml/setup-ocaml/lint-opam@8cc6339f55862749298d198ef84788b8d6acdacc # v2.2.10

  docker:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Build Docker image
        run: docker build -t coqfmt .

      - name: Run Docker image
        run: docker run --rm coqfmt --version

  ensure_trailing_newline:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: List all non-empty files tracked by git that do not end with a newline
        run: git ls-files | xargs -I {} sh -c '[ -s "{}" ] && echo "{}"' | xargs -I {} sh -c 'tail -c 1 {} | read -r _ || (echo "{}"; false)'

  hadolint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: hadolint/hadolint-action@54c9adbab1582c2ef04b2016b760714a4bfde3cf # v3.1.0

  markdown-lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - uses: DavidAnson/markdownlint-cli2-action@b4c9feab76d8025d1e83c653fa3990936df0e6c8 # v16.0.0
