aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/compile_with_fallback_test.go
blob: f9da441ab0bdd557fb0782e456fe19476153df08 (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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2019 The Chromium OS 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 main

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"syscall"
	"testing"
)

// Save this off before goroutines start running, since this necessarily involves modifying the
// value for our umask, and that screams subtle race conditions. :)
var umaskAtStartup = func() os.FileMode {
	umask := syscall.Umask(0)
	syscall.Umask(umask)
	return os.FileMode(umask)
}()

func TestOmitFallbackCompileForSuccessfulCall(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
		if ctx.cmdCount != 1 {
			t.Errorf("expected 1 call. Got: %d", ctx.cmdCount)
		}
	})
}

func TestOmitFallbackCompileForGeneralError(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			return errors.New("someerror")
		}
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
		if err := verifyInternalError(stderr); err != nil {
			t.Fatal(err)
		}
		if !strings.Contains(stderr, "someerror") {
			t.Errorf("unexpected error. Got: %s", stderr)
		}
		if ctx.cmdCount != 1 {
			t.Errorf("expected 1 call. Got: %d", ctx.cmdCount)
		}
	})
}

func TestCompileWithFallbackForNonZeroExitCode(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				return newExitCodeError(1)
			case 2:
				if err := verifyPath(cmd, "fallback_compiler/clang"); err != nil {
					return err
				}
				if err := verifyEnvUpdate(cmd, "ANDROID_LLVM_PREBUILT_COMPILER_PATH="); err != nil {
					return err
				}
				return nil
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
		if ctx.cmdCount != 2 {
			t.Errorf("expected 2 calls. Got: %d", ctx.cmdCount)
		}
	})
}

func TestCompileWithFallbackForwardStdoutAndStderr(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				fmt.Fprint(stdout, "originalmessage")
				fmt.Fprint(stderr, "originalerror")
				return newExitCodeError(1)
			case 2:
				fmt.Fprint(stdout, "fallbackmessage")
				fmt.Fprint(stderr, "fallbackerror")
				return nil
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
		if err := verifyNonInternalError(ctx.stderrString(), "originalerrorfallbackerror"); err != nil {
			t.Error(err)
		}
		if !strings.Contains(ctx.stdoutString(), "originalmessagefallbackmessage") {
			t.Errorf("unexpected stdout. Got: %s", ctx.stdoutString())
		}
	})
}

func TestForwardGeneralErrorWhenFallbackCompileFails(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				return newExitCodeError(1)
			case 2:
				return errors.New("someerror")
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))
		if err := verifyInternalError(stderr); err != nil {
			t.Error(err)
		}
		if !strings.Contains(stderr, "someerror") {
			t.Errorf("unexpected stderr. Got: %s", stderr)
		}
	})
}

func TestForwardExitCodeWhenFallbackCompileFails(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				return newExitCodeError(1)
			case 2:
				return newExitCodeError(2)
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		exitCode := callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc))
		if exitCode != 2 {
			t.Errorf("unexpected exit code. Got: %d", exitCode)
		}
	})
}

func TestForwardStdinToFallbackCompile(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			stdinStr := ctx.readAllString(stdin)
			if stdinStr != "someinput" {
				return fmt.Errorf("unexpected stdin. Got: %s", stdinStr)
			}

			switch ctx.cmdCount {
			case 1:
				return newExitCodeError(1)
			case 2:
				return nil
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		io.WriteString(&ctx.stdinBuffer, "someinput")
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, "-", mainCc)))
	})
}

func TestCompileWithFallbackExtraArgs(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		testData := []struct {
			compiler        string
			expectExtraArgs bool
		}{
			{"./clang", true},
			{"./clang++", true},
			{"./clang-tidy", false},
		}
		ctx.env = append(ctx.env, "ANDROID_LLVM_FALLBACK_DISABLED_WARNINGS=-a -b")
		extraArgs := []string{"-fno-color-diagnostics", "-a", "-b"}
		for _, tt := range testData {
			ctx.cmdCount = 0
			ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
				switch ctx.cmdCount {
				case 1:
					if tt.expectExtraArgs {
						if err := verifyArgOrder(cmd, extraArgs...); err != nil {
							return err
						}
					} else {
						for _, arg := range extraArgs {
							if err := verifyArgCount(cmd, 0, arg); err != nil {
								return err
							}
						}
					}
					return newExitCodeError(1)
				case 2:
					for _, arg := range extraArgs {
						if err := verifyArgCount(cmd, 0, arg); err != nil {
							return err
						}
					}
					return nil
				default:
					t.Fatalf("unexpected command: %#v", cmd)
					return nil
				}
			}
			ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(tt.compiler, mainCc)))
			if ctx.cmdCount != 2 {
				t.Errorf("expected 2 calls. Got: %d", ctx.cmdCount)
			}
		}
	})
}

func TestCompileWithFallbackLogCommandAndErrors(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.NoteTestReadsFromUmask()

		ctx.env = append(ctx.env, "ANDROID_LLVM_FALLBACK_DISABLED_WARNINGS=-a -b")
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				fmt.Fprint(stderr, "someerror\n")
				return newExitCodeError(1)
			case 2:
				return nil
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))

		log := readCompileWithFallbackErrorLog(ctx)
		if log != `==================COMMAND:====================
./clang.real main.cc -fno-color-diagnostics -a -b

someerror
==============================================

` {
			t.Errorf("unexpected log. Got: %s", log)
		}

		entry, _ := os.Lstat(filepath.Join(ctx.tempDir, "fallback_stderr"))
		if entry.Mode()&0777 != 0644 & ^umaskAtStartup {
			t.Errorf("unexpected mode for logfile. Got: %#o", entry.Mode())
		}
	})
}

func TestCompileWithFallbackAppendToLog(t *testing.T) {
	withCompileWithFallbackTestContext(t, func(ctx *testContext) {
		ctx.writeFile(filepath.Join(ctx.tempDir, "fallback_stderr"), "oldContent\n")
		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			switch ctx.cmdCount {
			case 1:
				return newExitCodeError(1)
			case 2:
				return nil
			default:
				t.Fatalf("unexpected command: %#v", cmd)
				return nil
			}
		}
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(clangAndroid, mainCc)))

		log := readCompileWithFallbackErrorLog(ctx)
		if !strings.Contains(log, "oldContent") {
			t.Errorf("old content not present: %s", log)
		}
		if !strings.Contains(log, "clang.real") {
			t.Errorf("new content not present: %s", log)
		}
	})
}

func withCompileWithFallbackTestContext(t *testing.T, work func(ctx *testContext)) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.isAndroidWrapper = true
		ctx.env = []string{
			"ANDROID_LLVM_PREBUILT_COMPILER_PATH=fallback_compiler",
			"ANDROID_LLVM_STDERR_REDIRECT=" + filepath.Join(ctx.tempDir, "fallback_stderr"),
		}
		work(ctx)
	})
}

func readCompileWithFallbackErrorLog(ctx *testContext) string {
	logFile := filepath.Join(ctx.tempDir, "fallback_stderr")
	data, err := ioutil.ReadFile(logFile)
	if err != nil {
		ctx.t.Fatalf("error reading log file %s: %s", logFile, err)
	}
	return string(data)
}