forked from dcjones/mk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmk_test.go
229 lines (212 loc) · 4.88 KB
/
mk_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
package main
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"testing"
"github.com/google/go-cmp/cmp"
)
type testvector struct {
input string
output string
errors string
passes bool
}
// For each test vector, confirm that it matches
func TestBasicMaking(t *testing.T) {
tests := []testvector{
{
// Really basic mk operation.
input: "testdata/test1.mk",
output: "testdata/test1.mk.expected",
errors: "",
passes: true,
},
{
// Environment variables are expanded in dependencies
input: "testdata/test2.mk",
output: "testdata/test2.mk.expected",
errors: "",
passes: true,
},
{
// Variables defined in the mkfile are expanded in dependencies
input: "testdata/test3.mk",
output: "testdata/test3.mk.expected",
errors: "",
passes: true,
},
{
// Pair of dependencies in an variable are expanded.
input: "testdata/test4.mk",
output: "testdata/test4.mk.expected",
errors: "",
passes: true,
},
{
// \ can escape newlines.
input: "testdata/test5.mk",
output: "testdata/test5.mk.expected",
errors: "",
passes: true,
},
{
// variables can be set included from another file
input: "testdata/test6.mk",
output: "testdata/test6.mk.expected",
errors: "",
passes: true,
},
{
// $vars are not expanded in import statements. expected to fail.
input: "testdata/test7.mk",
output: "testdata/test7.mk.expected",
errors: "",
passes: true,
},
{
// Variables expanded in recipes.
input: "testdata/test8.mk",
output: "testdata/test8.mk.expected",
errors: "",
passes: true,
},
{
// EOF can end a variable if no \n present.
input: "testdata/test9.mk",
output: "testdata/test9.mk.expected",
errors: "",
passes: true,
},
{
// External commands can generate variables
input: "testdata/test10.mk",
output: "testdata/test10.mk.expected",
errors: "",
passes: true,
},
{
// mkfile variables are expanded in backquote substitution
input: "testdata/test11.mk",
output: "testdata/test11.mk.expected",
errors: "",
passes: true,
},
{
// mkfile variables are expanded in backquote substitution
input: "testdata/test13.mk",
output: "testdata/test13.mk.expected",
errors: "",
passes: false,
},
{
// Rules can be created by pipeing commands
input: "testdata/test14.mk",
output: "testdata/test14.mk.expected",
errors: "",
passes: true,
},
{
// Test alternative recipe shell
input: "testdata/test15.mk",
output: "testdata/test15.mk.expected",
errors: "",
passes: true,
},
{
// Test alternative recipe shell
input: "testdata/test16.mk",
output: "testdata/test16.mk.expected",
errors: "",
passes: true,
},
{
// Test
input: "testdata/test17.mk",
output: "testdata/test17.mk.expected",
errors: "",
passes: true,
},
}
for _, tv := range tests {
// TODO(rjk): Validate generated errors.
got, _, err := startMk("-n", "-f", tv.input)
if err != nil {
if !tv.passes {
t.Logf("%s expected failure", tv.input)
t.Logf("%s exec failed: %v", tv.input, err)
} else {
t.Errorf("%s exec failed: %v", tv.input, err)
}
}
efd, err := os.Open(tv.output)
if err != nil {
t.Errorf("%s can't open: %v", tv.input, err)
continue
}
want, err := ioutil.ReadAll(efd)
if err != nil {
t.Errorf("%s can't read: %v", tv.input, err)
continue
}
// TODO(rjk): Read expected errors if they exist.
if diff := cmp.Diff(string(want), string(got)); diff != "" {
if !tv.passes {
t.Logf("%s expected failure", tv.input)
t.Logf("%s: mismatch (-want +got):\n%s", tv.input, diff)
} else {
t.Errorf("%s: mismatch (-want +got):\n%s", tv.input, diff)
}
}
}
}
// Make sure that recipes get mk variables as environment.
func TestRecipesHaveEnv(t *testing.T) {
input := "testdata/test12.mk"
got, _, err := startMk("-f", input)
if err != nil {
t.Errorf("%s exec failed: %v", input, err)
}
// Make sure that the output has the right variables in it.
// got should be the contents of the environment.
envs := make([]string, 0)
for _, b := range bytes.Split(got, []byte("\n")) {
envs = append(envs, string(b))
}
outer:
for _, ekv := range []string{
"bar=thebigness",
"TEST_MAIN=mk",
"shell=sh",
} {
for _, v := range envs {
if v == ekv {
continue outer
}
}
t.Errorf("%s: output missing %s", input, ekv)
}
}
func TestMain(m *testing.M) {
switch os.Getenv("TEST_MAIN") {
case "mk":
main()
default:
e := m.Run()
os.Exit(e)
}
}
func startMk(args ...string) ([]byte, []byte, error) {
outbuffy := new(bytes.Buffer)
errbuffy := new(bytes.Buffer)
mkcmd := exec.Command(os.Args[0], args...)
mkcmd.Env = append(os.Environ(), "TEST_MAIN=mk")
mkcmd.Stdout = outbuffy
mkcmd.Stderr = errbuffy
// log.Println("mkcmd", mkcmd)
if err := mkcmd.Run(); err != nil {
return nil, nil, err
}
return outbuffy.Bytes(), errbuffy.Bytes(), nil
}