From 579f492a3a6ae3f011ed61698f7783de306f3413 Mon Sep 17 00:00:00 2001 From: tariq1890 Date: Sat, 4 Aug 2018 12:08:36 -0700 Subject: [PATCH] Adding Unit tests to increase coverage --- .../check/pruneopts_changed/final/Gopkg.lock | 17 ++++ .../check/pruneopts_changed/final/Gopkg.toml | 8 ++ .../pruneopts_changed/initial/Gopkg.lock | 17 ++++ .../pruneopts_changed/initial/Gopkg.toml | 8 ++ .../check/pruneopts_changed/initial/main.go | 16 ++++ .../github.com/sdboyer/deptest/deptest.go | 3 + .../check/pruneopts_changed/stdout.txt | 3 + .../check/pruneopts_changed/testcase.json | 9 +++ gps/_testdata/src/slash-star_confl/a.go | 9 +++ gps/_testdata/src/slash-star_confl/b.go | 9 +++ gps/pkgtree/pkgtree_test.go | 18 +++++ gps/verify/digest.go | 4 +- gps/verify/digest_test.go | 77 +++++++++++++++++++ 13 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.lock create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.toml create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.lock create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.toml create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/main.go create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/vendor/github.com/sdboyer/deptest/deptest.go create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/stdout.txt create mode 100644 cmd/dep/testdata/harness_tests/check/pruneopts_changed/testcase.json create mode 100644 gps/_testdata/src/slash-star_confl/a.go create mode 100644 gps/_testdata/src/slash-star_confl/b.go diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.lock new file mode 100644 index 00000000..188ece4f --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.lock @@ -0,0 +1,17 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:ddbbbe7f7a81c86d54e89fa388b532f4c144d666a14e8e483ba04fa58265b135" + name = "github.com/sdboyer/deptest" + packages = ["."] + pruneopts = "" + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v1.0.0" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = ["github.com/sdboyer/deptest"] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.toml new file mode 100644 index 00000000..51a3481d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/final/Gopkg.toml @@ -0,0 +1,8 @@ + +[[constraint]] + name = "github.com/sdboyer/deptest" + version = "1.0.0" + +[prune] + go-tests = true + unused-packages = true diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.lock b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.lock new file mode 100644 index 00000000..188ece4f --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.lock @@ -0,0 +1,17 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:ddbbbe7f7a81c86d54e89fa388b532f4c144d666a14e8e483ba04fa58265b135" + name = "github.com/sdboyer/deptest" + packages = ["."] + pruneopts = "" + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v1.0.0" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = ["github.com/sdboyer/deptest"] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.toml b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.toml new file mode 100644 index 00000000..51a3481d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/Gopkg.toml @@ -0,0 +1,8 @@ + +[[constraint]] + name = "github.com/sdboyer/deptest" + version = "1.0.0" + +[prune] + go-tests = true + unused-packages = true diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/main.go b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/main.go new file mode 100644 index 00000000..71416f17 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/main.go @@ -0,0 +1,16 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "github.com/sdboyer/deptest" +) + +func main() { + err := nil + if err != nil { + deptest.Map["yo yo!"] + } +} diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/vendor/github.com/sdboyer/deptest/deptest.go b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/vendor/github.com/sdboyer/deptest/deptest.go new file mode 100644 index 00000000..89983b5a --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/initial/vendor/github.com/sdboyer/deptest/deptest.go @@ -0,0 +1,3 @@ +package deptest + +type Foo int diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/stdout.txt b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/stdout.txt new file mode 100644 index 00000000..f04d526a --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/stdout.txt @@ -0,0 +1,3 @@ +# Gopkg.lock is out of sync: +github.com/sdboyer/deptest: prune options changed ( -> UT) + diff --git a/cmd/dep/testdata/harness_tests/check/pruneopts_changed/testcase.json b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/testcase.json new file mode 100644 index 00000000..d06367e4 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/check/pruneopts_changed/testcase.json @@ -0,0 +1,9 @@ +{ + "commands": [ + ["check"] + ], + "should-fail": true, + "vendor-final": [ + "github.com/sdboyer/deptest" + ] +} diff --git a/gps/_testdata/src/slash-star_confl/a.go b/gps/_testdata/src/slash-star_confl/a.go new file mode 100644 index 00000000..428a16b1 --- /dev/null +++ b/gps/_testdata/src/slash-star_confl/a.go @@ -0,0 +1,9 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package canonical /* import "vanity1" */ + +var ( + A = "A" +) diff --git a/gps/_testdata/src/slash-star_confl/b.go b/gps/_testdata/src/slash-star_confl/b.go new file mode 100644 index 00000000..1fa3c2f0 --- /dev/null +++ b/gps/_testdata/src/slash-star_confl/b.go @@ -0,0 +1,9 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package canonical /* import "vanity2" */ + +var ( + B = "B" +) diff --git a/gps/pkgtree/pkgtree_test.go b/gps/pkgtree/pkgtree_test.go index a2b9a9a2..9c3c7b1c 100644 --- a/gps/pkgtree/pkgtree_test.go +++ b/gps/pkgtree/pkgtree_test.go @@ -1442,6 +1442,24 @@ func TestListPackages(t *testing.T) { }, }, }, + "slash-star": { + fileRoot: j("slash-star_confl"), + importRoot: "slash-star_confl", + out: PackageTree{ + ImportRoot: "slash-star_confl", + Packages: map[string]PackageOrErr{ + "slash-star_confl": { + Err: &ConflictingImportComments{ + ImportPath: "slash-star_confl", + ConflictingImportComments: []string{ + "vanity1", + "vanity2", + }, + }, + }, + }, + }, + }, } for name, fix := range table { diff --git a/gps/verify/digest.go b/gps/verify/digest.go index ba8add7d..88a3a476 100644 --- a/gps/verify/digest.go +++ b/gps/verify/digest.go @@ -299,10 +299,10 @@ const ( func (ls VendorStatus) String() string { switch ls { - case NotInTree: - return "not in tree" case NotInLock: return "not in lock" + case NotInTree: + return "not in tree" case NoMismatch: return "match" case EmptyDigestInLock: diff --git a/gps/verify/digest_test.go b/gps/verify/digest_test.go index 9d479d93..58d46736 100644 --- a/gps/verify/digest_test.go +++ b/gps/verify/digest_test.go @@ -117,6 +117,7 @@ func TestDigestFromDirectory(t *testing.T) { // ensure hash ignores prefix. t.Run("AbsolutePrefix", func(t *testing.T) { + t.Parallel() prefix := getTestdataVerifyRoot(t) got, err := DigestFromDirectory(filepath.Join(prefix, relativePathname)) if err != nil { @@ -128,6 +129,7 @@ func TestDigestFromDirectory(t *testing.T) { }) t.Run("RelativePrefix", func(t *testing.T) { + t.Parallel() prefix := "../_testdata/digest" got, err := DigestFromDirectory(filepath.Join(prefix, relativePathname)) if err != nil { @@ -232,6 +234,81 @@ func TestVerifyDepTree(t *testing.T) { checkStatus(t, status, "github.com/charlie/notInTree", NotInTree) checkStatus(t, status, "launchpad.net/match", HashVersionMismatch) }) + + t.Run("Non-existent directory", func(t *testing.T) { + t.Parallel() + wantDigests := make(map[string]VersionedDigest) + for k, v := range wantSums { + wantDigests[k] = VersionedDigest{ + HashVersion: HashVersion + 1, + Digest: v, + } + } + + status, err := CheckDepTree("fooVendorRoot", wantDigests) + if err != nil { + t.Fatal(err) + } + + if got, want := len(status), 6; got != want { + t.Errorf("Unexpected result count from VerifyDepTree:\n\t(GOT): %v\n\t(WNT): %v", got, want) + } + + checkStatus(t, status, "github.com/alice/match", NotInTree) + checkStatus(t, status, "github.com/alice/mismatch", NotInTree) + checkStatus(t, status, "github.com/bob/match", NotInTree) + checkStatus(t, status, "github.com/bob/emptyDigest", NotInTree) + checkStatus(t, status, "github.com/charlie/notInTree", NotInTree) + checkStatus(t, status, "launchpad.net/match", NotInTree) + + }) +} + +func TestParseVersionedDigest(t *testing.T) { + t.Run("Parse valid VersionedDigest", func(t *testing.T) { + t.Parallel() + input := "1:60861e762bdbe39c4c7bf292c291329b731c9925388fd41125888f5c1c595feb" + vd, err := ParseVersionedDigest(input) + if err != nil { + t.Fatal() + } + + expectedHash := "60861e762bdbe39c4c7bf292c291329b731c9925388fd41125888f5c1c595feb" + if got, want := vd.Digest, expectedHash; bytes.Equal(got, []byte(expectedHash)) { + t.Errorf("Unexpected result from ParseVersionedDigest:\n\t(GOT): %s\n\t(WNT): %s", got, want) + } + + if got, want := vd.String(), input; got != want { + t.Errorf("Unexpected result from ParseVersionedDigest String:\n\t(GOT): %s\n\t(WNT): %s", got, want) + } + }) + + t.Run("Parse VersionedDigest with invalid format", func(t *testing.T) { + t.Parallel() + input := "1abc" + _, err := ParseVersionedDigest(input) + if err == nil { + t.Error("expected error for invalid VersionedDigest format") + } + }) + + t.Run("Parse VersionedDigest with invalid hex string", func(t *testing.T) { + t.Parallel() + input := "1:60861g762bdbe39c4c7bf292c291329b731c9925388fd41125888f5c1c595feb" + _, err := ParseVersionedDigest(input) + if err == nil { + t.Error("expected error VersionedDigest with invalid hex string") + } + }) + + t.Run("Parse VersionedDigest with invalid hash version", func(t *testing.T) { + t.Parallel() + input := "a:60861e762bdbe39c4c7bf292c291329b731c9925388fd41125888f5c1c595feb" + _, err := ParseVersionedDigest(input) + if err == nil { + t.Error("expected error VersionedDigest with invalid hash version") + } + }) } func BenchmarkDigestFromDirectory(b *testing.B) {