name: Build and Test Dafny Runtimes

on:
  workflow_dispatch:
  pull_request:
    branches: [ master, main-* ]
  merge_group:
  
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check-deep-tests:
     uses: ./.github/workflows/check-deep-tests-reusable.yml

  build:
    needs: check-deep-tests
    if: always() && (( github.event_name == 'pull_request' && (needs.check-deep-tests.result == 'success' || contains(github.event.pull_request.labels.*.name, 'run-deep-tests'))) || ( github.event_name == 'push' && ( github.ref_name == 'master' || vars.TEST_ON_FORK == 'true' )))

    runs-on: ubuntu-latest

    steps:
      - name: Checkout Dafny
        uses: actions/checkout@v4
        with:
          submodules: true
      # The Windows image was recently updated with a .NET 9 CLI which made CI fail, 
      # We added a global.json to force it to use any .NET CLI with 8 as a major version
      - name: Setup dotnet
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.0.x
      # If we do not also install .NET 6 as well, then our doc-tests fail for an unknown reason
      # Failing run: https://github.com/dafny-lang/dafny/actions/runs/12276984671/job/34255376460
      - name: Setup dotnet
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 6.0.x
      - name: Set up JDK 18
        uses: actions/setup-java@v4
        with:
          java-version: 18
          distribution: corretto
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.21'
          cache: false
      - name: Set up goimports
        env:
          GO111MODULE: on
        run: go install golang.org/x/tools/cmd/goimports@latest
      - name: Build Dafny
        run: dotnet build Source/Dafny.sln
      - name: Get Z3
        run: make z3-ubuntu
      - name: Test DafnyCore
        run: |
          cd ./Source/DafnyCore
          make test
          make check-format
          make check-extract
      - name: Test DafnyRuntime (C#, Rust)
        run: |
          cd ./Source/DafnyRuntime
          make all
      - name: Test DafnyRuntimeDafny
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeDafny
          make all
      - name: Test DafnyRuntimeGo
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeGo
          make all
      - name: Test DafnyRuntimeGo-gomod
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeGo-gomod
          make all
      - name: Test DafnyRuntimeJava
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeJava
          make all
      - name: Test DafnyRuntimePython
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimePython
          make all
      - name: Test DafnyRuntimeJs
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeJs
          make all
      - name: Test DafnyRuntimeRust
        run: |
          cd ./Source/DafnyRuntime/DafnyRuntimeRust
          make all
