Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Adding Unit tests to increase coverage #1979

Merged
merged 2 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
@@ -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!"]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Gopkg.lock is out of sync:
github.com/sdboyer/deptest: prune options changed ( -> UT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"commands": [
["check"]
],
"should-fail": true,
"vendor-final": [
"github.com/sdboyer/deptest"
]
}
9 changes: 9 additions & 0 deletions gps/_testdata/src/slash-star_confl/a.go
Original file line number Diff line number Diff line change
@@ -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"
)
9 changes: 9 additions & 0 deletions gps/_testdata/src/slash-star_confl/b.go
Original file line number Diff line number Diff line change
@@ -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"
)
18 changes: 18 additions & 0 deletions gps/pkgtree/pkgtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,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 {
Expand Down
4 changes: 2 additions & 2 deletions gps/verify/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
77 changes: 77 additions & 0 deletions gps/verify/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down