name: Program Metaplex

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

env:
  CARGO_TERM_COLOR: always
  SOLANA_VERSION: 1.9.14
  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
    - 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:
            - 'metaplex/**'

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

    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 }}

      # Run test
      - name: test-metaplex-program
        id: run_test
        working-directory: ./metaplex/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
