summaryrefslogtreecommitdiffstats
path: root/.github/workflows/ci.yaml
blob: 11027ba85b1482cc044d3c4c3b6918e4c1ca73d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
name: ci
on:
  pull_request:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  get-msrv:
    name: Get declared MSRV from Cargo.toml
    runs-on: ubuntu-latest
    outputs:
      msrv: ${{ steps.get_msrv.outputs.msrv }}
    steps:
      - name: Install ripgrep
        run: sudo apt-get install -y ripgrep

      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Get MSRV
        id: get_msrv
        run: rg '^\s*rust-version\s*=\s*"(\d+(\.\d+){0,2})"' --replace 'msrv=$1' Cargo.toml >> "$GITHUB_OUTPUT"

  check-fmt:
    name: Check code formatting
    runs-on: ubuntu-latest
    needs: get-msrv
    strategy:
      fail-fast: false
      matrix:
        rust:
          - ${{ needs.get-msrv.outputs.msrv }}
          - stable
          - nightly
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
          components: rustfmt
      
      - name: Check formatting
        run: cargo fmt --all -- --check

  test:
    name: Test on each target
    needs: get-msrv
    env:
      # Emit backtraces on panics.
      RUST_BACKTRACE: 1
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        build:
          - android-aarch64
          - linux-aarch64-gnu
          - linux-aarch64-musl
          - linux-armv7-gnueabihf
          - linux-armv7-musleabihf
          - linux-x64-gnu
          - linux-x64-musl
          - macos-aarch64
          - macos-x64
          - windows-x64-msvc
        rust:
          - ${{ needs.get-msrv.outputs.msrv }}
          - stable
          - nightly
        include:
          - os: ubuntu-latest # default
          - cargo: cargo # default; overwrite with `cross` if necessary
          - build: android-aarch64
            target: aarch64-linux-android
            cargo: cross
          - build: linux-aarch64-gnu
            target: aarch64-unknown-linux-gnu
            cargo: cross
          - build: linux-aarch64-musl
            target: aarch64-unknown-linux-musl
            cargo: cross
          - build: linux-armv7-gnueabihf 
            target: armv7-unknown-linux-gnueabihf
            cargo: cross
          - build: linux-armv7-musleabihf
            target: armv7-unknown-linux-musleabihf
            cargo: cross
          - build: linux-x64-gnu
            target: x86_64-unknown-linux-gnu
          - build: linux-x64-musl
            target: x86_64-unknown-linux-musl
          - build: macos-aarch64
            # Go back ot `macos-latest` after migration is complete
            # See https://github.blog/changelog/2024-04-01-macos-14-sonoma-is-generally-available-and-the-latest-macos-runner-image/.
            os: macos-14
            target: aarch64-apple-darwin
          - build: macos-x64
            os: macos-14
            target: x86_64-apple-darwin
          - build: windows-x64-msvc
            os: windows-latest
            target: x86_64-pc-windows-msvc
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
          targets: ${{ matrix.target }}
          components: clippy

      - name: Install cross
        if: matrix.cargo == 'cross'
        # The latest release of `cross` is not able to build/link for `aarch64-linux-android`
        # See: https://github.com/cross-rs/cross/issues/1222
        # This is fixed on `main` but not yet released. To avoid a breakage somewhen in the future
        # pin the cross revision used to the latest HEAD at 04/2024. 
        # Go back to taiki-e/install-action once cross 0.3 is released.
        uses: taiki-e/cache-cargo-install-action@v1
        with:
          tool: cross
          git: https://github.com/cross-rs/cross.git
          rev: 085092c

      - name: Build
        id: build
        run: ${{ matrix.cargo }} build --verbose --target ${{ matrix.target }}

      # This is useful for debugging problems when the expected build artifacts
      # (like shell completions and man pages) aren't generated.
      - name: Show build.rs stderr
        shell: bash
        run: |
          # it's probably okay to assume no spaces?
          STDERR_FILES=$(find "./target/debug" -name stderr | grep bandwhich || true)
          for FILE in $STDERR_FILES; do
            echo "::group::$FILE"
            cat "$FILE"
            echo "::endgroup::"
          done

      - name: Run clippy
        run: ${{ matrix.cargo }} clippy --all-targets --target ${{ matrix.target }} -- -D warnings

      - name: Install npcap on Windows
        # PRs from other repositories cannot be trusted with repository secrets
        if: matrix.os == 'windows-latest' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
        env:
          NPCAP_OEM_URL: ${{ secrets.NPCAP_OEM_URL }}
        run: |
          Invoke-WebRequest -Uri "$env:NPCAP_OEM_URL" -OutFile "$env:TEMP/npcap-oem.exe"
          # for this ridiculous `&` syntax alone, I'd rather use COBOL than Powershell
          # see https://stackoverflow.com/a/1674950/5637701
          & "$env:TEMP/npcap-oem.exe" /S

      - name: Run tests
        id: run_tests
        # npcap is needed to run tests on Windows, so unfortunately we cannot run tests
        # on PRs from other repositories
        if: matrix.os != 'windows-latest' || github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        env:
          # make insta generate new snapshots in CI
          INSTA_UPDATE: new
        run: ${{ matrix.cargo }} test --all-targets --target ${{ matrix.target }}

      - name: Upload snapshots of failed tests
        if: ${{ failure() && steps.run_tests.outcome == 'failure' }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-${{ matrix.rust }}-failed_snapshots
          path: '**/*.snap.new'

      - name: Upload binaries
        if: ${{ success() || steps.build.outcome == 'success' }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}-${{ matrix.rust }}
          path: |
            target/${{ matrix.target }}/debug/bandwhich
            target/${{ matrix.target }}/debug/bandwhich.exe
            target/${{ matrix.target }}/debug/bandwhich.pdb