-
Notifications
You must be signed in to change notification settings - Fork 199
/
helpers.go
144 lines (120 loc) · 2.95 KB
/
helpers.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
package context
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"get.porter.sh/porter/pkg/test"
"github.com/spf13/afero"
)
type TestContext struct {
*Context
input *bytes.Buffer
output *bytes.Buffer
T *testing.T
}
// NewTestContext initializes a configuration suitable for testing, with the output buffered, and an in-memory file system.
func NewTestContext(t *testing.T) *TestContext {
// Provide a way for tests to provide and capture stdin and stdout
input := &bytes.Buffer{}
output := &bytes.Buffer{}
// Copy output to the test log simultaneously, use go test -v to see the output
aggOutput := io.MultiWriter(output, test.Logger{T: t})
c := &TestContext{
Context: &Context{
Debug: true,
FileSystem: &afero.Afero{Fs: afero.NewMemMapFs()},
In: input,
Out: aggOutput,
Err: aggOutput,
NewCommand: NewTestCommand(),
},
output: output,
T: t,
}
return c
}
func NewTestCommand() CommandBuilder {
return func(command string, args ...string) *exec.Cmd {
testArgs := append([]string{command}, args...)
cmd := exec.Command(os.Args[0], testArgs...)
cmd.Env = []string{
fmt.Sprintf("%s=true", test.MockedCommandEnv),
fmt.Sprintf("%s=%s", test.ExpectedCommandEnv, os.Getenv(test.ExpectedCommandEnv)),
}
return cmd
}
}
// TODO: Replace these functions with a union file system for test data
func (c *TestContext) AddTestFile(src, dest string) []byte {
data, err := ioutil.ReadFile(src)
if err != nil {
c.T.Fatal(err)
}
err = c.FileSystem.WriteFile(dest, data, os.ModePerm)
if err != nil {
c.T.Fatal(err)
}
return data
}
func (c *TestContext) AddTestFileContents(file []byte, dest string) error {
return c.FileSystem.WriteFile(dest, file, os.ModePerm)
}
func (c *TestContext) AddTestDirectory(srcDir, destDir string) {
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip the root src directory
if path == srcDir {
return nil
}
// Translate the path from the src to the final destination
dest := filepath.Join(destDir, strings.TrimPrefix(path, srcDir))
if info.IsDir() {
return c.FileSystem.MkdirAll(dest, os.ModePerm)
}
c.AddTestFile(path, dest)
return nil
})
if err != nil {
c.T.Fatal(err)
}
}
func (c *TestContext) GetOutput() string {
return string(c.output.Bytes())
}
func (c *TestContext) FindBinDir() string {
var binDir string
d, err := os.Getwd()
if err != nil {
c.T.Fatal(err)
}
for {
binDir = c.getBinDir(d)
if binDir != "" {
return binDir
}
d = filepath.Dir(d)
if d == "." || d == "" {
c.T.Fatal("could not find the bin directory")
}
}
}
func (c *TestContext) getBinDir(dir string) string {
children, err := ioutil.ReadDir(dir)
if err != nil {
c.T.Fatal(err)
}
for _, child := range children {
if child.IsDir() && child.Name() == "bin" {
return filepath.Join(dir, child.Name())
}
}
return ""
}