From 0f6ba760df738f57f702050e67d37e399ade8dbd Mon Sep 17 00:00:00 2001 From: Serge Zaitsev Date: Fri, 21 Sep 2018 15:06:56 +0200 Subject: [PATCH] add vcs tests --- pkg/vcs/git_test.go | 134 ++++++++++++++++++++++++++++++++++++++++++++ pkg/vcs/vcs_test.go | 18 ++++++ 2 files changed, 152 insertions(+) create mode 100644 pkg/vcs/git_test.go create mode 100644 pkg/vcs/vcs_test.go diff --git a/pkg/vcs/git_test.go b/pkg/vcs/git_test.go new file mode 100644 index 0000000..859385e --- /dev/null +++ b/pkg/vcs/git_test.go @@ -0,0 +1,134 @@ +package vcs + +import ( + "archive/zip" + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "fmt" + "io" + "io/ioutil" + "sort" + "testing" +) + +func TestGit(t *testing.T) { + if testing.Short() { + t.Skip("testing with external VCS might be slow") + } + for _, test := range []struct { + Module string + Tag string + Timestamp string + Checksum string + Private bool + }{ + { + // Repository with no tags and only a single master branch + Module: "bitbucket.org/gomodproxytest/head", + Tag: "v0.0.0-20180921102730-cbd3c6886e0f", + Timestamp: "2018-09-21", + Checksum: "RpG4dddANe2fu9Ebi0nER6ZJTfZDGgKx+yKOabKdljA=", + }, + { + // Repository that uses lightweight tags + Module: "bitbucket.org/gomodproxytest/tags", + Tag: "v1.0.0", + Timestamp: "2018-09-21", + Checksum: "YOW4xf9px088oQ+OP4xgRqjO8mk6eQ5vz7/kY+0Plqo=", + }, + { + // Repository that uses annotated tags + Module: "bitbucket.org/gomodproxytest/annotated", + Tag: "v1.0.0", + Timestamp: "2018-09-21", + Checksum: "e7FcVp32XxvewwsiUuyU1Z06JjcEMnQvK5uBBw6kCR8=", + }, + { + // Repository that contains vendor directory + Module: "bitbucket.org/gomodproxytest/vendor", + Tag: "v1.0.0", + Timestamp: "2018-09-21", + Checksum: "YIusKhLwlEKiuwowBFdEElpP0hIGDOCaSnQaMufLB00=", + }, + { + // Just a frequently used module from github + Module: "github.com/pkg/errors", + Tag: "v0.8.0", + Timestamp: "2016-09-29", + Checksum: "WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=", + }, + } { + if test.Module == "" { + continue + } + auth := NoAuth() + if test.Tag != "" { + t.Run(test.Module+"/List", func(t *testing.T) { + git := NewGit(t.Log, test.Module, auth) + list, err := git.List(context.Background()) + if err != nil { + t.Fatal(err) + } + for _, version := range list { + if string(version) == test.Tag { + return + } + } + t.Fatal("tag not found") + }) + } + if test.Timestamp != "" { + t.Run(test.Module+"/Timestamp", func(t *testing.T) { + git := NewGit(t.Log, test.Module, auth) + timestamp, err := git.Timestamp(context.Background(), Version(test.Tag)) + if err != nil { + t.Fatal(err) + } + if timestamp.Format("2006-01-02") != test.Timestamp { + t.Fatal(timestamp, test.Timestamp) + } + }) + } + if test.Checksum != "" { + t.Run(test.Module+"/ZIP", func(t *testing.T) { + git := NewGit(t.Log, test.Module, auth) + r, err := git.Zip(context.Background(), Version(test.Tag)) + if err != nil { + t.Fatal(err) + } + defer r.Close() + b, err := ioutil.ReadAll(r) + if err != nil { + t.Fatal(err) + } + h := sha256.New() + zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) + if err != nil { + t.Fatal(err) + } + fileSet := map[string]*zip.File{} + fileList := []string{} + for _, zf := range zr.File { + fileSet[zf.Name] = zf + fileList = append(fileList, zf.Name) + } + sort.Strings(fileList) + for _, name := range fileList { + f, err := fileSet[name].Open() + if err != nil { + t.Fatal(name, err) + } + defer f.Close() + hf := sha256.New() + io.Copy(hf, f) + fmt.Fprintf(h, "%x %s\n", hf.Sum(nil), name) + } + if cksum := base64.StdEncoding.EncodeToString(h.Sum(nil)); cksum != test.Checksum { + t.Fatal(cksum, test.Checksum) + } + }) + } + } +} diff --git a/pkg/vcs/vcs_test.go b/pkg/vcs/vcs_test.go new file mode 100644 index 0000000..f83bb87 --- /dev/null +++ b/pkg/vcs/vcs_test.go @@ -0,0 +1,18 @@ +package vcs + +import "testing" + +func TestVersion(t *testing.T) { + if !Version("v1.0.0").IsSemVer() { + t.Fatal() + } + if Version("1.0.0").IsSemVer() { + t.Fatal() + } + if Version("v0.0.0-20180910181607-0e37d006457b").IsSemVer() { + t.Fatal() + } + if Version("v0.0.0-20180910181607-0e37d006457b").Hash() != "0e37d006457b" { + t.Fatal() + } +}