Skip to content

Commit

Permalink
Make writing of a file independent of the instrumenter
Browse files Browse the repository at this point in the history
  • Loading branch information
rillig committed Oct 7, 2023
1 parent 62959fc commit 5fd02e5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (i *instrumenter) instrumentFile(filename string, astFile *ast.File, dstDir

var out strings.Builder
ok(printer.Fprint(&out, i.fset, astFile))
i.writeFile(filepath.Join(dstDir, filepath.Base(filename)), out.String())
writeFile(filepath.Join(dstDir, filepath.Base(filename)), out.String())
}

func (i *instrumenter) instrumentFileNode(f *ast.File) {
Expand Down Expand Up @@ -580,11 +580,11 @@ func (i *instrumenter) writeGobcoFiles(tmpDir string, pkgs []*ast.Package) {
str = strings.TrimPrefix(str, "//go:build ignore\n// +build ignore\n\n")
return strings.Replace(str, "package main\n", "package "+pkgname+"\n", 1)
}
i.writeFile(filepath.Join(tmpDir, "gobco_fixed.go"), fixPkgname(fixedTemplate))
writeFile(filepath.Join(tmpDir, "gobco_fixed.go"), fixPkgname(fixedTemplate))
i.writeGobcoGo(filepath.Join(tmpDir, "gobco_variable.go"), pkgname)

if !i.hasTestMain {
i.writeFile(filepath.Join(tmpDir, "gobco_no_testmain_test.go"), fixPkgname(noTestMainTemplate))
writeFile(filepath.Join(tmpDir, "gobco_no_testmain_test.go"), fixPkgname(noTestMainTemplate))
}

i.writeGobcoBlackBox(pkgs, tmpDir)
Expand All @@ -609,7 +609,7 @@ func (i *instrumenter) writeGobcoGo(filename, pkgname string) {
sb.WriteString("\t},\n")
sb.WriteString("}\n")

i.writeFile(filename, sb.String())
writeFile(filename, sb.String())
}

// writeGobcoBlackBox makes the function 'GobcoCover' available
Expand Down Expand Up @@ -651,11 +651,7 @@ func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, dstDir string) {
"\t" + "return " + pkgName + ".GobcoCover(idx, cond)\n" +
"}\n"

i.writeFile(filepath.Join(dstDir, "gobco_bridge_test.go"), text)
}

func (i *instrumenter) writeFile(filename string, content string) {
ok(os.WriteFile(filename, []byte(content), 0o666))
writeFile(filepath.Join(dstDir, "gobco_bridge_test.go"), text)
}

func (i *instrumenter) str(expr ast.Expr) string {
Expand Down Expand Up @@ -902,3 +898,7 @@ func shouldBuild(filename string) bool {
ok(err)
return m
}

func writeFile(filename string, content string) {
ok(os.WriteFile(filename, []byte(content), 0o666))
}

0 comments on commit 5fd02e5

Please sign in to comment.