Skip to content

Commit

Permalink
fixbug : parse private versioning yarn.lock (go-dep-parser#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoyamachi authored and knqyf263 committed May 21, 2019
1 parent 7c536d5 commit fa272d4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
36 changes: 18 additions & 18 deletions pkg/yarn/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ package yarn
import (
"bufio"
"fmt"
"github.com/knqyf263/go-dep-parser/pkg/types"
"golang.org/x/xerrors"
"io"
"regexp"
"strings"

"github.com/knqyf263/go-dep-parser/pkg/types"
"golang.org/x/xerrors"
)

var (
yarnPackageRegexp = regexp.MustCompile(`(?P<package>.*?)@[\^~*><= ]*[0-9\*]`)
yarnPackageRegexp = regexp.MustCompile(`"?(?P<package>.+?)@.+`)
yarnVersionPrefix = ` version "`
)

type LockFile struct {
Dependencies map[string]Dependency
}
type Dependency struct {
Version string
Version string
// TODO : currently yarn can't recognize Dev flag.
// That need to parse package.json for Dev flag
Dev bool
Dependencies map[string]Dependency
}

func getPackageName(target string) (packagename string, err error) {
capture := yarnPackageRegexp.FindStringSubmatch(target)
if len(capture) < 2 {
return "", xerrors.New("not package format")
}

return capture[len(capture)-1], nil
}

func Parse(r io.Reader) (libs []types.Library, err error) {
scanner := bufio.NewScanner(r)
unique := map[string]struct{}{}
Expand All @@ -42,7 +52,7 @@ func Parse(r io.Reader) (libs []types.Library, err error) {
return nil, xerrors.New("Invalid yarn.lock format")
}
// fetch between version prefix and last double-quote
version := line[11:(len(line) -1)]
version := line[11:(len(line) - 1)]
symbol := fmt.Sprintf("%s@%s", lib.Name, version)
if _, ok := unique[symbol]; ok {
lib = types.Library{}
Expand All @@ -57,20 +67,10 @@ func Parse(r io.Reader) (libs []types.Library, err error) {
}

// packagename line start 1 char
if line[:1] != " " && line[:1] != "#" {
capture := yarnPackageRegexp.FindStringSubmatch(line)
if len(capture) < 2 {
continue
}

matched := capture[len(capture) - 1]
if line[:1] != " " && line[:1] != "#" {
var name string
// sometimes package name start double-quote
// ex) "string-width@^1.0.2 || 2", string-width@^2.0.0, string-width@^2.1.1:
if strings.HasPrefix(matched, `"`) {
name = matched[1:]
} else {
name = matched
if name, err = getPackageName(line); err != nil {
continue
}
lib.Name = name
}
Expand Down
48 changes: 44 additions & 4 deletions pkg/yarn/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
package yarn

import (
"github.com/knqyf263/go-dep-parser/pkg/types"
"github.com/kylelemons/godebug/pretty"
"os"
"path"
"sort"
"strings"
"testing"

"github.com/knqyf263/go-dep-parser/pkg/types"
"github.com/kylelemons/godebug/pretty"
)

func TestGetPackageName(t *testing.T) {
vectors := []struct {
target string // Test input file
expect string
occurErr bool
}{
{
target: `"@babel/code-frame@^7.0.0"`,
expect: "@babel/code-frame",
},
{
target: `[email protected].*:`,
expect: "grunt-contrib-cssmin",
},
{
target: "grunt-contrib-uglify-es@gruntjs/grunt-contrib-uglify#harmony:",
expect: "grunt-contrib-uglify-es",
},
{
target: `"jquery@git+https://xxxx:[email protected]/tomoyamachi/jquery":`,
expect: "jquery",
},
{
target: `normal line`,
occurErr: true,
},
}

for _, v := range vectors {
actual, err := getPackageName(v.target)

if v.occurErr != (err != nil) {
t.Errorf("expect error %t but err is %s", v.occurErr, err)
continue
}

if actual != v.expect {
t.Errorf("got %s, want %s, target :%s", actual, v.expect, v.target)
}
}
}

func TestParse(t *testing.T) {
vectors := []struct {
Expand All @@ -36,7 +78,6 @@ func TestParse(t *testing.T) {
file: "testdata/yarn_realworld.lock",
libraries: YarnRealWorld,
},

}

for _, v := range vectors {
Expand Down Expand Up @@ -82,4 +123,3 @@ func TestParse(t *testing.T) {
})
}
}

0 comments on commit fa272d4

Please sign in to comment.