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

Commit

Permalink
Enable import testcases to check for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs committed Sep 1, 2017
1 parent 6de23fd commit 7d8bad4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
16 changes: 12 additions & 4 deletions cmd/dep/base_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"log"
"sort"
"strings"
"testing"

"github.com/golang/dep"
Expand Down Expand Up @@ -388,6 +389,7 @@ type convertTestCase struct {
wantRevision gps.Revision
wantVersion string
wantIgnored []string
wantWarning string
}

func (tc convertTestCase) Exec(t *testing.T, convert func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error)) error {
Expand All @@ -400,15 +402,15 @@ func (tc convertTestCase) Exec(t *testing.T, convert func(logger *log.Logger, sm
defer sm.Release()

// Capture stderr so we can verify warnings
verboseOutput := &bytes.Buffer{}
ctx.Err = log.New(verboseOutput, "", 0)
output := &bytes.Buffer{}
ctx.Err = log.New(output, "", 0)

manifest, lock, convertErr := convert(ctx.Err, sm)
return tc.validate(manifest, lock, convertErr)
return tc.validate(manifest, lock, convertErr, output)
}

// validate returns an error if any of the testcase validations failed.
func (tc convertTestCase) validate(manifest *dep.Manifest, lock *dep.Lock, convertErr error) error {
func (tc convertTestCase) validate(manifest *dep.Manifest, lock *dep.Lock, convertErr error, output *bytes.Buffer) error {
if tc.wantConvertErr {
if convertErr == nil {
return errors.New("Expected the conversion to fail, but it did not return an error")
Expand Down Expand Up @@ -503,6 +505,12 @@ func (tc convertTestCase) validate(manifest *dep.Manifest, lock *dep.Lock, conve
}
}

if tc.wantWarning != "" {
if !strings.Contains(output.String(), tc.wantWarning) {
return errors.Errorf("Expected the output to include the warning '%s'", tc.wantWarning)
}
}

return nil
}

Expand Down
71 changes: 28 additions & 43 deletions cmd/dep/glide_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"log"
"path/filepath"
"strings"
"testing"

"github.com/golang/dep"
Expand Down Expand Up @@ -143,6 +142,34 @@ func TestGlideConfig_Convert(t *testing.T) {
wantConvertErr: true,
},
},
"warn unused os field": {
glideYaml{
Imports: []glidePackage{
{
Name: importerTestProject,
OS: "windows",
},
}},
glideLock{},
convertTestCase{
wantConstraint: "*",
wantWarning: "specified an os",
},
},
"warn unused arch field": {
glideYaml{
Imports: []glidePackage{
{
Name: importerTestProject,
Arch: "i686",
},
}},
glideLock{},
convertTestCase{
wantConstraint: "*",
wantWarning: "specified an arch",
},
},
}

for name, testCase := range testCases {
Expand Down Expand Up @@ -209,45 +236,3 @@ func TestGlideConfig_Import(t *testing.T) {
}
}
}

func TestGlideConfig_Convert_WarnsForUnusedFields(t *testing.T) {
testCases := map[string]glidePackage{
"specified an os": {OS: "windows"},
"specified an arch": {Arch: "i686"},
}

for wantWarning, pkg := range testCases {
t.Run(wantWarning, func(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.Parallel()

pkg.Name = "github.com/sdboyer/deptest"
pkg.Reference = "v1.0.0"

ctx := newTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

// Capture stderr so we can verify warnings
verboseOutput := &bytes.Buffer{}
ctx.Err = log.New(verboseOutput, "", 0)

g := newGlideImporter(ctx.Err, true, sm)
g.glideConfig = glideYaml{
Imports: []glidePackage{pkg},
}

_, _, err = g.convert(testProjectRoot)
if err != nil {
t.Fatal(err)
}

warnings := verboseOutput.String()
if !strings.Contains(warnings, wantWarning) {
t.Errorf("Expected the output to include the warning '%s'", wantWarning)
}
})
}
}

0 comments on commit 7d8bad4

Please sign in to comment.