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

Apply common validations to all import conversion unit tests #992

Merged
merged 2 commits into from
Aug 14, 2017
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
174 changes: 61 additions & 113 deletions cmd/dep/glide_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,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 @@ -66,13 +57,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 @@ -91,42 +83,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: testProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
convertTestCase: &convertTestCase{
projectRoot: testProjectRoot,
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: testProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
convertTestCase: &convertTestCase{
projectRoot: testProjectRoot,
wantIgnoreCount: 1,
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
},
},
"bad input, empty package name": {
yaml: glideYaml{
Imports: []glidePackage{{Name: ""}},
},
projectRoot: testProjectRoot,
wantConvertErr: true,
convertTestCase: &convertTestCase{
projectRoot: testProjectRoot,
wantConvertErr: true,
},
},
}

Expand All @@ -147,86 +171,10 @@ 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)
}
t.Fatalf("%#v", err)
}
})
}
Expand Down
Loading