diff --git a/.github/actions/go-test-setup/action.yml b/.github/actions/go-test-setup/action.yml new file mode 100644 index 000000000..d61ec8ece --- /dev/null +++ b/.github/actions/go-test-setup/action.yml @@ -0,0 +1,11 @@ +name: "Go Test Setup" +# https://github.com/protocol/.github#setup-actions +description: "Setup the Go test environment." + +runs: + using: "composite" + steps: + - name: Drop conformance tests - keep only unit tests + shell: bash + run: | + rm -rf ./tests diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml new file mode 100644 index 000000000..3833fc229 --- /dev/null +++ b/.github/workflows/automerge.yml @@ -0,0 +1,11 @@ +# File managed by web3-bot. DO NOT EDIT. +# See https://github.com/protocol/.github/ for details. + +name: Automerge +on: [ pull_request ] + +jobs: + automerge: + uses: protocol/.github/.github/workflows/automerge.yml@master + with: + job: 'automerge' diff --git a/.github/workflows/go-check.yml b/.github/workflows/go-check.yml new file mode 100644 index 000000000..cc65ce68a --- /dev/null +++ b/.github/workflows/go-check.yml @@ -0,0 +1,67 @@ +# File managed by web3-bot. DO NOT EDIT. +# See https://github.com/protocol/.github/ for details. + +on: [push, pull_request] +name: Go Checks + +jobs: + unit: + runs-on: ubuntu-latest + name: All + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + - id: config + uses: protocol/.github/.github/actions/read-config@master + - uses: actions/setup-go@v3 + with: + go-version: 1.20.x + - name: Run repo-specific setup + uses: ./.github/actions/go-check-setup + if: hashFiles('./.github/actions/go-check-setup') != '' + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@4970552d932f48b71485287748246cf3237cebdf # 2023.1 (v0.4.0) + - name: Check that go.mod is tidy + uses: protocol/multiple-go-modules@v1.2 + with: + run: | + go mod tidy + if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then + echo "go.sum was added by go mod tidy" + exit 1 + fi + git diff --exit-code -- go.sum go.mod + - name: gofmt + if: success() || failure() # run this step even if the previous one failed + run: | + out=$(gofmt -s -l .) + if [[ -n "$out" ]]; then + echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}' + exit 1 + fi + - name: go vet + if: success() || failure() # run this step even if the previous one failed + uses: protocol/multiple-go-modules@v1.2 + with: + run: go vet ./... + - name: staticcheck + if: success() || failure() # run this step even if the previous one failed + uses: protocol/multiple-go-modules@v1.2 + with: + run: | + set -o pipefail + staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g' + - name: go generate + uses: protocol/multiple-go-modules@v1.2 + if: (success() || failure()) && fromJSON(steps.config.outputs.json).gogenerate == true + with: + run: | + git clean -fd # make sure there aren't untracked files / directories + go generate -x ./... + # check if go generate modified or added any files + if ! $(git add . && git diff-index HEAD --exit-code --quiet); then + echo "go generated caused changes to the repository:" + git status --short + exit 1 + fi diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 000000000..c5cb3efc7 --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,76 @@ +# File managed by web3-bot. DO NOT EDIT. +# See https://github.com/protocol/.github/ for details. + +on: [push, pull_request] +name: Go Test + +jobs: + unit: + strategy: + fail-fast: false + matrix: + os: [ "ubuntu", "windows", "macos" ] + go: ["1.19.x","1.20.x"] + env: + COVERAGES: "" + runs-on: ${{ fromJSON(vars[format('UCI_GO_TEST_RUNNER_{0}', matrix.os)] || format('"{0}-latest"', matrix.os)) }} + name: ${{ matrix.os }} (go ${{ matrix.go }}) + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + - id: config + uses: protocol/.github/.github/actions/read-config@master + - uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go }} + - name: Go information + run: | + go version + go env + - name: Use msys2 on windows + if: matrix.os == 'windows' + shell: bash + # The executable for msys2 is also called bash.cmd + # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#shells + # If we prepend its location to the PATH + # subsequent 'shell: bash' steps will use msys2 instead of gitbash + run: echo "C:/msys64/usr/bin" >> $GITHUB_PATH + - name: Run repo-specific setup + uses: ./.github/actions/go-test-setup + if: hashFiles('./.github/actions/go-test-setup') != '' + - name: Run tests + if: contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false + uses: protocol/multiple-go-modules@v1.2 + with: + # Use -coverpkg=./..., so that we include cross-package coverage. + # If package ./A imports ./B, and ./A's tests also cover ./B, + # this means ./B's coverage will be significantly higher than 0%. + run: go test -v -shuffle=on -coverprofile=module-coverage.txt -coverpkg=./... ./... + - name: Run tests (32 bit) + # can't run 32 bit tests on OSX. + if: matrix.os != 'macos' && + fromJSON(steps.config.outputs.json).skip32bit != true && + contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false + uses: protocol/multiple-go-modules@v1.2 + env: + GOARCH: 386 + with: + run: | + export "PATH=$PATH_386:$PATH" + go test -v -shuffle=on ./... + - name: Run tests with race detector + # speed things up. Windows and OSX VMs are slow + if: matrix.os == 'ubuntu' && + contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false + uses: protocol/multiple-go-modules@v1.2 + with: + run: go test -v -race ./... + - name: Collect coverage files + shell: bash + run: echo "COVERAGES=$(find . -type f -name 'module-coverage.txt' | tr -s '\n' ',' | sed 's/,$//')" >> $GITHUB_ENV + - name: Upload coverage to Codecov + uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + files: '${{ env.COVERAGES }}' + env_vars: OS=${{ matrix.os }}, GO=${{ matrix.go }} diff --git a/.github/workflows/test-tooling.yml b/.github/workflows/test-tooling.yml deleted file mode 100644 index 72565e9bf..000000000 --- a/.github/workflows/test-tooling.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Test Tooling - -on: - workflow_dispatch: - push: - branches: - - main - pull_request: - -jobs: - test: - runs-on: 'ubuntu-latest' - strategy: - fail-fast: false - defaults: - run: - shell: bash - steps: - - name: Setup Go - uses: actions/setup-go@v3 - with: - go-version: 1.20.4 - - uses: actions/checkout@v3 - with: - path: 'gateway-conformance' - - uses: protocol/cache-go-action@v1 - - name: Run the tests - shell: bash - working-directory: ./gateway-conformance - run: | - go test ./tooling/... \ No newline at end of file diff --git a/cmd/gateway-conformance/main.go b/cmd/gateway-conformance/main.go index fbb41e995..b6f0b4f98 100644 --- a/cmd/gateway-conformance/main.go +++ b/cmd/gateway-conformance/main.go @@ -17,11 +17,6 @@ import ( "github.com/urfave/cli/v2" ) -type event struct { - Action string - Test string `json:",omitempty"` -} - type out struct { Writer io.Writer Filter func(s string) bool @@ -118,8 +113,8 @@ func main() { Destination: &specs, }, &cli.BoolFlag{ - Name: "verbose", - Usage: "Prints all the output to the console.", + Name: "verbose", + Usage: "Prints all the output to the console.", Value: false, Destination: &verbose, }, @@ -155,12 +150,15 @@ func main() { } cmd.Stderr = os.Stderr - fmt.Println("Running tests...\n") + fmt.Println("Running tests...") + fmt.Println() testErr := cmd.Run() - fmt.Println("\nDONE!\n") + fmt.Println("\nDONE!") + fmt.Println() if testErr != nil { - fmt.Println("\nLooking for details...\n") + fmt.Println("\nLooking for details...") + fmt.Println() strOutput := output.String() lineDump := []string{} for _, line := range strings.Split(strOutput, "\n") { @@ -176,7 +174,8 @@ func main() { lineDump = append(lineDump, line) } } - fmt.Println("\nDONE!\n") + fmt.Println("\nDONE!") + fmt.Println() } if jsonOutput != "" { @@ -206,7 +205,8 @@ func main() { if err != nil { return err } - fmt.Println("DONE!\n") + fmt.Println("DONE!") + fmt.Println() } return testErr diff --git a/tooling/check/car_test.go b/tooling/check/car_test.go index d4446588c..c38e1e082 100644 --- a/tooling/check/car_test.go +++ b/tooling/check/car_test.go @@ -1,7 +1,7 @@ package check import ( - "io/ioutil" + "io" "os" "testing" @@ -15,7 +15,7 @@ func loadFile(t *testing.T, carFilePath string) []byte { } defer file.Close() - fileBytes, err := ioutil.ReadAll(file) + fileBytes, err := io.ReadAll(file) if err != nil { t.Fatalf("failed to read car file: %v", err) } diff --git a/tooling/test/sugar.go b/tooling/test/sugar.go index e2918b2b0..301391306 100644 --- a/tooling/test/sugar.go +++ b/tooling/test/sugar.go @@ -1,7 +1,6 @@ package test import ( - "bytes" "net/url" "github.com/ipfs/gateway-conformance/tooling/check" @@ -128,7 +127,9 @@ func (r RequestBuilder) Clone() RequestBuilder { Headers_: clonedHeaders, FollowRedirects_: r.FollowRedirects_, Query_: clonedQuery, - Body_: bytes.Clone(r.Body_), + // TODO: replace this call with bytes.Clone when we switch to Go 1.20 + // See https://github.com/golang/go/issues/45038#issuecomment-799795384 + Body_: append([]byte(nil), r.Body_...), } } @@ -172,14 +173,14 @@ func (e ExpectBuilder) Body(body interface{}) ExpectBuilder { e.Body_ = []byte(body) case []byte: e.Body_ = body - case check.Check[string]: - e.Body_ = body case check.CheckWithHint[string]: e.Body_ = body - case check.Check[[]byte]: - e.Body_ = body case check.CheckWithHint[[]byte]: e.Body_ = body + case check.Check[string]: + e.Body_ = body + case check.Check[[]byte]: + e.Body_ = body default: panic("body must be string, []byte, or a regular check") }