Skip to content

Commit 9f2c7fe

Browse files
committed
feat(pactwritemode): allow user to set pact write mode.Fixes #30
1 parent 2550447 commit 9f2c7fe

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

Diff for: README.md

+20
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,26 @@ publishing or retrieving Pact files to/from a Pact Broker:
452452
* `BrokerUsername` - the username for Pact Broker basic authentication.
453453
* `BrokerPassword` - the password for Pact Broker basic authentication.
454454

455+
### Splitting tests across multiple files
456+
457+
Pact tests tend to be quite long, due to the need to be specific about request/response payloads. Often times it is nicer to be able to split your tests across multiple files for manageability.
458+
459+
You have two options to achieve this feat:
460+
461+
1. Set `PactFileWriteMode` to `"update"` when creating a `Pact` struct:
462+
463+
This will allow you to have multiple independent tests for a given Consumer-Provider pair, without it clobbering previous interactions.
464+
465+
See this [PR](https://github.com/pact-foundation/pact-js/pull/48) for background.
466+
467+
_NOTE_: If using this approach, you *must* be careful to clear out existing pact files (e.g. `rm ./pacts/*.json`) before you run tests to ensure you don't have left over requests that are no longer relevent.
468+
469+
1. Create a Pact test helper to orchestrate the setup and teardown of the mock service for multiple tests.
470+
471+
In larger test bases, this can reduce test suite time and the amount of code you have to manage.
472+
473+
See the JS [example](https://github.com/tarciosaraiva/pact-melbjs/blob/master/helper.js) and related [issue](https://github.com/pact-foundation/pact-js/issues/11) for more.
474+
455475
### Output Logging
456476

457477
Pact Go uses a simple log utility ([logutils](https://github.com/hashicorp/logutils))

Diff for: dsl/mock_service.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ type MockService struct {
2222

2323
// Provider name.
2424
Provider string
25+
26+
// PactFileWriteMode specifies how to write to the Pact file, for the life
27+
// of a Mock Service.
28+
// "overwrite" will always truncate and replace the pact after each run
29+
// "update" will append to the pact file, which is useful if your tests
30+
// are split over multiple files and instantiations of a Mock Server
31+
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
32+
PactFileWriteMode string
2533
}
2634

2735
// call sends a message to the Pact service
@@ -84,16 +92,22 @@ func (m *MockService) Verify() error {
8492
// WritePact writes the pact file to disk.
8593
func (m *MockService) WritePact() error {
8694
log.Println("[DEBUG] mock service write pact")
95+
8796
if m.Consumer == "" || m.Provider == "" {
8897
return errors.New("Consumer and Provider name need to be provided")
8998
}
90-
pact := map[string]map[string]string{
99+
if m.PactFileWriteMode == "" {
100+
m.PactFileWriteMode = "overwrite"
101+
}
102+
103+
pact := map[string]interface{}{
91104
"consumer": map[string]string{
92105
"name": m.Consumer,
93106
},
94107
"provider": map[string]string{
95108
"name": m.Provider,
96109
},
110+
"pactFileWriteMode": m.PactFileWriteMode,
97111
}
98112

99113
url := fmt.Sprintf("%s/pact", m.BaseURL)

Diff for: dsl/pact.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ type Pact struct {
4848
// Defaults to `<cwd>/pacts`.
4949
PactDir string
5050

51+
// PactFileWriteMode specifies how to write to the Pact file, for the life
52+
// of a Mock Service.
53+
// "overwrite" will always truncate and replace the pact after each run
54+
// "update" will append to the pact file, which is useful if your tests
55+
// are split over multiple files and instantiations of a Mock Server
56+
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
57+
PactFileWriteMode string
58+
5159
// Specify which version of the Pact Specification should be used (1 or 2).
5260
// Defaults to 2.
5361
SpecificationVersion int
@@ -192,9 +200,10 @@ func (p *Pact) WritePact() error {
192200
p.Setup(true)
193201
log.Printf("[DEBUG] pact write Pact file")
194202
mockServer := MockService{
195-
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
196-
Consumer: p.Consumer,
197-
Provider: p.Provider,
203+
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
204+
Consumer: p.Consumer,
205+
Provider: p.Provider,
206+
PactFileWriteMode: p.PactFileWriteMode,
198207
}
199208
err := mockServer.WritePact()
200209
if err != nil {

0 commit comments

Comments
 (0)