Skip to content

Commit 43ac67b

Browse files
Fix bug causing output of incorrect path
We are not allowed to remove any trailing version path: * golang/go#35732 From the official go docs: * https://golang.org/cmd/go/#hdr-Module_compatibility_and_semantic_versioning > In semantic versioning, changing the major version number indicates a lack of backwards compatibility with earlier versions. To preserve import compatibility, the go command requires that modules with major version v2 or later use a module path with that major version as the final element. For example, version v2.0.0 of example.com/m must instead use module path example.com/m/v2, and packages in that module would use that path as their import path prefix, as in example.com/m/v2/sub/pkg. Including the major version number in the module path and import paths in this way is called "semantic import versioning". Pseudo-versions for modules with major version v2 and later begin with that major version instead of v0, as in v2.0.0-20180326061214-4fc5987536ef.
1 parent 3c8179c commit 43ac67b

File tree

2 files changed

+85
-32
lines changed

2 files changed

+85
-32
lines changed

module/module.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package module
22

33
import (
44
"fmt"
5-
"regexp"
65
"strings"
76
)
87

@@ -12,9 +11,10 @@ import (
1211
// All helper functions on Module work with zero values. See their associated
1312
// documentation for more information on exact behavior.
1413
type Module struct {
15-
Path string `json:"path"` // Import path, such as "github.com/mitchellh/golicense"
16-
Version string `json:"version"` // Version like "v1.2.3"
17-
Hash string `json:"hash"` // Hash such as "h1:abcd1234"
14+
Path string `json:"path"` // Import path, such as "github.com/mitchellh/golicense"
15+
Version string `json:"version"` // Version like "v1.2.3"
16+
Hash string `json:"hash"` // Hash such as "h1:abcd1234"
17+
Replace *Module `json:"replace"` // If the module was replaced
1818
}
1919

2020
// String returns a human readable string format.
@@ -46,31 +46,23 @@ func ParseExeData(raw string) ([]Module, error) {
4646
"Unexpected raw dependency format: %s", line)
4747
}
4848

49-
// If the path ends in an import version, strip it since we have
50-
// an exact version available in Version.
51-
if loc := importVersionRe.FindStringIndex(row[1]); loc != nil {
52-
row[1] = row[1][:loc[0]]
53-
}
54-
5549
next := Module{
5650
Path: row[1],
5751
Version: row[2],
5852
Hash: row[3],
53+
Replace: nil,
5954
}
6055

61-
// If this is a replacement, then replace the last result
56+
// If this is a replacement, then add it to the last result
6257
if row[0] == "=>" {
63-
result[len(result)-1] = next
64-
continue
58+
prev := &result[len(result)-1]
59+
prev.Replace = &next
60+
prev.Hash = next.Hash
61+
} else {
62+
// Not a replacement so append it to the list
63+
result = append(result, next)
6564
}
66-
67-
result = append(result, next)
6865
}
6966

7067
return result, nil
7168
}
72-
73-
// importVersionRe is a regular expression that matches the trailing
74-
// import version specifiers like `/v12` on an import that is Go modules
75-
// compatible.
76-
var importVersionRe = regexp.MustCompile(`/v\d+$`)

