• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pact-foundation / pact-go / 9770864205

03 Jul 2024 02:18AM UTC coverage: 29.365% (+0.2%) from 29.134%
9770864205

Pull #430

github

YOU54F
fix: move installing of signal handlers, after plugins have been started
Pull Request #430: chore(ci): test MacOS & Docker images in CI

28 of 54 new or added lines in 9 files covered. (51.85%)

1 existing line in 1 file now uncovered.

1872 of 6375 relevant lines covered (29.36%)

12.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.22
/internal/native/verifier.go
1
package native
2

3
/*
4
#include "pact.h"
5
*/
6
import "C"
7

8
import (
9
        "fmt"
10
        "log"
11
        "strings"
12
        "unsafe"
13
)
14

15
type Verifier struct {
16
        handle *C.VerifierHandle
17
}
18

19
func (v *Verifier) Verify(args []string) error {
9✔
20
        log.Println("[DEBUG] executing verifier FFI with args", args)
9✔
21
        cargs := C.CString(strings.Join(args, "\n"))
9✔
22
        defer free(cargs)
9✔
23

9✔
24
        InstallSignalHandlers()
9✔
25
        result := C.pactffi_verify(cargs)
9✔
26

9✔
27
        /// | Error | Description |
9✔
28
        /// |-------|-------------|
9✔
29
        /// | 1 | The verification process failed, see output for errors |
9✔
30
        /// | 2 | A null pointer was received |
9✔
31
        /// | 3 | The method panicked |
9✔
32
        switch int(result) {
9✔
33
        case 0:
×
34
                return nil
×
35
        case 1:
×
36
                return ErrVerifierFailed
×
37
        case 2:
×
38
                return ErrInvalidVerifierConfig
×
39
        case 3:
×
40
                return ErrVerifierPanic
×
41
        default:
9✔
42
                return fmt.Errorf("an unknown error (%d) ocurred when verifying the provider (this indicates a defect in the framework)", int(result))
9✔
43
        }
44
}
45

46
// Version returns the current semver FFI interface version
47
func (v *Verifier) Version() string {
×
48
        return Version()
×
49
}
×
50

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

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

58
        //ErrVerifierFailed is the standard error if a verification failed (e.g. beacause the pact verification was not successful)
59
        ErrVerifierFailed = fmt.Errorf("the verifier failed to successfully verify the pacts, this indicates an issue with the provider API")
60
        //ErrVerifierFailedToRun indicates the verification process was unable to run
61
        ErrVerifierFailedToRun = fmt.Errorf("the verifier failed to execute (this is most likely a defect in the framework)")
62
)
63

64
func NewVerifier(name string, version string) *Verifier {
45✔
65
        cName := C.CString(name)
45✔
66
        cVersion := C.CString(version)
45✔
67
        defer free(cName)
45✔
68
        defer free(cVersion)
45✔
69

45✔
70
        h := C.pactffi_verifier_new_for_application(cName, cVersion)
45✔
71

45✔
72
        return &Verifier{
45✔
73
                handle: h,
45✔
74
        }
45✔
75
}
45✔
76

77
func (v *Verifier) Shutdown() {
9✔
78
        C.pactffi_verifier_shutdown(v.handle)
9✔
79
}
9✔
80

81
func (v *Verifier) SetProviderInfo(name string, scheme string, host string, port uint16, path string) {
9✔
82
        cName := C.CString(name)
9✔
83
        defer free(cName)
9✔
84
        cScheme := C.CString(scheme)
9✔
85
        defer free(cScheme)
9✔
86
        cHost := C.CString(host)
9✔
87
        defer free(cHost)
9✔
88
        cPort := C.ushort(port)
9✔
89
        cPath := C.CString(path)
9✔
90
        defer free(cPath)
9✔
91

9✔
92
        C.pactffi_verifier_set_provider_info(v.handle, cName, cScheme, cHost, cPort, cPath)
9✔
93
}
9✔
94

95
func (v *Verifier) AddTransport(protocol string, port uint16, path string, scheme string) {
×
96
        log.Println("[DEBUG] Adding transport with protocol:", protocol, "port:", port, "path:", path, "scheme:", scheme)
×
97
        cProtocol := C.CString(protocol)
×
98
        defer free(cProtocol)
×
NEW
99
        cPort := C.ushort(port)
×
100
        cPath := C.CString(path)
×
101
        defer free(cPath)
×
102
        cScheme := C.CString(scheme)
×
103
        defer free(cScheme)
×
104

×
105
        C.pactffi_verifier_add_provider_transport(v.handle, cProtocol, cPort, cPath, cScheme)
×
106
}
×
107

108
func (v *Verifier) SetFilterInfo(description string, state string, noState bool) {
×
109
        cFilterDescription := C.CString(description)
×
110
        defer free(cFilterDescription)
×
111
        cFilterState := C.CString(state)
×
112
        defer free(cFilterState)
×
113

×
114
        C.pactffi_verifier_set_filter_info(v.handle, cFilterDescription, cFilterState, boolToCInt(noState))
×
115
}
×
116

117
func (v *Verifier) SetProviderState(url string, teardown bool, body bool) {
×
118
        cURL := C.CString(url)
×
119
        defer free(cURL)
×
120

×
121
        C.pactffi_verifier_set_provider_state(v.handle, cURL, boolToCInt(teardown), boolToCInt(body))
×
122
}
×
123

124
func (v *Verifier) SetVerificationOptions(disableSSLVerification bool, requestTimeout int64) {
×
125
        // TODO: this returns an int and therefore can error. We should have all of these functions return values??
×
126
        C.pactffi_verifier_set_verification_options(v.handle, boolToCInt(disableSSLVerification), C.ulong(requestTimeout))
×
127
}
×
128

