Skip to content

Commit

Permalink
ci: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams committed Oct 15, 2024
1 parent ec902ed commit cb04c5c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 76 deletions.
74 changes: 38 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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
45 changes: 45 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 0 additions & 33 deletions .golangci.yml

This file was deleted.

2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}))
Expand Down
1 change: 1 addition & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (testState *MockT) assertFailedWithFatal() {
}

func TestApiUrl(t *testing.T) {
t.Parallel()
// Arrange
mockedAPI := API(NewTestingMock(t))
defer func() { mockedAPI.Close() }()
Expand Down
18 changes: 17 additions & 1 deletion invocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

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

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

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

Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions stubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestApiNotStubbedEndpoint(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
Expand All @@ -26,6 +27,7 @@ func TestApiNotStubbedEndpoint(t *testing.T) {
}

func TestApiStubbedEndpoint(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
Expand All @@ -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)
Expand All @@ -55,6 +57,7 @@ func TestApiStubbedEndpoint(t *testing.T) {
}

func TestApiStubbedEndpointWithJson(t *testing.T) {
t.Parallel()
// Arrange
testState := NewTestingMock(t)
mockedAPI := API(testState)
Expand All @@ -79,7 +82,3 @@ func TestApiStubbedEndpointWithJson(t *testing.T) {

responseObject.Value("value").IsEqual("Hello")
}

type TestDto struct {
Value string `json:"value"`
}
Loading

0 comments on commit cb04c5c

Please sign in to comment.