forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcase.go
196 lines (166 loc) · 5.12 KB
/
testcase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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 importertest
import (
"bytes"
"io/ioutil"
"log"
"sort"
"strings"
"testing"
"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/test"
"github.com/pkg/errors"
)
// TestCase is a common set of validations applied to the result
// of an importer converting from an external config format to dep's.
type TestCase struct {
DefaultConstraintFromLock bool
WantConvertErr bool
WantSourceRepo string
WantConstraint string
WantRevision gps.Revision
WantVersion string
WantIgnored []string
WantWarning string
}
// NewTestContext creates a unique context with its own GOPATH for a single test.
func NewTestContext(h *test.Helper) *dep.Ctx {
h.TempDir("src")
pwd := h.Path(".")
discardLogger := log.New(ioutil.Discard, "", 0)
return &dep.Ctx{
GOPATH: pwd,
Out: discardLogger,
Err: discardLogger,
}
}
// Execute and validate the test case.
func (tc TestCase) Execute(t *testing.T, convert func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error)) error {
h := test.NewHelper(t)
defer h.Cleanup()
// Disable parallel tests until we can resolve this error on the Windows builds:
// "remote repository at https://github.com/carolynvs/deptest-importers does not exist, or is inaccessible"
//h.Parallel()
ctx := NewTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()
// Capture stderr so we can verify warnings
output := &bytes.Buffer{}
ctx.Err = log.New(output, "", 0)
manifest, lock, convertErr := convert(ctx.Err, sm)
return tc.validate(manifest, lock, convertErr, output)
}
// validate returns an error if any of the testcase validations failed.
func (tc TestCase) 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")
}
return nil
}
if convertErr != nil {
return errors.Wrap(convertErr, "Expected the conversion to pass, but it returned an error")
}
if !equalSlice(manifest.Ignored, tc.WantIgnored) {
return errors.Errorf("unexpected set of ignored projects: \n\t(GOT) %v \n\t(WNT) %v",
manifest.Ignored, tc.WantIgnored)
}
wantConstraintCount := 0
if tc.WantConstraint != "" {
wantConstraintCount = 1
}
gotConstraintCount := len(manifest.Constraints)
if gotConstraintCount != wantConstraintCount {
return errors.Errorf("unexpected number of constraints: \n\t(GOT) %v \n\t(WNT) %v",
gotConstraintCount, wantConstraintCount)
}
if tc.WantConstraint != "" {
d, ok := manifest.Constraints[Project]
if !ok {
return errors.Errorf("Expected the manifest to have a dependency for '%v'",
Project)
}
gotConstraint := d.Constraint.String()
if gotConstraint != tc.WantConstraint {
return errors.Errorf("unexpected constraint: \n\t(GOT) %v \n\t(WNT) %v",
gotConstraint, tc.WantConstraint)
}
}
// Lock checks.
wantLockCount := 0
if tc.WantRevision != "" {
wantLockCount = 1
}
gotLockCount := 0
if lock != nil {
gotLockCount = len(lock.P)
}
if gotLockCount != wantLockCount {
return errors.Errorf("unexpected number of locked projects: \n\t(GOT) %v \n\t(WNT) %v",
gotLockCount, wantLockCount)
}
if tc.WantRevision != "" {
lp := lock.P[0]
gotProjectRoot := lp.Ident().ProjectRoot
if gotProjectRoot != Project {
return errors.Errorf("unexpected root project in lock: \n\t(GOT) %v \n\t(WNT) %v",
gotProjectRoot, Project)
}
gotSource := lp.Ident().Source
if gotSource != tc.WantSourceRepo {
return errors.Errorf("unexpected source repository: \n\t(GOT) %v \n\t(WNT) %v",
gotSource, tc.WantSourceRepo)
}
// Break down the locked "version" into a version (optional) and revision
var gotVersion string
var gotRevision gps.Revision
if lpv, ok := lp.Version().(gps.PairedVersion); ok {
gotVersion = lpv.String()
gotRevision = lpv.Revision()
} else if lr, ok := lp.Version().(gps.Revision); ok {
gotRevision = lr
} else {
return errors.New("could not determine the type of the locked version")
}
if gotRevision != tc.WantRevision {
return errors.Errorf("unexpected locked revision: \n\t(GOT) %v \n\t(WNT) %v",
gotRevision,
tc.WantRevision)
}
if gotVersion != tc.WantVersion {
return errors.Errorf("unexpected locked version: \n\t(GOT) %v \n\t(WNT) %v",
gotVersion,
tc.WantVersion)
}
}
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
}
// equalSlice is comparing two string slices for equality.
func equalSlice(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
sort.Strings(a)
sort.Strings(b)
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}