name: Create a pipeline and test it
on:
  push:
    branches:
      - dev
      # https://docs.renovatebot.com/key-concepts/automerge/#branch-vs-pr-automerging
      - "renovate/**" # branches Renovate creates
    paths-ignore:
      - "docs/**"
      - "CHANGELOG.md"
  pull_request:
    paths-ignore:
      - "docs/**"
      - "CHANGELOG.md"
  release:
    types: [published]
  workflow_dispatch:

# Cancel if a newer run is started
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  RunTestWorkflow:
    runs-on:
      - runs-on=${{ github.run_id }}-run-test-worfklow
      - runner=4cpu-linux-x64
    env:
      NXF_ANSI_LOG: false

    strategy:
      matrix:
        NXF_VER:
          - "24.04.2"
          - "latest-everything"
    steps:
      - name: go to working directory
        run: |
          mkdir -p create-test-wf
          cd create-test-wf
          export NXF_WORK=$(pwd)

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        name: Check out source-code repository

      - name: Set up Python 3.12
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
        with:
          python-version: "3.13"

      - name: Install python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install .

      - name: Install Nextflow
        uses: nf-core/setup-nextflow@v2
        with:
          version: ${{ matrix.NXF_VER }}

      - name: Install nf-test
        uses: nf-core/setup-nf-test@v1
        with:
          version: "0.9.2"
          install-pdiff: true

      - name: Run nf-core/tools to create pipeline
        run: |
          mkdir create-test-wf && cd create-test-wf
          export NXF_WORK=$(pwd)
          nf-core --log-file log.txt pipelines create -n testpipeline -d "This pipeline is for testing" -a "Testing McTestface"

      - name: copy snapshot file
        run: |
          cp ${{ github.workspace }}/.github/snapshots/default.nf.test.snap create-test-wf/nf-core-testpipeline/tests/default.nf.test.snap

      - name: Run nf-test
        shell: bash
        run: |
          cd create-test-wf/nf-core-testpipeline
          nf-test test \
            --profile=+docker \
            --verbose \
            --tap=test.tap \
            --ci

          # Save the absolute path of the test.tap file to the output
          echo "tap_file_path=$(realpath test.tap)" >> $GITHUB_OUTPUT

      - name: Upload log file artifact
        if: ${{ always() }}
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: nf-core-log-file-${{ matrix.NXF_VER }}
          path: create-test-wf/log.txt

      - name: Cleanup work directory
        # cleanup work directory
        run: |
          sudo rm -rf create-test-wf
          sudo rm -rf /home/ubuntu/tests/
        if: always()
        shell: bash