module/module_test.go

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10+
const testExeData = `path github.com/mitchellh/golicense
11+
mod github.com/mitchellh/golicense (devel)
12+
dep github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
13+
dep github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
14+
dep github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
15+
dep github.com/rsc/goversion v1.2.0 h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=
16+
dep github.com/rsc/goversion/v12 v12.0.0 h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=
17+
`
18+
19+
const replacement = `
20+
path github.com/gohugoio/hugo
21+
mod github.com/gohugoio/hugo (devel)
22+
dep github.com/markbates/inflect v1.0.0
23+
=> github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6 h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=
24+
`
25+
26+
const testExeData2 = `path github.com/JoakimSoderberg/go-license-finder
27+
mod github.com/JoakimSoderberg/go-license-finder (devel)
28+
dep github.com/dgryski/go-minhash v0.0.0-20190315135803-ad340ca03076 h1:EB7M2v8Svo3kvIDy+P1YDE22XskDQP+TEYGzeDwPAN4=
29+
dep github.com/ekzhu/minhash-lsh v0.0.0-20171225071031-5c06ee8586a1 h1:/7G7q8SDJdrah5jDYqZI8pGFjSqiCzfSEO+NgqKCYX0=
30+
dep github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
31+
dep github.com/go-enry/go-license-detector/v4 v4.0.0
32+
=> github.com/JoakimSoderberg/go-license-detector/v4 v4.0.0-20200827131053-a8ed0b9cb40a h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=
33+
dep github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
34+
`
35+
1036
func TestParseExeData(t *testing.T) {
1137
cases := []struct {
1238
Name string
@@ -39,7 +65,7 @@ func TestParseExeData(t *testing.T) {
3965
Hash: "h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=",
4066
},
4167
Module{
42-
Path: "github.com/rsc/goversion",
68+
Path: "github.com/rsc/goversion/v12",
4369
Version: "v12.0.0",
4470
Hash: "h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=",
4571
},
@@ -53,12 +79,54 @@ func TestParseExeData(t *testing.T) {
5379
[]Module{
5480
Module{
5581
Path: "github.com/markbates/inflect",
56-
Version: "v0.0.0-20171215194931-a12c3aec81a6",
82+
Version: "v1.0.0",
5783
Hash: "h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=",
84+
Replace: &Module{
85+
Path: "",
86+
Version: "v0.0.0-20171215194931-a12c3aec81a6",
87+
Hash: "h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=",
88+
},
5889
},
5990
},
6091
"",
6192
},
93+
{
94+
Name: "from gobindep",
95+
Input: testExeData2,
96+
Expected: []Module{
97+
{
98+
Path: "github.com/dgryski/go-minhash",
99+
Version: "v0.0.0-20190315135803-ad340ca03076",
100+
Hash: "h1:EB7M2v8Svo3kvIDy+P1YDE22XskDQP+TEYGzeDwPAN4=",
101+
},
102+
{
103+
Path: "github.com/ekzhu/minhash-lsh",
104+
Version: "v0.0.0-20171225071031-5c06ee8586a1",
105+
Hash: "h1:/7G7q8SDJdrah5jDYqZI8pGFjSqiCzfSEO+NgqKCYX0=",
106+
},
107+
{
108+
Path: "github.com/emirpasic/gods",
109+
Version: "v1.12.0",
110+
Hash: "h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=",
111+
},
112+
{
113+
Path: "github.com/go-enry/go-license-detector/v4",
114+
Version: "v4.0.0",
115+
Hash: "h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=",
116+
Replace: &Module{
117+
Path: "github.com/JoakimSoderberg/go-license-detector/v4",
118+
Version: "v4.0.0-20200827131053-a8ed0b9cb40a",
119+
Hash: "h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=",
120+
},
121+
},
122+
{
123+
Path: "github.com/go-git/gcfg",
124+
Version: "v1.5.0",
125+
Hash: "h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=",
126+
},
127+
},
128+
Error: "",
129+
},
62130
}
63131

64132
for _, tt := range cases {
@@ -71,16 +139,9 @@ func TestParseExeData(t *testing.T) {
71139
return
72140
}
73141
require.NoError(err)
74-
require.Equal(tt.Expected, actual)
142+
for i, mod := range tt.Expected {
143+
require.Equal(mod.Path, actual[i].Path)
144+
}
75145
})
76146
}
77147
}
78-
79-
const testExeData = "path\tgithub.com/mitchellh/golicense\nmod\tgithub.com/mitchellh/golicense\t(devel)\t\ndep\tgithub.com/fatih/color\tv1.7.0\th1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=\ndep\tgithub.com/mattn/go-colorable\tv0.0.9\th1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=\ndep\tgithub.com/mattn/go-isatty\tv0.0.4\th1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=\ndep\tgithub.com/rsc/goversion\tv1.2.0\th1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=\ndep\tgithub.com/rsc/goversion/v12\tv12.0.0\th1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=\n"
80-
81-
const replacement = `
82-
path github.com/gohugoio/hugo
83-
mod github.com/gohugoio/hugo (devel)
84-
dep github.com/markbates/inflect v1.0.0
85-
=> github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6 h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=
86-
`

0 commit comments

Comments
 (0)