diff --git a/dsl/pact.go b/dsl/pact.go index a12a59822..8e54c1e34 100644 --- a/dsl/pact.go +++ b/dsl/pact.go @@ -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 @@ -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) diff --git a/examples/consumer/goconsumer/user_service_test.go b/examples/consumer/goconsumer/user_service_test.go index e0f39686b..3c21e6f0c 100644 --- a/examples/consumer/goconsumer/user_service_test.go +++ b/examples/consumer/goconsumer/user_service_test.go @@ -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 @@ -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() { @@ -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), @@ -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), @@ -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), @@ -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() }