Skip to content

Commit

Permalink
Fix error handling when the instrumented code does not compile
Browse files Browse the repository at this point in the history
In these cases, it is expected that the JSON data is not available,
and there's no reason to exit prematurely. The proper error handling
ensures that the -keep option properly reports the location of the
temporary files, without relying on the -verbose option.
  • Loading branch information
rillig committed Oct 7, 2023
1 parent 7718011 commit cd0952c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ func (g *gobco) runGoTest() {
}

func (g *gobco) printOutput() {
conds := g.load(g.statsFilename)
conds, err := g.load(g.statsFilename)
if err != nil && g.exitCode != 0 {
return // skip silently
}
if err != nil {
g.logger.errf("%s", err)
}

cnt := 0
for _, c := range conds {
Expand All @@ -322,7 +328,7 @@ func (g *gobco) printOutput() {
func (g *gobco) cleanUp() {
if g.keep {
g.errf("")
g.errf("the temporary files are in %s", g.tmpdir)
g.errf("gobco: the temporary files are in %s", g.tmpdir)
} else {
err := os.RemoveAll(g.tmpdir)
if err != nil {
Expand All @@ -331,9 +337,11 @@ func (g *gobco) cleanUp() {
}
}

func (g *gobco) load(filename string) []condition {
func (g *gobco) load(filename string) ([]condition, error) {
file, err := os.Open(filename)
g.check(err)
if err != nil {
return nil, err
}

defer func() {
closeErr := file.Close()
Expand All @@ -345,7 +353,7 @@ func (g *gobco) load(filename string) []condition {
decoder.DisallowUnknownFields()
g.check(decoder.Decode(&data))

return data
return data, nil
}

func (g *gobco) printCond(cond condition) {
Expand Down Expand Up @@ -412,7 +420,7 @@ func (t goTest) run(

err := goTest.Run()
if err != nil {
e.errf("%s", err)
e.errf("go test %s: %s", arg.arg, err)
return 1
} else {
e.verbosef("Finished %s", cmdline)
Expand Down

0 comments on commit cd0952c

Please sign in to comment.