From 8819608ceb188d0b07c6dc7e3924128293face21 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sun, 30 Apr 2017 14:25:55 +1000 Subject: [PATCH] feat(ruby): clean ruby stack trace in pact provider verification failures #19 --- dsl/client.go | 18 +++++++++++++++++- dsl/client_test.go | 16 ++++++++++++++++ dsl/pact_integration_test.go | 4 ++-- scripts/pact.sh | 6 +++--- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/dsl/client.go b/dsl/client.go index d14fb31bb..9655729eb 100644 --- a/dsl/client.go +++ b/dsl/client.go @@ -6,6 +6,7 @@ import ( "net" "net/rpc" "net/url" + "regexp" "strconv" "strings" "time" @@ -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") diff --git a/dsl/client_test.go b/dsl/client_test.go index 8b7c1a760..3e63d960c 100644 --- a/dsl/client_test.go +++ b/dsl/client_test.go @@ -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)) + } + } +} diff --git a/dsl/pact_integration_test.go b/dsl/pact_integration_test.go index 21c77fa16..287bd91de 100644 --- a/dsl/pact_integration_test.go +++ b/dsl/pact_integration_test.go @@ -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 @@ -202,7 +202,7 @@ func setupProviderAPI() int { [ [ { - "size": 10, + "size": 10, "colour": "red", "tag": [ [ diff --git a/scripts/pact.sh b/scripts/pact.sh index a913facbb..dc1eb3c0d 100755 --- a/scripts/pact.sh +++ b/scripts/pact.sh @@ -48,8 +48,8 @@ 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 @@ -57,7 +57,7 @@ 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 ..