Skip to content

Commit

Permalink
chore: tidy up verifier exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 21, 2021
1 parent 3f2c845 commit 16138a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
32 changes: 30 additions & 2 deletions v3/internal/native/verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int verify(char* s);
import "C"

import (
"fmt"
"log"
"strings"
"unsafe"
Expand All @@ -37,13 +38,29 @@ func (v *Verifier) Init() {
C.init(logLevel)
}

func (v *Verifier) Verify(args []string) int {
func (v *Verifier) Verify(args []string) error {
log.Println("[DEBUG] executing verifier FFI with args", args)
cargs := C.CString(strings.Join(args, "\n"))
defer freeString(cargs)
result := C.verify(cargs)

return int(result)
/// | Error | Description |
/// |-------|-------------|
/// | 1 | The verification process failed, see output for errors |
/// | 2 | A null pointer was received |
/// | 3 | The method panicked |
switch int(result) {
case 0:
return nil
case 1:
return ErrVerifierFailed
case 2:
return ErrInvalidVerifierConfig
case 3:
return ErrVerifierPanic
default:
return fmt.Errorf("an unknown error ocurred when verifying the provider (this indicates a defect in the framework")
}
}

func freeString(str *C.char) {
Expand All @@ -57,3 +74,14 @@ func free(p unsafe.Pointer) {
func libRustFree(str *C.char) {
C.free_string(str)
}

var (
// ErrVerifierPanic indicates a panic ocurred when invoking the verifier.
ErrVerifierPanic = fmt.Errorf("a general panic occured when starting/invoking verifier (this indicates a defect in the framework)")

// ErrInvalidVerifierConfig indicates an issue configuring the verifier
ErrInvalidVerifierConfig = fmt.Errorf("configuration for the verifier was invalid and an unknown error occurred (this is most likely a defect in the framework)")

//ErrVerifierFailed is the standard error if a verification failed (e.g. beacause the pact verification was not successful)
ErrVerifierFailed = fmt.Errorf("the verifier failed to successfully verify the pacts, this indicates an issue with the provider API")
)
42 changes: 22 additions & 20 deletions v3/internal/native/verifier/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package verifier
import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestVerifier_Version(t *testing.T) {
Expand All @@ -11,25 +13,25 @@ func TestVerifier_Version(t *testing.T) {
}

func TestVerifier_Verify(t *testing.T) {
v := Verifier{}
v.Init()
args := []string{
"--afile",
"/Users/matthewfellows/go/src/github.com/pact-foundation/pact-go/examples/v3/pacts/V3Consumer-V3Provider.json",
"--hostname",
"localhost",
"--port",
"55827",
"--state-change-url",
"http://localhost:55827/__setup/",
"--loglevel",
"trace",
}
res := v.Verify(args)
fmt.Println("result: ", res)
}
t.Run("invalid args returns an error", func(t *testing.T) {

v := Verifier{}
v.Init()
args := []string{
"--file",
"/non/existent/path.json",
"--hostname",
"localhost",
"--port",
"55827",
"--state-change-url",
"http://localhost:55827/__setup/",
"--loglevel",
"trace",
}

res := v.Verify(args)

func TestFoo(t *testing.T) {
s := `error: Found argument \'--afile\' which wasn\'t expected, or isn\'t valid in this context\n\tDid you mean \u{1b}[32m--\u{1b}[0m\u{1b}[32mfile\u{1b}[0m?\n\nUSAGE:\n pact_verifier_cli [FLAGS] [OPTIONS] --broker-url <broker-url>... --dir <dir>... --file <file>... --provider-name <provider-name> --url <url>...\n\nFor more information try --help`
fmt.Println(s)
assert.Error(t, res)
})
}

0 comments on commit 16138a7

Please sign in to comment.