# Build and test script to run on PR and main build
name: Build and Test

permissions:
  deployments: write

on:
  pull_request:
    branches:
      - main
  check_run:
    types: [rerequested, requested_action]

env:
  NODE_VERSION: 16.14.2
  NPM_VERSION: 8.15.1
  IS_CI: 1
  SCREENSHOT_PATH: 'src/test/*-screenshot.png'
  PYLANCE_LOG_PATH: 'src/test/pylance-log.txt'

jobs:
  filecheck:
    name: Check for package.json edit
    runs-on: ubuntu-latest
    steps:
      - name: 'Check for package.json edit'
        uses: brettcannon/check-for-changed-files@v1.1.0
        with:
          file-pattern: 'package.json'
          skip-label: 'skip package.json'
          failure-message: 'Package.json must be changed on every submission in order for publish to work'
  build:
    name: Build Distribution
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Use Node ${{env.NODE_VERSION}}
        uses: actions/setup-node@v2.1.5
        with:
          node-version: ${{env.NODE_VERSION}}

      - run: npm i -g npm@${{env.NPM_VERSION}}
        name: npm install npm@8.15.1

      - run: npm install
        name: npm install

      - run: npm run webpack
        name: webpack for production
  test:
    timeout-minutes: 30
    name: Unit and Integration Tess
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        pythonVersion: [3.8]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Use Node ${{env.NODE_VERSION}}
        uses: actions/setup-node@v2.1.5
        with:
          node-version: ${{env.NODE_VERSION}}

      - run: npm i -g npm@${{env.NPM_VERSION}}
        name: npm install npm@8.15.1

      - run: npm install
        name: npm install

      - name: Use Python ${{matrix.pythonVersion}}
        uses: actions/setup-python@v2
        with:
          python-version: ${{matrix.pythonVersion}}

      - name: Set CI Path
        uses: ./.github/actions/set-python
        id: set-python
        if: matrix.python != 'conda' && matrix.python != 'noPython'
        with:
          PYTHON_VERSION: ${{matrix.pythonVersion}}

      - name: Download VS code API (needed for build to work)
        run: yes | npm run download-api

      - name: Run tests using xvfb
        env:
          DISPLAY: 10
        uses: GabrielBB/xvfb-action@v1.4
        id: test_id
        with:
          run: npm run test

      - name: Upload screenshots
        uses: actions/upload-artifact@v2
        if: steps.test_id.outcome == 'failure' && failure()
        with:
          path: './${{env.SCREENSHOT_PATH}}'

      - name: Upload pylance log
        uses: actions/upload-artifact@v2
        if: steps.test_id.outcome == 'failure' && failure()
        with:
          path: './${{env.PYLANCE_LOG_PATH}}'
