name: Main workflow

on:
  pull_request:
    paths-ignore:
      - '**.md'
  push:
    branches:
      - main
      - releases/*
    paths-ignore:
      - '**.md'

jobs:
  build:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macos-13]
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Set Node.js 12
      uses: actions/setup-node@v1
      with:
        node-version: 12.x
    - run: npm ci
    - run: npm run build
    - run: npm run format-check
    - run: npm test
    - name: Verify no unstaged changes
      if: runner.os != 'windows'
      run: __tests__/verify-no-unstaged-changes.sh
  
  test-setup-multiple-versions:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macOS-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear toolcache
        shell: pwsh
        run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
      - name: Setup dotnet 2.2.402 and 3.1.404
        uses: ./
        with:
          dotnet-version: |
             2.2.402
             3.1.404
             3.0.x
      - name: Verify dotnet
        shell: pwsh
        run: __tests__/verify-dotnet.ps1 2.2.402 3.1.404 '3.0'

  test-setup-full-version:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macos-13]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear toolcache
        shell: pwsh
        run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
      # Side-by-side install of 2.2 and 3.1 used for the test project
      - name: Setup dotnet 2.2.402
        uses: ./
        with:
          dotnet-version: 2.2.402
      - name: Setup dotnet 3.1.201
        uses: ./
        with:
          dotnet-version: 3.1.201
          # We are including this veriable to force the generation of the nuget config file to verify that it is created in the correct place
          source-url: https://api.nuget.org/v3/index.json
        env:
          NUGET_AUTH_TOKEN: NOTATOKEN
      - name: Verify nuget config file
        shell: pwsh
        run: |
          if (-Not (Test-Path "../nuget.config")) { throw "nuget file not generated correctly" }
      - name: Verify dotnet
        shell: pwsh
        run: __tests__/verify-dotnet.ps1 3.1.201 2.2.402

  test-setup-without-patch-version:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macOS-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear toolcache
        shell: pwsh
        run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
      # 2.0, 3.0, 5.0 needs to be in single quotes to interpret as a string instead of as an integer
      - name: Setup dotnet '3.1'
        uses: ./
        with:
          dotnet-version: '3.1'
      - name: Setup dotnet '2.2'
        uses: ./
        with:
          dotnet-version: '2.2'
      - name: Verify dotnet
        shell: pwsh
        run: __tests__/verify-dotnet.ps1 3.1 2.2
  
  test-setup-latest-patch-version:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macOS-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear toolcache
        shell: pwsh
        run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
      - name: Setup dotnet 3.1.x
        uses: ./
        with:
          dotnet-version: 3.1.x
      - name: Setup dotnet 2.2.x
        uses: ./
        with:
          dotnet-version: 2.2.x
      - name: Verify dotnet
        shell: pwsh
        run: __tests__/verify-dotnet.ps1 3.1 2.2

  test-setup-with-wildcard:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-22.04, windows-latest, macOS-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear toolcache
        shell: pwsh
        run: __tests__/clear-toolcache.ps1 ${{ runner.os }}
      - name: Setup dotnet 3.1.*
        uses: ./
        with:
          dotnet-version: 3.1.*
      - name: Setup dotnet 2.2.*
        uses: ./
        with:
          dotnet-version: 2.2.*
      - name: Verify dotnet
        shell: pwsh
        run: __tests__/verify-dotnet.ps1 3.1 2.2

  test-proxy:
    runs-on: ubuntu-20.04
    container:
      image: ubuntu:20.04
      options: --dns 127.0.0.1
    services:
      squid-proxy:
        image: ubuntu/squid:latest
        ports:
          - 3128:3128
    env:
      https_proxy: http://squid-proxy:3128
      http_proxy: http://squid-proxy:3128
      DOTNET_SYSTEM_GLOBALIZATION_INVARIANT: true

    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear tool cache
        run: rm -rf "/usr/share/dotnet"
      - name: Install curl
        run: |
          apt update
          apt -y install curl libssl-dev
      - name: Setup dotnet 3.1.201
        uses: ./
        with:
          dotnet-version: 3.1.201
          source-url: https://api.nuget.org/v3/index.json
        env:
          NUGET_AUTH_TOKEN: NOTATOKEN
      - name: Verify dotnet
        run: __tests__/verify-dotnet.sh 3.1.201

  test-bypass-proxy:
    runs-on: ubuntu-22.04
    env:
      https_proxy: http://no-such-proxy:3128
      no_proxy: github.com,dotnetcli.blob.core.windows.net,download.visualstudio.microsoft.com,api.nuget.org,dotnetcli.azureedge.net
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Clear tool cache
        run: rm -rf "/usr/share/dotnet"
      - name: Setup dotnet 3.1.201
        uses: ./
        with:
          dotnet-version: 3.1.201
          source-url: https://api.nuget.org/v3/index.json
        env:
          NUGET_AUTH_TOKEN: NOTATOKEN
      - name: Verify dotnet
        run: __tests__/verify-dotnet.sh 3.1.201