Skip to content

Commit

Permalink
ci: add ci with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams authored Jul 5, 2023
1 parent 2f2bf85 commit 80740d0
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 108 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
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

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 ./...
- name: Testing
continue-on-error: ${{ matrix.go-version == 'tip' }}
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
33 changes: 33 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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.
59 changes: 29 additions & 30 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,86 @@ import (
"strings"
)

// TestingT is an interface wrapper around *testing.T
// TestingT is an interface wrapper around *testing.T.
type TestingT interface {
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
}

type ApiMock struct {
type APIMock struct {
testServer *httptest.Server
calls map[HttpCall]http.HandlerFunc
calls map[HTTPCall]http.HandlerFunc
testState TestingT
invocations map[HttpCall][]*Invocation
invocations map[HTTPCall][]*Invocation
}

type HttpCall struct {
type HTTPCall struct {
Method string
Path string
}

func Api(testState TestingT) *ApiMock {
mockedApi := &ApiMock{
calls: map[HttpCall]http.HandlerFunc{},
func API(testState TestingT) *APIMock {
mockedAPI := &APIMock{
calls: map[HTTPCall]http.HandlerFunc{},
testState: testState,
invocations: map[HttpCall][]*Invocation{},
invocations: map[HTTPCall][]*Invocation{},
}

testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, request *http.Request) {

call := HttpCall{
call := HTTPCall{
Method: strings.ToLower(request.Method),
Path: request.RequestURI,
}

invocations := mockedApi.invocations[call]
invocations := mockedAPI.invocations[call]
invocations = append(invocations, newInvocation(request, testState))
mockedApi.invocations[call] = invocations
mockedAPI.invocations[call] = invocations

handler := mockedApi.calls[call]
handler := mockedAPI.calls[call]
if handler != nil {
handler(res, request)
} else {
res.WriteHeader(404)
testState.Fatalf("unmocked invocation %s %s\n", call.Method, call.Path)
}
}))
mockedApi.testServer = testServer
mockedAPI.testServer = testServer

return mockedApi
return mockedAPI
}

func (mockedApi *ApiMock) Close() {
mockedApi.testServer.Close()
func (mockedAPI *APIMock) Close() {
mockedAPI.testServer.Close()
}

func (mockedApi *ApiMock) GetUrl() *url.URL {
testServerUrl, err := url.Parse(mockedApi.testServer.URL)
func (mockedAPI *APIMock) GetURL() *url.URL {
testServerURL, err := url.Parse(mockedAPI.testServer.URL)
if err != nil {
mockedApi.testState.Fatal(err)
mockedAPI.testState.Fatal(err)
}
return testServerUrl
return testServerURL
}

func (mockedApi *ApiMock) GetHost() string {
return mockedApi.GetUrl().Host
func (mockedAPI *APIMock) GetHost() string {
return mockedAPI.GetURL().Host
}

func (mockedApi *ApiMock) Stub(method string, path string) *StubBuilder {
func (mockedAPI *APIMock) Stub(method string, path string) *StubBuilder {
return &StubBuilder{
api: mockedApi,
call: &HttpCall{
api: mockedAPI,
call: &HTTPCall{
Method: strings.ToLower(method),
Path: path,
},
}
}

func (mockedApi *ApiMock) Verify(method string, path string) *CallVerifier {
func (mockedAPI *APIMock) Verify(method string, path string) *CallVerifier {
return &CallVerifier{
api: mockedApi,
call: &HttpCall{
api: mockedAPI,
call: &HTTPCall{
Method: strings.ToLower(method),
Path: path,
},
Expand Down
11 changes: 6 additions & 5 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package mockhttp

import (
assertions "github.com/stretchr/testify/assert"
"testing"

assertions "github.com/stretchr/testify/assert"
)

type MockT struct {
Expand Down Expand Up @@ -62,11 +63,11 @@ func (testState *MockT) assertFailedWithFatal() {

func TestApiUrl(t *testing.T) {
// Arrange
mockedApi := Api(NewTestingMock(t))
defer func() { mockedApi.Close() }()
mockedAPI := API(NewTestingMock(t))
defer func() { mockedAPI.Close() }()

// Assert
assert := assertions.New(t)
assert.Equal(mockedApi.testServer.URL, mockedApi.GetUrl().String())
assert.Equal(mockedApi.GetUrl().Host, mockedApi.GetHost())
assert.Equal(mockedAPI.testServer.URL, mockedAPI.GetURL().String())
assert.Equal(mockedAPI.GetURL().Host, mockedAPI.GetHost())
}
5 changes: 3 additions & 2 deletions invocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package mockhttp
import (
"bytes"
"encoding/json"
assertions "github.com/stretchr/testify/assert"
"io"
"net/http"

assertions "github.com/stretchr/testify/assert"
)

type Invocation struct {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (call *Invocation) WithStringPayload(expected string) *Invocation {
return call
}

func (call *Invocation) ReadJsonPayload(obj any) {
func (call *Invocation) ReadJSONPayload(obj any) {
err := json.Unmarshal(call.GetPayload(), obj)
if err != nil {
call.testState.Fatal(err)
Expand Down
10 changes: 4 additions & 6 deletions invocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package mockhttp

import (
"bytes"
assertions "github.com/stretchr/testify/assert"
"net/http"
"testing"

assertions "github.com/stretchr/testify/assert"
)

func TestInvocation_GetRequest(t *testing.T) {
Expand Down Expand Up @@ -126,7 +127,6 @@ func TestInvocation_WithPayload_Pass(t *testing.T) {
invocation.WithPayload([]byte("foo"))

testState.assertDidNotFailed()

}

func TestInvocation_WithPayload_Fail(t *testing.T) {
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestInvocation_ReadJsonPayload(t *testing.T) {
Foo string `json:"foo"`
}{}

invocation.ReadJsonPayload(&json)
invocation.ReadJSONPayload(&json)

assert := assertions.New(t)
assert.Equal("bar", json.Foo)
Expand All @@ -190,7 +190,7 @@ func TestInvocation_ReadJsonPayload_ErrorHandling(t *testing.T) {
json := struct {
Foo string `json:"foo"`
}{}
invocation.ReadJsonPayload(&json)
invocation.ReadJSONPayload(&json)

testState.assertFailedWithFatal()
}
Expand All @@ -201,7 +201,6 @@ func buildRequest(t *testing.T, method string, url string, data []byte) *http.Re
t.Fatal(err)
}
return request

}

func buildRequestWithBody(t *testing.T, data []byte) *http.Request {
Expand All @@ -210,5 +209,4 @@ func buildRequestWithBody(t *testing.T, data []byte) *http.Request {
t.Fatal(err)
}
return request

}
12 changes: 5 additions & 7 deletions stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import (
)

type StubBuilder struct {
api *ApiMock
call *HttpCall
api *APIMock
call *HTTPCall
}

func (stub *StubBuilder) With(handler http.HandlerFunc) *ApiMock {
func (stub *StubBuilder) With(handler http.HandlerFunc) *APIMock {
stub.api.calls[*stub.call] = handler
return stub.api
}

func (stub *StubBuilder) WithStatusCode(statusCode int) *ApiMock {
func (stub *StubBuilder) WithStatusCode(statusCode int) *APIMock {
return stub.With(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(statusCode)
})
}

func (stub *StubBuilder) WithJson(statusCode int, content interface{}) *ApiMock {
func (stub *StubBuilder) WithJSON(statusCode int, content interface{}) *APIMock {
return stub.With(func(writer http.ResponseWriter, request *http.Request) {

writer.Header().Add("Content-Type", "application/json")
writer.WriteHeader(statusCode)

Expand All @@ -36,6 +35,5 @@ func (stub *StubBuilder) WithJson(statusCode int, content interface{}) *ApiMock
if err != nil {
log.Fatal(err)
}

})
}
Loading

0 comments on commit 80740d0

Please sign in to comment.