name: Memory Leak Detect

env:
  DEBUG: 'napi:*'

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build_and_test:
    name: Memory leak detect job
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14
          check-latest: true

      - name: Install stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable-x86_64-unknown-linux-gnu
          profile: minimal
          override: true

      - name: Generate Cargo.lock
        uses: actions-rs/cargo@v1
        with:
          command: generate-lockfile

      - name: Cache cargo registry
        uses: actions/cache@v1
        with:
          path: ~/.cargo/registry
          key: stable-memory-leak-detect-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v1
        with:
          path: ~/.cargo/git
          key: stable-memory-leak-detect-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo build
        uses: actions/cache@v1
        with:
          path: target
          key: stable-memory-leak-detect-cargo-build-trimmed-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache NPM dependencies
        uses: actions/cache@v1
        with:
          path: node_modules
          key: memory-leak-detect-${{ hashFiles('yarn.lock') }}

      - name: 'Install dependencies'
        run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000

      - name: 'Build TypeScript'
        run: yarn build

      - name: 'Pull docker image'
        run: docker pull node:lts-slim

      - name: 'Build memory test specs'
        run: yarn build:memory

      - name: Memory leak tests
        run: yarn test:memory
        env:
          RUST_BACKTRACE: 1

      - name: Clear the cargo caches
        run: |
          cargo install cargo-cache --no-default-features --features ci-autoclean
          cargo-cache