-
Notifications
You must be signed in to change notification settings - Fork 3
/
oya_test.go
279 lines (243 loc) · 7.2 KB
/
oya_test.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const SOPS_PGP_KEY = "317D 6971 DD80 4501 A6B8 65B9 0F1F D46E 2E8C 7202"
type SuiteContext struct {
binDir string
projectDir string
lastCommandErr error
stdout *bytes.Buffer
stderr *bytes.Buffer
}
func (c *SuiteContext) MustSetUp() {
projectDir, err := ioutil.TempDir("", "oya")
if err != nil {
panic(err)
}
setEnv(projectDir)
log.SetLevel(log.DebugLevel)
c.projectDir = projectDir
c.stdout = bytes.NewBuffer(nil)
c.stderr = bytes.NewBuffer(nil)
}
func (c *SuiteContext) MustTearDown() {
// err := os.RemoveAll(c.projectDir)
// if err != nil {
// panic(err)
// }
}
func setEnv(projectDir string) {
err := os.Setenv("OYA_HOME", projectDir)
if err != nil {
panic(err)
}
err = os.Setenv("SOPS_PGP_FP", SOPS_PGP_KEY)
if err != nil {
panic(err)
}
}
func (c *SuiteContext) writeFile(path, contents string) error {
targetPath := c.resolvePath(path)
dir := filepath.Dir(targetPath)
err := os.MkdirAll(dir, 0700)
if err != nil {
return err
}
return ioutil.WriteFile(targetPath, []byte(contents), 0600)
}
func (c *SuiteContext) readFile(path string) (string, error) {
sourcePath := c.resolvePath(path)
contents, err := ioutil.ReadFile(sourcePath)
if err != nil {
return "", err
}
return string(contents), err
}
func (c *SuiteContext) resolvePath(path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(c.projectDir, path)
}
func (c *SuiteContext) iAmInProjectDir() error {
return os.Chdir(c.projectDir)
}
func (c *SuiteContext) imInDir(subdir string) error {
return os.Chdir(subdir)
}
func (c *SuiteContext) fileContaining(path string, contents *gherkin.DocString) error {
return c.writeFile(path, contents.Content)
}
func (c *SuiteContext) environmentVariableSet(name, value string) error {
return os.Setenv(name, value)
}
func (c *SuiteContext) fileContains(path string, contents *gherkin.DocString) error {
actual, err := c.readFile(path)
if err != nil {
return err
}
if actual != contents.Content {
return fmt.Errorf("unexpected file %v contents: %q expected: %q", path, actual, contents.Content)
}
return nil
}
func (c *SuiteContext) fileDoesNotContain(path string, contents *gherkin.DocString) error {
actual, err := c.readFile(path)
if err != nil {
return err
}
re := regexp.MustCompile(".*" + contents.Content + ".*")
if len(re.FindString(actual)) > 0 {
return fmt.Errorf("unexpected file %v contents: %q NOT expected: %q", path, actual, contents.Content)
}
return nil
}
func (c *SuiteContext) fileExists(path string) error {
_, err := os.Stat(c.resolvePath(path))
return err
}
func (c *SuiteContext) fileDoesNotExist(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
return errors.Errorf("expected %v to not exist", path)
}
func (c *SuiteContext) execute(command string) error {
c.stdout.Reset()
c.stderr.Reset()
c.lastCommandErr = nil
oldArgs := os.Args
os.Args = strings.Fields(command)
defer func() {
os.Args = oldArgs
}()
cmdFlds := strings.Fields(command)
oyaBin := fmt.Sprintf("%v/%v", c.binDir, cmdFlds[0])
path := fmt.Sprintf("PATH=%v:%v", c.binDir, os.Getenv("PATH"))
cmd := exec.Command(oyaBin, cmdFlds[1:]...)
cmd.Env = append(os.Environ(), path)
cmd.Stdout = c.stdout
cmd.Stderr = c.stderr
err := cmd.Run()
if err != nil {
c.lastCommandErr = errors.New(c.stderr.String())
}
return nil
}
func (c *SuiteContext) iRunOya(command string) error {
return c.execute("oya " + command)
}
func (c *SuiteContext) modifyFileToContain(path string, contents *gherkin.DocString) error {
return c.writeFile(path, contents.Content)
}
func (c *SuiteContext) theCommandSucceeds() error {
if c.lastCommandErr != nil {
log.Println(c.stdout.String())
log.Println(c.stderr.String())
return errors.Wrap(c.lastCommandErr, "command unexpectedly failed")
}
return nil
}
func (c *SuiteContext) theCommandFailsWithError(errMsg *gherkin.DocString) error {
errMsg.Content = fmt.Sprintf("^%v$", errMsg.Content)
return c.theCommandFailsWithErrorMatching(errMsg)
}
func (c *SuiteContext) theCommandFailsWithErrorMatching(errMsg *gherkin.DocString) error {
if c.lastCommandErr == nil {
return errors.Errorf("last command unexpectedly succeeded")
}
rx := regexp.MustCompile(errMsg.Content)
if !rx.MatchString(c.lastCommandErr.Error()) {
return errors.Wrap(c.lastCommandErr,
fmt.Sprintf("unexpected error %q; expected to match %q", c.lastCommandErr, errMsg.Content))
}
return nil
}
func (c *SuiteContext) theCommandOutputs(target string, expected *gherkin.DocString) error {
var actual string
switch target {
case "stdout":
actual = c.stdout.String()
case "stderr":
actual = c.stderr.String()
default:
return fmt.Errorf("Unexpected command output target: %v", target)
}
if actual != expected.Content {
return fmt.Errorf("unexpected %v output: %q expected: %q", target, actual, expected.Content)
}
return nil
}
func (c *SuiteContext) theCommandOutputsTextMatching(target string, expected *gherkin.DocString) error {
var actual string
switch target {
case "stdout":
actual = c.stdout.String()
case "stderr":
actual = c.stderr.String()
default:
return fmt.Errorf("Unexpected command output target: %v", target)
}
rx := regexp.MustCompile(expected.Content)
if !rx.MatchString(actual) {
return fmt.Errorf("unexpected %v output: %q expected to match: %q", target, actual, expected.Content)
}
return nil
}
func (c *SuiteContext) CompileOya() {
binDir, err := ioutil.TempDir("", "oya-bin")
if err != nil {
panic(err)
}
binPath := filepath.Join(binDir, "oya")
cmd := exec.Command("go", "build", "-o", binPath, "oya.go")
err = cmd.Run()
if err != nil {
panic(err)
}
c.binDir = binDir
}
func FeatureContext(s *godog.Suite) {
c := SuiteContext{}
c.CompileOya()
s.Step(`^I'm in project dir$`, c.iAmInProjectDir)
s.Step(`^I\'m in the (.+) dir$`, c.imInDir)
s.Step(`^file (.+) containing$`, c.fileContaining)
s.Step(`^I run "oya (.+)"$`, c.iRunOya)
s.Step(`^I modify file (.+) to contain$`, c.modifyFileToContain)
s.Step(`^file (.+) contains$`, c.fileContains)
s.Step(`^file (.+) does not contain$`, c.fileDoesNotContain)
s.Step(`^file (.+) exists$`, c.fileExists)
s.Step(`^file (.+) does not exist$`, c.fileDoesNotExist)
s.Step(`^the command succeeds$`, c.theCommandSucceeds)
s.Step(`^the command fails with error$`, c.theCommandFailsWithError)
s.Step(`^the command fails with error matching$`, c.theCommandFailsWithErrorMatching)
s.Step(`^the command outputs to (stdout|stderr)$`, c.theCommandOutputs)
s.Step(`^the command outputs to (stdout|stderr) text matching$`, c.theCommandOutputsTextMatching)
s.Step(`^the ([^ ]+) environment variable set to "([^"]*)"$`, c.environmentVariableSet)
s.BeforeScenario(func(interface{}) { c.MustSetUp() })
s.AfterScenario(func(interface{}, error) { c.MustTearDown() })
// TODO after all remove c.binDir
}
// sourceFileDirectory returns the current .go source file directory.
func sourceFileDirectory() string {
_, filename, _, _ := runtime.Caller(1)
return filepath.Dir(filename)
}