Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion command/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func captureOutput(action func()) string {

action()

w.Close()
_ = w.Close()
out, _ := io.ReadAll(r)
os.Stderr = rescueStderr

Expand Down
2 changes: 1 addition & 1 deletion consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (p *httpMockProvider) displayMismatches(t *testing.T, mismatches []native.M
fmt.Println("\t\tDiff:")
log.Println("[INFO] pact validation failed, errors: ")
for _, m := range mismatches {
formattedRequest := fmt.Sprintf("%s %s", m.Request.Method, m.Request.Path)
formattedRequest := fmt.Sprintf("%s %s", m.Method, m.Path)
switch m.Type {
case "missing-request":
fmt.Printf("\t\texpected: \t%s (Expected request that was not received)\n", formattedRequest)
Expand Down
2 changes: 1 addition & 1 deletion consumer/http_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewV2Pact(config MockHTTPProviderConfig) (*V2HTTPMockProvider, error) {
// AddInteraction to the pact
func (p *V2HTTPMockProvider) AddInteraction() *V2UnconfiguredInteraction {
log.Println("[DEBUG] pact add V2 interaction")
interaction := p.httpMockProvider.mockserver.NewInteraction("")
interaction := p.mockserver.NewInteraction("")

i := &V2UnconfiguredInteraction{
interaction: &Interaction{
Expand Down
2 changes: 1 addition & 1 deletion consumer/http_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewV3Pact(config MockHTTPProviderConfig) (*V3HTTPMockProvider, error) {
// AddInteraction to the pact
func (p *V3HTTPMockProvider) AddInteraction() *V3UnconfiguredInteraction {
log.Println("[DEBUG] pact add V3 interaction")
interaction := p.httpMockProvider.mockserver.NewInteraction("")
interaction := p.mockserver.NewInteraction("")

i := &V3UnconfiguredInteraction{
interaction: &Interaction{
Expand Down
11 changes: 10 additions & 1 deletion consumer/http_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewV4Pact(config MockHTTPProviderConfig) (*V4HTTPMockProvider, error) {
// AddInteraction to the pact
func (p *V4HTTPMockProvider) AddInteraction() *V4UnconfiguredInteraction {
log.Println("[DEBUG] pact add V4 interaction")
interaction := p.httpMockProvider.mockserver.NewInteraction("")
interaction := p.mockserver.NewInteraction("")

i := &V4UnconfiguredInteraction{
interaction: &Interaction{
Expand Down Expand Up @@ -92,6 +92,15 @@ func (i *V4UnconfiguredInteraction) UponReceiving(description string) *V4Unconfi
return i
}

// AddExternalReference records a reference to an external resource (such as a ticket or
// pull request) against the interaction. References appear under
// comments.references[group][name] in the Pact file. May be called multiple times.
func (i *V4UnconfiguredInteraction) AddExternalReference(group, name, value string) *V4UnconfiguredInteraction {
i.interaction.interaction.WithReference(group, name, value)

return i
}

// WithRequest provides a builder for the expected request
func (i *V4UnconfiguredInteraction) WithCompleteRequest(request Request) *V4InteractionWithCompleteRequest {
i.interaction.WithCompleteRequest(request)
Expand Down
20 changes: 20 additions & 0 deletions consumer/http_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consumer

import (
"fmt"
"net/http"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -85,6 +86,25 @@ func TestHttpV4TypeSystem(t *testing.T) {

}

func TestV4HTTPAddExternalReference(t *testing.T) {
p, err := NewV4Pact(MockHTTPProviderConfig{
Consumer: "consumer",
Provider: "provider",
})
assert.NoError(t, err)

err = p.AddInteraction().
UponReceiving("a request with an external reference").
AddExternalReference("Jira", "TICKET-123", "https://jira.example.com/browse/TICKET-123").
WithRequest("GET", "/", func(b *V4RequestBuilder) {}).
WillRespondWith(200, func(b *V4ResponseBuilder) {}).
ExecuteTest(t, func(msc MockServerConfig) error {
_, err := http.Get(fmt.Sprintf("http://%s:%d/", msc.Host, msc.Port))
return err
})
assert.NoError(t, err)
}

var Like = matchers.Like
var EachLike = matchers.EachLike
var Term = matchers.Term
Expand Down
3 changes: 2 additions & 1 deletion examples/avro/codec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package avro

import (

"os"

"github.com/linkedin/goavro/v2"
)

//nolint:unused // Retained as a reusable helper for the example package.
func getCodec() *goavro.Codec {
schema, err := os.ReadFile("user.avsc")
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions examples/grpc/routeguide/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import (
"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide/data"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"

"github.com/golang/protobuf/proto"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

pb "github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
)
Expand All @@ -55,6 +54,15 @@ var (
port = flag.Int("port", 50051, "The server port")
)

// Keep example flags/entrypoint available for standalone usage.
var (
_ = tls
_ = certFile
_ = keyFile
_ = port
_ = main
)

type routeGuideServer struct {
pb.UnimplementedRouteGuideServer
savedFeatures []*pb.Feature // read-only after initialized
Expand Down Expand Up @@ -247,7 +255,9 @@ func main() {
}
grpcServer := grpc.NewServer(opts...)
pb.RegisterRouteGuideServer(grpcServer, NewServer())
grpcServer.Serve(lis)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

// exampleData is a copy of testdata/route_guide_db.json. It's to avoid
Expand Down
16 changes: 11 additions & 5 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ var setMacOSInstallName = func(file string) error {
return fmt.Errorf("error setting install name on pact lib: %s", err)
}

log.Println("[DEBUG] output from command", stdoutStderr)
log.Println("[DEBUG] output from command", string(stdoutStderr))

return err
}
Expand Down Expand Up @@ -388,7 +388,7 @@ const (
var packages = map[string]packageInfo{
FFIPackage: {
libName: "libpact_ffi",
version: "0.4.28",
version: "0.5.6",
semverRange: ">= 0.4.0, < 1.0.0",
},
}
Expand Down Expand Up @@ -417,13 +417,17 @@ func (d *defaultDownloader) download(src string, dst string) error {
if err != nil {
return fmt.Errorf("failed to create output file; %w", err)
}
defer f.Close()
defer func() {
_ = f.Close()
}()

resp, err := http.Get(src)
if err != nil {
return fmt.Errorf("failed http call to %s; %w", src, err)
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()

archive, err := gzip.NewReader(resp.Body)
if err != nil {
Expand Down Expand Up @@ -523,7 +527,9 @@ func (d *defaultHasher) hash(src string) (string, error) {
if err != nil {
return "", err
}
defer f.Close()
defer func() {
_ = f.Close()
}()

h := md5.New()
if _, err := io.Copy(h, f); err != nil {
Expand Down
32 changes: 21 additions & 11 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (m *Message) WithContents(part interactionPart, contentType string, body []
defer free(cHeader)

cBody := C.CString(string(body))
defer free(cBody)
defer free(cBody)

res := C.pactffi_with_body(m.handle, C.int(part), cHeader, cBody)
log.Println("[DEBUG] response from pactffi_interaction_contents", (bool(res)))
Expand Down Expand Up @@ -264,7 +264,7 @@ func (m *MessageServer) UsingPlugin(pluginName string, pluginVersion string) err
return ErrHandleNotFound
default:
if res != 0 {
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code:", res)
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code", res)
}
}

Expand Down Expand Up @@ -302,7 +302,7 @@ func (m *Message) WithPluginInteractionContents(part interactionPart, contentTyp
return ErrPluginSpecificError
default:
if res != 0 {
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code:", res)
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code", res)
}
}

Expand Down Expand Up @@ -543,10 +543,7 @@ func (m *MessageServer) WritePactFile(dir string, overwrite bool) error {
cDir := C.CString(dir)
defer free(cDir)

overwritePact := false
if overwrite {
overwritePact = true
}
overwritePact := overwrite

res := int(C.pactffi_write_message_pact_file(m.messagePact.handle, cDir, C.bool(overwritePact)))

Expand All @@ -572,10 +569,7 @@ func (m *MessageServer) WritePactFileForServer(port int, dir string, overwrite b
cDir := C.CString(dir)
defer free(cDir)

overwritePact := false
if overwrite {
overwritePact = true
}
overwritePact := overwrite

res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.bool(overwritePact)))

Expand All @@ -596,3 +590,19 @@ func (m *MessageServer) WritePactFileForServer(port int, dir string, overwrite b
return fmt.Errorf("an unknown error ocurred when writing to pact file")
}
}

// WithReference records an external reference (e.g. a ticket or pull request)
// against the interaction. References are stored under comments.references[group][name]
// in the Pact file. This is a V4-only feature.
func (m *Message) WithReference(group, name, value string) *Message {
cGroup := C.CString(group)
defer free(cGroup)
cName := C.CString(name)
defer free(cName)
cValue := C.CString(value)
defer free(cValue)

C.pactffi_add_interaction_reference(m.handle, cGroup, cName, cValue)

return m
}
8 changes: 6 additions & 2 deletions internal/native/message_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ func TestGrpcPluginInteraction(t *testing.T) {
if err != nil {
l.Fatalf("did not connect: %v", err)
}
defer conn.Close()
defer func() {
_ = conn.Close()
}()
c := NewPactPluginClient(conn)

// Contact the server and print out its response.
Expand Down Expand Up @@ -480,7 +482,9 @@ func TestGrpcPluginInteraction_ErrorResponse(t *testing.T) {
if err != nil {
l.Fatalf("did not connect: %v", err)
}
defer conn.Close()
defer func() {
_ = conn.Close()
}()
c := NewPactPluginClient(conn)

// Contact the server and print out its response.
Expand Down
Loading