Skip to content

Commit da712b4

Browse files
committed
handle trailing comments
1 parent 73d7ce7 commit da712b4

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

internal/gitsemver/versioninfo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ func findPackageName(repo, s string) (pkgName string, err error) {
3737
if b, err = os.ReadFile(filepath.Join(repo, "go.mod")); /*#nosec G304*/ err == nil {
3838
for _, s := range strings.Split(string(b), "\n") {
3939
s = strings.TrimSpace(s)
40-
if strings.HasPrefix(s, "module") {
41-
pkgName = LastName(s)
40+
fields := strings.Fields(s)
41+
if len(fields) >= 2 && fields[0] == "module" {
42+
pkgName = LastName(fields[1])
43+
break
4244
}
4345
}
4446
}

internal/gitsemver/versioninfo_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package gitsemver_test
22

33
import (
4+
"os"
5+
"path/filepath"
46
"strings"
57
"testing"
68

@@ -27,6 +29,22 @@ func Test_VersionInfo_GoPackage(t *testing.T) {
2729
}
2830
}
2931

32+
func Test_VersionInfo_GoPackage_ModuleWithInlineComment(t *testing.T) {
33+
repo := t.TempDir()
34+
goMod := "module example.com/my_pkg // inline comment\n\ngo 1.25\n"
35+
if err := os.WriteFile(filepath.Join(repo, "go.mod"), []byte(goMod), 0o644); err != nil {
36+
t.Fatal(err)
37+
}
38+
vi := &gitsemver.VersionInfo{Tag: "v1.2.3", Branch: "mybranch", Build: "456"}
39+
txt, err := vi.GoPackage(repo, "")
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
if !strings.Contains(txt, "package my_pkg") || !strings.Contains(txt, "const PkgName = \"my_pkg\"") {
44+
t.Fatalf("unexpected generated package text: %s", txt)
45+
}
46+
}
47+
3048
func Test_VersionInfo_IncPatch(t *testing.T) {
3149
vi := &gitsemver.VersionInfo{Tag: "v1.2", Tags: []gitsemver.GitTag{{Tag: "v1.2"}}}
3250
if !vi.HasTag("v1.2") {

0 commit comments

Comments
 (0)