name: 🔍 Basic Checks

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch:

jobs:
  changes:
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    outputs:
      any_modified: ${{ steps.filter.outputs.any_modified }}
    steps:
      - uses: actions/checkout@v3
      - uses: tj-actions/changed-files@v36
        id: filter
        with:
          files_ignore: |
            docs
            LICENSE
            **.md
      - name: List all changed files
        run: |
          for file in ${{ steps.filter.outputs.all_modified_files }}; do
            echo "$file was changed"
          done

  check:
    needs: [changes]
    if: needs.changes.outputs.any_modified == 'true'
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v3
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt, clippy

      - name: typos-action
        uses: crate-ci/typos@v1.14.12

      - uses: taiki-e/install-action@just

      - name: Install requirements
        run: sudo env PATH=$PATH just ci-prepare

      - name: Check formatting and lints
        run: just lint

  tests:
    needs: [changes]
    if: needs.changes.outputs.any_modified == 'true'
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v3
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1
      - uses: taiki-e/install-action@just
      - name: Install requirements
        run: sudo env PATH=$PATH just ci-prepare
      - name: Run tests
        run: just test-basic
      - name: Run feature tests
        run: just test-features

  # TODO: musl testing is flaky. Turn it back on when we can resolve it.
  # # musl target testing is religated to a separate job because our current build
  # # for musl requires nightly. It is difficult to mix stable and nightly
  # # toolchains in the same job.
  # musl:
  #   needs: [changes]
  #   if: needs.changes.outputs.any_modified == 'true'
  #   runs-on: ubuntu-22.04
  #   timeout-minutes: 20
  #   steps:
  #     - uses: actions/checkout@v3
  #     # We do not use `actions-rust-lang/setup-rust-toolchain` here because we
  #     # want to override the default toolchain file to use nightly toolchains.
  #     # The `setup-rust-toolchain` action will always choose toolchain file with
  #     # no way to override. 
  #     - name: Setup Rust toolchain and cache
  #       uses: dtolnay/rust-toolchain@v1
  #       with:
  #         toolchain: nightly
  #         target: x86_64-unknown-linux-musl
  #         components: rust-src
  #     - uses: taiki-e/install-action@just
  #     - name: Install requirements
  #       run: sudo env PATH=$PATH just ci-musl-prepare
  #     - name: Run test against musl target
  #       run: just test-musl


  coverage:
    needs: [changes]
    if: needs.changes.outputs.any_modified == 'true'
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    name: Run test coverage
    steps:
      - uses: actions/checkout@v3
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1.3.7
      - name: Install llvm-tools-preview
        run: rustup component add llvm-tools-preview
      - name: install cargo-llvm-cov
        uses: taiki-e/install-action@v1
        with:
          tool: cargo-llvm-cov@0.4.0
      - uses: taiki-e/install-action@just
      - name: Install requirements
        run: sudo env PATH=$PATH just ci-prepare
      - name: Run Test Coverage for youki
        run: |
          cargo llvm-cov clean --workspace
          cargo llvm-cov --no-report
          cargo llvm-cov --no-run --lcov --output-path ./coverage.lcov
      - name: Upload Youki Code Coverage Results
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.lcov
