Skip to content

Commit

Permalink
fix: pass pact-file-write-mode to CLI #71
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 20, 2018
1 parent 7bc8362 commit e2362ea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
6 changes: 6 additions & 0 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
p.pactClient = client
}

if p.PactFileWriteMode == "" {
p.PactFileWriteMode = "overwrite"
}

// Need to predefine due to scoping
var port int
var perr error
Expand All @@ -145,6 +149,8 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
p.Consumer,
"--provider",
p.Provider,
"--pact-file-write-mode",
p.PactFileWriteMode,
}

p.Server = p.pactClient.StartServer(args, port)
Expand Down
99 changes: 56 additions & 43 deletions examples/consumer/goconsumer/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package goconsumer
import (
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/pact-foundation/pact-go/types"
)

// Common test data
Expand All @@ -37,41 +34,41 @@ var commonHeaders = map[string]string{
}

// Use this to control the setup and teardown of Pact
func TestMain(m *testing.M) {
// Setup Pact and related test stuff
setup()

// Run all the tests
code := m.Run()

// Shutdown the Mock Service and Write pact files to disk
pact.WritePact()
pact.Teardown()

// Enable when running E2E/integration tests before a release
if os.Getenv("PACT_INTEGRATED_TESTS") != "" {
var brokerHost = os.Getenv("PACT_BROKER_HOST")

// Publish the Pacts...
p := dsl.Publisher{}
err := p.Publish(types.PublishRequest{
PactURLs: []string{filepath.FromSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))},
PactBroker: brokerHost,
ConsumerVersion: "1.0.0",
Tags: []string{"latest", "sit4"},
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
})

if err != nil {
log.Println("ERROR: ", err)
}
} else {
log.Println("Skipping publishing")
}

os.Exit(code)
}
// func TestMain(m *testing.M) {
// // Setup Pact and related test stuff
// setup()

// // Run all the tests
// code := m.Run()

// // Shutdown the Mock Service and Write pact files to disk
// pact.WritePact()
// pact.Teardown()

// // Enable when running E2E/integration tests before a release
// if os.Getenv("PACT_INTEGRATED_TESTS") != "" {
// var brokerHost = os.Getenv("PACT_BROKER_HOST")

// // Publish the Pacts...
// p := dsl.Publisher{}
// err := p.Publish(types.PublishRequest{
// PactURLs: []string{filepath.FromSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))},
// PactBroker: brokerHost,
// ConsumerVersion: "1.0.0",
// Tags: []string{"latest", "sit4"},
// BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
// BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
// })

// if err != nil {
// log.Println("ERROR: ", err)
// }
// } else {
// log.Println("Skipping publishing")
// }

// os.Exit(code)
// }

// Setup common test data
func setup() {
Expand All @@ -95,15 +92,18 @@ func setup() {
func createPact() dsl.Pact {
pactDaemonPort := 6666
return dsl.Pact{
Port: pactDaemonPort,
Consumer: "billy",
Provider: "bobby",
LogDir: logDir,
PactDir: pactDir,
Port: pactDaemonPort,
Consumer: "billy",
Provider: "bobby",
LogDir: logDir,
PactDir: pactDir,
PactFileWriteMode: "merge",
}
}

func TestPactConsumerLoginHandler_UserExists(t *testing.T) {
setup()

var testBillyExists = func() error {
client := Client{
Host: fmt.Sprintf("http://localhost:%d", pact.Server.Port),
Expand Down Expand Up @@ -147,9 +147,14 @@ func TestPactConsumerLoginHandler_UserExists(t *testing.T) {
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}

// Shutdown the Mock Service and Write pact files to disk
pact.WritePact()
pact.Teardown()
}

func TestPactConsumerLoginHandler_UserDoesNotExist(t *testing.T) {
setup()
var testBillyDoesNotExists = func() error {
client := Client{
Host: fmt.Sprintf("http://localhost:%d", pact.Server.Port),
Expand Down Expand Up @@ -182,9 +187,14 @@ func TestPactConsumerLoginHandler_UserDoesNotExist(t *testing.T) {
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}

// Shutdown the Mock Service and Write pact files to disk
pact.WritePact()
pact.Teardown()
}

func TestPactConsumerLoginHandler_UserUnauthorised(t *testing.T) {
setup()
var testBillyUnauthorized = func() error {
client := Client{
Host: fmt.Sprintf("http://localhost:%d", pact.Server.Port),
Expand Down Expand Up @@ -217,4 +227,7 @@ func TestPactConsumerLoginHandler_UserUnauthorised(t *testing.T) {
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}
// Shutdown the Mock Service and Write pact files to disk
pact.WritePact()
pact.Teardown()
}

0 comments on commit e2362ea

Please sign in to comment.