name: Test
on:
  push:
    branches:
    - master
  pull_request:
jobs:
  check:
    name: "Check"
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1.0.0
    - name: "Check that author is present in the NOTICE file"
      if: github.event_name == 'pull_request'
      run: |
        AUTHOR=$(git log -1 --format="%aE")
        if [ -z "$AUTHOR" ]; then
          printf "\Cannot perform NOTICE check: Commit does not include an email address.\n" &&
          exit 1;
        elif ! grep -q "$AUTHOR" NOTICE || false; then
          printf "\nAuthor '$AUTHOR' does not appear to be listed in the NOTICE file, yet.\n" &&
          printf "Please see https://github.com/AssemblyScript/assemblyscript/blob/master/CONTRIBUTING.md\n" &&
          exit 1;
        else
          printf "\nOK: Author is present in the NOTICE file.\n";
        fi
    - 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/master/CONTRIBUTING.md\n" &&
          exit 1;
        else
          printf "\nOK: Distributions files have not been modified.\n";
        fi
  test:
    name: "Compiler (Linux, node ${{ matrix.node_version }})"
    runs-on: ubuntu-latest
    needs: check
    strategy:
      matrix:
        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: Clean distribution files
      run: npm run clean
    - name: Check sources
      run: npm run check
    - name: Test sources
      run: |
        if [[ `node bin/asc --version` != *"-dev" ]]; then
          echo "ERROR: Not testing sources"
          exit 1
        fi
        npm test
    - name: Build distribution files
      run: npm run build
    - name: Test distribution
      run: |
        if [[ `node bin/asc --version` == *"-dev" ]]; then
          echo "ERROR: Not testing distribution"
          exit 1
        fi
        npm test
    - name: Test browser build
      run: node tests/browser-asc
  test-windows:
    name: "Compiler (Windows, node current)"
    runs-on: windows-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: Clean distribution files
      run: npm run clean
    - name: Test sources
      run: npm test
    - name: Build distribution files
      run: npm run build
    - name: Test distribution
      run: npm test
    - name: Test browser build
      run: node tests/browser-asc
  test-macos:
    name: "Compiler (MacOS, node current)"
    runs-on: macos-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: Clean distribution files
      run: npm run clean
    - name: Test sources
      run: npm test
    - name: Build distribution files
      run: npm run build
    - name: Test distribution
      run: npm test
    - name: Test browser build
      run: node tests/browser-asc
  test-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: "15.0.0-v8-canary202007077c53168ead"
    - name: Install dependencies
      run: npm ci --no-audit
    - name: Clean distribution files
      run: npm run clean
    - name: Test experimental features
      env:
        ASC_FEATURES: mutable-globals,threads,reference-types,bigint-integration
      run: |
        npm run test:compiler rt/flags features/js-bigint-integration features/reference-types features/threads
  test-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: Clean distribution files
      run: npm run clean
    - name: Test full runtime
      run: |
        cd tests/allocators/rt-full
        npm run build
        cd ..
        npm test rt-full
    - name: Test stub runtime
      run: |
        cd tests/allocators/rt-stub
        npm run build
        cd ..
        npm test rt-stub
  test-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: Clean distribution files
      run: npm run clean
    - name: Test the loader
      run: |
        cd lib/loader
        npm run asbuild
        npm run test
  test-bootstrap:
    name: "Bootstrap"
    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: Clean distribution files
      run: npm run clean
    - name: Test self-compilation
      run: |
        npm run asbuild
        npm run astest
