From 87edaf0868cb5968f2e91a8b5c1ca18c917f75f4 Mon Sep 17 00:00:00 2001 From: 1st-vil Date: Tue, 20 Feb 2024 17:11:08 +0900 Subject: [PATCH 1/5] duplicate findInModule as a function --- main.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index d03e634..2f415da 100644 --- a/main.go +++ b/main.go @@ -205,27 +205,38 @@ func (g *gobco) gopaths() string { return filepath.Join(home, "go") } -func (g *gobco) findInModule(dir string) (moduleRoot, moduleRel string) { - absDir, err := filepath.Abs(dir) +func (g *gobco) findInModule(dir string) (string, string) { + moduleRoot, moduleRel, err := findInModule(dir) g.check(err) + return moduleRoot, moduleRel +} + +// findInModule finds path of moduleRoot and relative path from the moduleRoot to dir +func findInModule(dir string) (moduleRoot, moduleRel string, err error) { + absDir, err := filepath.Abs(dir) + if err != nil { + return "", "", err + } abs := absDir for { if _, err := os.Lstat(filepath.Join(abs, "go.mod")); err == nil { rel, err := filepath.Rel(abs, absDir) - g.check(err) + if err != nil { + return "", "", err + } root := abs if rel == "." { root = dir } - return root, rel + return root, rel, nil } parent := filepath.Dir(abs) if parent == abs { - return "", "" + return "", "", nil } abs = parent } From 7e38cb2d04ac7ebf3511754b21a5fb553f7401dc Mon Sep 17 00:00:00 2001 From: 1st-vil Date: Tue, 20 Feb 2024 17:12:26 +0900 Subject: [PATCH 2/5] implement findPackagePath --- go.mod | 2 ++ go.sum | 44 ++++++++++++++++++++++++++++++++++++++++++++ instrumenter.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/go.mod b/go.mod index e9aee40..5a0445c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/rillig/gobco go 1.16 + +require golang.org/x/mod v0.15.0 diff --git a/go.sum b/go.sum index e69de29..f612015 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,44 @@ +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/instrumenter.go b/instrumenter.go index fff6a27..2ac230c 100644 --- a/instrumenter.go +++ b/instrumenter.go @@ -15,6 +15,8 @@ import ( "sort" "strconv" "strings" + + "golang.org/x/mod/modfile" ) // cond is a condition from the code that is instrumented. @@ -99,6 +101,37 @@ func (i *instrumenter) instrument(srcDir, singleFile, dstDir string) bool { return true } +// findPackagePath finds import path of a package that srcDir indicates +func findPackagePath(srcDir string) (string, error) { + moduleRoot, moduleRel, err := findInModule(srcDir) + if err != nil { + return "", err + } + + // Read the content of the go.mod file + modFilePath := filepath.Join(moduleRoot, "go.mod") + modFileContent, err := os.ReadFile(modFilePath) + if err != nil { + return "", err + } + + // Parse the content of the go.mod file + modFile, err := modfile.Parse("", modFileContent, nil) + if err != nil { + return "", err + } + + // Get the module name from the parsed go.mod file + moduleName := modFile.Module.Mod.Path + + if moduleRel == "." { + return moduleName, nil + } else { + pkgPath := fmt.Sprintf("%s/%s", moduleName, moduleRel) + return pkgPath, nil + } +} + func (i *instrumenter) instrumentFile(filename string, astFile *ast.File, dstDir string) { isTest := strings.HasSuffix(filename, "_test.go") if (i.coverTest || !isTest) && shouldBuild(filename) { From d7e323d13f0f208f1278ec3ff9a347ffb443898a Mon Sep 17 00:00:00 2001 From: 1st-vil Date: Tue, 20 Feb 2024 17:14:04 +0900 Subject: [PATCH 3/5] modify a way to get package import path --- instrumenter.go | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/instrumenter.go b/instrumenter.go index 2ac230c..2981e30 100644 --- a/instrumenter.go +++ b/instrumenter.go @@ -13,7 +13,6 @@ import ( "reflect" "runtime" "sort" - "strconv" "strings" "golang.org/x/mod/modfile" @@ -97,7 +96,11 @@ func (i *instrumenter) instrument(srcDir, singleFile, dstDir string) bool { i.instrumentFile(name, file, dstDir) }) } - i.writeGobcoFiles(dstDir, pkgs) + + pkgPath, err := findPackagePath(srcDir) + ok(err) + + i.writeGobcoFiles(dstDir, pkgPath, pkgs) return true } @@ -607,7 +610,7 @@ var fixedTemplate string //go:embed templates/gobco_no_testmain_test.go var noTestMainTemplate string -func (i *instrumenter) writeGobcoFiles(tmpDir string, pkgs []*ast.Package) { +func (i *instrumenter) writeGobcoFiles(tmpDir, pkgPath string, pkgs []*ast.Package) { pkgname := pkgs[0].Name fixPkgname := func(str string) string { str = strings.TrimPrefix(str, "//go:build ignore\n// +build ignore\n\n") @@ -620,7 +623,7 @@ func (i *instrumenter) writeGobcoFiles(tmpDir string, pkgs []*ast.Package) { writeFile(filepath.Join(tmpDir, "gobco_no_testmain_test.go"), fixPkgname(noTestMainTemplate)) } - i.writeGobcoBlackBox(pkgs, tmpDir) + i.writeGobcoBlackBox(pkgs, tmpDir, pkgPath) } func (i *instrumenter) writeGobcoGo(filename, pkgname string) { @@ -648,32 +651,12 @@ func (i *instrumenter) writeGobcoGo(filename, pkgname string) { // writeGobcoBlackBox makes the function 'GobcoCover' available // to black box tests (those in 'package x_test' instead of 'package x') // by delegating to the function of the same name in the main package. -func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, dstDir string) { +func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, dstDir, pkgPath string) { if len(pkgs) < 2 { return } - // Copy the 'import' directive from one of the existing files. - pkgName, pkgPath := "", "" - for _, pkg := range pkgs { - forEachFile(pkg, func(name string, file *ast.File) { - for _, imp := range file.Imports { - var impName string - p, err := strconv.Unquote(imp.Path.Value) - ok(err) - if imp.Name != nil { - impName = imp.Name.Name - } else { - impName = filepath.Base(p) - } - - if impName == pkgs[0].Name { - pkgName = impName - pkgPath = p - } - } - }) - } + pkgName := filepath.Base(pkgPath) text := "" + "package " + pkgs[0].Name + "_test\n" + From 506b5db885c4ce64920489ec27693d5b2172e012 Mon Sep 17 00:00:00 2001 From: 1st-vil Date: Wed, 21 Feb 2024 10:28:42 +0900 Subject: [PATCH 4/5] call findPackagePath in writeGobcoBlackBox --- instrumenter.go | 75 ++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/instrumenter.go b/instrumenter.go index 2981e30..1ff44de 100644 --- a/instrumenter.go +++ b/instrumenter.go @@ -97,44 +97,10 @@ func (i *instrumenter) instrument(srcDir, singleFile, dstDir string) bool { }) } - pkgPath, err := findPackagePath(srcDir) - ok(err) - - i.writeGobcoFiles(dstDir, pkgPath, pkgs) + i.writeGobcoFiles(srcDir, dstDir, pkgs) return true } -// findPackagePath finds import path of a package that srcDir indicates -func findPackagePath(srcDir string) (string, error) { - moduleRoot, moduleRel, err := findInModule(srcDir) - if err != nil { - return "", err - } - - // Read the content of the go.mod file - modFilePath := filepath.Join(moduleRoot, "go.mod") - modFileContent, err := os.ReadFile(modFilePath) - if err != nil { - return "", err - } - - // Parse the content of the go.mod file - modFile, err := modfile.Parse("", modFileContent, nil) - if err != nil { - return "", err - } - - // Get the module name from the parsed go.mod file - moduleName := modFile.Module.Mod.Path - - if moduleRel == "." { - return moduleName, nil - } else { - pkgPath := fmt.Sprintf("%s/%s", moduleName, moduleRel) - return pkgPath, nil - } -} - func (i *instrumenter) instrumentFile(filename string, astFile *ast.File, dstDir string) { isTest := strings.HasSuffix(filename, "_test.go") if (i.coverTest || !isTest) && shouldBuild(filename) { @@ -610,7 +576,7 @@ var fixedTemplate string //go:embed templates/gobco_no_testmain_test.go var noTestMainTemplate string -func (i *instrumenter) writeGobcoFiles(tmpDir, pkgPath string, pkgs []*ast.Package) { +func (i *instrumenter) writeGobcoFiles(srcDir, tmpDir string, pkgs []*ast.Package) { pkgname := pkgs[0].Name fixPkgname := func(str string) string { str = strings.TrimPrefix(str, "//go:build ignore\n// +build ignore\n\n") @@ -623,7 +589,7 @@ func (i *instrumenter) writeGobcoFiles(tmpDir, pkgPath string, pkgs []*ast.Packa writeFile(filepath.Join(tmpDir, "gobco_no_testmain_test.go"), fixPkgname(noTestMainTemplate)) } - i.writeGobcoBlackBox(pkgs, tmpDir, pkgPath) + i.writeGobcoBlackBox(pkgs, srcDir, tmpDir) } func (i *instrumenter) writeGobcoGo(filename, pkgname string) { @@ -648,14 +614,47 @@ func (i *instrumenter) writeGobcoGo(filename, pkgname string) { writeFile(filename, sb.String()) } +// findPackagePath finds import path of a package that srcDir indicates +func findPackagePath(srcDir string) (string, error) { + moduleRoot, moduleRel, err := findInModule(srcDir) + if err != nil { + return "", err + } + + // Read the content of the go.mod file + modFilePath := filepath.Join(moduleRoot, "go.mod") + modFileContent, err := os.ReadFile(modFilePath) + if err != nil { + return "", err + } + + // Parse the content of the go.mod file + modFile, err := modfile.Parse("", modFileContent, nil) + if err != nil { + return "", err + } + + // Get the module name from the parsed go.mod file + moduleName := modFile.Module.Mod.Path + + if moduleRel == "." { + return moduleName, nil + } else { + pkgPath := fmt.Sprintf("%s/%s", moduleName, moduleRel) + return pkgPath, nil + } +} + // writeGobcoBlackBox makes the function 'GobcoCover' available // to black box tests (those in 'package x_test' instead of 'package x') // by delegating to the function of the same name in the main package. -func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, dstDir, pkgPath string) { +func (i *instrumenter) writeGobcoBlackBox(pkgs []*ast.Package, srcDir, dstDir string) { if len(pkgs) < 2 { return } + pkgPath, err := findPackagePath(srcDir) + ok(err) pkgName := filepath.Base(pkgPath) text := "" + From cad1236081fa2afde254ff05fd11fa252a7c8d1d Mon Sep 17 00:00:00 2001 From: 1st-vil Date: Mon, 15 Apr 2024 14:14:53 +0900 Subject: [PATCH 5/5] use github.com/rillig/gobco to get the module name --- go.mod | 2 -- go.sum | 44 -------------------------------------------- instrumenter.go | 27 ++++++++++++--------------- 3 files changed, 12 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index 5a0445c..e9aee40 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module github.com/rillig/gobco go 1.16 - -require golang.org/x/mod v0.15.0 diff --git a/go.sum b/go.sum index f612015..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,44 +0,0 @@ -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= -golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/instrumenter.go b/instrumenter.go index 1ff44de..28224e1 100644 --- a/instrumenter.go +++ b/instrumenter.go @@ -9,13 +9,12 @@ import ( "go/printer" "go/token" "os" + "os/exec" "path/filepath" "reflect" "runtime" "sort" "strings" - - "golang.org/x/mod/modfile" ) // cond is a condition from the code that is instrumented. @@ -616,27 +615,16 @@ func (i *instrumenter) writeGobcoGo(filename, pkgname string) { // findPackagePath finds import path of a package that srcDir indicates func findPackagePath(srcDir string) (string, error) { - moduleRoot, moduleRel, err := findInModule(srcDir) - if err != nil { - return "", err - } - - // Read the content of the go.mod file - modFilePath := filepath.Join(moduleRoot, "go.mod") - modFileContent, err := os.ReadFile(modFilePath) + _, moduleRel, err := findInModule(srcDir) if err != nil { return "", err } - // Parse the content of the go.mod file - modFile, err := modfile.Parse("", modFileContent, nil) + moduleName, err := getModuleName() if err != nil { return "", err } - // Get the module name from the parsed go.mod file - moduleName := modFile.Module.Mod.Path - if moduleRel == "." { return moduleName, nil } else { @@ -645,6 +633,15 @@ func findPackagePath(srcDir string) (string, error) { } } +func getModuleName() (string, error) { + cmd := exec.Command("go", "list", "-m") + output, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} + // writeGobcoBlackBox makes the function 'GobcoCover' available // to black box tests (those in 'package x_test' instead of 'package x') // by delegating to the function of the same name in the main package.