Skip to content

Commit 78c7d84

Browse files
committed
fix: return non-zero exit code on test failures
1 parent 97b8265 commit 78c7d84

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

cmd/test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
)
1010

1111
var (
12-
testCwd string
13-
testRunFilter string
14-
testFormat string
15-
testOutputPath string
16-
testCommit bool
17-
testNoRestore bool
18-
testForceRestore bool
12+
testCwd string
13+
testRunFilter string
14+
testFormat string
15+
testOutputPath string
16+
testCommit bool
17+
testNoRestore bool
18+
testForceRestore bool
19+
testFailOnSkipped bool
1920

2021
testCmd = &cobra.Command{
2122
Use: "test [flags]",
@@ -25,7 +26,7 @@ var (
2526
fmt.Print(err.Error())
2627
os.Exit(1)
2728
}
28-
regresql.Test(testCwd, testRunFilter, testFormat, testOutputPath, testCommit, testNoRestore, testForceRestore)
29+
regresql.Test(testCwd, testRunFilter, testFormat, testOutputPath, testCommit, testNoRestore, testForceRestore, testFailOnSkipped)
2930
},
3031
}
3132
)
@@ -40,4 +41,5 @@ func init() {
4041
testCmd.Flags().BoolVar(&testCommit, "commit", false, "Commit transactions instead of rollback (use with caution)")
4142
testCmd.Flags().BoolVar(&testNoRestore, "no-restore", false, "Skip snapshot restore before test")
4243
testCmd.Flags().BoolVar(&testForceRestore, "force-restore", false, "Force snapshot restore even if unchanged")
44+
testCmd.Flags().BoolVar(&testFailOnSkipped, "fail-on-skipped", false, "Exit with code 2 if skipped tests exist")
4345
}

regresql/regresql.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,16 @@ func validateServerSettings(cfg config, root string) error {
256256

257257
// Test runs regression tests for all queries.
258258
// Each query runs in its own transaction that rolls back (unless commit is true).
259-
func Test(root, runFilter, formatName, outputPath string, commit, noRestore, forceRestore bool) {
259+
//
260+
// Exit codes:
261+
//
262+
// 0 - all tests passed
263+
// 1 - test failures
264+
// 2 - skipped tests (if failOnSkipped)
265+
// 3 - config error
266+
// 13 - query execution error
267+
// 14 - invalid formatter
268+
func Test(root, runFilter, formatName, outputPath string, commit, noRestore, forceRestore, failOnSkipped bool) {
260269
config, err := ReadConfig(root)
261270
ignorePatterns := []string{}
262271
if err == nil {
@@ -314,10 +323,17 @@ func Test(root, runFilter, formatName, outputPath string, commit, noRestore, for
314323
os.Exit(14)
315324
}
316325

317-
if err := suite.testQueries(config.PgUri, formatter, outputPath, commit); err != nil {
326+
summary, err := suite.testQueries(config.PgUri, formatter, outputPath, commit)
327+
if err != nil {
318328
fmt.Print(err.Error())
319329
os.Exit(13)
320330
}
331+
if summary.Failed > 0 {
332+
os.Exit(1)
333+
}
334+
if failOnSkipped && summary.Skipped > 0 {
335+
os.Exit(2)
336+
}
321337
}
322338

323339
// List walks a repository, builds a Suite instance and pretty prints it.

regresql/suite.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,22 +289,23 @@ func (s *Suite) createExpectedResults(pguri string, commit bool) error {
289289
// necessary. It then compares the actual output to the expected output and
290290
// reports results using the specified formatter.
291291
// Each query runs in its own transaction that rolls back (unless commit is true).
292-
func (s *Suite) testQueries(pguri string, formatter OutputFormatter, outputPath string, commit bool) error {
292+
// Returns the test summary for exit code determination.
293+
func (s *Suite) testQueries(pguri string, formatter OutputFormatter, outputPath string, commit bool) (*TestSummary, error) {
293294
db, err := sql.Open("pgx", pguri)
294295
if err != nil {
295-
return fmt.Errorf("Failed to connect to '%s': %s\n", pguri, err)
296+
return nil, fmt.Errorf("Failed to connect to '%s': %s\n", pguri, err)
296297
}
297298
defer db.Close()
298299

299300
w, close, err := getWriter(outputPath)
300301
if err != nil {
301-
return err
302+
return nil, err
302303
}
303304
defer close()
304305

305306
summary := NewTestSummary()
306307
if err := formatter.Start(w); err != nil {
307-
return err
308+
return nil, err
308309
}
309310

310311
for _, folder := range s.Dirs {
@@ -319,7 +320,7 @@ func (s *Suite) testQueries(pguri string, formatter OutputFormatter, outputPath
319320

320321
queries, err := parseQueryFile(qfile)
321322
if err != nil {
322-
return err
323+
return nil, err
323324
}
324325

325326
for _, q := range queries {
@@ -334,7 +335,7 @@ func (s *Suite) testQueries(pguri string, formatter OutputFormatter, outputPath
334335

335336
p, err := q.GetPlan(rdir)
336337
if err != nil {
337-
return err
338+
return nil, err
338339
}
339340

340341
if err := s.runInTransaction(db, commit, func(tx *sql.Tx) error {
@@ -362,13 +363,16 @@ func (s *Suite) testQueries(pguri string, formatter OutputFormatter, outputPath
362363
}
363364
return nil
364365
}); err != nil {
365-
return err
366+
return nil, err
366367
}
367368
}
368369
}
369370
}
370371

371-
return formatter.Finish(summary, w)
372+
if err := formatter.Finish(summary, w); err != nil {
373+
return nil, err
374+
}
375+
return summary, nil
372376
}
373377

374378
// runInTransaction executes fn within a transaction, rolling back on error or if commit is false

0 commit comments

Comments
 (0)