aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/compiler_wrapper_test.go
blob: 71cd36dfc73c81127e9cf6719101abf55eb5ec33 (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
// 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 (
	"bytes"
	"errors"
	"fmt"
	"io"
	"path/filepath"
	"strings"
	"syscall"
	"testing"
)

func TestAddCommonFlags(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.commonFlags = []string{"-someflag"}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyArgOrder(cmd, "-someflag", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestAddGccConfigFlags(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.gccFlags = []string{"-someflag"}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyArgOrder(cmd, "-someflag", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestAddClangConfigFlags(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.clangFlags = []string{"-someflag"}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
			ctx.newCommand(clangX86_64, mainCc)))
		if err := verifyArgOrder(cmd, "-someflag", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestLogGeneralExecError(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		testOldWrapperPaths := []string{
			"",
			filepath.Join(ctx.tempDir, "fakewrapper"),
		}
		for _, testOldWrapperPath := range testOldWrapperPaths {
			ctx.cfg.oldWrapperPath = testOldWrapperPath
			// Note: No need to write the old wrapper as we don't execute
			// it due to the general error from the new error.
			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(gccX86_64, mainCc)))
			if err := verifyInternalError(stderr); err != nil {
				t.Fatal(err)
			}
			if !strings.Contains(stderr, gccX86_64) {
				t.Errorf("could not find compiler path on stderr. Got: %s", stderr)
			}
			if !strings.Contains(stderr, "someerror") {
				t.Errorf("could not find original error on stderr. Got: %s", stderr)
			}
		}
	})
}

func TestForwardStdin(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		io.WriteString(&ctx.stdinBuffer, "someinput")
		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)
			}
			return nil
		}
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-", mainCc)))
	})
}

func TestLogMissingCCacheExecError(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.useCCache = true

		testOldWrapperPaths := []string{
			"",
			filepath.Join(ctx.tempDir, "fakewrapper"),
		}
		for _, testOldWrapperPath := range testOldWrapperPaths {
			ctx.cfg.oldWrapperPath = testOldWrapperPath
			// Note: No need to write the old wrapper as we don't execute
			// it due to the general error from the new error.
			ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
				return syscall.ENOENT
			}
			ctx.stderrBuffer.Reset()
			stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
			if err := verifyNonInternalError(stderr, "ccache not found under .*. Please install it"); err != nil {
				t.Fatal(err)
			}
		}
	})
}

func TestLogExitCodeErrorWhenComparingToOldWrapper(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.cfg.mockOldWrapperCmds = false
		ctx.cfg.oldWrapperPath = filepath.Join(ctx.tempDir, "fakewrapper")

		ctx.cmdMock = func(cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
			writePythonMockWrapper(ctx, &mockWrapperConfig{
				Cmds: []*mockWrapperCmd{
					{
						Path:     cmd.Path,
						Args:     cmd.Args,
						ExitCode: 2,
					},
				},
			})
			fmt.Fprint(stderr, "someerror")
			return newExitCodeError(2)
		}

		exitCode := callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc))
		if exitCode != 2 {
			t.Fatalf("Expected exit code 2. Got: %d", exitCode)
		}
		if err := verifyNonInternalError(ctx.stderrString(), "someerror"); err != nil {
			t.Fatal(err)
		}
	})
}

func TestErrorOnLogRusageAndForceDisableWError(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.env = []string{
			"FORCE_DISABLE_WERROR=1",
			"GETRUSAGE=rusage.log",
		}
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyNonInternalError(stderr, "GETRUSAGE is meaningless with FORCE_DISABLE_WERROR"); err != nil {
			t.Error(err)
		}
	})
}

func TestErrorOnLogRusageAndBisect(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.env = []string{
			"BISECT_STAGE=xyz",
			"GETRUSAGE=rusage.log",
		}
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyNonInternalError(stderr, "BISECT_STAGE is meaningless with GETRUSAGE"); err != nil {
			t.Error(err)
		}
	})
}

func TestErrorOnBisectAndForceDisableWError(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.env = []string{
			"BISECT_STAGE=xyz",
			"FORCE_DISABLE_WERROR=1",
		}
		stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyNonInternalError(stderr, "BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR"); err != nil {
			t.Error(err)
		}
	})
}

func TestPrintUserCompilerError(t *testing.T) {
	buffer := bytes.Buffer{}
	printCompilerError(&buffer, newUserErrorf("abcd"))
	if buffer.String() != "abcd\n" {
		t.Errorf("Unexpected string. Got: %s", buffer.String())
	}
}

func TestPrintOtherCompilerError(t *testing.T) {
	buffer := bytes.Buffer{}
	printCompilerError(&buffer, errors.New("abcd"))
	if buffer.String() != "Internal error. Please report to chromeos-toolchain@google.com.\nabcd\n" {
		t.Errorf("Unexpected string. Got: %s", buffer.String())
	}
}