Skip to content

Commit

Permalink
Add golangci-lint github action on PR (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchurm authored Mar 7, 2024
1 parent f714305 commit 6148f71
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: golangci-lint
on:
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.56

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --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 all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ coverage:
update_deps:
go get -t -u ./...

lint:
golangci-lint run

SMOCKER_VERSION=0.18.5

start-smocker:
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func main() {

## Functions

* `ResetAllSessionsAndMocks` - Clears the Smocker server of all sessions and mocks. Leaving it in a clean state.
* `StartSession` - Starts a new session on the Smocker server with the given name. New mocks will be added to the latest
session started.
* `AddMock` - Adds a new mock to the current session on the Smocker server. Mocks can be made using the provided
builders
or raw json option detailed below.
* `VerifyMocksInCurrentSession` - Checks all the mocks in the session have been called and that no other calls have been
made
- `ResetAllSessionsAndMocks` - Clears the Smocker server of all sessions and mocks. Leaving it in a clean state.
- `StartSession` - Starts a new session on the Smocker server with the given name. New mocks will be added to the latest
session started.
- `AddMock` - Adds a new mock to the current session on the Smocker server. Mocks can be made using the provided
builders
or raw json option detailed below.
- `VerifyMocksInCurrentSession` - Checks all the mocks in the session have been called and that no other calls have been
made

## Mock Definitions

Expand Down Expand Up @@ -115,4 +115,9 @@ mockJson := `
}
}`
mockDefinition := mock.NewRawJsonDefinition(mockJson)
```
```

## Development Tools

- Docker
- [golangci-lint](https://golangci-lint.run/)
41 changes: 25 additions & 16 deletions smockerclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package smockerclient_test
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -33,7 +33,8 @@ func TestStartSession(t *testing.T) {
"id": "1d6d264b-4d13-4e0b-a51e-e44fc80eca9f",
"name": "` + sessionName + `"
}`
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand Down Expand Up @@ -64,7 +65,8 @@ func TestStartSession_WithNameThatNeedsUrlEscaping(t *testing.T) {
"id": "1d6d264b-4d13-4e0b-a51e-e44fc80eca9f",
"name": "` + sessionName + `"
}`
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -80,7 +82,7 @@ func TestStartSession_WithNameThatNeedsUrlEscaping(t *testing.T) {
func TestStartSession_WhenServerDoesNotReturn200_ReturnsError(t *testing.T) {
sessionName := "my-new-session"

server, serverCallCount := newBadResponseServer()
server, serverCallCount := newBadResponseServer(t)
defer server.Close()

smockerInstance := smockerclient.Instance{Url: server.URL}
Expand All @@ -107,14 +109,15 @@ func TestAddMock(t *testing.T) {
contentType := r.Header.Get("Content-Type")
assert.Equal(t, jsonContentType, contentType)

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.JSONEq(t, expectedJson, string(body))

resp := `{
"message": "Mocks registered successfully"
}`
w.Write([]byte(resp))
_, err = w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand Down Expand Up @@ -153,7 +156,7 @@ func TestAddMock_WhenServerDoesNotReturn200_ReturnsError(t *testing.T) {
expectedJson := `{"example": 1234}`
fakeMock := FakeMock{Json: expectedJson}

server, serverCallCount := newBadResponseServer()
server, serverCallCount := newBadResponseServer(t)
defer server.Close()

smockerInstance := smockerclient.Instance{Url: server.URL}
Expand All @@ -177,7 +180,8 @@ func TestResetAllSessionsAndMocks(t *testing.T) {
resp := `{
"message": "Reset successful"
}`
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -191,7 +195,7 @@ func TestResetAllSessionsAndMocks(t *testing.T) {
}

func TestResetAllSessionsAndMocks_WhenServerDoesNotReturn200_ReturnsError(t *testing.T) {
server, serverCallCount := newBadResponseServer()
server, serverCallCount := newBadResponseServer(t)
defer server.Close()

smockerInstance := smockerclient.Instance{Url: server.URL}
Expand Down Expand Up @@ -223,7 +227,8 @@ func TestVerifyMocksInLatestSession_WhenAllMocksHaveBeenCalled_ReturnsNoError(t
"message": "History is clean"
}
}`
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -248,7 +253,8 @@ func TestVerifyMocksInLatestSession_WhenSomeMocksHaveNotBeenCalled_ReturnsError(
assert.Equal(t, "/sessions/verify", r.URL.Path)

resp := getVerifyBodyWhenSomeMocksAreNotCalled()
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -274,7 +280,8 @@ func TestVerifyMocksInLatestSession_WhenSomeMocksHaveNotBeenCalled_AndExtraCalls
assert.Equal(t, "/sessions/verify", r.URL.Path)

resp := getVerifyBodyWhenExtraCallsHaveBeenMade()
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -300,7 +307,8 @@ func TestVerifyMocksInLatestSession_WhenExtraCallsHaveBeenMade_ReturnsError(t *t
assert.Equal(t, "/sessions/verify", r.URL.Path)

resp := getVerifyBodyWhenSomeMocksAreNotCalledAndExtraCallsMade()
w.Write([]byte(resp))
_, err := w.Write([]byte(resp))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand All @@ -315,7 +323,7 @@ func TestVerifyMocksInLatestSession_WhenExtraCallsHaveBeenMade_ReturnsError(t *t
}

func TestVerifyMocksInLatestSession_WhenServerDoesNotReturn200_ReturnsError(t *testing.T) {
server, serverCallCount := newBadResponseServer()
server, serverCallCount := newBadResponseServer(t)
defer server.Close()

smockerInstance := smockerclient.Instance{Url: server.URL}
Expand All @@ -325,15 +333,16 @@ func TestVerifyMocksInLatestSession_WhenServerDoesNotReturn200_ReturnsError(t *t
assert.EqualError(t, err, "smockerclient unable to verify mocks in current session. received status:400 and message:400 Bad Request")
}

func newBadResponseServer() (*httptest.Server, *int) {
func newBadResponseServer(t *testing.T) (*httptest.Server, *int) {
serverCallCount := 0

server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
serverCallCount++
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 Bad Request"))
_, err := w.Write([]byte("400 Bad Request"))
assert.NoError(t, err, "httptest server write failed")
},
),
)
Expand Down

0 comments on commit 6148f71

Please sign in to comment.