Skip to content

Commit

Permalink
Update CI, fix lint (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 24, 2024
1 parent e4aaa04 commit 1eb0b7e
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 80 deletions.
24 changes: 3 additions & 21 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
version: v1.59.1
2 changes: 1 addition & 1 deletion .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<<EOF" >> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
Expand Down
16 changes: 6 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 23 additions & 20 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"}`))
Expand Down Expand Up @@ -77,34 +80,34 @@ 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)
assert.Equal(t, "abc", val)
}

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)
}))
Expand All @@ -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\\\"}\"")
}

Expand Down Expand Up @@ -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)
}))
Expand Down Expand Up @@ -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\\\"}\"")
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
36 changes: 18 additions & 18 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -244,23 +244,23 @@ 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))
}()
}

wg.Wait()
assert.NoError(t, mock.ExpectationsWereMet())
require.NoError(t, mock.ExpectationsWereMet())
}

func TestServer_ResetExpectations(t *testing.T) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
}()

Expand All @@ -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))
}
}()
Expand All @@ -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())
}

0 comments on commit 1eb0b7e

Please sign in to comment.