129
func (v *Verifier) SetConsumerFilters(consumers []string) {
9✔
130
        // TODO: check if this actually works!
9✔
131
        C.pactffi_verifier_set_consumer_filters(v.handle, stringArrayToCStringArray(consumers), C.ushort(len(consumers)))
9✔
132
}
9✔
133

134
func (v *Verifier) AddCustomHeader(name string, value string) {
×
135
        cHeaderName := C.CString(name)
×
136
        defer free(cHeaderName)
×
137
        cHeaderValue := C.CString(value)
×
138
        defer free(cHeaderValue)
×
139

×
140
        C.pactffi_verifier_add_custom_header(v.handle, cHeaderName, cHeaderValue)
×
141
}
×
142

143
func (v *Verifier) AddFileSource(file string) {
×
144
        cFile := C.CString(file)
×
145
        defer free(cFile)
×
146

×
147
        C.pactffi_verifier_add_file_source(v.handle, cFile)
×
148
}
×
149

150
func (v *Verifier) AddDirectorySource(directory string) {
×
151
        cDirectory := C.CString(directory)
×
152
        defer free(cDirectory)
×
153

×
154
        C.pactffi_verifier_add_directory_source(v.handle, cDirectory)
×
155
}
×
156

157
func (v *Verifier) AddURLSource(url string, username string, password string, token string) {
×
158
        cUrl := C.CString(url)
×
159
        defer free(cUrl)
×
160
        cUsername := C.CString(username)
×
161
        defer free(cUsername)
×
162
        cPassword := C.CString(password)
×
163
        defer free(cPassword)
×
164
        cToken := C.CString(token)
×
165
        defer free(cToken)
×
166

×
167
        C.pactffi_verifier_url_source(v.handle, cUrl, cUsername, cPassword, cToken)
×
168
}
×
169

170
func (v *Verifier) BrokerSourceWithSelectors(url string, username string, password string, token string, enablePending bool, includeWipPactsSince string, providerTags []string, providerBranch string, selectors []string, consumerVersionTags []string) {
×
171
        cUrl := C.CString(url)
×
172
        defer free(cUrl)
×
173
        cUsername := C.CString(username)
×
174
        defer free(cUsername)
×
175
        cPassword := C.CString(password)
×
176
        defer free(cPassword)
×
177
        cToken := C.CString(token)
×
178
        defer free(cToken)
×
179
        cIncludeWipPactsSince := C.CString(includeWipPactsSince)
×
180
        defer free(cIncludeWipPactsSince)
×
181
        cProviderBranch := C.CString(providerBranch)
×
182
        defer free(cProviderBranch)
×
183

×
NEW
184
        C.pactffi_verifier_broker_source_with_selectors(v.handle, cUrl, cUsername, cPassword, cToken, boolToCInt(enablePending), cIncludeWipPactsSince, stringArrayToCStringArray(providerTags), C.ushort(len(providerTags)), cProviderBranch, stringArrayToCStringArray(selectors), C.ushort(len(selectors)), stringArrayToCStringArray(consumerVersionTags), C.ushort(len(consumerVersionTags)))
×
185
}
×
186

187
func (v *Verifier) SetPublishOptions(providerVersion string, buildUrl string, providerTags []string, providerBranch string) {
×
188
        cProviderVersion := C.CString(providerVersion)
×
189
        defer free(cProviderVersion)
×
190
        cBuildUrl := C.CString(buildUrl)
×
191
        defer free(cBuildUrl)
×
192
        cProviderBranch := C.CString(providerBranch)
×
193
        defer free(cProviderBranch)
×
194

×
NEW
195
        C.pactffi_verifier_set_publish_options(v.handle, cProviderVersion, cBuildUrl, stringArrayToCStringArray(providerTags), C.ushort(len(providerTags)), cProviderBranch)
×
196
}
×
197

198
func (v *Verifier) Execute() error {
9✔
199
        // TODO: Validate
9✔
200
        InstallSignalHandlers()
9✔
201
        result := C.pactffi_verifier_execute(v.handle)
9✔
202

9✔
203
        /// | Error | Description |
9✔
204
        /// |-------|-------------|
9✔
205
        /// | 1     | The verification process failed, see output for errors |
9✔
206
        switch int(result) {
9✔
207
        case 0:
9✔
208
                return nil
9✔
209
        case 1:
×
210
                return ErrVerifierFailed
×
211
        case 2:
×
212
                return ErrVerifierFailedToRun
×
213
        default:
×
214
                return fmt.Errorf("an unknown error (%d) ocurred when verifying the provider (this indicates a defect in the framework)", int(result))
×
215
        }
216
}
217

218
func (v *Verifier) SetNoPactsIsError(isError bool) {
×
219
        C.pactffi_verifier_set_no_pacts_is_error(v.handle, boolToCInt(isError))
×
220
}
×
221

222
func (v *Verifier) SetColoredOutput(isColoredOutput bool) {
×
223
        C.pactffi_verifier_set_coloured_output(v.handle, boolToCInt(isColoredOutput))
×
224
}
×
225

226
func stringArrayToCStringArray(inputs []string) **C.char {
9✔
227
        if len(inputs) == 0 {
9✔
228
                return nil
×
229
        }
×
230

231
        output := make([]*C.char, len(inputs))
9✔
232

9✔
233
        for i, consumer := range inputs {
27✔
234
                output[i] = C.CString(consumer)
18✔
235
        }
18✔
236

237
        return (**C.char)(unsafe.Pointer(&output[0]))
9✔
238
}
239

NEW
240
func boolToCInt(val bool) C.uchar {
×
241
        if val {
×
NEW
242
                return C.uchar(1)
×
243
        }
×
NEW
244
        return C.uchar(0)
×
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc