Skip to content

Commit

Permalink
feat(cli): add PACT_DISABLE_TOOL_VALIDITY_CHECK env var
Browse files Browse the repository at this point in the history
- Allow PACT_DISABLE_TOOL_VALIDITY_CHECK env var to disable tool check
- Document re-running specific provider tests

Fixes #44
  • Loading branch information
mefellows committed May 20, 2018
1 parent 4cfdc51 commit d64e50b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
63 changes: 56 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ Read [Getting started with Pact] for more information for beginners.
- [Integrated examples](#integrated-examples)
- [Troubleshooting](#troubleshooting)
- [Splitting tests across multiple files](#splitting-tests-across-multiple-files)
- [Output Logging](#output-logging)
- [Output Logging](#output-logging)
- [Check if the CLI tools are up to date](#check-if-the-cli-tools-are-up-to-date)
- [Disable CLI checks to speed up tests](#disable-cli-checks-to-speed-up-tests)
- [Re-run a specific provider verification test](#re-run-a-specific-provider-verification-test)
- [Contact](#contact)
- [Documentation](#documentation)
- [Troubleshooting](#troubleshooting-1)
- [Roadmap](#roadmap)
- [Contributing](#contributing)

Expand Down Expand Up @@ -713,7 +715,7 @@ You have two options to achieve this feat:

See the JS [example](https://github.com/tarciosaraiva/pact-melbjs/blob/master/helper.js) and related [issue](https://github.com/pact-foundation/pact-js/issues/11) for more.

### Output Logging
#### Output Logging

Pact Go uses a simple log utility ([logutils](https://github.com/hashicorp/logutils))
to filter log messages. The CLI already contains flags to manage this,
Expand All @@ -726,6 +728,57 @@ pact := Pact{
}
```

#### Check if the CLI tools are up to date

Pact ships with a CLI that you can also use to check if the tools are up to date. Simply run `pact-go install`, exit status `0` is good, `1` or higher is bad.

#### Disable CLI checks to speed up tests

Pact relies on a number of CLI tools for successful operation, and it performs some pre-emptive checks
during test runs to ensure that everything will run smoothly. This check, unfortunately, can add up
if spread across a large test suite. You can disable the check by setting the environment variable `PACT_DISABLE_TOOL_VALIDITY_CHECK=1` or specifying it when creating a `dsl.Pact` struct:

```go
dsl.Pact{
...
DisableToolValidityCheck: true,
}
```

You can then [check if the CLI tools are up to date](#check-if-the-cli-tools-are-up-to-date) as part of your CI process once up-front and speed up the rest of the process!

#### Re-run a specific provider verification test

Sometimes you want to target a specific test for debugging an issue or some other reason.

This is easy for the consumer side, as each consumer test can be controlled
within a valid `*testing.T` function, however this is not possible for Provider verification.

But there is a way! Given an interaction that looks as follows (taken from the message examples):

```go
message := pact.AddMessage()
message.
Given("user with id 127 exists").
ExpectsToReceive("a user").
WithMetadata(commonHeaders).
WithContent(map[string]interface{}{
"id": like(127),
"name": "Baz",
"access": eachLike(map[string]interface{}{
"role": term("admin", "admin|controller|user"),
}, 3),
}).
AsType(&types.User{})
```

and the function used to run provider verification is `go test -run TestMessageProvider`, you can test the verification of this specific interaction by setting two environment variables `PACT_DESCRIPTION` and `PACT_PROVIDER_STATE` and re-running the command. For example:

```
cd examples/message/provider
PACT_DESCRIPTION="a user" PACT_PROVIDER_STATE="user with id 127 exists" go test -v .
```

## Contact

Join us in slack: [![slack](http://slack.pact.io/badge.svg)](http://slack.pact.io)
Expand All @@ -741,10 +794,6 @@ or

Additional documentation can be found at the main [Pact website](http://pact.io) and in the [Pact Wiki].

## Troubleshooting

See [TROUBLESHOOTING] for some helpful tips/tricks.

## Roadmap

The [roadmap](https://docs.pact.io/roadmap/) for Pact and Pact Go is outlined on our main website.
Expand Down
2 changes: 2 additions & 0 deletions client/verification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"log"
"os"
)

// VerificationService is a wrapper for the Pact Provider Verifier Service.
Expand All @@ -26,6 +27,7 @@ func (v *VerificationService) NewService(args []string) Service {

v.Args = args
v.Cmd = getVerifierCommandPath()
v.Env = append(os.Environ(), `PACT_INTERACTION_RERUN_COMMAND="To re-run this specific test, set the following environment variables and run your test again: PACT_DESCRIPTION=\"<PACT_DESCRIPTION>\" PACT_PROVIDER_STATE=\"<PACT_PROVIDER_STATE>\""`)

return v
}
Expand Down
2 changes: 1 addition & 1 deletion dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
p.Network = "tcp"
}

if !p.toolValidityCheck && !p.DisableToolValidityCheck {
if !p.toolValidityCheck && !(p.DisableToolValidityCheck || os.Getenv("PACT_DISABLE_TOOL_VALIDITY_CHECK") != "") {
checkCliCompatibility()
p.toolValidityCheck = true
}
Expand Down
9 changes: 7 additions & 2 deletions examples/consumer/goconsumer/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@ func TestMain(m *testing.M) {

// Enable when running E2E/integration tests before a release
if os.Getenv("PACT_INTEGRATED_TESTS") != "" {
var brokerHost = os.Getenv("PACT_BROKER_HOST")
brokerHost := os.Getenv("PACT_BROKER_HOST")
version := "1.0.0"
if os.Getenv("TRAVIS_BUILD_NUMBER") != "" {
version = "1.0." + os.Getenv("TRAVIS_BUILD_NUMBER")
}

// 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.1",
ConsumerVersion: version,
Tags: []string{"latest", "sit4"},
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
Expand Down

0 comments on commit d64e50b

Please sign in to comment.