name: Rust Tests

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

jobs:
  format:
    runs-on: ubuntu-latest
    steps:
      - name: checkout repo
        uses: actions/checkout@v1

      - name: install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: install rustfmt
        run: rustup component add rustfmt

      - name: rustfmt check
        run: cargo fmt --all -- --check

  test-macos-latest-stable:
    name: macos-latest test suite stable
    needs: [format]
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso

  test-macos-latest-nightly:
    name: macos-latest test suite nightly
    needs: [format]
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso

  test-windows-latest-stable:
    name: windows-latest test suite stable
    needs: [format]
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso

  test-windows-latest-nightly:
    name: windows-latest test suite nightly
    needs: [format]
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso

  test-ubuntu-latest-stable-with-coverage:
    name: ubuntu-latest test suite stable with coverage
    needs: [format]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso
      - name: run cargo-tarpaulin
        uses: actions-rs/tarpaulin@v0.1
        with:
          version: '0.16.0'
          args: '-- --test-threads 1'
      - name: upload to codecov.io
        uses: codecov/codecov-action@v1.0.2
        with:
          token: ${{secrets.CODECOV_TOKEN}}
      - name: archive code coverage results
        uses: actions/upload-artifact@v1
        with:
          name: code-coverage-report
          path: cobertura.xml

  test-ubuntu-latest-nightly:
    name: ubuntu-latest test suite nightly
    needs: [format]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
      - name: cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
      - name: on poem
        run: cargo run --features cli poems/lovely-poem.eso
