name: PR

on: [pull_request]

env:
  # Run all cargo commands with --verbose.
  CARGO_TERM_VERBOSE: true
  RUST_BACKTRACE: 1
  # Deny all compiler warnings.
  RUSTFLAGS: "-D warnings"

jobs:
  clippy:
    name: Rustfmt-Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust and required components
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Check if the code is formatted correctly.
      - name: Check formatting
        run: cargo fmt --all -- --check

      # Run Clippy.
      - shell: bash
        run: cargo clippy --all-targets

  check_pr_size:
      name: Check PR size doesn't break set limit
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v2
      - uses: maidsafe/pr_size_checker@v1.1
        with:
          max_lines_changed: 200
  
  # Run `cargo build` in the root directory to test all build scripts.
  build-script:
    name: Build Scripts
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Test build scripts.
      - name: Build
        run: cargo build

  cargo-udeps:
    name: Unused dependency check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # Install Rust and required components
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true

      # Install and run cargo udeps to find unused cargo dependencies
      - name: cargo-udeps unused dependency check
        run: |
          cargo install cargo-udeps --locked
          cargo +nightly udeps --all-targets

  # Run test suite.
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # TODO: Reenable as we get tests going again
      # Run tests.
      # - shell: bash
      #   run: ./scripts/test-real-network

      - name: Doc Tests
        shell: bash
        run: cargo test --release --doc

  test-immutable-data:
    name: Test Immutable Data Upload
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Test
      - name: Immutable Data Tests
        run: cargo test create_and_retrieve # id tests only

  # Test publish using --dry-run.
  test-publish:
    name: Test Publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Publish Dry Run
        run: cargo publish --dry-run
