@@ -10,6 +10,7 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"path/filepath"
13
+ "regexp"
13
14
"strings"
14
15
"text/scanner"
15
16
@@ -62,9 +63,16 @@ type TestImportCase struct {
62
63
ImportName string
63
64
64
65
// A string containing any expected runtime error during evaluation. If
65
- // this field is non-empty, a runtime error is expected to occur, and the
66
- // Sentinel output is searched for the string given here. If a match is
67
- // found, the test passes. If it is not found the test will fail.
66
+ // this field is non-empty, a runtime error is expected to occur, and
67
+ // the Sentinel output is searched for the string given here. If the
68
+ // output contains the string, the test passes. If it does not contain
69
+ // the string, the test will fail.
70
+ //
71
+ // More advanced matches can be done with regular expression patterns.
72
+ // If the Error string is delimited by slashes (/), the string is
73
+ // compiled as a regular expression and the Sentinel output is matched
74
+ // against the resulting pattern. If a match is found, the test passes.
75
+ // If it does not match, the tests will fail.
68
76
Error string
69
77
}
70
78
@@ -187,7 +195,7 @@ func TestImportDir(t testing.T, path string, customize func(*TestImportCase)) {
187
195
// support a t.Run(), and adding context about which policy is failing
188
196
// to the error is obtuse otherwise, so we'll just log the policy file
189
197
// name here to give that context to the developer.
190
- t .Logf ("Checking %s" , file )
198
+ t .Logf ("Checking %s ... " , file )
191
199
TestImport (t , tc )
192
200
}
193
201
}
@@ -269,9 +277,21 @@ func TestImport(t testing.T, c TestImportCase) {
269
277
output , err := cmd .CombinedOutput ()
270
278
if err != nil {
271
279
if c .Error != "" {
272
- if ! strings .Contains (string (output ), c .Error ) {
273
- t .Fatalf ("expected error %q not found:\n \n %s" ,
274
- c .Error , string (output ))
280
+ if c .Error [:1 ]+ c .Error [len (c .Error )- 1 :] == "//" {
281
+ pattern := c .Error [1 : len (c .Error )- 1 ]
282
+ exp , err := regexp .Compile (pattern )
283
+ if err != nil {
284
+ t .Fatalf ("error compiling expected error pattern: %s" , err )
285
+ }
286
+ if ! exp .Match (output ) {
287
+ t .Fatalf ("the resulting error does not match the expected pattern: %s\n \n Error output:\n \n %s" ,
288
+ c .Error , string (output ))
289
+ }
290
+ } else {
291
+ if ! strings .Contains (string (output ), c .Error ) {
292
+ t .Fatalf ("resulting error does not contain %q\n \n Error output:\n \n %s" ,
293
+ c .Error , string (output ))
294
+ }
275
295
}
276
296
} else {
277
297
t .Fatalf ("error executing test. output:\n \n %s" , string (output ))
0 commit comments