diff --git a/consumer/http.go b/consumer/http.go index ee7c23d99..168b4c2e7 100644 --- a/consumer/http.go +++ b/consumer/http.go @@ -179,7 +179,7 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe func (p *httpMockProvider) reset() { p.mockserver.CleanupMockServer(p.config.Port) p.config.Port = 0 - p.configure() + _ = p.configure() } // TODO: improve / pretty print this to make it really easy to understand the problems diff --git a/consumer/http_v4.go b/consumer/http_v4.go index 8b243bdc5..8d60d5d35 100644 --- a/consumer/http_v4.go +++ b/consumer/http_v4.go @@ -458,7 +458,7 @@ func (i *V4InteractionWithPluginRequestBuilder) Headers(headers matchers.Headers // PluginContents configures a plugin. This may be called once per plugin registered. func (i *V4InteractionWithPluginRequestBuilder) PluginContents(contentType string, contents string) *V4InteractionWithPluginRequestBuilder { - i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) + _ = i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) return i } @@ -539,7 +539,7 @@ func (i *V4InteractionWithPluginResponseBuilder) Headers(headers matchers.Header // PluginContents configures a plugin. This may be called once per plugin registered. func (i *V4InteractionWithPluginResponseBuilder) PluginContents(contentType string, contents string) *V4InteractionWithPluginResponseBuilder { - i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_RESPONSE, contentType, contents) + _ = i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_RESPONSE, contentType, contents) return i } diff --git a/installer/installer.go b/installer/installer.go index 281fa627e..7ec7af83f 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -44,7 +44,7 @@ func NewInstaller(opts ...installerConfig) (*Installer, error) { i := &Installer{downloader: &defaultDownloader{}, fs: afero.NewOsFs(), hasher: &defaultHasher{}, config: &configuration{}} for _, opt := range opts { - opt(i) + _ = opt(i) } if _, ok := supportedOSes[runtime.GOOS]; !ok { diff --git a/installer/installer_test.go b/installer/installer_test.go index 12e0b6c54..6f65935c8 100644 --- a/installer/installer_test.go +++ b/installer/installer_test.go @@ -136,7 +136,7 @@ func TestInstallerCheckInstallation(t *testing.T) { for pkg := range packages { dst, _ := i.getLibDstForPackage(pkg) - mockFs.Create(dst) + _, _ = mockFs.Create(dst) } err := i.CheckInstallation() @@ -158,7 +158,7 @@ func TestInstallerCheckPackageInstall(t *testing.T) { callFunc: func() { for pkg := range packages { dst, _ := i.getLibDstForPackage(pkg) - mockFs.Create(dst) + _, _ = mockFs.Create(dst) } }, }, diff --git a/internal/native/message_server.go b/internal/native/message_server.go index c118df379..c61e89bda 100644 --- a/internal/native/message_server.go +++ b/internal/native/message_server.go @@ -598,7 +598,8 @@ func (m *MessageServer) MockServerMismatchedRequests(port int) []MismatchedReque return []MismatchedRequest{} } - json.Unmarshal([]byte(C.GoString(mismatches)), &res) + // TODO: return no requests if error on unmarshal ? + _ = json.Unmarshal([]byte(C.GoString(mismatches)), &res) return res } diff --git a/internal/native/message_server_test.go b/internal/native/message_server_test.go index eb88e3334..e592f9dcd 100644 --- a/internal/native/message_server_test.go +++ b/internal/native/message_server_test.go @@ -150,7 +150,8 @@ func TestGetAsyncMessageContentsAsBytes(t *testing.T) { var v struct { Some string `json:"some"` } - json.Unmarshal(bytes, &v) + err = json.Unmarshal(bytes, &v) + assert.NoError(t, err) assert.Equal(t, "json", v.Some) } @@ -181,7 +182,8 @@ func TestGetSyncMessageContentsAsBytes(t *testing.T) { var v struct { Some string `json:"some"` } - json.Unmarshal(bytes[0], &v) + err = json.Unmarshal(bytes[0], &v) + assert.NoError(t, err) assert.Equal(t, "response", v.Some) } @@ -189,7 +191,8 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) { m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test - m.UsingPlugin("protobuf", "0.3.0") + err := m.UsingPlugin("protobuf", "0.3.0") + assert.NoError(t, err) i := m.NewSyncMessageInteraction("grpc interaction") @@ -214,10 +217,11 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) { } }` - i. + err = i. Given("plugin state"). // For gRPC interactions we prpvide the config once for both the request and response parts WithPluginInteractionContents(INTERACTION_PART_REQUEST, "application/protobuf", grpcInteraction) + assert.NoError(t, err) bytes, err := i.GetMessageRequestContents() assert.NoError(t, err) @@ -225,7 +229,8 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) { // Should be able to convert request body back into a protobuf p := &InitPluginRequest{} - proto.Unmarshal(bytes, p) + err = proto.Unmarshal(bytes, p) + assert.NoError(t, err) assert.Equal(t, "0.0.0", p.Version) // Should be able to convert response into a protobuf @@ -233,7 +238,8 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, bytes) r := &InitPluginResponse{} - proto.Unmarshal(response[0], r) + err = proto.Unmarshal(response[0], r) + assert.NoError(t, err) assert.Equal(t, "test", r.Catalogue[0].Key) } @@ -242,7 +248,7 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) { m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test - m.UsingPlugin("protobuf", "0.3.0") + _ = m.UsingPlugin("protobuf", "0.3.0") i := m.NewAsyncMessageInteraction("grpc interaction") @@ -257,10 +263,11 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) { "version": "matching(semver, '0.0.0')" }` - i. + err := i. Given("plugin state"). // For gRPC interactions we prpvide the config once for both the request and response parts WithPluginInteractionContents(INTERACTION_PART_REQUEST, "application/protobuf", protobufInteraction) + assert.NoError(t, err) bytes, err := i.GetMessageRequestContents() assert.NoError(t, err) @@ -268,33 +275,20 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) { // Should be able to convert body back into a protobuf p := &InitPluginRequest{} - proto.Unmarshal(bytes, p) + err = proto.Unmarshal(bytes, p) + assert.NoError(t, err) assert.Equal(t, "0.0.0", p.Version) } -type binaryMessage struct { - ProviderStates []map[string]interface{} `json:"providerStates"` - Description string `json:"description"` - Metadata map[string]string `json:"metadata"` - Contents string `json:"contents"` // base 64 encoded - // Contents []byte `json:"contents"` -} -type jsonMessage struct { - ProviderStates []map[string]interface{} `json:"providerStates"` - Description string `json:"description"` - Metadata map[string]string `json:"metadata"` - Contents interface{} `json:"contents"` -} - func TestGrpcPluginInteraction(t *testing.T) { tmpPactFolder, err := ioutil.TempDir("", "pact-go") assert.NoError(t, err) - log.SetLogLevel("TRACE") + _ = log.SetLogLevel("TRACE") m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test - m.UsingPlugin("protobuf", "0.3.0") + _ = m.UsingPlugin("protobuf", "0.3.0") i := m.NewSyncMessageInteraction("grpc interaction") @@ -319,10 +313,11 @@ func TestGrpcPluginInteraction(t *testing.T) { } }` - i. + err = i. Given("plugin state"). // For gRPC interactions we prpvide the config once for both the request and response parts WithPluginInteractionContents(INTERACTION_PART_REQUEST, "application/protobuf", grpcInteraction) + assert.NoError(t, err) // Start the gRPC mock server port, err := m.StartTransport("grpc", "127.0.0.1", 0, make(map[string][]interface{})) diff --git a/internal/native/mock_server.go b/internal/native/mock_server.go index 38ec61e4e..697d032a3 100644 --- a/internal/native/mock_server.go +++ b/internal/native/mock_server.go @@ -252,10 +252,10 @@ func Init(logLevel string) { if os.Getenv("PACT_LOG_PATH") != "" { log.Println("[DEBUG] initialised native log to log to file:", os.Getenv("PACT_LOG_PATH")) - logToFile(os.Getenv("PACT_LOG_PATH"), l) + _ = logToFile(os.Getenv("PACT_LOG_PATH"), l) } else { log.Println("[DEBUG] initialised native log to log to stdout") - logToStdout(l) + _ = logToStdout(l) } } } @@ -356,7 +356,7 @@ func (m *MockServer) MockServerMismatchedRequests(port int) []MismatchedRequest return []MismatchedRequest{} } - json.Unmarshal([]byte(C.GoString(mismatches)), &res) + _ = json.Unmarshal([]byte(C.GoString(mismatches)), &res) return res } diff --git a/internal/native/mock_server_test.go b/internal/native/mock_server_test.go index 886a13e8f..23a2d427c 100644 --- a/internal/native/mock_server_test.go +++ b/internal/native/mock_server_test.go @@ -167,12 +167,12 @@ func TestHandleBasedHTTPTests(t *testing.T) { func TestPluginInteraction(t *testing.T) { tmpPactFolder, err := ioutil.TempDir("", "pact-go") assert.NoError(t, err) - log.SetLogLevel("trace") + _ = log.SetLogLevel("trace") m := NewHTTPPact("test-plugin-consumer", "test-plugin-provider") // Protobuf plugin test - m.UsingPlugin("protobuf", "0.0.3") + _ = m.UsingPlugin("protobuf", "0.0.3") m.WithSpecificationVersion(SPECIFICATION_VERSION_V4) i := m.NewInteraction("some plugin interaction") @@ -188,11 +188,12 @@ func TestPluginInteraction(t *testing.T) { "version": "matching(semver, '0.0.0')" }` - i.UponReceiving("some interaction"). + err = i.UponReceiving("some interaction"). Given("plugin state"). WithRequest("GET", "/protobuf"). WithStatus(200). WithPluginInteractionContents(INTERACTION_PART_RESPONSE, "application/protobuf", protobufInteraction) + assert.NoError(t, err) port, err := m.Start("0.0.0.0:0", false) assert.NoError(t, err) @@ -205,7 +206,7 @@ func TestPluginInteraction(t *testing.T) { assert.NoError(t, err) initPluginRequest := &InitPluginRequest{} - proto.Unmarshal(bytes, initPluginRequest) + err = proto.Unmarshal(bytes, initPluginRequest) assert.NoError(t, err) assert.Equal(t, "pact-go-driver", initPluginRequest.Implementation) diff --git a/matchers/matcher.go b/matchers/matcher.go index c550ee5e2..f0bdb9e34 100644 --- a/matchers/matcher.go +++ b/matchers/matcher.go @@ -59,10 +59,6 @@ func (m like) GetValue() interface{} { func (m like) isMatcher() { } -func (m like) string() string { - return fmt.Sprintf("%s", m.Value) -} - type term struct { Value string `json:"value"` Type string `json:"pact:matcher:type"` @@ -76,10 +72,6 @@ func (m term) GetValue() interface{} { func (m term) isMatcher() { } -func (m term) string() string { - return string(m.Value) -} - func (m term) MarshalJSON() ([]byte, error) { type marshaler term diff --git a/matchers/matcher_test.go b/matchers/matcher_test.go index 923881522..83dfed919 100644 --- a/matchers/matcher_test.go +++ b/matchers/matcher_test.go @@ -344,16 +344,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") 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") } - return string(out.Bytes()) + return out.String() } // Instrument the StructMatcher type to be able to assert the diff --git a/message/v4/asynchronous_message.go b/message/v4/asynchronous_message.go index d0b3bf61b..8c6b03ba7 100644 --- a/message/v4/asynchronous_message.go +++ b/message/v4/asynchronous_message.go @@ -62,7 +62,7 @@ type UnconfiguredAsynchronousMessageBuilder struct { // AddMessage creates a new asynchronous consumer expectation func (m *UnconfiguredAsynchronousMessageBuilder) UsingPlugin(config PluginConfig) *AsynchronousMessageWithPlugin { - m.rootBuilder.pact.messageserver.UsingPlugin(config.Plugin, config.Version) + _ = m.rootBuilder.pact.messageserver.UsingPlugin(config.Plugin, config.Version) return &AsynchronousMessageWithPlugin{ rootBuilder: m.rootBuilder, @@ -74,7 +74,7 @@ type AsynchronousMessageWithPlugin struct { } func (s *AsynchronousMessageWithPlugin) WithContents(contents string, contentType string) *AsynchronousMessageWithPluginContents { - s.rootBuilder.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) + _ = s.rootBuilder.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) return &AsynchronousMessageWithPluginContents{ rootBuilder: s.rootBuilder, diff --git a/message/v4/asynchronous_message_test.go b/message/v4/asynchronous_message_test.go index b4fc484ed..d5d756086 100644 --- a/message/v4/asynchronous_message_test.go +++ b/message/v4/asynchronous_message_test.go @@ -14,7 +14,7 @@ func TestAsyncTypeSystem(t *testing.T) { Provider: "asyncprovider", PactDir: "/tmp/", }) - log.SetLogLevel("TRACE") + _ = log.SetLogLevel("TRACE") type foo struct { Foo string `json:"foo"` diff --git a/message/v4/synchronous_message.go b/message/v4/synchronous_message.go index 28d9c8eae..7fb14d6f0 100644 --- a/message/v4/synchronous_message.go +++ b/message/v4/synchronous_message.go @@ -34,8 +34,6 @@ type SynchronousMessage struct { // SynchronousMessageBuilder is a representation of a single, bidirectional message type SynchronousMessageBuilder struct { - messageHandle *native.Message - pact *SynchronousPact } // Given specifies a provider state @@ -65,7 +63,7 @@ type UnconfiguredSynchronousMessageBuilder struct { // UsingPlugin enables a plugin for use in the current test case func (m *UnconfiguredSynchronousMessageBuilder) UsingPlugin(config PluginConfig) *SynchronousMessageWithPlugin { - m.pact.mockserver.UsingPlugin(config.Plugin, config.Version) + _ = m.pact.mockserver.UsingPlugin(config.Plugin, config.Version) return &SynchronousMessageWithPlugin{ pact: m.pact, @@ -75,7 +73,7 @@ func (m *UnconfiguredSynchronousMessageBuilder) UsingPlugin(config PluginConfig) // UsingPlugin enables a plugin for use in the current test case func (m *SynchronousMessageWithPlugin) UsingPlugin(config PluginConfig) *SynchronousMessageWithPlugin { - m.pact.mockserver.UsingPlugin(config.Plugin, config.Version) + _ = m.pact.mockserver.UsingPlugin(config.Plugin, config.Version) return m } @@ -185,7 +183,7 @@ type SynchronousMessageWithPlugin struct { } func (s *SynchronousMessageWithPlugin) WithContents(contents string, contentType string) *SynchronousMessageWithPluginContents { - s.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) + _ = s.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents) return &SynchronousMessageWithPluginContents{ pact: s.pact, diff --git a/message/v4/synchronous_message_test.go b/message/v4/synchronous_message_test.go index c37d4257e..a10293eb5 100644 --- a/message/v4/synchronous_message_test.go +++ b/message/v4/synchronous_message_test.go @@ -16,7 +16,7 @@ func TestSyncTypeSystem(t *testing.T) { Provider: "provider", PactDir: "/tmp/", }) - log.SetLogLevel("TRACE") + _ = log.SetLogLevel("TRACE") dir, _ := os.Getwd() path := fmt.Sprintf("%s/../../internal/native/pact_plugin.proto", dir) @@ -40,7 +40,7 @@ func TestSyncTypeSystem(t *testing.T) { }` // Sync - no plugin - p.AddSynchronousMessage("some description"). + _ = p.AddSynchronousMessage("some description"). Given("some state"). WithRequest(func(r *SynchronousMessageWithRequestBuilder) { r.WithJSONContent(map[string]string{"foo": "bar"}) @@ -83,7 +83,7 @@ func TestSyncTypeSystem(t *testing.T) { Provider: "provider", PactDir: "/tmp/", }) - p.AddSynchronousMessage("some description"). + _ = p.AddSynchronousMessage("some description"). Given("some state"). UsingPlugin(PluginConfig{ Plugin: "csv", diff --git a/message/verifier.go b/message/verifier.go index e7162b7e0..073a26bac 100644 --- a/message/verifier.go +++ b/message/verifier.go @@ -111,7 +111,7 @@ func CreateMessageHandler(messageHandlers Handlers) proxy.Middleware { } w.WriteHeader(http.StatusOK) - w.Write(body) + _, _ = w.Write(body) return } diff --git a/provider/verifier.go b/provider/verifier.go index e2a123efc..37eecb4b8 100644 --- a/provider/verifier.go +++ b/provider/verifier.go @@ -62,7 +62,7 @@ func (v *Verifier) validateConfig() error { func (v *Verifier) startDefaultHTTPServer(port int) { mux := http.NewServeMux() - http.ListenAndServe(fmt.Sprintf("%s:%d", v.Hostname, port), mux) + _ = http.ListenAndServe(fmt.Sprintf("%s:%d", v.Hostname, port), mux) } // VerifyProviderRaw reads the provided pact files and runs verification against @@ -223,7 +223,8 @@ func getStateFromRequest(r *http.Request) (stateHandlerAction, error) { var state stateHandlerAction buf := new(strings.Builder) tr := io.TeeReader(r.Body, buf) - io.ReadAll(tr) + // TODO: return err if unable to read ? + _, _ = io.ReadAll(tr) // Body is consumed above, need to put it back after ;P r.Body = ioutil.NopCloser(strings.NewReader(buf.String())) @@ -254,7 +255,8 @@ func stateHandlerMiddleware(stateHandlers models.StateHandlers, afterEach Hook) var state stateHandlerAction buf := new(strings.Builder) tr := io.TeeReader(r.Body, buf) - io.ReadAll(tr) + // TODO: should return an error if unable to read ? + _, _ = io.ReadAll(tr) // Body is consumed above, need to put it back after ;P r.Body = ioutil.NopCloser(strings.NewReader(buf.String())) @@ -325,7 +327,8 @@ func stateHandlerMiddleware(stateHandlers models.StateHandlers, afterEach Hook) w.Header().Add("content-type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resBody) + // TODO: return interal server error if unable to write ? + _, _ = w.Write(resBody) return } } diff --git a/provider/verify_request.go b/provider/verify_request.go index 4910f0f28..36b33d633 100644 --- a/provider/verify_request.go +++ b/provider/verify_request.go @@ -321,15 +321,3 @@ func getPort(rawURL string) int { return -1 } - -// Get the address given a URL -func getHost(rawURL string) string { - parsedURL, err := url.Parse(rawURL) - if err != nil { - return "" - } - - // TODO: use parseURL.Hostname() instead ? - splitHost := strings.Split(parsedURL.Host, ":") - return splitHost[0] -} diff --git a/provider/verify_request_test.go b/provider/verify_request_test.go index 47894a191..1cb282678 100644 --- a/provider/verify_request_test.go +++ b/provider/verify_request_test.go @@ -33,7 +33,7 @@ func TestVerifyRequestValidate(t *testing.T) { t.Run(tt.name, func(t *testing.T) { if tt.panic { assert.Panics(t, (func() { - tt.request.validate(handle) + _ = tt.request.validate(handle) })) } else { err := tt.request.validate(handle) diff --git a/proxy/http.go b/proxy/http.go index 275d4b485..4aee3f7e0 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -97,7 +97,9 @@ 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 func() { + _ = http.ListenAndServe(fmt.Sprintf(":%d", port), wrapper(proxy)) + }() return port, nil } diff --git a/proxy/http_test.go b/proxy/http_test.go index 47700ca76..b8fc5978b 100644 --- a/proxy/http_test.go +++ b/proxy/http_test.go @@ -1,7 +1,6 @@ package proxy import ( - "fmt" "net/http" "net/http/httptest" "testing" @@ -32,7 +31,7 @@ func TestLoggingMiddleware(t *testing.T) { loggingMiddleware(dummyHandler("X-Dummy-Handler")).ServeHTTP(rr, req) - if h := rr.HeaderMap.Get("X-Dummy-Handler"); h != "true" { + if h := rr.Result().Header.Get("X-Dummy-Handler"); h != "true" { t.Errorf("expected handler to set the header 'X-Dummy-Handler: true' but got '%v'", h) } @@ -63,7 +62,7 @@ func TestChainHandlers(t *testing.T) { middlewareChain(dummyHandler("X-Dummy-Handler")).ServeHTTP(rr, req) for _, h := range headers { - if v := rr.HeaderMap.Get(h); v != "true" { + if v := rr.Result().Header.Get(h); v != "true" { t.Errorf("expected handler to set the header '%v: true' but got '%v'", h, v) } @@ -78,7 +77,7 @@ func TestHTTPReverseProxy(t *testing.T) { DummyMiddleware("1"), }, TargetScheme: "http", - TargetAddress: fmt.Sprintf("127.0.0.1:1234"), + TargetAddress: "127.0.0.1:1234", }) if err != nil { diff --git a/utils/deepcopy.go b/utils/deepcopy.go deleted file mode 100644 index 16c50ff92..000000000 --- a/utils/deepcopy.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -// Simplistic map copy -func copyMap(src map[string]interface{}) map[string]interface{} { - dst := make(map[string]interface{}, len(src)) - - for k, v := range src { - dst[k] = v - } - return dst -} diff --git a/utils/json_utils.go b/utils/json_utils.go index 97ba004c6..16a5189b2 100644 --- a/utils/json_utils.go +++ b/utils/json_utils.go @@ -8,8 +8,8 @@ import ( // Format a JSON document to make comparison easier. func FormatJSONString(object string) string { var out bytes.Buffer - json.Indent(&out, []byte(object), "", "\t") - return string(out.Bytes()) + _ = json.Indent(&out, []byte(object), "", "\t") + return out.String() } // Format a JSON document for creating Pact files.