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 24, 2018
1 parent 424be67 commit eba2758
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
6 changes: 6 additions & 0 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
p.pactClient = NewClient(&client.MockService{}, &client.VerificationService{})
}

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

// Need to predefine due to scoping
var port int
var perr error
Expand All @@ -142,6 +146,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
88 changes: 50 additions & 38 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 @@ -35,41 +32,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 Down Expand Up @@ -100,6 +97,8 @@ func createPact() dsl.Pact {
}

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 @@ -145,9 +144,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 @@ -180,9 +184,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 @@ -215,4 +224,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 eba2758

Please sign in to comment.