aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/remote_build_flag_test.go
blob: 23a22e1a975ff7f197892a89adf2fff2f2e7eb74 (plain)
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
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"os"
	"path"
	"reflect"
	"testing"
)

func TestCommandlineFlagParsing(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		type testCase struct {
			extraFlags []string
			// If this is nonempty, expectedValue is ignored. Otherwise, expectedValue
			// has the expected value for the flag, and expectedCommand has the expected
			// (extra) flags in the builder after filtering.
			expectedError      string
			expectedValue      string
			expectedExtraFlags []string
		}

		const flagName = "--flag"
		testCases := []testCase{
			{
				extraFlags:    nil,
				expectedError: errNoSuchCmdlineArg.Error(),
			},
			{
				extraFlags:    []string{flagName + "a"},
				expectedError: errNoSuchCmdlineArg.Error(),
			},
			{
				extraFlags:    []string{flagName},
				expectedError: "flag \"" + flagName + "\" requires a value",
			},
			{
				extraFlags:         []string{flagName, "foo"},
				expectedValue:      "foo",
				expectedExtraFlags: nil,
			},
			{
				extraFlags:         []string{flagName + "=foo"},
				expectedValue:      "foo",
				expectedExtraFlags: nil,
			},
			{
				extraFlags:         []string{flagName + "="},
				expectedValue:      "",
				expectedExtraFlags: nil,
			},
			{
				extraFlags:         []string{flagName + "=foo", flagName + "=bar"},
				expectedValue:      "foo",
				expectedExtraFlags: []string{flagName + "=bar"},
			},
		}

		for _, testCase := range testCases {
			cmd := ctx.newCommand(gccX86_64, testCase.extraFlags...)
			builder, err := newCommandBuilder(ctx, ctx.cfg, cmd)
			if err != nil {
				t.Fatalf("Failed creating a command builder: %v", err)
			}

			flagValue, err := removeOneUserCmdlineFlagWithValue(builder, flagName)
			if err != nil {
				if testCase.expectedError == "" {
					t.Errorf("given extra flags %q, got unexpected error removing %q: %v", testCase.extraFlags, flagName, err)
					continue
				}

				if e := err.Error(); e != testCase.expectedError {
					t.Errorf("given extra flags %q, got error %q; wanted %q", testCase.extraFlags, e, testCase.expectedError)
				}
				continue
			}

			if testCase.expectedError != "" {
				t.Errorf("given extra flags %q, got no error, but expected %q", testCase.extraFlags, testCase.expectedError)
				continue
			}

			if flagValue != testCase.expectedValue {
				t.Errorf("given extra flags %q, got value %q, but expected %q", testCase.extraFlags, flagValue, testCase.expectedValue)
			}

			currentFlags := []string{}
			// Chop off the first arg, which should just be the compiler
			for _, a := range builder.args {
				currentFlags = append(currentFlags, a.value)
			}

			sameFlags := (len(currentFlags) == 0 && len(testCase.expectedExtraFlags) == 0) || reflect.DeepEqual(currentFlags, testCase.expectedExtraFlags)
			if !sameFlags {
				t.Errorf("given extra flags %q, got post-removal flags %q, but expected %q", testCase.extraFlags, currentFlags, testCase.expectedExtraFlags)
			}
		}
	})
}

func TestCallGomaccIfEnvIsGivenAndValid(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		ctx.env = []string{"GOMACC_PATH=" + gomaPath}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyPath(cmd, gomaPath); err != nil {
			t.Error(err)
		}
		if err := verifyArgOrder(cmd, gccX86_64+".real", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestOmitGomaccIfEnvIsGivenButInvalid(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		if err := os.Remove(gomaPath); err != nil {
			t.Fatalf("failed removing fake goma file at %q: %v", gomaPath, err)
		}

		ctx.env = []string{"GOMACC_PATH=" + gomaPath}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
			t.Error(err)
		}
	})
}

func TestCallGomaccIfArgIsGivenAndValid(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--gomacc-path", gomaPath)))
		if err := verifyPath(cmd, gomaPath); err != nil {
			t.Error(err)
		}
		if err := verifyArgCount(cmd, 0, "--gomacc-path"); err != nil {
			t.Error(err)
		}
		if err := verifyArgCount(cmd, 0, gomaPath); err != nil {
			t.Error(err)
		}
		if err := verifyArgOrder(cmd, gccX86_64+".real", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestOmitGomaccIfArgIsGivenButInvalid(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		if err := os.Remove(gomaPath); err != nil {
			t.Fatalf("failed removing fake goma file at %q: %v", gomaPath, err)
		}

		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--gomacc-path", gomaPath)))
		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
			t.Error(err)
		}
	})
}

func TestErrorOnGomaccArgWithoutValue(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--gomacc-path")))
		if err := verifyNonInternalError(stderr, "flag \"--gomacc-path\" requires a value"); err != nil {
			t.Error(err)
		}
	})
}

func TestOmitGomaccByDefault(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
			t.Error(err)
		}
	})
}

func withGomaccTestContext(t *testing.T, f func(*testContext, string)) {
	withTestContext(t, func(ctx *testContext) {
		gomaPath := path.Join(ctx.tempDir, "gomacc")
		// Create a file so the gomacc path is valid.
		ctx.writeFile(gomaPath, "")
		f(ctx, gomaPath)
	})
}

func TestRewrapperDefersToTheWrapperProperly(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--rewrapper-path", "/rewrapper", "--rewrapper-cfg", "/some-cfg", "some", "other", "args")))
		if err := verifyPath(cmd, "/rewrapper"); err != nil {
			t.Error(err)
		}
		if err := verifyArgOrder(cmd, "-cfg", "/some-cfg", gccX86_64+".real", mainCc, "some", "other", "args"); err != nil {
			t.Error(err)
		}
	})
}

func TestRewrapperCfgMustBePrsentIfRewrapperPathIs(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--rewrapper-path", "/rewrapper")))
		if err := verifyNonInternalError(stderr, "--rewrapper-cfg must be specified if --rewrapper-path is"); err != nil {
			t.Error(err)
		}
	})
}

func TestRewrapperPathMustBePrsentIfRewrapperCfgIs(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--rewrapper-cfg", "/some-cfg")))
		if err := verifyNonInternalError(stderr, "--rewrapper-path must be specified if --rewrapper-cfg is"); err != nil {
			t.Error(err)
		}
	})
}

func TestRewrapperAndGomaAreMutuallyExclusive(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--rewrapper-path", "/rewrapper", "--rewrapper-cfg", "/some-cfg", "--gomacc-path", gomaPath)))
		if err := verifyNonInternalError(stderr, "rewrapper and gomacc are mutually exclusive"); err != nil {
			t.Error(err)
		}
	})
}

func TestRewrapperBlocksGomaInheritanceFromEnv(t *testing.T) {
	withGomaccTestContext(t, func(ctx *testContext, gomaPath string) {
		ctx.env = []string{"GOMACC_PATH=" + gomaPath}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc, "--rewrapper-path", "/rewrapper", "--rewrapper-cfg", "/some-cfg")))
		if err := verifyPath(cmd, "/rewrapper"); err != nil {
			t.Error(err)
		}
		if err := verifyArgOrder(cmd, "-cfg", "/some-cfg", gccX86_64+".real", mainCc); err != nil {
			t.Error(err)
		}
	})
}