name: Program Token Entangler

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

env:
  CARGO_TERM_COLOR: always
  SOLANA_VERSION: 1.9.15
  RUST_TOOLCHAIN: stable

jobs:
  changes:
    runs-on: ubuntu-latest
    # Set job outputs to values from filter step
    outputs:
      core: ${{ steps.filter.outputs.core }}
      package: ${{ steps.filter.outputs.package }}
    steps:
      - uses: actions/checkout@v2
      # For pull requests it's not necessary to checkout the code
      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            core:
              - 'core/**'
            package:
              - 'token-entangler/**'

  build-and-test-token-entangler:
    needs: changes
    if: ${{ needs.changes.outputs.core == 'true' || needs.changes.outputs.package == 'true' }}
    runs-on: ubuntu-latest
    env:
      cache_id: program-token-entangler

    steps:
      # Setup Deps
      - uses: actions/checkout@v2
      - uses: ./.github/actions/install-linux-build-deps
      - uses: ./.github/actions/install-solana
        with:
          solana_version: ${{ env.SOLANA_VERSION }}
      - uses: ./.github/actions/install-rust
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}

      # Restore Cache from previous build/test
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ./rust/target
          key:
            ${{ env.cache_id }}-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{
            env.RUSTC_HASH }}

      # Build deps
      - uses: ./.github/actions/build-metaplex
      - uses: ./.github/actions/build-token-metadata

      # Run test
      - name: test-token-entangler-program
        id: run_test
        working-directory: ./token-entangler/program
        run: |
          cargo +${{ env.RUST_TOOLCHAIN }} test -- --nocapture --test-threads 1
          cargo +${{ env.RUST_TOOLCHAIN }} test-bpf --version
          cargo +${{ env.RUST_TOOLCHAIN }} test-bpf -- --nocapture --test-threads 1
