Skip to content

Commit ef90c5e

Browse files
Fix: Make tokengen tests platform-agnostic by avoiding hardcoded path separators and exact error string matches
Signed-off-by: SurbhiAgarwal1 <agarwalsurbhi1807@gmail.com>
1 parent 0d751f7 commit ef90c5e

2 files changed

Lines changed: 44 additions & 17 deletions

File tree

cmd/tokengen/main_test.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,42 +250,54 @@ func TestGenFailure(t *testing.T) {
250250
defer gexec.CleanupBuildArtifacts()
251251

252252
type T struct {
253-
Args []string
254-
ErrMsg string
253+
Args []string
254+
ErrMsgs []string
255255
}
256256
var tests []T
257-
for _, driver := range []string{fabtokenv1.DriverIdentifier} {
257+
for _, driverIdentifier := range []string{fabtokenv1.DriverIdentifier} {
258258
tests = append(tests, []T{
259259
{
260260
Args: []string{
261261
"gen",
262-
driver,
262+
driverIdentifier,
263263
"--auditors", "a,b"},
264-
ErrMsg: "Error: failed to generate public parameters: failed to get auditor identity [a]",
264+
ErrMsgs: []string{"failed to generate public parameters", "failed to get auditor identity [a]"},
265265
},
266266
{
267267
Args: []string{
268268
"gen",
269-
driver,
269+
driverIdentifier,
270270
"--auditors", "aOrg1MSP,b",
271271
},
272-
ErrMsg: "Error: failed to generate public parameters: failed to get auditor identity [aOrg1MSP]: failed to load certificates from aOrg1MSP/signcerts: stat aOrg1MSP/signcerts: no such file or directory",
272+
ErrMsgs: []string{
273+
"failed to generate public parameters",
274+
"failed to get auditor identity [aOrg1MSP]",
275+
"failed to load certificates from",
276+
"aOrg1MSP",
277+
"signcerts",
278+
},
273279
},
274280
{
275281
Args: []string{
276282
"gen",
277-
driver,
283+
driverIdentifier,
278284
"--issuers", "a,b",
279285
},
280-
ErrMsg: "Error: failed to generate public parameters: failed to get issuer identity [a]",
286+
ErrMsgs: []string{"failed to generate public parameters", "failed to get issuer identity [a]"},
281287
},
282288
{
283289
Args: []string{
284290
"gen",
285-
driver,
291+
driverIdentifier,
286292
"--issuers", "aOrg1MSP,b",
287293
},
288-
ErrMsg: "Error: failed to generate public parameters: failed to get issuer identity [aOrg1MSP]: failed to load certificates from aOrg1MSP/signcerts: stat aOrg1MSP/signcerts: no such file or directory",
294+
ErrMsgs: []string{
295+
"failed to generate public parameters",
296+
"failed to get issuer identity [aOrg1MSP]",
297+
"failed to load certificates from",
298+
"aOrg1MSP",
299+
"signcerts",
300+
},
289301
},
290302
}...,
291303
)
@@ -297,7 +309,13 @@ func TestGenFailure(t *testing.T) {
297309
zkatdlognoghv1.DriverIdentifier,
298310
"--issuers", "aOrg1MSP,b",
299311
},
300-
ErrMsg: "failed to generate public parameters: failed to load issuer public key: failed reading idemix issuer public key [msp/IssuerPublicKey]: open msp/IssuerPublicKey: no such file or directory",
312+
ErrMsgs: []string{
313+
"failed to generate public parameters",
314+
"failed to load issuer public key",
315+
"failed reading idemix issuer public key",
316+
"msp",
317+
"IssuerPublicKey",
318+
},
301319
},
302320
{
303321
Args: []string{
@@ -306,14 +324,21 @@ func TestGenFailure(t *testing.T) {
306324
"--idemix", "./testdata/idemix",
307325
"--issuers", "Error: failed to generate public parameters: failed to get issuer identity [aOrg1MSP]: invalid input [aOrg1MSP]",
308326
},
309-
ErrMsg: "Error: failed to generate public parameters: failed to setup issuer and auditors: failed to get issuer identity [Error: failed to generate public parameters: failed to get issuer identity [aOrg1MSP]: invalid input [aOrg1MSP]]: failed to load certificates from Error: failed to generate public parameters: failed to get issuer identity [aOrg1MSP]: invalid input [aOrg1MSP]/signcerts: stat Error: failed to generate public parameters: failed to get issuer identity [aOrg1MSP]: invalid input [aOrg1MSP]/signcerts: no such file or directory",
327+
ErrMsgs: []string{
328+
"failed to generate public parameters",
329+
"failed to setup issuer and auditors",
330+
"failed to get issuer identity",
331+
"failed to load certificates from",
332+
"aOrg1MSP",
333+
"signcerts",
334+
},
310335
},
311336
}...,
312337
)
313338

314339
for i, test := range tests {
315340
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
316-
testGenRunWithError(gt, tokengen, test.Args, test.ErrMsg)
341+
testGenRunWithError(gt, tokengen, test.Args, test.ErrMsgs...)
317342
})
318343
}
319344
}
@@ -360,10 +385,12 @@ func validateOutputEquivalent(
360385
}
361386

362387
// testGenRunWithError runs the tokengen command and expects an error.
363-
func testGenRunWithError(gt *WithT, tokengen string, args []string, errMsg string) {
388+
func testGenRunWithError(gt *WithT, tokengen string, args []string, errMsgs ...string) {
364389
b, err := exec.Command(tokengen, args...).CombinedOutput()
365390
gt.Expect(err).To(HaveOccurred())
366-
gt.Expect(string(b)).To(ContainSubstring(errMsg))
391+
for _, errMsg := range errMsgs {
392+
gt.Expect(string(b)).To(ContainSubstring(errMsg))
393+
}
367394
}
368395

369396
// testGenRun runs the tokengen command and expects no error.

0 commit comments

Comments
 (0)