edgefirst-imu 3.0.0

EdgeFirst IMU Service for BNO08x sensors
# GitHub Actions workflow for testing and code quality
#
# This workflow runs tests, performs static analysis, and collects coverage data
# for the EdgeFirst Fusion project.
#
# Action Versions (verified November 2025):
# - actions/checkout@v4 (latest)
# - dtolnay/rust-toolchain@stable (latest)
# - actions/upload-artifact@v4 (latest)
# - codecov/codecov-action@v3 (latest)

name: Test

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]
  workflow_call:  # Allow other workflows to call this

env:
  CARGO_TERM_COLOR: always

jobs:
  format:
    name: Format Check
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --all --check

  clippy:
    name: Lint with Clippy
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-index-

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-clippy-

      - name: Run Clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

  test:
    name: Test and Coverage (${{ matrix.platform.name }})
    runs-on: ${{ matrix.platform.runner }}
    permissions:
      contents: read
      checks: write
      pull-requests: write
    strategy:
      matrix:
        platform:
          - name: x86_64
            runner: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
          - name: aarch64
            runner: ubuntu-22.04-arm
            target: aarch64-unknown-linux-gnu
      fail-fast: false

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for better analysis

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-index-

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-test-

      - name: Run unit tests
        run: cargo test --lib --workspace

      - name: Run doc tests
        run: cargo test --doc --workspace

      # Note: Integration tests require hardware (DMA buffers, G2D, sensors)
      # and are skipped in CI. Run on target hardware with: cargo test --test '*'

      - name: Generate coverage
        run: |
          # Only generate coverage for unit tests (--lib) and doc tests (--doc)
          # Integration tests require hardware and are excluded
          cargo llvm-cov --lib --workspace --cobertura --output-path coverage.xml
          cargo llvm-cov --doc --workspace --cobertura --output-path coverage-doc.xml || true

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: coverage.xml
          flags: ${{ matrix.platform.name }}
          fail_ci_if_error: false

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results-${{ matrix.platform.name }}
          path: |
            coverage.xml
          retention-days: 30

  sonarcloud:
    name: SonarCloud Analysis
    runs-on: ubuntu-22.04
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Shallow clones should be disabled for better analysis

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Generate coverage for SonarCloud
        run: |
          cargo llvm-cov --lib --workspace --cobertura --output-path target/rust-coverage.xml || true

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}