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

Commit

Permalink
Apply common validations to all import conversion unit tests
Browse files Browse the repository at this point in the history
* Remove matchVersionPair flag and validate the cast anytime a
* wantRevision was specified.
* Always validate lock properties when a lock is generated.
  • Loading branch information
carolynvs committed Aug 11, 2017
1 parent 044c4ef commit c123f59
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 197 deletions.
172 changes: 60 additions & 112 deletions cmd/dep/glide_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,9 @@ func newTestContext(h *test.Helper) *dep.Ctx {

func TestGlideConfig_Convert(t *testing.T) {
testCases := map[string]struct {
yaml glideYaml
lock *glideLock
wantConvertErr bool
matchPairedVersion bool
projectRoot gps.ProjectRoot
wantSourceRepo string
wantConstraint string
wantRevision gps.Revision
wantVersion string
wantLockCount int
wantIgnoreCount int
wantIgnoredPackages []string
*convertTestCase
yaml glideYaml
lock *glideLock
}{
"project": {
yaml: glideYaml{
Expand All @@ -68,13 +59,14 @@ func TestGlideConfig_Convert(t *testing.T) {
},
},
},
projectRoot: "github.com/sdboyer/deptest",
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
matchPairedVersion: true,
wantConstraint: "^1.0.0",
wantLockCount: 1,
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
wantVersion: "v1.0.0",
convertTestCase: &convertTestCase{
projectRoot: "github.com/sdboyer/deptest",
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
wantConstraint: "^1.0.0",
wantLockCount: 1,
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
wantVersion: "v1.0.0",
},
},
"test project": {
yaml: glideYaml{
Expand All @@ -93,42 +85,74 @@ func TestGlideConfig_Convert(t *testing.T) {
},
},
},
projectRoot: "github.com/sdboyer/deptest",
wantLockCount: 1,
wantConstraint: "^1.0.0",
wantVersion: "v1.0.0",
convertTestCase: &convertTestCase{
projectRoot: "github.com/sdboyer/deptest",
wantLockCount: 1,
wantConstraint: "^1.0.0",
wantVersion: "v1.0.0",
},
},
"revision only": {
yaml: glideYaml{
Imports: []glidePackage{
{
Name: "github.com/sdboyer/deptest",
},
},
},
lock: &glideLock{
Imports: []glideLockedPackage{
{
Name: "github.com/sdboyer/deptest",
Reference: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
},
},
},
convertTestCase: &convertTestCase{
projectRoot: "github.com/sdboyer/deptest",
wantLockCount: 1,
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
},
},
"with ignored package": {
yaml: glideYaml{
Ignores: []string{"github.com/sdboyer/deptest"},
},
projectRoot: "github.com/sdboyer/deptest",
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
convertTestCase: &convertTestCase{
projectRoot: "github.com/sdboyer/deptest",
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
},
},
"with exclude dir": {
yaml: glideYaml{
ExcludeDirs: []string{"samples"},
},
projectRoot: testGlideProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
convertTestCase: &convertTestCase{
projectRoot: testGlideProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
},
},
"exclude dir ignores mismatched package name": {
yaml: glideYaml{
Name: "github.com/golang/mismatched-package-name",
ExcludeDirs: []string{"samples"},
},
projectRoot: testGlideProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
convertTestCase: &convertTestCase{
projectRoot: testGlideProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
},
},
"bad input, empty package name": {
yaml: glideYaml{
Imports: []glidePackage{{Name: ""}},
},
projectRoot: testGlideProjectRoot,
wantConvertErr: true,
convertTestCase: &convertTestCase{
projectRoot: testGlideProjectRoot,
wantConvertErr: true,
},
},
}

Expand All @@ -149,87 +173,11 @@ func TestGlideConfig_Convert(t *testing.T) {
g.lock = testCase.lock
}

manifest, lock, err := g.convert(testCase.projectRoot)
manifest, lock, convertErr := g.convert(testCase.projectRoot)
err := validateConvertTestCase(testCase.convertTestCase, manifest, lock, convertErr)
if err != nil {
if testCase.wantConvertErr {
return
}

t.Fatal(err)
}

// Lock checks.
if lock != nil && len(lock.P) != testCase.wantLockCount {
t.Fatalf("Expected lock to have %d project(s), got %d",
testCase.wantLockCount,
len(lock.P))
}

// Ignored projects checks.
if len(manifest.Ignored) != testCase.wantIgnoreCount {
t.Fatalf("Expected manifest to have %d ignored project(s), got %d",
testCase.wantIgnoreCount,
len(manifest.Ignored))
}

if !equalSlice(manifest.Ignored, testCase.wantIgnoredPackages) {
t.Fatalf("Expected manifest to have ignore %s, got %s",
strings.Join(testCase.wantIgnoredPackages, ", "),
strings.Join(manifest.Ignored, ", "))
}

// Constraints checks below. Skip if there is no want constraint.
if testCase.wantConstraint == "" {
return
}

d, ok := manifest.Constraints[testCase.projectRoot]
if !ok {
t.Fatalf("Expected the manifest to have a dependency for '%s' but got none",
testCase.projectRoot)
}

v := d.Constraint.String()
if v != testCase.wantConstraint {
t.Fatalf("Expected manifest constraint to be %s, got %s", testCase.wantConstraint, v)
}

p := lock.P[0]

if p.Ident().ProjectRoot != testCase.projectRoot {
t.Fatalf("Expected the lock to have a project for '%s' but got '%s'",
testCase.projectRoot,
p.Ident().ProjectRoot)
}

if p.Ident().Source != testCase.wantSourceRepo {
t.Fatalf("Expected locked source to be %s, got '%s'", testCase.wantSourceRepo, p.Ident().Source)
}

lv := p.Version()
lpv, ok := lv.(gps.PairedVersion)

if !ok {
if testCase.matchPairedVersion {
t.Fatalf("Expected locked version to be PairedVersion but got %T", lv)
}

return
}

ver := lpv.String()
if ver != testCase.wantVersion {
t.Fatalf("Expected locked version to be '%s', got %s", testCase.wantVersion, ver)
}

if testCase.wantRevision != "" {
rev := lpv.Revision()
if rev != testCase.wantRevision {
t.Fatalf("Expected locked revision to be '%s', got %s",
testCase.wantRevision,
rev)
}
}
})
}
}
Expand Down
Loading

0 comments on commit c123f59

Please sign in to comment.