name: build

on:
  push:
    branches: [ master ]
    paths:
      - 'src/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
  pull_request:
    branches: [ master ]
    paths:
      - 'src/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:

  build:
    strategy:
      matrix:
        os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v4
    - uses: actions/cache@v4
      id: cache-deps
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}
    - name: Build
      run: cargo build --verbose
    - name: Run tests
      run: cargo test --verbose
    - name: Build test image and run tests in Docker
      if: matrix.os == 'ubuntu-latest'
      run: |
        cargo build --release
        mkdir docker-test && cp target/release/fselect docker-test/
        cat > docker-test/Dockerfile <<EOF
        FROM jhspetersson/fselect-tests
        COPY fselect /opt/
        ENTRYPOINT ["/opt/run_tests.sh"]
        EOF
        docker build -t fselect-test-img docker-test
        docker run --rm fselect-test-img
      shell: bash
