Skip to content

Commit 359e7b1

Browse files
authored
Revert "Revert "Feat/add external reference (#592)""
This reverts commit d976beb.
1 parent d976beb commit 359e7b1

28 files changed

Lines changed: 447 additions & 439 deletions

command/root_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func captureOutput(action func()) string {
5454

5555
action()
5656

57-
w.Close()
57+
_ = w.Close()
5858
out, _ := io.ReadAll(r)
5959
os.Stderr = rescueStderr
6060

consumer/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (p *httpMockProvider) displayMismatches(t *testing.T, mismatches []native.M
201201
fmt.Println("\t\tDiff:")
202202
log.Println("[INFO] pact validation failed, errors: ")
203203
for _, m := range mismatches {
204-
formattedRequest := fmt.Sprintf("%s %s", m.Request.Method, m.Request.Path)
204+
formattedRequest := fmt.Sprintf("%s %s", m.Method, m.Path)
205205
switch m.Type {
206206
case "missing-request":
207207
fmt.Printf("\t\texpected: \t%s (Expected request that was not received)\n", formattedRequest)

consumer/http_v2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewV2Pact(config MockHTTPProviderConfig) (*V2HTTPMockProvider, error) {
3535
// AddInteraction to the pact
3636
func (p *V2HTTPMockProvider) AddInteraction() *V2UnconfiguredInteraction {
3737
log.Println("[DEBUG] pact add V2 interaction")
38-
interaction := p.httpMockProvider.mockserver.NewInteraction("")
38+
interaction := p.mockserver.NewInteraction("")
3939

4040
i := &V2UnconfiguredInteraction{
4141
interaction: &Interaction{

consumer/http_v3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewV3Pact(config MockHTTPProviderConfig) (*V3HTTPMockProvider, error) {
3535
// AddInteraction to the pact
3636
func (p *V3HTTPMockProvider) AddInteraction() *V3UnconfiguredInteraction {
3737
log.Println("[DEBUG] pact add V3 interaction")
38-
interaction := p.httpMockProvider.mockserver.NewInteraction("")
38+
interaction := p.mockserver.NewInteraction("")
3939

4040
i := &V3UnconfiguredInteraction{
4141
interaction: &Interaction{

consumer/http_v4.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewV4Pact(config MockHTTPProviderConfig) (*V4HTTPMockProvider, error) {
3636
// AddInteraction to the pact
3737
func (p *V4HTTPMockProvider) AddInteraction() *V4UnconfiguredInteraction {
3838
log.Println("[DEBUG] pact add V4 interaction")
39-
interaction := p.httpMockProvider.mockserver.NewInteraction("")
39+
interaction := p.mockserver.NewInteraction("")
4040

4141
i := &V4UnconfiguredInteraction{
4242
interaction: &Interaction{
@@ -92,6 +92,15 @@ func (i *V4UnconfiguredInteraction) UponReceiving(description string) *V4Unconfi
9292
return i
9393
}
9494

95+
// AddExternalReference records a reference to an external resource (such as a ticket or
96+
// pull request) against the interaction. References appear under
97+
// comments.references[group][name] in the Pact file. May be called multiple times.
98+
func (i *V4UnconfiguredInteraction) AddExternalReference(group, name, value string) *V4UnconfiguredInteraction {
99+
i.interaction.interaction.WithReference(group, name, value)
100+
101+
return i
102+
}
103+
95104
// WithRequest provides a builder for the expected request
96105
func (i *V4UnconfiguredInteraction) WithCompleteRequest(request Request) *V4InteractionWithCompleteRequest {
97106
i.interaction.WithCompleteRequest(request)

consumer/http_v4_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package consumer
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"strings"
78
"testing"
@@ -85,6 +86,25 @@ func TestHttpV4TypeSystem(t *testing.T) {
8586

8687
}
8788

89+
func TestV4HTTPAddExternalReference(t *testing.T) {
90+
p, err := NewV4Pact(MockHTTPProviderConfig{
91+
Consumer: "consumer",
92+
Provider: "provider",
93+
})
94+
assert.NoError(t, err)
95+
96+
err = p.AddInteraction().
97+
UponReceiving("a request with an external reference").
98+
AddExternalReference("Jira", "TICKET-123", "https://jira.example.com/browse/TICKET-123").
99+
WithRequest("GET", "/", func(b *V4RequestBuilder) {}).
100+
WillRespondWith(200, func(b *V4ResponseBuilder) {}).
101+
ExecuteTest(t, func(msc MockServerConfig) error {
102+
_, err := http.Get(fmt.Sprintf("http://%s:%d/", msc.Host, msc.Port))
103+
return err
104+
})
105+
assert.NoError(t, err)
106+
}
107+
88108
var Like = matchers.Like
89109
var EachLike = matchers.EachLike
90110
var Term = matchers.Term

examples/avro/codec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package avro
22

33
import (
4-
54
"os"
5+
66
"github.com/linkedin/goavro/v2"
77
)
88

9+
//nolint:unused // Retained as a reusable helper for the example package.
910
func getCodec() *goavro.Codec {
1011
schema, err := os.ReadFile("user.avsc")
1112
if err != nil {

examples/grpc/routeguide/server/server.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ import (
4040
"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide/data"
4141
"google.golang.org/grpc/codes"
4242
"google.golang.org/grpc/credentials"
43-
"google.golang.org/grpc/status"
44-
45-
"github.com/golang/protobuf/proto"
43+
"google.golang.org/grpc/status"
44+
"google.golang.org/protobuf/proto"
4645

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

57+
// Keep example flags/entrypoint available for standalone usage.
58+
var (
59+
_ = tls
60+
_ = certFile
61+
_ = keyFile
62+
_ = port
63+
_ = main
64+
)
65+
5866
type routeGuideServer struct {
5967
pb.UnimplementedRouteGuideServer
6068
savedFeatures []*pb.Feature // read-only after initialized
@@ -247,7 +255,9 @@ func main() {
247255
}
248256
grpcServer := grpc.NewServer(opts...)
249257
pb.RegisterRouteGuideServer(grpcServer, NewServer())
250-
grpcServer.Serve(lis)
258+
if err := grpcServer.Serve(lis); err != nil {
259+
log.Fatalf("failed to serve: %v", err)
260+
}
251261
}
252262

253263
// exampleData is a copy of testdata/route_guide_db.json. It's to avoid

installer/installer.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ var setMacOSInstallName = func(file string) error {
302302
return fmt.Errorf("error setting install name on pact lib: %s", err)
303303
}
304304

305-
log.Println("[DEBUG] output from command", stdoutStderr)
305+
log.Println("[DEBUG] output from command", string(stdoutStderr))
306306

307307
return err
308308
}
@@ -388,7 +388,7 @@ const (
388388
var packages = map[string]packageInfo{
389389
FFIPackage: {
390390
libName: "libpact_ffi",
391-
version: "0.4.28",
391+
version: "0.5.6",
392392
semverRange: ">= 0.4.0, < 1.0.0",
393393
},
394394
}
@@ -417,13 +417,17 @@ func (d *defaultDownloader) download(src string, dst string) error {
417417
if err != nil {
418418
return fmt.Errorf("failed to create output file; %w", err)
419419
}
420-
defer f.Close()
420+
defer func() {
421+
_ = f.Close()
422+
}()
421423

422424
resp, err := http.Get(src)
423425
if err != nil {
424426
return fmt.Errorf("failed http call to %s; %w", src, err)
425427
}
426-
defer resp.Body.Close()
428+
defer func() {
429+
_ = resp.Body.Close()
430+
}()
427431

428432
archive, err := gzip.NewReader(resp.Body)
429433
if err != nil {
@@ -523,7 +527,9 @@ func (d *defaultHasher) hash(src string) (string, error) {
523527
if err != nil {
524528
return "", err
525529
}
526-
defer f.Close()
530+
defer func() {
531+
_ = f.Close()
532+
}()
527533

528534
h := md5.New()
529535
if _, err := io.Copy(h, f); err != nil {

internal/native/message_server.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (m *Message) WithContents(part interactionPart, contentType string, body []
232232
defer free(cHeader)
233233

234234
cBody := C.CString(string(body))
235-
defer free(cBody)
235+
defer free(cBody)
236236

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

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

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

546-
overwritePact := false
547-
if overwrite {
548-
overwritePact = true
549-
}
546+
overwritePact := overwrite
550547

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

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

575-
overwritePact := false
576-
if overwrite {
577-
overwritePact = true
578-
}
572+
overwritePact := overwrite
579573

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

@@ -596,3 +590,19 @@ func (m *MessageServer) WritePactFileForServer(port int, dir string, overwrite b
596590
return fmt.Errorf("an unknown error ocurred when writing to pact file")
597591
}
598592
}
593+
594+
// WithReference records an external reference (e.g. a ticket or pull request)
595+
// against the interaction. References are stored under comments.references[group][name]
596+
// in the Pact file. This is a V4-only feature.
597+
func (m *Message) WithReference(group, name, value string) *Message {
598+
cGroup := C.CString(group)
599+
defer free(cGroup)
600+
cName := C.CString(name)
601+
defer free(cName)
602+
cValue := C.CString(value)
603+
defer free(cValue)
604+
605+
C.pactffi_add_interaction_reference(m.handle, cGroup, cName, cValue)
606+
607+
return m
608+
}

0 commit comments

Comments
 (0)