constriction 0.3.0

Entropy coders for research and production (Rust and Python).
Documentation
name: test

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

env:
  CARGO_TERM_COLOR: always

jobs:
  rust-test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Get rust version
        id: versions
        shell: bash
        run: |
          version=$(rustc --version | awk '{ print $2 }')
          echo "Found version: $version"
          echo "rustc=$version" >> $GITHUB_OUTPUT

      - name: Cache Rust dependencies
        uses: actions/cache@v2
        with:
          path: |
            target
            .cargo_home
            .cargo
          key: ${{ runner.os }}-no_pybindings-rust-${{ steps.versions.outputs.rustc }}-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-no_pybindings-rust-${{ steps.versions.outputs.rustc }}-

      - name: Lint with rustfmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: -- --check

      - name: Lint with clippy
        uses: actions-rs/cargo@v1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: clippy
          args: --all-targets --all-features

      - name: Test in development mode
        uses: actions-rs/cargo@v1.0.1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: test
          toolchain: stable

      - name: Test in release mode
        uses: actions-rs/cargo@v1.0.1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: test
          toolchain: stable
          args: --release

  miri-test:
    name: no_std and miri
    runs-on: ${{ matrix.os }}
    needs: rust-test
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Get latest rust nightly version that has miri
        id: versions
        shell: bash
        run: |
          case ${{ matrix.os }} in
            ubuntu-latest)
              arch="x86_64-unknown-linux-gnu"
              ;;

            macos-latest)
              arch="x86_64-apple-darwin"
              ;;

            windows-latest)
              arch="x86_64-pc-windows-msvc"
              ;;
          esac

          version="nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/$arch/miri)"
          echo "Found version: $version"
          echo "rustc=$version" >> $GITHUB_OUTPUT

      - name: Cache Rust dependencies
        uses: actions/cache@v2
        with:
          path: |
            target
            .cargo_home
            .cargo
          key: ${{ runner.os }}-no_pybindings-miri-${{ steps.versions.outputs.rustc }}-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-no_pybindings-miri-${{ steps.versions.outputs.rustc }}-

      - name: Install rust ${{ steps.versions.outputs.rustc }}
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ steps.versions.outputs.rustc }}
          profile: minimal
          components: miri, rust-src
          override: true

      - name: Test `no_std` compatibility
        shell: bash
        working-directory: ensure_no_std
        run: cargo +${{ steps.versions.outputs.rustc }} build

      - name: Run tests in miri
        env:
          RUSTFLAGS: "-Zrandomize-layout"
          MIRIFLAGS: "-Zmiri-symbolic-alignment-check -Zmiri-disable-isolation"
        run: |
          cargo miri test --no-fail-fast --all-targets

  python-test:
    runs-on: ${{ matrix.os }}
    needs: rust-test
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

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

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Get rust version
        id: versions
        shell: bash
        run: |
          version=$(rustc --version | awk '{ print $2 }')
          echo "Found version: $version"
          echo "rustc=$version" >> $GITHUB_OUTPUT

      - name: Cache Rust dependencies
        uses: actions/cache@v2
        with:
          path: |
            target
            .cargo_home
            .cargo
          key: ${{ runner.os }}-pybindings-rust-${{ steps.versions.outputs.rustc }}-${{ hashFiles('Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-pybindings-rust-${{ steps.versions.outputs.rustc }}-

      - name: Build Python package
        run: poetry run maturin develop --release --features pybindings

      - name: pytest
        shell: bash
        run: |
          poetry run pytest tests/python | tee pytest.out
          exit ${PIPESTATUS[0]}

      - name: Verify that pytest used correct python version (unix)
        if: matrix.os != 'windows-latest'
        run: |
          USED_PYTHON_VERSION=$(grep -A1 -iE '^=+ test session starts =+$' pytest.out | perl -ne 'if ($_ =~ / -- Python (\d+\.\d+)\.\d+/i) { print "$1"; }')
          echo "Expected python version: ${{ matrix.python-version }}"
          echo "Found python version:    $USED_PYTHON_VERSION"
          if [ "x${{ matrix.python-version }}y" == "x${USED_PYTHON_VERSION}y" ]; then
            echo "Versions match."
          else
            echo "ERROR: versions don't match."
            exit 1
          fi

      - name: Verify that pytest used correct python version (windows)
        if: matrix.os == 'windows-latest'
        run: |
          $USED_PYTHON_VERSION = grep -A1 -iE '^=+ test session starts =+$' pytest.out | perl -ne 'if ($_ =~ / -- Python (\d+\.\d+)\.\d+/i) { print "$1"; }'
          echo "Expected python version: ${{ matrix.python-version }}"
          echo "Found python version:    $USED_PYTHON_VERSION"
          if ( "x${{ matrix.python-version }}y" -eq "x${USED_PYTHON_VERSION}y" )
          {
            echo "Versions match."
          }
          else
          {
            throw "ERROR: versions don't match."
          }

  testall:
    runs-on: ubuntu-latest
    name: Meta job for all tests
    needs: [rust-test, miri-test, python-test]
    steps:
      - name: Done
        run: echo "All tests successful."