Skip to content

Commit c11f5a8

Browse files
committed
refactor: remove the v3 package and split into
- consumer - provider - message - models - matchers - version - log
1 parent 1c6a597 commit c11f5a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1346
-1489
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ TEST?=./...
44

55
.DEFAULT_GOAL := ci
66

7-
# ci:: clean deps bin pactv3
87
ci:: docker deps clean bin test pact #goveralls
98

109
docker:
@@ -29,6 +28,7 @@ deps:
2928
goveralls:
3029
goveralls -service="travis-ci" -coverprofile=coverage.txt -repotoken $(COVERALLS_TOKEN)
3130

31+
cli:
3232
@if [ ! -d pact/bin ]; then\
3333
echo "--- 🐿 Installing Pact CLI dependencies"; \
3434
curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | bash -x; \
@@ -41,8 +41,8 @@ install: bin
4141
pact: clean install #docker
4242
@echo "--- 🔨 Running Pact examples"
4343
mkdir -p ./examples/v3/pacts
44-
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/examples/v3/...
45-
go test -v -timeout=10s -tags=provider -count=1 github.com/pact-foundation/pact-go/examples/v3/...
44+
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/examples/...
45+
go test -v -timeout=10s -tags=provider -count=1 github.com/pact-foundation/pact-go/examples/...
4646

4747
release:
4848
echo "--- 🚀 Releasing it"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Pact Go runs as part of your regular Go tests.
151151
We'll run through a simple example to get an understanding the concepts:
152152

153153
1. `go get github.com/pact-foundation/pact-go`
154-
1. `cd $GOPATH/src/github.com/pact-foundation/pact-go/examples/v2/`
154+
1. `cd $GOPATH/src/github.com/pact-foundation/pact-go/examples/`
155155
1. `go test -v -run TestConsumer`.
156156

157157
The simple example looks like this:
@@ -222,7 +222,7 @@ func TestConsumer(t *testing.T) {
222222
### Provider API Testing
223223

224224
1. `go get github.com/pact-foundation/pact-go`
225-
1. `cd $GOPATH/src/github.com/pact-foundation/pact-go/examples/v2/`
225+
1. `cd $GOPATH/src/github.com/pact-foundation/pact-go/examples/`
226226
1. `go test -v -run TestProvider`.
227227

228228
Here is the Provider test process broker down:

command/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"log"
55
"os"
66

7-
"github.com/pact-foundation/pact-go/v3/installer"
7+
"github.com/pact-foundation/pact-go/installer"
88

99
"github.com/spf13/cobra"
1010
)

v3/http.go renamed to consumer/http.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Package v3 contains the main Pact DSL used in the Consumer
1+
// package consumer contains the main Pact DSL used in the Consumer
22
// collaboration test cases, and Provider contract test verification.
3-
package v3
3+
package consumer
44

55
// TODO: setup a proper state machine to prevent actions
66
// Current issues
@@ -17,12 +17,14 @@ import (
1717
"path/filepath"
1818
"time"
1919

20+
native "github.com/pact-foundation/pact-go/internal/native/mockserver"
21+
logging "github.com/pact-foundation/pact-go/log"
22+
"github.com/pact-foundation/pact-go/models"
2023
"github.com/pact-foundation/pact-go/utils"
21-
native "github.com/pact-foundation/pact-go/v3/internal/native/mockserver"
2224
)
2325

2426
func init() {
25-
initLogging()
27+
logging.InitLogging()
2628
}
2729

