Skip to content

Commit

Permalink
Merge pull request #23 from pact-foundation/feat/clean-ruby
Browse files Browse the repository at this point in the history
Filter out Ruby guff in Pact Verification failures
  • Loading branch information
mefellows authored Apr 30, 2017
2 parents 2b4a5e1 + d8931dc commit 5a0cdf9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
6 changes: 3 additions & 3 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestShutdownDaemon(t *testing.T) {
func connectToDaemon(port int, t *testing.T) {
for {
select {
case <-time.After(1 * time.Second):
case <-time.After(250 * time.Millisecond):
t.Fatalf("Expected server to start < 1s.")
case <-time.After(50 * time.Millisecond):
_, err := net.Dial("tcp", fmt.Sprintf(":%d", port))
Expand All @@ -104,7 +104,7 @@ func waitForDaemonToShutdown(port int, daemon *Daemon, t *testing.T) {
daemon.signalChan <- os.Interrupt
}
t.Logf("Waiting for deamon to shutdown before next test")
timeout := time.After(1 * time.Second)
timeout := time.After(250 * time.Millisecond)
for {
select {
case <-timeout:
Expand Down Expand Up @@ -474,7 +474,7 @@ func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
<-time.After(1 * time.Second)
<-time.After(250 * time.Millisecond)

// some code here to check arguments perhaps?
// Fail :(
Expand Down
1 change: 0 additions & 1 deletion daemon/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (s *ServiceManager) removeServiceMonitor() {
}
}

// Stop a Service and returns the exit status.
// Stop a Service and returns the exit status.
func (s *ServiceManager) Stop(pid int) (bool, error) {
log.Println("[DEBUG] stopping service with pid", pid)
Expand Down
18 changes: 17 additions & 1 deletion dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/rpc"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -127,12 +128,27 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) (string, error)
}

if res.ExitCode > 0 {
return "", fmt.Errorf("provider verification failed: %s", res.Message)
return "", fmt.Errorf("provider verification failed: %s", sanitiseRubyResponse(res.Message))
}

return res.Message, err
}

// sanitiseRubyResponse removes Ruby-isms from the response content
// making the output much more human readable
func sanitiseRubyResponse(response string) string {
r := regexp.MustCompile("(?m)^\\s*#.*$")
s := r.ReplaceAllString(response, "")

r = regexp.MustCompile("(?m).*bundle exec rake pact:verify.*$")
s = r.ReplaceAllString(s, "")

r = regexp.MustCompile("\\n+")
s = r.ReplaceAllString(s, "\n")

return s
}

// ListServers lists all running Pact Mock Servers.
func (p *PactClient) ListServers() *types.PactListResponse {
log.Println("[DEBUG] client: listing servers")
Expand Down
16 changes: 16 additions & 0 deletions dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,19 @@ func TestHelperProcess(t *testing.T) {
fmt.Fprintf(os.Stdout, "COMMAND: oh yays!\n")
os.Exit(0)
}

func Test_sanitiseRubyResponse(t *testing.T) {
var tests = map[string]string{
"this is a sentence with a hash # so it should be in tact": "this is a sentence with a hash # so it should be in tact",
"this is a sentence with a hash and newline\n#so it should not be in tact": "this is a sentence with a hash and newline",
"this is a sentence with a ruby statement bundle exec rake pact:verify so it should not be in tact": "",
"this is a sentence with a ruby statement\nbundle exec rake pact:verify so it should not be in tact": "this is a sentence with a ruby statement",
"this is a sentence with multiple new lines \n\n\n\n\nit should not be in tact": "this is a sentence with multiple new lines \nit should not be in tact",
}
for k, v := range tests {
test := sanitiseRubyResponse(k)
if !strings.EqualFold(strings.TrimSpace(test), strings.TrimSpace(v)) {
log.Fatalf("Got `%s', Expected `%s`", strings.TrimSpace(test), strings.TrimSpace(v))
}
}
}
4 changes: 2 additions & 2 deletions dsl/pact_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../pacts", dir)
var logDir = fmt.Sprintf("%s/../logs", dir)
var logDir = fmt.Sprintf("%s/../log", dir)

func TestPact_Integration(t *testing.T) {
// Enable when running E2E/integration tests before a release
Expand Down Expand Up @@ -202,7 +202,7 @@ func setupProviderAPI() int {
[
[
{
"size": 10,
"size": 10,
"colour": "red",
"tag": [
[
Expand Down
6 changes: 2 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ VERSION=$(go version)
echo "==> Go version ${VERSION}"

echo "==> Getting dependencies..."
export GO15VENDOREXPERIMENT=1

go get github.com/mitchellh/gox
go get github.com/inconshreveable/mousetrap # windows dep
go get -d ./...

echo "==> Creating binaries..."
gox -os="darwin" -arch="amd64" -output="build/pact-go_{{.OS}}_{{.Arch}}"
gox -os="windows" -arch="386" -output="build/pact-go_{{.OS}}_{{.Arch}}"
gox -os="linux" -arch="386" -output="build/pact-go_{{.OS}}_{{.Arch}}"
Expand Down
14 changes: 5 additions & 9 deletions scripts/pact.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash +e

############################################################
## Start and stop the Pact Mock Server daemon ##
Expand All @@ -12,15 +12,14 @@ CUR_DIR=$(pwd)
function shutdown() {
step "Shutting down stub server"
log "Finding Pact daemon PID"
PID=$(ps -ef | grep pact-go | awk -F" " '{print $2}' | head -n 1)
PID=$(ps -ef | grep "pact-go daemon"| grep -v grep | awk -F" " '{print $2}' | head -n 1)
if [ "${PID}" != "" ]; then
log "Killing ${PID}"
kill $PID
fi
cd $CUR_DIR
}

# mkdir -p bin
if [ ! -f "dist/pact-go" ]; then
cd dist
platform=$(detect_os)
Expand Down Expand Up @@ -49,24 +48,21 @@ if [ ! -f "dist/pact-go" ]; then
fi

step "Starting Daemon"
mkdir -p ./logs
./dist/pact-go daemon -v -l DEBUG > logs/daemon.log 2>&1 &
mkdir -p ./log
./dist/pact-go daemon -v -l DEBUG > log/daemon.log 2>&1 &

step "Running integration tests"
export PACT_INTEGRATED_TESTS=1
export PACT_BROKER_HOST="https://test.pact.dius.com.au"
export PACT_BROKER_USERNAME="dXfltyFMgNOFZAxr8io9wJ37iUpY42M"
export PACT_BROKER_PASSWORD="JN4kVfO5AIZWxelWbLvqMd8PkAVycBJh2Psyg11wtkubJC4xlOH5GmIfwO9gWe"
cd dsl
go test -run TestPact_Integration
go test -v -run TestPact_Integration
SCRIPT_STATUS=$?
cd ..

shutdown

# step "Stopping Metrics API"
# make stop

if [ "${SCRIPT_STATUS}" = "0" ]; then
step "Integration testing succeeded!"
else
Expand Down

0 comments on commit 5a0cdf9

Please sign in to comment.