diff --git a/Makefile b/Makefile index 56f4d1c..ca05329 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,8 @@ all: build build: @go build -test: all - go test -tags=online ./... - ./e2e.sh +test: + go test -tags=online,e2e ./... install: @go install diff --git a/e2e.sh b/e2e.sh deleted file mode 100755 index 54bdb2d..0000000 --- a/e2e.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -set -e - -for given in testdata/*_modules.txt; do - m=${given#testdata/} - m=${m%_modules.txt} - expected=testdata/${m}_expected.txt - actual=/tmp/${m}_actual.txt - - ./modules2tuple ${given} > ${actual} - diff -u ${expected} ${actual} - rm ${actual} -done diff --git a/go.mod b/go.mod index 9e35f71..ef8e489 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/dmgk/modules2tuple go 1.13 + +require github.com/sergi/go-diff v1.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4dedeae --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/parser/parser_e2e_test.go b/parser/parser_e2e_test.go new file mode 100644 index 0000000..a7808b5 --- /dev/null +++ b/parser/parser_e2e_test.go @@ -0,0 +1,78 @@ +// +build e2e + +package parser + +import ( + "fmt" + "io/ioutil" + "path/filepath" + "strings" + "testing" + + "github.com/sergi/go-diff/diffmatchpatch" +) + +const testdataPath = "../testdata" + +type example struct { + modules, expected string +} + +func TestParserE2E(t *testing.T) { + examples, err := loadExamples() + if err != nil { + t.Fatal(err) + } + + for n, x := range examples { + expected, err := ioutil.ReadFile(x.expected) + if err != nil { + t.Fatal(err) + } + + actual, err := Load(x.modules) + if err != nil { + t.Fatal(err) + } + + dmp := diffmatchpatch.New() + diffs := dmp.DiffMain(strings.TrimSpace(string(expected)), strings.TrimSpace(actual.String()), false) + if dmp.DiffLevenshtein(diffs) > 0 { + t.Errorf("%s: expected output doesn't match actual:\n%s", n, dmp.DiffPrettyText(diffs)) + } + } +} + +func loadExamples() (map[string]*example, error) { + res := map[string]*example{} + + dir, err := ioutil.ReadDir(testdataPath) + if err != nil { + return nil, err + } + + for _, f := range dir { + name := f.Name() + parts := strings.SplitN(strings.TrimSuffix(name, filepath.Ext(name)), "_", 2) + if len(parts) < 2 { + return nil, fmt.Errorf("unexpected testdata file name: %q", name) + } + key, kind := parts[0], parts[1] + + x, ok := res[key] + if !ok { + x = &example{} + res[key] = x + } + switch kind { + case "modules": + x.modules = filepath.Join(testdataPath, name) + case "expected": + x.expected = filepath.Join(testdataPath, name) + default: + return nil, fmt.Errorf("unexpected testdata file name: %q", name) + } + } + + return res, nil +} diff --git a/parser/parser_offline_test.go b/parser/parser_offline_test.go index faec68e..895a3ec 100644 --- a/parser/parser_offline_test.go +++ b/parser/parser_offline_test.go @@ -1,4 +1,4 @@ -// +build !online +// +build !online,!e2e package parser diff --git a/tuple/tuple.go b/tuple/tuple.go index 1f33112..2843fa3 100644 --- a/tuple/tuple.go +++ b/tuple/tuple.go @@ -106,7 +106,7 @@ type Tuple struct { source Source // tuple source (Github ot Gitlab) account string // source account project string // source project - hidden bool + hidden bool // if true, tuple will be excluded from G{H,L}_TUPLE } var underscoreRe = regexp.MustCompile(`[^\w]+`) @@ -363,14 +363,6 @@ func fixGithubProjectsAndTags(s Slice) error { return nil } -// func reverseVersion(v string) string { -// parts := strings.Split(v, "/") -// for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { -// parts[i], parts[j] = parts[j], parts[i] -// } -// return strings.Join(parts, "/") -// } - // fixSubdirs ensures that all subdirs are unique and makes symlinks as needed. func fixSubdirs(s Slice) { var maxSubdir, maxVersion, maxModule int @@ -427,6 +419,8 @@ func fixSubdirs(s Slice) { } } +// fixFsNotify takes care of fsnotify annoying ability to appear under multiple different import paths. +// github.com/fsnotify/fsnotify is the canonical package name. func fixFsNotify(s Slice) { key := func(i int) string { return fmt.Sprintf("%s:%s:%s:%s", s[i].account, s[i].project, s[i].version, s[i].group) @@ -460,6 +454,7 @@ func fixFsNotify(s Slice) { // Otherwise omit the first line continuation for more compact representation. const largeLimit = 3 +// String returns G{H,L}_TUPLE variables contents. func (s Slice) String() string { sort.Slice(s, func(i, j int) bool { return s[i].defaultSortKey() < s[j].defaultSortKey() @@ -507,6 +502,7 @@ func (s Slice) String() string { type Links []*Tuple +// Links returns a slice of tuples that require symlinking. func (s Slice) Links() Links { var res Links @@ -525,7 +521,7 @@ func (s Slice) Links() Links { return res } -// String generates "post-extract" target that creates required symlinks. +// String returns "post-extract" target contents. func (l Links) String() string { if len(l) == 0 { return ""