2830
type MockHTTPProviderConfig struct {
@@ -79,7 +81,7 @@ type MockHTTPProviderConfig struct {
7981
// httpMockProvider is the entrypoint for http consumer tests
8082
// This object is not thread safe
8183
type httpMockProvider struct {
82-
specificationVersion SpecificationVersion
84+
specificationVersion models.SpecificationVersion
8385
config MockHTTPProviderConfig
8486
mockserver *native.MockServer
8587
}
@@ -126,9 +128,9 @@ func (p *httpMockProvider) validateConfig() error {
126128

127129
p.mockserver = native.NewHTTPMockServer(p.config.Consumer, p.config.Provider)
128130
switch p.specificationVersion {
129-
case V2:
131+
case models.V2:
130132
p.mockserver.WithSpecificationVersion(native.SPECIFICATION_VERSION_V2)
131-
case V3:
133+
case models.V3:
132134
p.mockserver.WithSpecificationVersion(native.SPECIFICATION_VERSION_V3)
133135
}
134136
native.Init()

v3/http_v2.go renamed to consumer/http_v2.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package v3
1+
package consumer
22

33
import (
44
"log"
5+
6+
"github.com/pact-foundation/pact-go/models"
57
)
68

79
// HTTPMockProviderV2 is the entrypoint for V3 http consumer tests
@@ -16,7 +18,7 @@ func NewV2Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV2, error) {
1618
provider := &HTTPMockProviderV2{
1719
httpMockProvider: &httpMockProvider{
1820
config: config,
19-
specificationVersion: V2,
21+
specificationVersion: models.V2,
2022
},
2123
}
2224
err := provider.validateConfig()
@@ -36,7 +38,7 @@ func (p *HTTPMockProviderV2) AddInteraction() *InteractionV2 {
3638

3739
i := &InteractionV2{
3840
Interaction: Interaction{
39-
specificationVersion: V2,
41+
specificationVersion: models.V2,
4042
interaction: interaction,
4143
},
4244
}

v3/http_v3.go renamed to consumer/http_v3.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package v3
1+
package consumer
22

33
import (
44
"log"
5+
6+
"github.com/pact-foundation/pact-go/models"
57
)
68

79
// HTTPMockProviderV3 is the entrypoint for V3 http consumer tests
@@ -15,7 +17,7 @@ func NewV3Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV3, error) {
1517
provider := &HTTPMockProviderV3{
1618
httpMockProvider: &httpMockProvider{
1719
config: config,
18-
specificationVersion: V3,
20+
specificationVersion: models.V3,
1921
},
2022
}
2123
err := provider.validateConfig()
@@ -35,7 +37,7 @@ func (p *HTTPMockProviderV3) AddInteraction() *InteractionV3 {
3537

3638
i := &InteractionV3{
3739
Interaction: Interaction{
38-
specificationVersion: V3,
40+
specificationVersion: models.V3,
3941
interaction: interaction,
4042
},
4143
}

v3/interaction.go renamed to consumer/interaction.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
package v3
1+
package consumer
22

33
import (
44
"encoding/json"
55
"fmt"
66
"log"
77
"strings"
88

9-
"github.com/pact-foundation/pact-go/v3/internal/native/mockserver"
9+
"github.com/pact-foundation/pact-go/internal/native/mockserver"
10+
"github.com/pact-foundation/pact-go/matchers"
11+
"github.com/pact-foundation/pact-go/models"
12+
"github.com/pact-foundation/pact-go/utils"
1013
)
1114

1215
// Interaction is the main implementation of the Pact interface.
1316
type Interaction struct {
1417
// Reference to the native rust handle
1518
interaction *mockserver.Interaction
16-
specificationVersion SpecificationVersion
19+
specificationVersion models.SpecificationVersion
1720
}
1821

1922
type InteractionRequest struct {
@@ -39,7 +42,7 @@ func (i *Interaction) UponReceiving(description string) *Interaction {
3942
// WithRequest specifies the details of the HTTP request that will be used to
4043
// confirm that the Provider provides an API listening on the given interface.
4144
// Mandatory.
42-
func (i *Interaction) WithRequest(method Method, path Matcher) *InteractionRequest {
45+
func (i *Interaction) WithRequest(method models.Method, path matchers.Matcher) *InteractionRequest {
4346
i.interaction.WithRequest(string(method), path)
4447

4548
return &InteractionRequest{
@@ -48,13 +51,13 @@ func (i *Interaction) WithRequest(method Method, path Matcher) *InteractionReque
4851
}
4952
}
5053

51-
func (i *InteractionRequest) WithQuery(key string, values ...Matcher) *InteractionRequest {
54+
func (i *InteractionRequest) WithQuery(key string, values ...matchers.Matcher) *InteractionRequest {
5255
i.interactionHandle.WithQuery(keyValuesToMapStringArrayInterface(key, values...))
5356

5457
return i
5558
}
5659

57-
func (i *InteractionRequest) WithHeader(key string, values ...Matcher) *InteractionRequest {
60+
func (i *InteractionRequest) WithHeader(key string, values ...matchers.Matcher) *InteractionRequest {
5861
i.interactionHandle.WithRequestHeaders(keyValuesToMapStringArrayInterface(key, values...))
5962

6063
return i
@@ -70,7 +73,7 @@ func (i *InteractionRequest) WithJSONBody(body interface{}) *InteractionRequest
7073
// Check if someone tried to add an object as a string representation
7174
// as per original allowed implementation, e.g.
7275
// { "foo": "bar", "baz": like("bat") }
73-
if isJSONFormattedObject(string(s)) {
76+
if utils.IsJSONFormattedObject(string(s)) {
7477
log.Println("[WARN] request body appears to be a JSON formatted object, " +
7578
"no matching will occur. Support for structured strings has been" +
7679
"deprecated as of 0.13.0. Please use the JSON() method instead")
@@ -92,7 +95,7 @@ func (i *InteractionRequest) WithBody(contentType string, body []byte) *Interact
9295
// Check if someone tried to add an object as a string representation
9396
// as per original allowed implementation, e.g.
9497
// { "foo": "bar", "baz": like("bat") }
95-
if isJSONFormattedObject(string(body)) {
98+
if utils.IsJSONFormattedObject(string(body)) {
9699
log.Println("[WARN] request body appears to be a JSON formatted object, " +
97100
"no matching will occur. Support for structured strings has been" +
98101
"deprecated as of 0.13.0. Please use the JSON() method instead")
@@ -104,7 +107,7 @@ func (i *InteractionRequest) WithBody(contentType string, body []byte) *Interact
104107
}
105108

106109
func (i *InteractionRequest) WithBodyMatch(body interface{}) *InteractionRequest {
107-
i.interactionHandle.WithJSONRequestBody(MatchV2(body))
110+
i.interactionHandle.WithJSONRequestBody(matchers.MatchV2(body))
108111

109112
return i
110113
}
@@ -122,7 +125,7 @@ func (i *InteractionRequest) WillRespondWith(status int) *InteractionResponse {
122125
}
123126
}
124127

125-
func (i *InteractionResponse) WithHeader(key string, values ...Matcher) *InteractionResponse {
128+
func (i *InteractionResponse) WithHeader(key string, values ...matchers.Matcher) *InteractionResponse {
126129
i.interactionHandle.WithRequestHeaders(keyValuesToMapStringArrayInterface(key, values...))
127130

128131
return i
@@ -138,7 +141,7 @@ func (i *InteractionResponse) WithJSONBody(body interface{}) *InteractionRespons
138141
// Check if someone tried to add an object as a string representation
139142
// as per original allowed implementation, e.g.
140143
// { "foo": "bar", "baz": like("bat") }
141-
if isJSONFormattedObject(string(s)) {
144+
if utils.IsJSONFormattedObject(string(s)) {
142145
log.Println("[WARN] request body appears to be a JSON formatted object, " +
143146
"no matching will occur. Support for structured strings has been" +
144147
"deprecated as of 0.13.0. Please use the JSON() method instead")
@@ -162,12 +165,12 @@ func (i *InteractionResponse) WithBody(contentType string, body []byte) *Interac
162165
}
163166

164167
func (i *InteractionResponse) WithBodyMatch(body interface{}) *InteractionResponse {
165-
i.interactionHandle.WithJSONRequestBody(MatchV2(body))
168+
i.interactionHandle.WithJSONRequestBody(matchers.MatchV2(body))
166169

167170
return i
168171
}
169172

170-
func validateMatchers(version SpecificationVersion, obj interface{}) error {
173+
func validateMatchers(version models.SpecificationVersion, obj interface{}) error {
171174
if obj == nil {
172175
return nil
173176
}
@@ -193,7 +196,7 @@ func validateMatchers(version SpecificationVersion, obj interface{}) error {
193196
return nil
194197
}
195198

196-
func hasMatcherGreaterThanSpec(version SpecificationVersion, obj map[string]interface{}) []string {
199+
func hasMatcherGreaterThanSpec(version models.SpecificationVersion, obj map[string]interface{}) []string {
197200
results := make([]string, 0)
198201

199202
for k, v := range obj {
@@ -235,7 +238,7 @@ func hasMatcherGreaterThanSpec(version SpecificationVersion, obj map[string]inte
235238
// return i
236239
// }
237240

238-
func keyValuesToMapStringArrayInterface(key string, values ...Matcher) map[string][]interface{} {
241+
func keyValuesToMapStringArrayInterface(key string, values ...matchers.Matcher) map[string][]interface{} {
239242
q := make(map[string][]interface{})
240243
for _, v := range values {
241244
q[key] = append(q[key], v)

v3/interaction_test.go renamed to consumer/interaction_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package v3
1+
package consumer
22

33
import (
44
"errors"
55
"testing"
66

7+
"github.com/pact-foundation/pact-go/matchers"
8+
"github.com/pact-foundation/pact-go/models"
79
"github.com/stretchr/testify/assert"
810
)
911

@@ -34,22 +36,22 @@ func TestInteraction(t *testing.T) {
3436
{
3537
description: "v3 matches should error",
3638
test: map[string]interface{}{
37-
"dateTime": Regex("2020-01-01", "[0-9\\-]+"),
38-
"name": S("Billy"),
39-
"superstring": Includes("foo"),
40-
"nested": map[string]Matcher{
41-
"val": Includes("val"),
39+
"dateTime": matchers.Regex("2020-01-01", "[0-9\\-]+"),
40+
"name": matchers.S("Billy"),
41+
"superstring": matchers.Includes("foo"),
42+
"nested": map[string]matchers.Matcher{
43+
"val": matchers.Includes("val"),
4244
},
4345
},
4446
want: errors.New("test error"),
4547
},
4648
{
4749
description: "v2 matches should no terror",
4850
test: map[string]interface{}{
49-
"dateTime": Regex("2020-01-01", "[0-9\\-]+"),
50-
"name": S("Billy"),
51-
"nested": map[string]Matcher{
52-
"val": Regex("val", ".*"),
51+
"dateTime": matchers.Regex("2020-01-01", "[0-9\\-]+"),
52+
"name": matchers.S("Billy"),
53+
"nested": map[string]matchers.Matcher{
54+
"val": matchers.Regex("val", ".*"),
5355
},
5456
},
5557
want: nil,
@@ -58,9 +60,9 @@ func TestInteraction(t *testing.T) {
5860

5961
for _, test := range testCases {
6062
if test.want != nil {
61-
assert.Error(t, validateMatchers(V2, test.test), test.description)
63+
assert.Error(t, validateMatchers(models.V2, test.test), test.description)
6264
} else {
63-
assert.NoError(t, validateMatchers(V2, test.test), test.description)
65+
assert.NoError(t, validateMatchers(models.V2, test.test), test.description)
6466
}
6567
}
6668
})

v3/interaction_v2.go renamed to consumer/interaction_v2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package v3
1+
package consumer
22

33
// InteractionV2 sets up an expected request/response on a mock server
44
// and is replayed on the provider side for verification

v3/interaction_v2_test.go renamed to consumer/interaction_v2_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package v3
1+
package consumer
22

33
// func TestInteraction_NewInteraction(t *testing.T) {
44
// i := &InteractionV2{}
@@ -120,7 +120,7 @@ package v3
120120
// }
121121

122122
// for testCase, want := range testCases {
123-
// if isJSONFormattedObject(testCase) != want {
123+
// if utils.IsJSONFormattedObject(testCase) != want {
124124
// t.Fatal("want", want, "got", !want, "for test case", testCase)
125125
// }
126126
// }

0 commit comments

Comments
 (0)