Skip to content

Commit

Permalink
Add assertions for unexpected error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
rillig committed Jan 7, 2023
1 parent 47c2ab8 commit 43e3628
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 2 additions & 12 deletions instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,7 @@ func (i *instrumenter) replace(n ast.Node) bool {
// related to the instrumented condition. Especially for switch statements, the
// position may differ from the expression that is wrapped.
func (i *instrumenter) callCover(expr ast.Expr, pos token.Pos, code string) ast.Expr {
if !pos.IsValid() {
panic("pos must refer to the code from before instrumentation")
}
assert(pos.IsValid(), "pos must refer to the code from before instrumentation")

start := i.fset.Position(pos)
if !strings.HasSuffix(start.Filename, ".go") {
Expand Down Expand Up @@ -551,9 +549,7 @@ func (i *instrumenter) instrumentTestMain(astFile *ast.File) {
i.hasTestMain = true

ast.Inspect(decl.Body, wrapOsExit)
if !seenOsExit {
panic("gobco: can only handle TestMain with explicit call to os.Exit")
}
assert(seenOsExit, "can only handle TestMain with explicit call to os.Exit")
}
}
}
Expand Down Expand Up @@ -886,9 +882,3 @@ func shouldBuild(filename string) bool {
ok(err)
return m
}

func ok(err error) {
if err != nil {
panic(err)
}
}
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ func (g *gobco) parseArgs(args []string) {
args = []string{"."}
}

if len(args) > 1 {
panic("gobco: checking multiple packages doesn't work yet")
}
assert(len(args) <= 1, "checking multiple packages doesn't work yet")

for _, arg := range args {
arg = filepath.FromSlash(arg)
Expand Down
12 changes: 12 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ func (s *sliceFlag) Set(str string) error {
*s.values = append(*s.values, str)
return nil
}

func ok(err error) {
if err != nil {
panic(err)
}
}

func assert(cond bool, msg string) {
if !cond {
panic(msg)
}
}

0 comments on commit 43e3628

Please sign in to comment.