name: ci

on:
  push:
    branches:
      - dev
    tags:
      - "[v]?[0-9]+.[0-9]+.[0-9]+*"
  pull_request:
    branches:
      - dev

env:
  CARGO_TERM_COLOR: always
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse

jobs:
  build_and_test:
    name: Build and test
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package:
          - components/cipher
          - components/universal-hash
          - components/aead
          - components/key-exchange
          - components/prf
          - components/tls
          - components/tls/tls-mpc
          - tlsn
          - notary
          - tlsn/tests-integration
        include:
          - package: components/tls/tls-mpc
            ignored: true
            release: true
          - package: notary
            release: true
          - package: tlsn
            all-features: true
          - package: tlsn/tests-integration
            release: true
            ignored: true
    defaults:
      run:
        working-directory: ${{ matrix.package }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: "Check formatting"
        run: cargo +nightly fmt --check --all

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

      - name: "Clippy"
        run: cargo clippy --all-features --examples -- -D warnings

      - name: Use caching
        uses: Swatinem/rust-cache@v2.7.3
        with:
          workspaces: ${{ matrix.package }}

      - name: "Build"
        run: cargo build ${{ matrix.release && '--release' }}

      - name: Add custom DNS entry to /etc/hosts for notary TLS test
        if: matrix.package == 'notary'
        run: echo "127.0.0.1 tlsnotaryserver.io" | sudo tee -a /etc/hosts

      - name: "Test ${{ matrix.release && '--release' || '' }} ${{ matrix.all-features && '--all-features' || '' }}"
        env:
          RELEASE_OPTION: ${{ matrix.release && '--release' || '' }}
          # Includes tests marked as `ignore`.
          IGNORED_OPTION: ${{ matrix.ignored && '--include-ignored' || '' }}
          ALL_FEATURES_OPTION: ${{ matrix.all-features && '--all-features' || '' }}
        # Run all tests (bins, examples, lib, integration and docs)
        # https://doc.rust-lang.org/cargo/commands/cargo-test.html#target-selection
        run: |
          echo "Running command: cargo test $RELEASE_OPTION $ALL_FEATURES_OPTION -- $IGNORED_OPTION"
          cargo test $RELEASE_OPTION $ALL_FEATURES_OPTION -- $IGNORED_OPTION

      - name: "Check that benches compile"
        run: cargo bench --no-run
