From 1eb0b7e135c3ca92f045c5e86c141a9b3a752255 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Mon, 24 Jun 2024 17:36:23 +0200 Subject: [PATCH] Update CI, fix lint (#16) --- .github/workflows/golangci-lint.yml | 24 ++-------------- .github/workflows/gorelease.yml | 2 +- .github/workflows/test-unit.yml | 12 ++++---- .golangci.yml | 16 ++++------- Makefile | 2 +- client_test.go | 43 +++++++++++++++-------------- go.mod | 2 +- go.sum | 4 +-- server_test.go | 36 ++++++++++++------------ 9 files changed, 61 insertions(+), 80 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index e651212..06b1f14 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -21,28 +21,10 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.20.x + go-version: 1.22.x - uses: actions/checkout@v2 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.4.0 + uses: golangci/golangci-lint-action@v6.0.1 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.51.1 - - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 - - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true - - # Optional: if set to true then the action will use pre-installed Go. - # skip-go-installation: true - - # Optional: if set to true then the action don't cache or restore ~/go/pkg. - # skip-pkg-cache: true - - # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. - # skip-build-cache: true \ No newline at end of file + version: v1.59.1 diff --git a/.github/workflows/gorelease.yml b/.github/workflows/gorelease.yml index 41767ba..6356a9d 100644 --- a/.github/workflows/gorelease.yml +++ b/.github/workflows/gorelease.yml @@ -9,7 +9,7 @@ concurrency: cancel-in-progress: true env: - GO_VERSION: 1.20.x + GO_VERSION: 1.22.x jobs: gorelease: runs-on: ubuntu-latest diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 2817dcc..7eb27c9 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -15,13 +15,13 @@ concurrency: env: GO111MODULE: "on" RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing. - COV_GO_VERSION: 1.20.x # Version of Go to collect coverage + COV_GO_VERSION: 1.22.x # Version of Go to collect coverage TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents jobs: test: strategy: matrix: - go-version: [ 1.13.x, 1.19.x, 1.20.x ] + go-version: [ 1.13.x, 1.21.x, 1.22.x ] runs-on: ubuntu-latest steps: - name: Install Go stable @@ -88,14 +88,14 @@ jobs: id: annotate if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != '' run: | - curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.6/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz + curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.4.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz gocovdiff_hash=$(git hash-object ./gocovdiff) - [ "$gocovdiff_hash" == "8e507e0d671d4d6dfb3612309b72b163492f28eb" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1) + [ "$gocovdiff_hash" == "c37862c73a677e5a9c069470287823ab5bbf0244" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1) git fetch origin master ${{ github.event.pull_request.base.sha }} - REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV}) + REP=$(./gocovdiff -mod github.com/$GITHUB_REPOSITORY -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV}) echo "${REP}" cat gha-unit.txt - DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file") + DIFF=$(test -e unit-base.txt && ./gocovdiff -mod github.com/$GITHUB_REPOSITORY -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file") TOTAL=$(cat delta-cov-unit.txt) echo "rep<> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT echo "diff<> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT diff --git a/.golangci.yml b/.golangci.yml index 3b7a72e..d86a0ba 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,32 +22,28 @@ linters-settings: linters: enable-all: true disable: - - goerr113 # TODO check issues + - err113 - lll - - maligned - gochecknoglobals - gomnd - wrapcheck - paralleltest - forbidigo - - exhaustivestruct - - interfacer # deprecated - forcetypeassert - - scopelint # deprecated - - ifshort # too many false positives - - golint # deprecated - varnamelen - tagliatelle - errname - ireturn - exhaustruct - nonamedreturns - - nosnakecase - structcheck - - varcheck - - deadcode - testableexamples - dupword + - depguard + - tagalign + - execinquery + - mnd + - testifylint issues: exclude-use-default: false diff --git a/Makefile b/Makefile index 949171f..80527a2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -#GOLANGCI_LINT_VERSION := "v1.51.1" # Optional configuration to pinpoint golangci-lint version. +#GOLANGCI_LINT_VERSION := "v1.59.1" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go diff --git a/client_test.go b/client_test.go index 730f8c6..15d612d 100644 --- a/client_test.go +++ b/client_test.go @@ -14,6 +14,7 @@ import ( "github.com/bool64/httpmock" "github.com/bool64/shared" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewClient(t *testing.T) { @@ -40,7 +41,9 @@ func TestNewClient(t *testing.T) { assert.Equal(t, "bar", c.Value) ncnt := atomic.AddInt64(&cnt, 1) + rw.Header().Set("Content-Type", "application/json") + if ncnt > 1 { rw.WriteHeader(http.StatusConflict) _, err := rw.Write([]byte(`{"error":"conflict"}`)) @@ -77,26 +80,26 @@ func TestNewClient(t *testing.T) { WithURI("/foo?q=1"). Concurrently() - assert.NoError(t, c.ExpectResponseStatus(http.StatusAccepted)) - assert.NoError(t, c.ExpectResponseBody([]byte(`{"bar":"foo","dyn":"$var1"}`))) - assert.NoError(t, c.ExpectResponseBodyCallback(func(received []byte) error { + require.NoError(t, c.ExpectResponseStatus(http.StatusAccepted)) + require.NoError(t, c.ExpectResponseBody([]byte(`{"bar":"foo","dyn":"$var1"}`))) + require.NoError(t, c.ExpectResponseBodyCallback(func(received []byte) error { return c.JSONComparer.FailMismatch([]byte(`{"bar":"foo"}`), received) })) - assert.Error(t, c.ExpectResponseBodyCallback(func(received []byte) error { + require.Error(t, c.ExpectResponseBodyCallback(func(received []byte) error { return c.JSONComparer.FailMismatch([]byte(`{"bar":"foo2"}`), received) })) - assert.NoError(t, c.ExpectResponseHeader("Content-Type", "application/json")) - assert.NoError(t, c.ExpectOtherResponsesStatus(http.StatusConflict)) - assert.NoError(t, c.ExpectOtherResponsesBody([]byte(`{"error":"conflict"}`))) - assert.NoError(t, c.ExpectOtherResponsesBodyCallback(func(received []byte) error { + require.NoError(t, c.ExpectResponseHeader("Content-Type", "application/json")) + require.NoError(t, c.ExpectOtherResponsesStatus(http.StatusConflict)) + require.NoError(t, c.ExpectOtherResponsesBody([]byte(`{"error":"conflict"}`))) + require.NoError(t, c.ExpectOtherResponsesBodyCallback(func(received []byte) error { return c.JSONComparer.FailMismatch([]byte(`{"error":"conflict"}`), received) })) - assert.Error(t, c.ExpectOtherResponsesBodyCallback(func(received []byte) error { + require.Error(t, c.ExpectOtherResponsesBodyCallback(func(received []byte) error { return c.JSONComparer.FailMismatch([]byte(`{"error":"conflict2"}`), received) })) - assert.NoError(t, c.ExpectOtherResponsesHeader("Content-Type", "application/json")) - assert.NoError(t, c.CheckUnexpectedOtherResponses()) - assert.EqualError(t, c.ExpectNoOtherResponses(), "unexpected response status, expected: 202 (Accepted), received: 409 (Conflict)") + require.NoError(t, c.ExpectOtherResponsesHeader("Content-Type", "application/json")) + require.NoError(t, c.CheckUnexpectedOtherResponses()) + require.EqualError(t, c.ExpectNoOtherResponses(), "unexpected response status, expected: 202 (Accepted), received: 409 (Conflict)") val, found := vars.Get("$var1") assert.True(t, found) @@ -104,7 +107,7 @@ func TestNewClient(t *testing.T) { } func TestNewClient_failedExpectation(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) { _, err := writer.Write([]byte(`{"bar":"foo"}`)) assert.NoError(t, err) })) @@ -117,7 +120,7 @@ func TestNewClient_failedExpectation(t *testing.T) { } c.WithURI("/") - assert.EqualError(t, c.ExpectResponseBody([]byte(`{"foo":"bar}"`)), + require.EqualError(t, c.ExpectResponseBody([]byte(`{"foo":"bar}"`)), "unexpected body, expected: \"{\\\"foo\\\":\\\"bar}\\\"\", received: \"{\\\"bar\\\":\\\"foo\\\"}\"") } @@ -147,12 +150,12 @@ func TestNewClient_followRedirects(t *testing.T) { c.WithURI("/one") - assert.NoError(t, c.ExpectResponseStatus(http.StatusOK)) - assert.NoError(t, c.ExpectResponseBody([]byte(`{"bar":"foo"}`))) + require.NoError(t, c.ExpectResponseStatus(http.StatusOK)) + require.NoError(t, c.ExpectResponseBody([]byte(`{"bar":"foo"}`))) } func TestNewClient_context(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) { _, err := writer.Write([]byte(`{"bar":"foo"}`)) assert.NoError(t, err) })) @@ -193,7 +196,7 @@ func TestNewClient_formData(t *testing.T) { c.WithURLEncodedFormDataParam("foo", "baz") c.WithURLEncodedFormDataParam("qux", "quux") - assert.EqualError(t, c.ExpectResponseBody([]byte(`{"foo":"bar}"`)), + require.EqualError(t, c.ExpectResponseBody([]byte(`{"foo":"bar}"`)), "unexpected body, expected: \"{\\\"foo\\\":\\\"bar}\\\"\", received: \"{\\\"bar\\\":\\\"foo\\\"}\"") } @@ -255,7 +258,7 @@ func TestClient_Fork(t *testing.T) { func TestClient_AllowRetries(t *testing.T) { tries := 0 - srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) { tries++ if tries == 5 { @@ -285,6 +288,6 @@ func TestClient_AllowRetries(t *testing.T) { return time.Millisecond })) - assert.NoError(t, c.ExpectResponseStatus(http.StatusOK)) + require.NoError(t, c.ExpectResponseStatus(http.StatusOK)) assert.Equal(t, 5, tries) } diff --git a/go.mod b/go.mod index 7a16a60..ecb5fc6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bool64/httpmock go 1.18 require ( - github.com/bool64/dev v0.2.27 + github.com/bool64/dev v0.2.35 github.com/bool64/shared v0.1.5 github.com/stretchr/testify v1.8.2 github.com/swaggest/assertjson v1.8.1 diff --git a/go.sum b/go.sum index 67d9f31..f69362d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc= -github.com/bool64/dev v0.2.27/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/bool64/dev v0.2.35 h1:M17TLsO/pV2J7PYI/gpe3Ua26ETkzZGb+dC06eoMqlk= +github.com/bool64/dev v0.2.35/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E= github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/server_test.go b/server_test.go index fbf38cb..69df550 100644 --- a/server_test.go +++ b/server_test.go @@ -119,14 +119,14 @@ func TestServer_ServeHTTP(t *testing.T) { assertRoundTrip(t, baseURL, exp1) // Expectations were not met yet. - assert.EqualError(t, mock.ExpectationsWereMet(), + require.EqualError(t, mock.ExpectationsWereMet(), "there are remaining expectations that were not met: POST /test?test=test") // Sending second request. assertRoundTrip(t, baseURL, exp2) // Expectations were met. - assert.NoError(t, mock.ExpectationsWereMet()) + require.NoError(t, mock.ExpectationsWereMet()) // Requesting mock without expectations fails. assertRoundTrip(t, baseURL, httpmock.Expectation{ @@ -244,15 +244,15 @@ func TestServer_ServeHTTP_concurrency(t *testing.T) { // Sending request with wrong header. req, err := http.NewRequest(http.MethodGet, url+"/test?test=test", nil) - require.NoError(t, err) + assert.NoError(t, err) req.Header.Set("X-Foo", "space") resp, err := http.DefaultTransport.RoundTrip(req) - require.NoError(t, err) + assert.NoError(t, err) respBody, err := ioutil.ReadAll(resp.Body) - require.NoError(t, resp.Body.Close()) - require.NoError(t, err) + assert.NoError(t, resp.Body.Close()) + assert.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, `body`, string(respBody)) @@ -260,7 +260,7 @@ func TestServer_ServeHTTP_concurrency(t *testing.T) { } wg.Wait() - assert.NoError(t, mock.ExpectationsWereMet()) + require.NoError(t, mock.ExpectationsWereMet()) } func TestServer_ResetExpectations(t *testing.T) { @@ -280,9 +280,9 @@ func TestServer_ResetExpectations(t *testing.T) { ResponseBody: []byte("body"), }) - assert.Error(t, mock.ExpectationsWereMet()) + require.Error(t, mock.ExpectationsWereMet()) mock.ResetExpectations() - assert.NoError(t, mock.ExpectationsWereMet()) + require.NoError(t, mock.ExpectationsWereMet()) } func TestServer_vars(t *testing.T) { @@ -335,15 +335,15 @@ func TestServer_ExpectAsync(t *testing.T) { defer wg.Done() req, err := http.NewRequest(http.MethodGet, url+"/async1", nil) - require.NoError(t, err) + assert.NoError(t, err) resp, err := http.DefaultTransport.RoundTrip(req) - require.NoError(t, err) + assert.NoError(t, err) body, err := ioutil.ReadAll(resp.Body) - require.NoError(t, err) + assert.NoError(t, err) - require.NoError(t, resp.Body.Close()) + assert.NoError(t, resp.Body.Close()) assert.Equal(t, `{"bar":"async1"}`, string(body)) }() @@ -352,15 +352,15 @@ func TestServer_ExpectAsync(t *testing.T) { for i := 0; i < 50; i++ { req, err := http.NewRequest(http.MethodGet, url+"/async2", nil) - require.NoError(t, err) + assert.NoError(t, err) resp, err := http.DefaultTransport.RoundTrip(req) - require.NoError(t, err) + assert.NoError(t, err) body, err := ioutil.ReadAll(resp.Body) - require.NoError(t, err) + assert.NoError(t, err) - require.NoError(t, resp.Body.Close()) + assert.NoError(t, resp.Body.Close()) assert.Equal(t, `{"bar":"async2"}`, string(body)) } }() @@ -378,5 +378,5 @@ func TestServer_ExpectAsync(t *testing.T) { assert.Equal(t, `{"bar":"foo"}`, string(body)) wg.Wait() - assert.NoError(t, sm.ExpectationsWereMet()) + require.NoError(t, sm.ExpectationsWereMet()) }