|
| 1 | +name: go test |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - v* |
| 7 | + branches: |
| 8 | + - master |
| 9 | + |
| 10 | + pull_request: |
| 11 | + |
| 12 | +permissions: |
| 13 | + pull_requests: read |
| 14 | + contents: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + module-matrix: |
| 18 | + name: Go module matrix |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + outputs: |
| 22 | + modules: ${{ steps.modules.outputs.modules }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v5 |
| 26 | + - name: Find go modules |
| 27 | + id: modules |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + # This script finds all go modules declared in this repo and resolves to a relative path to each of those. |
| 31 | + # |
| 32 | + # NOTES: |
| 33 | + # |
| 34 | + # > * git bash on a windows runner should support GNU find. find flags should be supported by find on macos. |
| 35 | + # > * with go.work file enabled, we may now collect all submodules with go list -m |
| 36 | + # > |
| 37 | + # > The outcome is currently only used for linting. Tests may now skip that part. |
| 38 | + set -euxo pipefail |
| 39 | +
|
| 40 | + root="$(git rev-parse --show-toplevel)" |
| 41 | + cd "${root}" |
| 42 | +
|
| 43 | + declare -i index=0 |
| 44 | + declare -a all_mods |
| 45 | + printf "modules=[" >> "$GITHUB_OUTPUT" |
| 46 | + while read module_location ; do |
| 47 | + if [ $index -gt 0 ] ; then |
| 48 | + printf "," >> "$GITHUB_OUTPUT" |
| 49 | + fi |
| 50 | + relative_location=${module_location#"$root"/} |
| 51 | + module_dir=${relative_location%"/go.mod"} |
| 52 | + base_dir="${module_dir#"./"}" |
| 53 | + printf " \"${base_dir}\"" >> "$GITHUB_OUTPUT" |
| 54 | + all_mods+=("${base_dir}") |
| 55 | + ((index++)) || true |
| 56 | + done < <(go list -f '{{.Dir}}' -m) |
| 57 | + printf "]" >> "$GITHUB_OUTPUT" |
| 58 | +
|
| 59 | + echo "::notice title=Modules found::${all_mods[@]}" |
| 60 | +
|
| 61 | + module-lint: |
| 62 | + name: Go module lint |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: [ module-matrix ] |
| 65 | + |
| 66 | + strategy: |
| 67 | + matrix: |
| 68 | + # all sub modules in this repo must be linted separately |
| 69 | + module: ${{ fromJSON(needs.module-matrix.outputs.modules) }} |
| 70 | + |
| 71 | + steps: |
| 72 | + - uses: actions/checkout@v5 |
| 73 | + - uses: actions/setup-go@v6 |
| 74 | + with: |
| 75 | + go-version: stable |
| 76 | + check-latest: true |
| 77 | + cache: true |
| 78 | + cache-dependency-path: '**/go.sum' |
| 79 | + - name: golangci-lint |
| 80 | + uses: golangci/golangci-lint-action@v8 |
| 81 | + with: |
| 82 | + version: latest |
| 83 | + only-new-issues: true |
| 84 | + skip-cache: true |
| 85 | + # golangci-lint doesn't support go.work to lint multiple modules in one single pass |
| 86 | + working-directory: '${{ matrix.module }}' |
| 87 | + |
| 88 | + lint: |
| 89 | + needs: [ module-lint ] |
| 90 | + name: Lint |
| 91 | + runs-on: ubuntu-latest |
| 92 | + steps: |
| 93 | + - name: Linting complete |
| 94 | + run: | |
| 95 | + echo "All modules linted" |
| 96 | +
|
| 97 | + module-test: |
| 98 | + name: Unit tests |
| 99 | + runs-on: ${{ matrix.os }} |
| 100 | + needs: [ module-matrix ] |
| 101 | + |
| 102 | + strategy: |
| 103 | + matrix: |
| 104 | + os: [ ubuntu-latest, macos-latest, windows-latest ] |
| 105 | + go_version: ['oldstable', 'stable' ] |
| 106 | + |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v5 |
| 109 | + - uses: actions/setup-go@v6 |
| 110 | + with: |
| 111 | + go-version: '${{ matrix.go_version }}' |
| 112 | + check-latest: true |
| 113 | + cache: true |
| 114 | + cache-dependency-path: '**/go.sum' |
| 115 | + |
| 116 | + - name: Run unit tests on all modules in this repo |
| 117 | + shell: bash |
| 118 | + env: |
| 119 | + # *.coverage.* pattern is automatically detected by codecov |
| 120 | + COVER_PROFILE: 'all_modules.coverage.${{ matrix.os }}.${{ matrix.go_version }}.out' |
| 121 | + run: | |
| 122 | + # when go1.25 becomes the oldstable, we may replace this bash with "go work test" |
| 123 | + declare -a ALL_MODULES |
| 124 | + BASH_MAJOR=$(echo $BASH_VERSION|cut -d'.' -f1) |
| 125 | + if [[ "${BASH_MAJOR}" -ge 4 ]] ; then |
| 126 | + mapfile ALL_MODULES < <(go list -f '{{.Dir}}/...' -m) |
| 127 | + else |
| 128 | + # for older bash versions, e.g. on macOS runner. This fallback will eventually disappear. |
| 129 | + while read line ; do |
| 130 | + ALL_MODULES+=("${line}") |
| 131 | + done < <(go list -f '{{.Dir}}/...' -m) |
| 132 | + fi |
| 133 | + echo "::notice title=Modules found::${ALL_MODULES[@]}" |
| 134 | +
|
| 135 | + # with go.work file enabled, go test recognizes sub-modules and collects all packages to be covered |
| 136 | + # without specifying -coverpkg. |
| 137 | + go test -v -race -coverprofile="${COVER_PROFILE}" -covermode=atomic ${ALL_MODULES[@]} |
| 138 | +
|
| 139 | + - name: Upload coverage to codecov |
| 140 | + uses: codecov/codecov-action@v5 |
| 141 | + with: |
| 142 | + name: Multi modules aggregated coverage |
| 143 | + flags: '${{ matrix.go_version }}-${{ matrix.os }}' |
| 144 | + fail_ci_if_error: false |
| 145 | + verbose: true |
| 146 | + |
| 147 | + test: |
| 148 | + needs: [ module-test ] |
| 149 | + name: Test |
| 150 | + runs-on: ubuntu-latest |
| 151 | + steps: |
| 152 | + - name: Tests complete |
| 153 | + run: | |
| 154 | + echo "All tests completed" |
0 commit comments