Skip to content

Commit

Permalink
feat(fix): don't start mock server if not required. Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Apr 28, 2017
1 parent 1a30f30 commit 33a0434
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Pact struct {
// AddInteraction creates a new Pact interaction, initialising all
// required things. Will automatically start a Mock Service if none running.
func (p *Pact) AddInteraction() *Interaction {
p.Setup()
p.Setup(true)
log.Printf("[DEBUG] pact add interaction")
i := &Interaction{}
p.Interactions = append(p.Interactions, i)
Expand All @@ -76,7 +76,7 @@ func (p *Pact) AddInteraction() *Interaction {
// Setup starts the Pact Mock Server. This is usually called before each test
// suite begins. AddInteraction() will automatically call this if no Mock Server
// has been started.
func (p *Pact) Setup() *Pact {
func (p *Pact) Setup(startMockServer bool) *Pact {
p.setupLogging()
log.Printf("[DEBUG] pact setup")
dir, _ := os.Getwd()
Expand All @@ -101,7 +101,7 @@ func (p *Pact) Setup() *Pact {
p.SpecificationVersion = 2
}

if p.Server == nil {
if p.Server == nil && startMockServer {
args := []string{
"--pact-specification-version",
fmt.Sprintf("%d", p.SpecificationVersion),
Expand Down Expand Up @@ -151,7 +151,7 @@ func (p *Pact) Teardown() *Pact {
// Verify runs the current test case against a Mock Service.
// Will cleanup interactions between tests within a suite.
func (p *Pact) Verify(integrationTest func() error) error {
p.Setup()
p.Setup(true)
log.Printf("[DEBUG] pact verify")
mockServer := &MockService{
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
Expand Down Expand Up @@ -185,7 +185,7 @@ func (p *Pact) Verify(integrationTest func() error) error {
// given Consumer <-> Provider pair. It will write out the Pact to the
// configured file.
func (p *Pact) WritePact() error {
p.Setup()
p.Setup(true)
log.Printf("[DEBUG] pact write Pact file")
mockServer := MockService{
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
Expand All @@ -203,7 +203,7 @@ func (p *Pact) WritePact() error {
// VerifyProvider reads the provided pact files and runs verification against
// a running Provider API.
func (p *Pact) VerifyProvider(request types.VerifyRequest) error {
p.Setup()
p.Setup(false)

// If we provide a Broker, we go to it to find consumers
if request.BrokerURL != "" {
Expand Down
10 changes: 8 additions & 2 deletions dsl/pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,16 @@ func TestPact_Setup(t *testing.T) {
createDaemon(port, true)

pact := &Pact{Port: port, LogLevel: "DEBUG"}
pact.Setup()
pact.Setup(true)
if pact.Server == nil {
t.Fatalf("Expected server to be created")
}

pact = &Pact{Port: port, LogLevel: "DEBUG"}
pact.Setup(false)
if pact.Server != nil {
t.Fatalf("Expected server to be nil")
}
}

func TestPact_Teardown(t *testing.T) {
Expand All @@ -189,7 +195,7 @@ func TestPact_Teardown(t *testing.T) {
waitForPortInTest(port, t)

pact := &Pact{Port: port, LogLevel: "DEBUG"}
pact.Setup()
pact.Setup(true)
pact.Teardown()
if pact.Server.Status != 0 {
t.Fatalf("Expected server exit status to be 0 but got %d", pact.Server.Status)
Expand Down

0 comments on commit 33a0434

Please sign in to comment.