name: Unit Test Coverage
on:
  push:
    paths-ignore:
      - "**.md"
      - "channel.yaml"
      - "install.sh"
      - "tests/snapshotter/**"
      - "tests/install/**"
      - "tests/cgroup/**"
      - ".github/**"
      - "!.github/workflows/unitcoverage.yaml"
  pull_request:
    paths-ignore:
      - "**.md"
      - "channel.yaml"
      - "install.sh"
      - "tests/snapshotter/**"
      - "tests/install/**"
      - "tests/cgroup/**"
      - ".github/**"
      - "!.github/workflows/unitcoverage.yaml"
  workflow_dispatch: {}

permissions:
  contents: read

jobs:
  test-unit-linux:
    name: Unit Tests (linux)
    runs-on: ubuntu-24.04
    timeout-minutes: 20
    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        fetch-depth: 1
    - name: Install Go
      uses: ./.github/actions/setup-go
    - name: Run Unit Tests
      run: |
        go test -coverpkg ./pkg/... -coverprofile coverage.out ./pkg/... -run Unit
        go tool cover -func coverage.out
    - name: On Failure, Launch Debug Session
      if: ${{ failure() }}
      uses: lhotari/action-upterm@v1
      with:
        wait-timeout-minutes: 5
    - name: Upload Results To Codecov
      uses: codecov/codecov-action@v5
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: ./coverage.out
        flags: unittests # optional
        verbose: true # optional (default = false)
  test-unit-windows:
    name: Unit Tests (windows)
    runs-on: windows-2022
    timeout-minutes: 20
    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        fetch-depth: 1
    - name: Install Go
      uses: ./.github/actions/setup-go
    - name: Run Unit Tests
      run: |
        go test -coverpkg ./pkg/... -coverprofile coverage.out ./pkg/... -run Unit
        go tool cover -func coverage.out
    - name: Upload Results To Codecov
      uses: codecov/codecov-action@v5
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: ./coverage.out
        flags: unittests # optional
        verbose: true # optional (default = false)
  test-mods:
    name: Test K8s Modules
    runs-on: ubuntu-24.04
    steps:
    - name: Checkout
      uses: actions/checkout@v6
    - name: Build test-mods
      run: docker build --target test-mods -t k3s:mod -f Dockerfile.test .
    - name: Run test-mods
      run: docker run -i k3s:mod