|
| 1 | +name: Go Test |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + go-versions: |
| 6 | + required: false |
| 7 | + type: string |
| 8 | + default: '["this", "next"]' |
| 9 | + secrets: |
| 10 | + CODECOV_TOKEN: |
| 11 | + required: false |
| 12 | + |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | + |
| 17 | +jobs: |
| 18 | + unit: |
| 19 | + strategy: |
| 20 | + fail-fast: false |
| 21 | + matrix: |
| 22 | + os: ["ubuntu", "windows", "macos"] |
| 23 | + go: ${{ fromJSON(inputs.go-versions) }} |
| 24 | + env: |
| 25 | + GOTESTFLAGS: -cover -coverprofile=module-coverage.txt -coverpkg=./... |
| 26 | + GO386FLAGS: "" |
| 27 | + GORACEFLAGS: "" |
| 28 | + runs-on: ${{ fromJSON(vars[format('UCI_GO_TEST_RUNNER_{0}', matrix.os)] || format('"{0}-latest"', matrix.os)) }} |
| 29 | + name: ${{ matrix.os }} (go ${{ matrix.go }}) |
| 30 | + steps: |
| 31 | + - name: Use msys2 on windows |
| 32 | + if: matrix.os == 'windows' |
| 33 | + # The executable for msys2 is also called bash.cmd |
| 34 | + # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#shells |
| 35 | + # If we prepend its location to the PATH |
| 36 | + # subsequent 'shell: bash' steps will use msys2 instead of gitbash |
| 37 | + run: echo "C:/msys64/usr/bin" >> $GITHUB_PATH |
| 38 | + - name: Check out the repository |
| 39 | + uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + submodules: recursive |
| 42 | + - name: Check out the latest stable version of Go |
| 43 | + uses: actions/setup-go@v5 |
| 44 | + with: |
| 45 | + go-version: stable |
| 46 | + # cache: false |
| 47 | + - name: Read the Unified GitHub Workflows configuration |
| 48 | + id: config |
| 49 | + uses: ipdxco/unified-github-workflows/.github/actions/read-config@main |
| 50 | + - name: Read the go.mod file |
| 51 | + id: go-mod |
| 52 | + uses: ipdxco/unified-github-workflows/.github/actions/read-go-mod@main |
| 53 | + - name: Determine the Go version to use based on the go.mod file |
| 54 | + id: go |
| 55 | + env: |
| 56 | + MATRIX_GO: ${{ matrix.go }} |
| 57 | + GO_MOD_VERSION: ${{ fromJSON(steps.go-mod.outputs.json).Go }} |
| 58 | + run: | |
| 59 | + if [[ "$MATRIX_GO" == "this" ]]; then |
| 60 | + echo "version=$GO_MOD_VERSION.x" >> $GITHUB_OUTPUT |
| 61 | + elif [[ "$MATRIX_GO" == "next" ]]; then |
| 62 | + MAJOR="${GO_MOD_VERSION%.[0-9]*}" |
| 63 | + MINOR="${GO_MOD_VERSION#[0-9]*.}" |
| 64 | + echo "version=$MAJOR.$(($MINOR+1)).x" >> $GITHUB_OUTPUT |
| 65 | + elif [[ "$MATRIX_GO" == "prev" ]]; then |
| 66 | + MAJOR="${GO_MOD_VERSION%.[0-9]*}" |
| 67 | + MINOR="${GO_MOD_VERSION#[0-9]*.}" |
| 68 | + echo "version=$MAJOR.$(($MINOR-1)).x" >> $GITHUB_OUTPUT |
| 69 | + else |
| 70 | + echo "version=$MATRIX_GO" >> $GITHUB_OUTPUT |
| 71 | + fi |
| 72 | + - name: Enable shuffle flag for go test command |
| 73 | + if: toJSON(fromJSON(steps.config.outputs.json).shuffle) != 'false' |
| 74 | + run: | |
| 75 | + echo "GOTESTFLAGS=-shuffle=on $GOTESTFLAGS" >> $GITHUB_ENV |
| 76 | + echo "GO386FLAGS=-shuffle=on $GO386FLAGS" >> $GITHUB_ENV |
| 77 | + echo "GORACEFLAGS=-shuffle=on $GORACEFLAGS" >> $GITHUB_ENV |
| 78 | + - name: Enable verbose flag for go test command |
| 79 | + if: toJSON(fromJSON(steps.config.outputs.json).verbose) != 'false' |
| 80 | + run: | |
| 81 | + echo "GOTESTFLAGS=-v $GOTESTFLAGS" >> $GITHUB_ENV |
| 82 | + echo "GO386FLAGS=-v $GO386FLAGS" >> $GITHUB_ENV |
| 83 | + echo "GORACEFLAGS=-v $GORACEFLAGS" >> $GITHUB_ENV |
| 84 | + - name: Set extra flags for go test command |
| 85 | + if: fromJSON(steps.config.outputs.json).gotestflags) != '' |
| 86 | + run: | |
| 87 | + echo "GOTESTFLAGS=${{ fromJSON(steps.config.outputs.json).gotestflags}} $GOTESTFLAGS" >> $GITHUB_ENV |
| 88 | + - name: Set up the Go version read from the go.mod file |
| 89 | + uses: actions/setup-go@v5 |
| 90 | + with: |
| 91 | + go-version: ${{ steps.go.outputs.version }} |
| 92 | + - name: Display the Go version and environment |
| 93 | + run: | |
| 94 | + go version |
| 95 | + go env |
| 96 | + - name: Run repo-specific setup |
| 97 | + uses: libp2p/uci/.github/actions/go-test-setup |
| 98 | + if: hashFiles('./.github/actions/go-test-setup') != '' |
| 99 | + - name: Run tests |
| 100 | + if: contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false |
| 101 | + |
| 102 | + env: |
| 103 | + GOFLAGS: ${{ format('{0} {1}', env.GOTESTFLAGS, env.GOFLAGS) }} |
| 104 | + with: |
| 105 | + run: go test ./... |
| 106 | + - name: Run tests (32 bit) |
| 107 | + # can't run 32 bit tests on OSX. |
| 108 | + if: matrix.os != 'macos' && |
| 109 | + fromJSON(steps.config.outputs.json).skip32bit != true && |
| 110 | + contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false |
| 111 | + |
| 112 | + env: |
| 113 | + GOARCH: 386 |
| 114 | + GOFLAGS: ${{ format('{0} {1}', env.GO386FLAGS, env.GOFLAGS) }} |
| 115 | + with: |
| 116 | + run: | |
| 117 | + export "PATH=$PATH_386:$PATH" |
| 118 | + go test ./... |
| 119 | + - name: Run tests with race detector |
| 120 | + # speed things up. Windows and OSX VMs are slow |
| 121 | + if: matrix.os == 'ubuntu' && |
| 122 | + fromJSON(steps.config.outputs.json).skipRace != true && |
| 123 | + contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false |
| 124 | + |
| 125 | + env: |
| 126 | + GOFLAGS: ${{ format('{0} {1}', env.GORACEFLAGS, env.GOFLAGS) }} |
| 127 | + with: |
| 128 | + run: go test -race ./... |
| 129 | + - name: Collect coverage files |
| 130 | + id: coverages |
| 131 | + run: echo "files=$(find . -type f -name 'module-coverage.txt' | tr -s '\n' ',' | sed 's/,$//')" >> $GITHUB_OUTPUT |
| 132 | + - name: Upload coverage to Codecov |
| 133 | + uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0 |
| 134 | + with: |
| 135 | + files: ${{ steps.coverages.outputs.files }} |
| 136 | + env_vars: OS=${{ matrix.os }}, GO=${{ steps.go.outputs.version }} |
| 137 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 138 | + fail_ci_if_error: false |
0 commit comments