Skip to content

Commit

Permalink
Fix bug causing output of incorrect path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoakimSoderberg committed Sep 8, 2020
1 parent 3c8179c commit 43ac67b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 32 deletions.
32 changes: 12 additions & 20 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package module

import (
"fmt"
"regexp"
"strings"
)

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

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

// If the path ends in an import version, strip it since we have
// an exact version available in Version.
if loc := importVersionRe.FindStringIndex(row[1]); loc != nil {
row[1] = row[1][:loc[0]]
}

next := Module{
Path: row[1],
Version: row[2],
Hash: row[3],
Replace: nil,
}

// If this is a replacement, then replace the last result
// If this is a replacement, then add it to the last result
if row[0] == "=>" {
result[len(result)-1] = next
continue
prev := &result[len(result)-1]
prev.Replace = &next
prev.Hash = next.Hash
} else {
// Not a replacement so append it to the list
result = append(result, next)
}

result = append(result, next)
}

return result, nil
}

// importVersionRe is a regular expression that matches the trailing
// import version specifiers like `/v12` on an import that is Go modules
// compatible.
var importVersionRe = regexp.MustCompile(`/v\d+$`)
85 changes: 73 additions & 12 deletions module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ import (
"github.com/stretchr/testify/require"
)

const testExeData = `path github.com/mitchellh/golicense
mod github.com/mitchellh/golicense (devel)
dep github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
dep github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
dep github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
dep github.com/rsc/goversion v1.2.0 h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=
dep github.com/rsc/goversion/v12 v12.0.0 h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=
`

const replacement = `
path github.com/gohugoio/hugo
mod github.com/gohugoio/hugo (devel)
dep github.com/markbates/inflect v1.0.0
=> github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6 h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=
`

const testExeData2 = `path github.com/JoakimSoderberg/go-license-finder
mod github.com/JoakimSoderberg/go-license-finder (devel)
dep github.com/dgryski/go-minhash v0.0.0-20190315135803-ad340ca03076 h1:EB7M2v8Svo3kvIDy+P1YDE22XskDQP+TEYGzeDwPAN4=
dep github.com/ekzhu/minhash-lsh v0.0.0-20171225071031-5c06ee8586a1 h1:/7G7q8SDJdrah5jDYqZI8pGFjSqiCzfSEO+NgqKCYX0=
dep github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
dep github.com/go-enry/go-license-detector/v4 v4.0.0
=> github.com/JoakimSoderberg/go-license-detector/v4 v4.0.0-20200827131053-a8ed0b9cb40a h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=
dep github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
`

func TestParseExeData(t *testing.T) {
cases := []struct {
Name string
Expand Down Expand Up @@ -39,7 +65,7 @@ func TestParseExeData(t *testing.T) {
Hash: "h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=",
},
Module{
Path: "github.com/rsc/goversion",
Path: "github.com/rsc/goversion/v12",
Version: "v12.0.0",
Hash: "h1:zVF4y5ciA/rw779S62bEAq4Yif1cBc/UwRkXJ2xZyT4=",
},
Expand All @@ -53,12 +79,54 @@ func TestParseExeData(t *testing.T) {
[]Module{
Module{
Path: "github.com/markbates/inflect",
Version: "v0.0.0-20171215194931-a12c3aec81a6",
Version: "v1.0.0",
Hash: "h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=",
Replace: &Module{
Path: "",
Version: "v0.0.0-20171215194931-a12c3aec81a6",
Hash: "h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=",
},
},
},
"",
},
{
Name: "from gobindep",
Input: testExeData2,
Expected: []Module{
{
Path: "github.com/dgryski/go-minhash",
Version: "v0.0.0-20190315135803-ad340ca03076",
Hash: "h1:EB7M2v8Svo3kvIDy+P1YDE22XskDQP+TEYGzeDwPAN4=",
},
{
Path: "github.com/ekzhu/minhash-lsh",
Version: "v0.0.0-20171225071031-5c06ee8586a1",
Hash: "h1:/7G7q8SDJdrah5jDYqZI8pGFjSqiCzfSEO+NgqKCYX0=",
},
{
Path: "github.com/emirpasic/gods",
Version: "v1.12.0",
Hash: "h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=",
},
{
Path: "github.com/go-enry/go-license-detector/v4",
Version: "v4.0.0",
Hash: "h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=",
Replace: &Module{
Path: "github.com/JoakimSoderberg/go-license-detector/v4",
Version: "v4.0.0-20200827131053-a8ed0b9cb40a",
Hash: "h1:YOPawvrqnDbtX+T+oM2b6UMNOCMqo+DoP6A6qzsfVHI=",
},
},
{
Path: "github.com/go-git/gcfg",
Version: "v1.5.0",
Hash: "h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=",
},
},
Error: "",
},
}

for _, tt := range cases {
Expand All @@ -71,16 +139,9 @@ func TestParseExeData(t *testing.T) {
return
}
require.NoError(err)
require.Equal(tt.Expected, actual)
for i, mod := range tt.Expected {
require.Equal(mod.Path, actual[i].Path)
}
})
}
}

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"

const replacement = `
path github.com/gohugoio/hugo
mod github.com/gohugoio/hugo (devel)
dep github.com/markbates/inflect v1.0.0
=> github.com/markbates/inflect v0.0.0-20171215194931-a12c3aec81a6 h1:LZhVjIISSbj8qLf2qDPP0D8z0uvOWAW5C85ly5mJW6c=
`

0 comments on commit 43ac67b

Please sign in to comment.