Skip to content

Commit

Permalink
re-read "path" when processing replace directive. fixes #256 (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhamail authored Nov 16, 2021
1 parent b634a54 commit 0e3cc7c
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 11 deletions.
19 changes: 10 additions & 9 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func GoListAgnostic(stdIn io.Reader) (deps types.ProjectList, err error) {
if module, ok := mod["Module"].(map[string]interface{}); ok {
// Ok, we are in Module town, so `go list -json -deps` has been run
if replace, ok := module["Replace"].(map[string]interface{}); ok {
// A replace block has been found, let's try and do something with it
// A 'Replace' block has been found, let's try and do something with it.
if version, ok := replace["Version"].(string); ok {
deps.Projects = append(deps.Projects, types.Projects{Version: version, Name: module["Path"].(string)})
deps.Projects = append(deps.Projects, types.Projects{Version: version, Name: replace["Path"].(string)})
continue
}
}
Expand All @@ -85,13 +85,14 @@ func GoListAgnostic(stdIn io.Reader) (deps types.ProjectList, err error) {
if path, ok := mod["Path"].(string); ok {
// Ok, we are in list town, so `go list -json -m all` has been run
if replace, ok := mod["Replace"].(map[string]interface{}); ok {
// A replace block has been found, let's try and do something with it
if version, ok := replace["Version"].(string); ok {
deps.Projects = append(deps.Projects, types.Projects{Version: version, Name: path})

continue
}

// A 'Replace' block has been found, let's try and do something with it.
if path, ok = replace["Path"].(string); ok { // re-read path from replace block
if version, ok := replace["Version"].(string); ok {
deps.Projects = append(deps.Projects, types.Projects{Version: version, Name: path})

continue
}
} // No path found in replace block, so just continue
// No version found in replace block, so just continue loop
continue
}
Expand Down
47 changes: 45 additions & 2 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,31 @@ func TestGoListDepsJsonReplace(t *testing.T) {
t.Error(err)
}
if len(deps.Projects) != 1 {
t.Errorf("Unsuccessfully parsed go list -deps -json ./... output, 112 dependencies were expected, but %d encountered", len(deps.Projects))
t.Errorf("Unsuccessfully parsed go list -deps -json ./... output, 1 dependency expected, but %d encountered", len(deps.Projects))
}
if deps.Projects[0].Version != "v1.0.1-vault-3" {
t.Errorf("Unsuccessfully parsed go list -deps -json ./... output, 112 dependencies were expected, but %d encountered", len(deps.Projects))
t.Errorf("Unexpected version: %s", deps.Projects[0].Version)
}
}

func TestGoListDepsJsonReplacePath(t *testing.T) {
goListFile, err := os.Open("testdata/golistdependenciesjsonreplacepath.out")
if err != nil {
t.Error(err)
}

deps, err := GoListAgnostic(goListFile)
if err != nil {
t.Error(err)
}
if len(deps.Projects) != 1 {
t.Errorf("Unsuccessfully parsed go list -deps -json ./... output, 1 dependency expected, but %d encountered", len(deps.Projects))
}
if deps.Projects[0].Name != "github.com/golang-jwt/jwt/v4" {
t.Errorf("Unexpected name: %s", deps.Projects[0].Name)
}
if deps.Projects[0].Version != "v4.1.0" {
t.Errorf("Unexpected version: %s", deps.Projects[0].Version)
}
}

Expand Down Expand Up @@ -108,6 +129,28 @@ func TestGoListJsonReplace(t *testing.T) {
}
}

func TestGoListJsonReplacePath(t *testing.T) {
goListJSONReplaceFile, err := os.Open("testdata/golistjsonreplacepath.out")
if err != nil {
t.Error(err)
}

deps, err := GoListAgnostic(goListJSONReplaceFile)
if err != nil {
t.Error(err)
}
if len(deps.Projects) != 1 {
t.Errorf("1 expected, but %d encountered", len(deps.Projects))
}

if deps.Projects[0].Name != "github.com/golang-jwt/jwt/v4" {
t.Errorf("Name encountered %s", deps.Projects[0].Name)
}
if deps.Projects[0].Version != "v4.1.0" {
t.Errorf("Version encountered %s", deps.Projects[0].Version)
}
}

func TestGoListReplace(t *testing.T) {
goListReplaceFile, err := os.Open("testdata/golistreplace.out")
if err != nil {
Expand Down
188 changes: 188 additions & 0 deletions parse/testdata/golistdependenciesjsonreplacepath.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"Dir": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"ImportPath": "github.com/dgrijalva/jwt-go",
"Name": "jwt",
"Doc": "Package jwt is a Go implementation of JSON Web Tokens: http://self-issued.info/docs/draft-jones-json-web-token.html See README.md for more info.",
"Root": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"Module": {
"Path": "github.com/dgrijalva/jwt-go",
"Version": "v3.2.0+incompatible",
"Replace": {
"Path": "github.com/golang-jwt/jwt/v4",
"Version": "v4.1.0",
"Time": "2021-09-24T19:32:29Z",
"Dir": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"GoMod": "/Users/bhamail/go/pkg/mod/cache/download/github.com/golang-jwt/jwt/v4/@v/v4.1.0.mod",
"GoVersion": "1.15"
},
"Dir": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"GoMod": "/Users/bhamail/go/pkg/mod/cache/download/github.com/golang-jwt/jwt/v4/@v/v4.1.0.mod",
"GoVersion": "1.15"
},
"DepOnly": true,
"Stale": true,
"StaleReason": "build ID mismatch",
"GoFiles": [
"claims.go",
"doc.go",
"ecdsa.go",
"ecdsa_utils.go",
"ed25519.go",
"ed25519_utils.go",
"errors.go",
"hmac.go",
"map_claims.go",
"none.go",
"parser.go",
"rsa.go",
"rsa_pss.go",
"rsa_utils.go",
"signing_method.go",
"token.go",
"types.go"
],
"Imports": [
"bytes",
"crypto",
"crypto/ecdsa",
"crypto/ed25519",
"crypto/hmac",
"crypto/rand",
"crypto/rsa",
"crypto/subtle",
"crypto/x509",
"encoding/base64",
"encoding/json",
"encoding/pem",
"errors",
"fmt",
"math/big",
"reflect",
"strconv",
"strings",
"sync",
"time"
],
"Deps": [
"bufio",
"bytes",
"context",
"crypto",
"crypto/aes",
"crypto/cipher",
"crypto/des",
"crypto/dsa",
"crypto/ecdsa",
"crypto/ed25519",
"crypto/ed25519/internal/edwards25519",
"crypto/elliptic",
"crypto/hmac",
"crypto/internal/randutil",
"crypto/internal/subtle",
"crypto/md5",
"crypto/rand",
"crypto/rsa",
"crypto/sha1",
"crypto/sha256",
"crypto/sha512",
"crypto/subtle",
"crypto/x509",
"crypto/x509/internal/macos",
"crypto/x509/pkix",
"encoding",
"encoding/asn1",
"encoding/base64",
"encoding/binary",
"encoding/hex",
"encoding/json",
"encoding/pem",
"errors",
"fmt",
"hash",
"internal/bytealg",
"internal/cpu",
"internal/fmtsort",
"internal/nettrace",
"internal/oserror",
"internal/poll",
"internal/race",
"internal/reflectlite",
"internal/singleflight",
"internal/syscall/execenv",
"internal/syscall/unix",
"internal/testlog",
"internal/unsafeheader",
"io",
"io/fs",
"math",
"math/big",
"math/bits",
"math/rand",
"net",
"net/url",
"os",
"path",
"reflect",
"runtime",
"runtime/cgo",
"runtime/internal/atomic",
"runtime/internal/math",
"runtime/internal/sys",
"sort",
"strconv",
"strings",
"sync",
"sync/atomic",
"syscall",
"time",
"unicode",
"unicode/utf16",
"unicode/utf8",
"unsafe",
"vendor/golang.org/x/crypto/cryptobyte",
"vendor/golang.org/x/crypto/cryptobyte/asn1",
"vendor/golang.org/x/net/dns/dnsmessage",
"vendor/golang.org/x/net/route"
],
"TestGoFiles": [
"map_claims_test.go"
],
"TestImports": [
"testing",
"time"
],
"XTestGoFiles": [
"ecdsa_test.go",
"ed25519_test.go",
"example_test.go",
"hmac_example_test.go",
"hmac_test.go",
"http_example_test.go",
"none_test.go",
"parser_test.go",
"rsa_pss_test.go",
"rsa_test.go",
"types_test.go"
],
"XTestImports": [
"bytes",
"crypto",
"crypto/ecdsa",
"crypto/rsa",
"encoding/json",
"fmt",
"github.com/golang-jwt/jwt/v4",
"github.com/golang-jwt/jwt/v4/request",
"github.com/golang-jwt/jwt/v4/test",
"io",
"io/ioutil",
"log",
"net",
"net/http",
"net/url",
"reflect",
"strings",
"testing",
"time"
]
}
15 changes: 15 additions & 0 deletions parse/testdata/golistjsonreplacepath.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Path": "github.com/dgrijalva/jwt-go",
"Version": "v3.2.0+incompatible",
"Replace": {
"Path": "github.com/golang-jwt/jwt/v4",
"Version": "v4.1.0",
"Time": "2021-09-24T19:32:29Z",
"Dir": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"GoMod": "/Users/bhamail/go/pkg/mod/cache/download/github.com/golang-jwt/jwt/v4/@v/v4.1.0.mod",
"GoVersion": "1.15"
},
"Dir": "/Users/bhamail/go/pkg/mod/github.com/golang-jwt/jwt/[email protected]",
"GoMod": "/Users/bhamail/go/pkg/mod/cache/download/github.com/golang-jwt/jwt/v4/@v/v4.1.0.mod",
"GoVersion": "1.15"
}

0 comments on commit 0e3cc7c

Please sign in to comment.