diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de02ae4..50b226e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,47 +8,49 @@ on: workflow_dispatch: jobs: - test: - strategy: - matrix: - go-version: [1.18.x, 1.19.x, tip] - lint-and-coverage: [false] - include: - - go-version: 1.20.x - lint-and-coverage: true + build: runs-on: ubuntu-latest - steps: - - name: Setup go - run: | - curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.4/install-go.pl | - perl - ${{ matrix.go-version }} $HOME/go - - name: Checkout code - uses: actions/checkout@v2 - - - name: Linting - if: matrix.lint-and-coverage - run: | - curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | - sh -s -- -b $HOME/go/bin v1.52.2 - $HOME/go/bin/golangci-lint run ./... + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: './go.mod' + - name: Build + run: go build -v ./... + + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: './go.mod' + - name: Lint + uses: golangci/golangci-lint-action@v6 + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: './go.mod' + - name: Lint + uses: golangci/golangci-lint-action@v6 - name: Testing - continue-on-error: ${{ matrix.go-version == 'tip' }} + continue-on-error: true run: | - go version - if [ ${{ matrix.lint-and-coverage }} = true ]; then - GO_TEST_OPTS="-covermode=atomic -coverprofile=coverage.out" - fi export GORACE="halt_on_error=1" - go test -race $GO_TEST_OPTS ./... - - - name: Reporting coverage - if: matrix.lint-and-coverage - env: - COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - go install github.com/mattn/goveralls@v0.0.11 - goveralls -coverprofile=coverage.out -service=github + go test -race -covermode=atomic -coverprofile=coverage.out ./... + - name: Reporting coverage to Coveralls + uses: coverallsapp/github-action@v2 + with: + file: ./coverage.out + format: golang diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..a6de59c --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,45 @@ + +linters: + enable: + - asasalint + - asciicheck + - bidichk + - bodyclose + - contextcheck + - errcheck + - errorlint + - gocheckcompilerdirectives + - gochecknoinits + - gochecksumtype + - gosec + - gosimple + - govet + - ineffassign + - loggercheck + - makezero + - musttag + - noctx + - paralleltest + - perfsprint + - prealloc + - predeclared + - reassign + - staticcheck + - tenv + - tparallel + - unconvert + - unparam + - unused + - usestdlibvars + - wastedassign + - whitespace + + +issues: + exclude-rules: + - path: (.+)_test.go + linters: + - bodyclose + - gosec + - noctx + - wrapcheck \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index 2ddd0ab..0000000 --- a/.golangci.yml +++ /dev/null @@ -1,33 +0,0 @@ -run: - # See the dedicated "run" documentation section. - -output: - format: colored-line-number - - -linters-settings: - # See the dedicated "linters-settings" documentation section. - -linters: - enable: - - asasalint - - asciicheck - - bidichk - - durationcheck - - exportloopref - - gocritic - - godot - - goimports - - govet - - misspell - - prealloc - - revive - - unconvert - - whitespace - -issues: - max-issues-per-linter: 0 - max-same-issues: 0 - -severity: - # See the dedicated "severity" documentation section. diff --git a/api.go b/api.go index c1ce364..31f6d20 100644 --- a/api.go +++ b/api.go @@ -52,7 +52,7 @@ func API(testState TestingT) *APIMock { if handler != nil { handler(res, request) } else { - res.WriteHeader(404) + res.WriteHeader(http.StatusNotFound) testState.Fatalf("unmocked invocation %s %s\n", call.Method, call.Path) } })) diff --git a/api_test.go b/api_test.go index 1e9b739..09516c3 100644 --- a/api_test.go +++ b/api_test.go @@ -62,6 +62,7 @@ func (testState *MockT) assertFailedWithFatal() { } func TestApiUrl(t *testing.T) { + t.Parallel() // Arrange mockedAPI := API(NewTestingMock(t)) defer func() { mockedAPI.Close() }() diff --git a/invocations_test.go b/invocations_test.go index b7864f0..455c5e8 100644 --- a/invocations_test.go +++ b/invocations_test.go @@ -9,7 +9,8 @@ import ( ) func TestInvocation_GetRequest(t *testing.T) { - expectedRequest := buildRequest(t, http.MethodGet, "/endpoint", nil) + t.Parallel() + expectedRequest := buildRequest(t, http.MethodGet, "/foo", nil) invocation := newInvocation(expectedRequest, t) @@ -18,6 +19,7 @@ func TestInvocation_GetRequest(t *testing.T) { } func TestInvocation_Chaining_Verifications(t *testing.T) { + t.Parallel() payload := []byte{42} request := buildRequest(t, http.MethodPost, "/endpoint", payload) request.Header.Add("foo", "bar") @@ -35,6 +37,7 @@ func TestInvocation_Chaining_Verifications(t *testing.T) { } func TestInvocation_WithHeader_Pass(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) request.Header.Add("foo", "bar") @@ -47,6 +50,7 @@ func TestInvocation_WithHeader_Pass(t *testing.T) { } func TestInvocation_WithHeader_MultipleValues_Pass(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) request.Header.Add("foo", "bar1") request.Header.Add("foo", "bar2") @@ -60,6 +64,7 @@ func TestInvocation_WithHeader_MultipleValues_Pass(t *testing.T) { } func TestInvocation_WithHeader_Fail_WhenMissingHeader(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) testState := NewTestingMock(t) @@ -71,6 +76,7 @@ func TestInvocation_WithHeader_Fail_WhenMissingHeader(t *testing.T) { } func TestInvocation_WithHeader_Fail_WhenHeaderWrongValue(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) request.Header.Add("foo", "bar") @@ -83,6 +89,7 @@ func TestInvocation_WithHeader_Fail_WhenHeaderWrongValue(t *testing.T) { } func TestInvocation_WithHeader_Fail_WhenHeaderWrongValues(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) request.Header.Add("foo", "bar1") request.Header.Add("foo", "bar2") @@ -96,6 +103,7 @@ func TestInvocation_WithHeader_Fail_WhenHeaderWrongValues(t *testing.T) { } func TestInvocation_WithoutHeader_Pass(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) testState := NewTestingMock(t) @@ -107,6 +115,7 @@ func TestInvocation_WithoutHeader_Pass(t *testing.T) { } func TestInvocation_WithoutHeader_Fail(t *testing.T) { + t.Parallel() request := buildRequest(t, http.MethodGet, "/endpoint", nil) request.Header.Add("foo", "bar") @@ -119,6 +128,7 @@ func TestInvocation_WithoutHeader_Fail(t *testing.T) { } func TestInvocation_WithPayload_Pass(t *testing.T) { + t.Parallel() testState := NewTestingMock(t) request := buildRequestWithBody(t, []byte("foo")) @@ -130,6 +140,7 @@ func TestInvocation_WithPayload_Pass(t *testing.T) { } func TestInvocation_WithPayload_Fail(t *testing.T) { + t.Parallel() testState := NewTestingMock(t) request := buildRequestWithBody(t, []byte{42}) invocation := newInvocation(request, testState) @@ -140,6 +151,7 @@ func TestInvocation_WithPayload_Fail(t *testing.T) { } func TestInvocation_GetRequestContent(t *testing.T) { + t.Parallel() expectedRequestContent := []byte{42} request := buildRequestWithBody(t, expectedRequestContent) invocation := newInvocation(request, t) @@ -149,6 +161,7 @@ func TestInvocation_GetRequestContent(t *testing.T) { } func TestInvocation_WithStringPayload_Pass(t *testing.T) { + t.Parallel() request := buildRequestWithBody(t, []byte("foo")) testState := NewTestingMock(t) invocation := newInvocation(request, testState) @@ -159,6 +172,7 @@ func TestInvocation_WithStringPayload_Pass(t *testing.T) { } func TestInvocation_WithStringPayload_Fail(t *testing.T) { + t.Parallel() request := buildRequestWithBody(t, []byte("foo")) testState := NewTestingMock(t) invocation := newInvocation(request, testState) @@ -169,6 +183,7 @@ func TestInvocation_WithStringPayload_Fail(t *testing.T) { } func TestInvocation_ReadJsonPayload(t *testing.T) { + t.Parallel() request := buildRequestWithBody(t, []byte(`{"foo":"bar"}`)) invocation := newInvocation(request, t) @@ -183,6 +198,7 @@ func TestInvocation_ReadJsonPayload(t *testing.T) { } func TestInvocation_ReadJsonPayload_ErrorHandling(t *testing.T) { + t.Parallel() request := buildRequestWithBody(t, []byte(`{"invalid json"}`)) testState := NewTestingMock(t) invocation := newInvocation(request, testState) diff --git a/stubs_test.go b/stubs_test.go index a3f3df6..08da08e 100644 --- a/stubs_test.go +++ b/stubs_test.go @@ -9,6 +9,7 @@ import ( ) func TestApiNotStubbedEndpoint(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -26,6 +27,7 @@ func TestApiNotStubbedEndpoint(t *testing.T) { } func TestApiStubbedEndpoint(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -35,7 +37,7 @@ func TestApiStubbedEndpoint(t *testing.T) { Stub(http.MethodGet, "/endpoint"). With(func(writer http.ResponseWriter, request *http.Request) { writer.Header().Add("Content-Type", "text/plain") - writer.WriteHeader(201) + writer.WriteHeader(http.StatusCreated) _, err := writer.Write([]byte("Hello")) if err != nil { t.Fatal(err) @@ -55,6 +57,7 @@ func TestApiStubbedEndpoint(t *testing.T) { } func TestApiStubbedEndpointWithJson(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -79,7 +82,3 @@ func TestApiStubbedEndpointWithJson(t *testing.T) { responseObject.Value("value").IsEqual("Hello") } - -type TestDto struct { - Value string `json:"value"` -} diff --git a/verify_test.go b/verify_test.go index f08ca49..2cd166e 100644 --- a/verify_test.go +++ b/verify_test.go @@ -9,6 +9,7 @@ import ( ) func TestVerifyingInvocationsCountPasses(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -31,6 +32,7 @@ func TestVerifyingInvocationsCountPasses(t *testing.T) { } func TestVerifyingInvocationsCountFails(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -53,6 +55,7 @@ func TestVerifyingInvocationsCountFails(t *testing.T) { } func TestVerifyingInvocationsCountReturnsThePerformedCalls(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -90,6 +93,7 @@ func TestVerifyingInvocationsCountReturnsThePerformedCalls(t *testing.T) { } func TestVerifyingSingleInvocationPasses(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -109,6 +113,7 @@ func TestVerifyingSingleInvocationPasses(t *testing.T) { } func TestVerifyingSingleInvocationFails(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState) @@ -129,6 +134,7 @@ func TestVerifyingSingleInvocationFails(t *testing.T) { } func TestVerifyingSingleInvocationReturnsThePerformedCall(t *testing.T) { + t.Parallel() // Arrange testState := NewTestingMock(t) mockedAPI := API(testState)