forked from golang/dep
/
integration_testcase.go
185 lines (165 loc) · 5.12 KB
/
integration_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
// Copyright 2017 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 test
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"unicode"
)
var (
UpdateGolden *bool = flag.Bool("update", false, "update golden files")
)
// IntegrationTestCase manages a test case directory structure and content
type IntegrationTestCase struct {
t *testing.T
name string
rootPath string
initialPath string
finalPath string
Commands [][]string `json:"commands"`
ErrorExpected string `json:"error-expected"`
GopathInitial map[string]string `json:"gopath-initial"`
VendorInitial map[string]string `json:"vendor-initial"`
VendorFinal []string `json:"vendor-final"`
InitPath string `json:"init-path"`
}
// NewTestCase creates a new IntegrationTestCase.
func NewTestCase(t *testing.T, dir, name string) *IntegrationTestCase {
rootPath := filepath.FromSlash(filepath.Join(dir, name))
n := &IntegrationTestCase{
t: t,
name: name,
rootPath: rootPath,
initialPath: filepath.Join(rootPath, "initial"),
finalPath: filepath.Join(rootPath, "final"),
}
j, err := ioutil.ReadFile(filepath.Join(rootPath, "testcase.json"))
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(j, n)
if err != nil {
t.Fatal(err)
}
return n
}
func (tc *IntegrationTestCase) InitialPath() string {
return tc.initialPath
}
// UpdateFile updates the golden file with the working result.
func (tc *IntegrationTestCase) UpdateFile(goldenPath, workingPath string) {
exists, working, err := getFile(workingPath)
if err != nil {
tc.t.Fatalf("Error reading project file %s: %s", goldenPath, err)
}
golden := filepath.Join(tc.finalPath, goldenPath)
if exists {
if err := tc.WriteFile(golden, working); err != nil {
tc.t.Fatal(err)
}
} else {
err := os.Remove(golden)
if err != nil && !os.IsNotExist(err) {
tc.t.Fatal(err)
}
}
}
// CompareFile compares the golden file with the working result.
func (tc *IntegrationTestCase) CompareFile(goldenPath, working string) {
golden := filepath.Join(tc.finalPath, goldenPath)
gotExists, got, err := getFile(working)
if err != nil {
tc.t.Fatalf("Error reading project file %s: %s", goldenPath, err)
}
wantExists, want, err := getFile(golden)
if err != nil {
tc.t.Fatalf("Error reading testcase file %s: %s", goldenPath, err)
}
if wantExists && gotExists {
if want != got {
tc.t.Errorf("expected %s, got %s", want, got)
}
} else if !wantExists && gotExists {
tc.t.Errorf("%s created where none was expected", goldenPath)
} else if wantExists && !gotExists {
tc.t.Errorf("%s not created where one was expected", goldenPath)
}
}
// CompareError compares expected and actual stdout output
func (tc *IntegrationTestCase) CompareOutput(stdout string) {
expected, err := ioutil.ReadFile(filepath.Join(tc.rootPath, "stdout.txt"))
if err != nil {
if os.IsNotExist(err) {
// Nothing to verify
return
}
panic(err)
}
expStr := normalizeLines(string(expected))
stdout = normalizeLines(stdout)
if expStr != stdout {
tc.t.Errorf("(WNT):\n%s\n(GOT):\n%s\n", expStr, stdout)
}
}
// normalizeLines returns a version with trailing whitespace stripped from each line.
func normalizeLines(s string) string {
lines := strings.Split(s, "\n")
for i := range lines {
lines[i] = strings.TrimRightFunc(lines[i], unicode.IsSpace)
}
return strings.Join(lines, "\n")
}
// CompareError compares exected and actual error
func (tc *IntegrationTestCase) CompareError(err error, stderr string) {
wantExists, want := tc.ErrorExpected != "", tc.ErrorExpected
gotExists, got := stderr != "" && err != nil, stderr
if wantExists && gotExists {
switch c := strings.Count(got, want); c {
case 0:
tc.t.Errorf("error did not contain expected string:\n\t(GOT): %s\n\t(WNT): %s", got, want)
case 1:
default:
tc.t.Errorf("expected error %s matches %d times to actual error %s", want, c, got)
}
} else if !wantExists && gotExists {
tc.t.Fatalf("error raised where none was expected: \n%v", stderr)
} else if wantExists && !gotExists {
tc.t.Error("error not raised where one was expected:", want)
}
}
func (tc *IntegrationTestCase) CompareVendorPaths(gotVendorPaths []string) {
if *UpdateGolden {
tc.VendorFinal = gotVendorPaths
} else {
wantVendorPaths := tc.VendorFinal
if len(gotVendorPaths) != len(wantVendorPaths) {
tc.t.Fatalf("Wrong number of vendor paths created: want %d got %d", len(wantVendorPaths), len(gotVendorPaths))
}
for ind := range gotVendorPaths {
if gotVendorPaths[ind] != wantVendorPaths[ind] {
tc.t.Errorf("Mismatch in vendor paths created: want %s got %s", wantVendorPaths, gotVendorPaths)
}
}
}
}
func (tc *IntegrationTestCase) WriteFile(src string, content string) error {
err := ioutil.WriteFile(src, []byte(content), 0666)
return err
}
func getFile(path string) (bool, string, error) {
_, err := os.Stat(path)
if err != nil {
return false, "", nil
}
f, err := ioutil.ReadFile(path)
if err != nil {
return true, "", err
}
return true, string(f), nil
}