name: Test
on:
  push:
    branches:
    - main
  pull_request:
jobs:
  check:
    name: "Check"
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1.0.0
    - name: "Check that distribution files are unmodified"
      if: github.event_name == 'pull_request'
      run: |
        if git --no-pager diff --name-only $(git rev-parse origin/${{ github.base_ref }})...${{ github.sha }} | grep -q "^dist/"; then
          printf "\nThe pull request modifies distribution files, but it shouldn't.\n" &&
          printf "Please see https://github.com/AssemblyScript/assemblyscript/blob/main/CONTRIBUTING.md\n" &&
          exit 1;
        else
          printf "\nOK: Distributions files have not been modified.\n";
        fi
  compiler:
    name: "Compiler (${{ matrix.os }}, node ${{ matrix.node_version }})"
    runs-on: ${{ matrix.os }}-latest
    needs: check
    strategy:
      matrix:
        os: ["ubuntu", "macos"]
        # TODO: re-enable "windows", see https://github.com/npm/cli/issues/4234
        node_version: ["current", "lts_latest"]
    steps:
    - uses: actions/checkout@v1.0.0
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-version: ${{ matrix.node_version }}
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Build
      run: npm run build
    - name: Check
      run: npm run check
    - name: Test
      run: npm test
  bootstrap:
    name: "Bootstrap (${{ matrix.target }})"
    runs-on: ubuntu-latest
    needs: check
    strategy:
      matrix:
        target: ["debug", "release"]
    steps:
    - uses: actions/checkout@v1.0.0
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-version: current
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Build
      run: npm run build
    - name: Bootstrap
      run: npm run bootstrap:${{ matrix.target }}
    - name: Test
      run: npm run test:compiler -- --wasm build/assemblyscript.${{ matrix.target }}-bootstrap.js
  features:
    name: "Features"
    runs-on: ubuntu-latest
    needs: check
    steps:
    - uses: actions/checkout@v1.0.0
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-mirror: https://nodejs.org/download/v8-canary/
        node-version: 18.0.0-v8-canary20211115037fd7ae8d
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Build
      run: npm run build
    - name: Test experimental features
      env:
        ASC_FEATURES: threads,reference-types,gc,exception-handling
      run: |
        npm run test:compiler features/threads features/reference-types features/gc features/exception-handling
  runtimes:
    name: "Runtimes"
    runs-on: ubuntu-latest
    needs: check
    steps:
    - uses: actions/checkout@v1.0.0
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-version: current
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Build
      run: npm run build
    - name: Test default allocator
      run: |
        cd tests/allocators/default
        npm run build
        cd ..
        npm test default
    - name: Test stub allocator
      run: |
        cd tests/allocators/stub
        npm run build
        cd ..
        npm test stub
  loader:
    name: "Loader"
    runs-on: ubuntu-latest
    needs: check
    steps:
    - uses: actions/checkout@v1.0.0
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-version: current
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Build
      run: npm run build
    - name: Test the loader
      run: |
        cd lib/loader
        npm run asbuild
        npm run test
