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

Commit

Permalink
test(status): Add TestCollectConstraints
Browse files Browse the repository at this point in the history
  • Loading branch information
darkowlzz committed Nov 2, 2017
1 parent 693a3ed commit 7bef6fc
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions cmd/dep/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ package main

import (
"bytes"
"io/ioutil"
"log"
"reflect"
"testing"
"text/tabwriter"

"strings"

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/test"
)

func TestStatusFormatVersion(t *testing.T) {
Expand Down Expand Up @@ -287,3 +291,109 @@ func TestBasicStatusGetConsolidatedLatest(t *testing.T) {
})
}
}

func TestCollectConstraints(t *testing.T) {
ver1, _ := gps.NewSemverConstraintIC("v1.0.0")
ver08, _ := gps.NewSemverConstraintIC("v0.8.0")
ver2, _ := gps.NewSemverConstraintIC("v2.0.0")

cases := []struct {
name string
project dep.Project
wantConstraints map[string][]gps.Constraint
}{
{
name: "without any constraints",
project: dep.Project{
Lock: &dep.Lock{
P: []gps.LockedProject{
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
gps.NewVersion("v1.0.0"),
[]string{"."},
),
},
},
},
wantConstraints: map[string][]gps.Constraint{},
},
{
name: "with multiple constraints",
project: dep.Project{
Lock: &dep.Lock{
P: []gps.LockedProject{
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
gps.NewVersion("v1.0.0"),
[]string{"."},
),
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
gps.NewVersion("v0.1.0"),
[]string{"."},
),
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")),
[]string{"."},
),
},
},
},
wantConstraints: map[string][]gps.Constraint{
"github.com/sdboyer/deptest": []gps.Constraint{ver1, ver08},
"github.com/sdboyer/deptestdos": []gps.Constraint{ver2},
"github.com/sdboyer/dep-test": []gps.Constraint{ver1},
},
},
{
name: "skip projects with invalid versions",
project: dep.Project{
Lock: &dep.Lock{
P: []gps.LockedProject{
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
gps.NewVersion("v0.1.0"),
[]string{"."},
),
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
gps.NewVersion("v1.0.0"),
[]string{"."},
),
},
},
},
wantConstraints: map[string][]gps.Constraint{
"github.com/sdboyer/deptest": []gps.Constraint{ver1},
},
},
}

h := test.NewHelper(t)
defer h.Cleanup()

h.TempDir("src")
pwd := h.Path(".")
discardLogger := log.New(ioutil.Discard, "", 0)

ctx := &dep.Ctx{
GOPATH: pwd,
Out: discardLogger,
Err: discardLogger,
}

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

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
gotConstraints := collectConstraints(ctx, &c.project, sm)

if !reflect.DeepEqual(gotConstraints, c.wantConstraints) {
t.Fatalf("Unexpected collected constraints: \n\t(GOT): %v\n\t(WNT): %v", gotConstraints, c.wantConstraints)
}
})
}
}

0 comments on commit 7bef6fc

Please sign in to comment.