This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
pact.go
61 lines (53 loc) · 1.95 KB
/
pact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package contracts_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"github.com/pact-foundation/pact-go/dsl"
"github.com/pact-foundation/pact-go/types"
"github.com/stretchr/testify/require"
)
// PactDir returns a path to the directory to store pact files (taken from PACT_DIR env variable)
func PactDir() string {
return os.Getenv("PACT_DIR")
}
// PactFile returns a path to the generated pact file
func PactFile(pactConsumer string, pactProvider string) string {
return fmt.Sprintf("%s/%s-%s.json", PactDir(), strings.ToLower(pactConsumer), strings.ToLower(pactProvider))
}
// CheckErrorAndCleanPact is a workaround for the https://github.com/pact-foundation/pact-go/issues/108 issue
// by manually clearing the interactions from pact and the mock service.
func CheckErrorAndCleanPact(t *testing.T, pact *dsl.Pact, err1 error) {
if err1 != nil {
pact.Interactions = make([]*dsl.Interaction, 0)
mockServer := &dsl.MockService{
BaseURL: fmt.Sprintf("http://%s:%d", pact.Host, pact.Server.Port),
Consumer: pact.Consumer,
Provider: pact.Provider,
}
err2 := mockServer.DeleteInteractions()
require.NoError(t, err2)
}
require.NoError(t, err1)
}
// PublishPactFileToBroker publishes given Pact files to a given Pact broker.
func PublishPactFileToBroker(pactFiles []string, pactBrokerURL string, pactBrokerUsername string, pactBrokerPassword string, pactVersion string, tags []string) {
log.Printf("Publishing pact files %s to a broker (%s)...\n", pactFiles, pactBrokerURL)
p := dsl.Publisher{}
log.SetOutput(ioutil.Discard) // suppress Pact.io Publisher's DEBUG output
err := p.Publish(types.PublishRequest{
PactURLs: pactFiles,
PactBroker: pactBrokerURL,
BrokerUsername: pactBrokerUsername,
BrokerPassword: pactBrokerPassword,
ConsumerVersion: pactVersion,
Tags: tags,
})
log.SetOutput(os.Stdout)
if err != nil {
log.Fatalf("Unable to publish pact to a broker (%s):\n%+v\n", pactBrokerURL, err)
}
}