# https://github.com/peripheryapp/periphery/blob/master/.github/workflows/test.yml

name: Test
on:
  push:
    branches:
      - main
  pull_request: {}
env:
  swift_package_resolve: swift package resolve
  swift_build: swift build --build-tests
  swift_test: swift test
  cache_version: 1
jobs:
  macOS:
    strategy:
      fail-fast: false
      matrix:
        xcode: ["13.4.1", "13.3.1"]
        include:
          - xcode: "13.4.1"
            macos: macOS-12
          - xcode: "13.3.1"
            macos: macOS-12
    runs-on: ${{ matrix.macos }}
    name: macOS
    steps:
    - uses: actions/checkout@master
    - name: Checkout submodules
      uses: textbook/git-checkout-submodule-action@master
    - name: Select Xcode version
      run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app; swift -version
    - name: Get Swift Version
      id: get-swift-version
      run: |
          echo "::set-output name=version::$(swift -version | head -n 1 | sed s/,// )"
      shell: bash
    - name: Cache resolved dependencies
      id: cache-resolved-dependencies
      uses: actions/cache@v2
      with:
        path: |
          .build
          Package.resolved
        key: ${{ runner.os }}-${{ matrix.xcode }}-${{ steps.get-swift-version.outputs.version }}-${{ env.cache_version }}-spm-deps-${{ hashFiles('Package.swift', 'Package.resolved') }}
        restore-keys: |
          ${{ runner.os }}-${{ matrix.xcode }}-${{ steps.get-swift-version.outputs.version }}-${{ env.cache_version }}-spm-deps-
    - name: Resolve dependencies
      if: steps.cache-resolved-dependencies.outputs.cache-hit != 'true'
      run: ${{ env.swift_package_resolve }}
    - name: Build
      run: ${{ env.swift_build }}
    - name: Test
      run: ${{ env.swift_test }}
  linux:
    strategy:
      fail-fast: false
      matrix:
        swift: ["5.6", "5.5"]
        include:
          - swift: "5.6"
            container: "swift:5.6"
            cache-version: 2
          - swift: "5.5"
            container: "swift:5.5"
            cache-version: 2
    runs-on: ubuntu-20.04
    container: ${{ matrix.container }}
    name: Linux
    steps:
    - name: Get Swift Version
      id: get-swift-version
      run: |
        echo "::set-output name=version::$(swift -version | head -n 1 | sed s/,// )"
      shell: bash
    - uses: actions/checkout@master
    - name: Checkout submodules
      uses: textbook/git-checkout-submodule-action@master
    - name: Cache resolved dependencies
      id: cache-resolved-dependencies
      uses: actions/cache@v2
      with:
        path: |
          .build
          Package.resolved
        key: ${{ matrix.cache-version }}-${{ runner.os }}-${{ steps.get-swift-version.outputs.version }}-${{ env.cache_version }}-spm-deps-${{ hashFiles('Package.swift', 'Package.resolved') }}
        restore-keys: |
          ${{ matrix.cache-version }}-${{ runner.os }}-${{ steps.get-swift-version.outputs.version }}-${{ env.cache_version }}-spm-deps-
    - name: Resolve dependencies
      if: steps.cache-resolved-dependencies.outputs.cache-hit != 'true'
      run: ${{ env.swift_package_resolve }}
    - name: Build
      run: ${{ env.swift_build }} --enable-test-discovery
    - name: Test
      run: ${{ env.swift_test }} --enable-test-discovery