Skip to content

Commit

Permalink
fix: fixed linter errors in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
antklim committed Jul 30, 2021
1 parent 6022d1c commit 1ccc97e
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 121 deletions.
25 changes: 7 additions & 18 deletions client/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var channelTimeout = 50 * time.Millisecond

func createServiceManager() *ServiceManager {
cs := []string{"-test.run=TestHelperProcess", "--", os.Args[0]}
env := []string{"GO_WANT_HELPER_PROCESS=1", fmt.Sprintf("GO_WANT_HELPER_PROCESS_TO_SUCCEED=true")}
env := []string{"GO_WANT_HELPER_PROCESS=1", "GO_WANT_HELPER_PROCESS_TO_SUCCEED=true"}
mgr := &ServiceManager{
Cmd: os.Args[0],
Args: cs,
Expand All @@ -24,9 +24,7 @@ func createServiceManager() *ServiceManager {
}

func TestServiceManager(t *testing.T) {
var manager interface{}
manager = new(ServiceManager)

var manager interface{} = new(ServiceManager)
if _, ok := manager.(*ServiceManager); !ok {
t.Fatalf("Must be a ServiceManager")
}
Expand All @@ -47,7 +45,7 @@ func TestServiceManager_Setup(t *testing.T) {
func TestServiceManager_removeServiceMonitor(t *testing.T) {
mgr := createServiceManager()
cmd := fakeExecCommand("", true, "")
cmd.Start()
cmd.Start() // nolint:errcheck
mgr.processMap.processes = map[int]*exec.Cmd{
cmd.Process.Pid: cmd,
}
Expand Down Expand Up @@ -75,7 +73,7 @@ func TestServiceManager_removeServiceMonitor(t *testing.T) {
func TestServiceManager_addServiceMonitor(t *testing.T) {
mgr := createServiceManager()
cmd := fakeExecCommand("", true, "")
cmd.Start()
cmd.Start() // nolint:errcheck
mgr.commandCreatedChan <- cmd
var timeout = time.After(channelTimeout)

Expand Down Expand Up @@ -128,12 +126,12 @@ func TestServiceManager_addServiceMonitorWithDeadJob(t *testing.T) {
func TestServiceManager_Stop(t *testing.T) {
mgr := createServiceManager()
cmd := fakeExecCommand("", true, "")
cmd.Start()
cmd.Start() // nolint:errcheck
mgr.processMap.processes = map[int]*exec.Cmd{
cmd.Process.Pid: cmd,
}

mgr.Stop(cmd.Process.Pid)
mgr.Stop(cmd.Process.Pid) // nolint:errcheck
var timeout = time.After(channelTimeout)
for {
mgr.processMap.Lock()
Expand All @@ -157,7 +155,7 @@ func TestServiceManager_Stop(t *testing.T) {
func TestServiceManager_List(t *testing.T) {
mgr := createServiceManager()
cmd := fakeExecCommand("", true, "")
cmd.Start()
cmd.Start() // nolint:errcheck
processes := map[int]*exec.Cmd{
cmd.Process.Pid: cmd,
}
Expand Down Expand Up @@ -196,15 +194,6 @@ func TestServiceManager_Start(t *testing.T) {
}
}

// Adapted from http://npf.io/2015/06/testing-exec-command/
var fakeExecSuccessCommand = func() *exec.Cmd {
return fakeExecCommand("", true, "")
}

var fakeExecFailCommand = func() *exec.Cmd {
return fakeExecCommand("", false, "")
}

func fakeExecCommand(command string, success bool, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
Expand Down
4 changes: 2 additions & 2 deletions dsl/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func setupMockBroker(auth bool) *httptest.Server {

w.Header().Set("WWW-Authenticate", `Basic realm="Broker Authentication Required"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
w.Write([]byte("401 Unauthorized\n")) // nolint:errcheck
})
}
} else {
Expand Down Expand Up @@ -67,7 +67,7 @@ func setupMockBroker(auth bool) *httptest.Server {
mux.Handle("/pacts/provider/broken/latest/dev", authFunc(func(w http.ResponseWriter, req *http.Request) {
log.Println("[DEBUG] broker broker response")
w.WriteHeader(500)
w.Write([]byte("500 Server Error\n"))
w.Write([]byte("500 Server Error\n")) // nolint:errcheck
}))

// Find 'bobby' consumers for tag 'dev'
Expand Down
9 changes: 6 additions & 3 deletions dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func TestClient_StartServerFail(t *testing.T) {
func TestClient_StopServer(t *testing.T) {
client, svc := createMockClient(true)

client.StopServer(&types.MockServer{})
_, err := client.StopServer(&types.MockServer{})
if err != nil {
t.Fatalf("Failed to stop server: %v", err)
}
if svc.ServiceStopCount != 1 {
t.Fatalf("Expected 1 server to have been stopped, got %d", svc.ServiceStartCount)
}
Expand Down Expand Up @@ -216,13 +219,13 @@ func createMockClient(success bool) (*PactClient, *ServiceMock) {

// Start all processes to get the Pids!
for _, s := range svc.ServiceList {
s.Start()
s.Start() // nolint:errcheck
}

// Cleanup all Processes when we finish
defer func() {
for _, s := range svc.ServiceList {
s.Process.Kill()
s.Process.Kill() // nolint:errcheck
}
}()

Expand Down
4 changes: 2 additions & 2 deletions dsl/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestInteraction_WithRequest(t *testing.T) {

var expect interface{}
body, _ := json.Marshal(obj)
json.Unmarshal(body, &expect)
json.Unmarshal(body, &expect) // nolint:errcheck

if _, ok := i.Request.Body.(map[string]string); !ok {
t.Fatal("Expected response to be of type 'map[string]string', but got", reflect.TypeOf(i.Request.Body))
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestInteraction_WillRespondWith(t *testing.T) {

var expect interface{}
body, _ := json.Marshal(obj)
json.Unmarshal(body, &expect)
json.Unmarshal(body, &expect) // nolint:errcheck

if _, ok := i.Response.Body.(map[string]string); !ok {
t.Fatal("Expected response to be of type 'map[string]string', but got", reflect.TypeOf(i.Response.Body))
Expand Down
6 changes: 3 additions & 3 deletions dsl/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,16 @@ func formatJSON(object interface{}) interface{} {
var out bytes.Buffer
switch content := object.(type) {
case string:
json.Indent(&out, []byte(content), "", "\t")
json.Indent(&out, []byte(content), "", "\t") // nolint:errcheck
default:
jsonString, err := json.Marshal(object)
if err != nil {
log.Println("[ERROR] unable to marshal json:", err)
}
json.Indent(&out, []byte(jsonString), "", "\t")
json.Indent(&out, []byte(jsonString), "", "\t") // nolint:errcheck
}

return string(out.Bytes())
return out.String()
}

// Instrument the StructMatcher type to be able to assert the
Expand Down
4 changes: 0 additions & 4 deletions dsl/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package dsl

import "testing"

type t struct {
ID int
}

func TestMessage_DSL(t *testing.T) {
m := &Message{}
m.Given("state string").
Expand Down
16 changes: 8 additions & 8 deletions dsl/pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func TestPact_BeforeEach(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h != "true" {
t.Error("expected http handler to be invoked")
}
}
Expand All @@ -435,7 +435,7 @@ func TestPact_BeforeEachNotSetupPath(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h != "true" {
t.Error("expected http handler to be invoked")
}
}
Expand All @@ -462,7 +462,7 @@ func TestPact_AfterEach(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h != "true" {
t.Error("expected http handler to be invoked")
}
}
Expand Down Expand Up @@ -490,7 +490,7 @@ func TestPact_AfterEachSetupPath(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h != "true" {
t.Error("expected http handler to be invoked")
}
}
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestPact_StateHandlerMiddlewareStateHandlerExists(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h == "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h == "true" {
t.Error("expected http handler to not be invoked")
}
}
Expand Down Expand Up @@ -558,7 +558,7 @@ func TestPact_StateHandlerMiddlewareStateHandlerNotExists(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h == "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h == "true" {
t.Error("expected http handler to not be invoked")
}
}
Expand Down Expand Up @@ -591,7 +591,7 @@ func TestPact_StateHandlerMiddlewareStateHandlerError(t *testing.T) {
}

// expect http handler to NOT have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h == "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h == "true" {
t.Error("expected http handler to not be invoked")
}
}
Expand All @@ -611,7 +611,7 @@ func TestPact_StateHandlerMiddlewarePassThroughInvalidPath(t *testing.T) {
mw(dummyHandler("X-Dummy-Handler")).ServeHTTP(rr, req)

// expect http handler to have been called
if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" {
if h := rr.Header().Get("X-Dummy-Handler"); h != "true" {
t.Errorf("expected target http handler to be invoked")
}
}
Expand Down
67 changes: 0 additions & 67 deletions dsl/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,13 @@ package dsl
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"github.com/pact-foundation/pact-go/types"
)

func createMockRemoteServer(valid bool) (*httptest.Server, string) {
file := createSimplePact(valid)
dir := filepath.Dir(file.Name())
path := filepath.Base(file.Name())
server := httptest.NewServer(http.FileServer(http.Dir(dir)))

return server, fmt.Sprintf("%s/%s", server.URL, path)
}

func createSimplePact(valid bool) *os.File {
var data []byte
if valid {
data = []byte(`
{
"consumer": {
"name": "Some Consumer"
},
"provider": {
"name": "Some Provider"
}
}
`)
} else {
data = []byte(`
{
"consumer": {
"name": "Some Consumer"
}
}
`)
}

tmpfile, err := ioutil.TempFile("", "pactgo")
if err != nil {
log.Fatal(err)
}

if _, err := tmpfile.Write(data); err != nil {
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}

return tmpfile
}

var checkAuth = func(w http.ResponseWriter, r *http.Request) bool {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
Expand All @@ -81,21 +29,6 @@ var checkAuth = func(w http.ResponseWriter, r *http.Request) bool {
return pair[0] == "foo" && pair[1] == "bar"
}

func createMockRemoteServerWithAuth(valid bool) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if checkAuth(w, r) {
w.Write([]byte("Authenticated!"))
return
}

w.Header().Set("WWW-Authenticate", `Basic realm="MY REALM"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
}))

return ts
}

func TestPublish_Publish(t *testing.T) {
c := newMockClient()
p := Publisher{
Expand Down
8 changes: 0 additions & 8 deletions dsl/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package dsl

import "testing"

func TestRequest_Interface(t *testing.T) {
var req interface{}
req = Request{}
if _, ok := req.(Request); !ok {
t.Fatalf("Must be a *Request")
}
}

func TestRequest(t *testing.T) {
req := Request{
Method: "GET",
Expand Down
2 changes: 1 addition & 1 deletion dsl/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *ServiceMock) Start() *exec.Cmd {

s.ServiceStartCount++
cmd := s.ExecFunc()
cmd.Start()
cmd.Start() // nolint:errcheck
if s.processes == nil {
s.processes = make(map[int]*exec.Cmd)
}
Expand Down
4 changes: 4 additions & 0 deletions install/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (i *Installer) CheckVersion(binary, version string) error {

log.Println("[DEBUG] checking if version", v, "within semver range", versionRange)
constraints, err := goversion.NewConstraint(versionRange)
if err != nil {
log.Println("[DEBUG] err", err)
return err
}
if constraints.Check(v) {
log.Println("[DEBUG]", v, "satisfies constraints", v, constraints)
return nil
Expand Down
2 changes: 1 addition & 1 deletion proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func HTTPReverseProxy(options Options) (int, error) {
wrapper := chainHandlers(append(options.Middleware, loggingMiddleware)...)

log.Println("[DEBUG] starting reverse proxy on port", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), wrapper(proxy))
go http.ListenAndServe(fmt.Sprintf(":%d", port), wrapper(proxy)) // nolint:errcheck

return port, nil
}
Expand Down
Loading

0 comments on commit 1ccc97e

Please sign in to comment.