aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/bisect_flag_test.go
blob: 15021327fe8dc0032f3b6fd373a4529e92bef6d2 (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
// 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"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

func TestCallBisectDriver(t *testing.T) {
	withBisectTestContext(t, func(ctx *testContext) {
		ctx.env = []string{
			"BISECT_STAGE=someBisectStage",
			"BISECT_DIR=someBisectDir",
		}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyPath(cmd, "/usr/bin/python2"); err != nil {
			t.Error(err)
		}
		if err := verifyArgOrder(cmd, "-c", regexp.QuoteMeta(bisectPythonCommand),
			"someBisectStage", "someBisectDir", filepath.Join(ctx.tempDir, gccX86_64+".real"), "--sysroot=.*", mainCc); err != nil {
			t.Error(err)
		}
	})
}

func TestCallBisectDriverWithCCache(t *testing.T) {
	withBisectTestContext(t, func(ctx *testContext) {
		ctx.cfg.useCCache = true
		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyPath(cmd, "/usr/bin/python2"); err != nil {
			t.Error(err)
		}
		if err := verifyArgCount(cmd, 1, "/usr/bin/ccache"); err != nil {
			t.Error(err)
		}
		if err := verifyEnvUpdate(cmd, "CCACHE_DIR=.*"); err != nil {
			t.Error(err)
		}
	})
}

func TestDefaultBisectDir(t *testing.T) {
	withBisectTestContext(t, func(ctx *testContext) {
		ctx.env = []string{
			"BISECT_STAGE=someBisectStage",
		}
		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
		if err := verifyArgOrder(cmd, "-c", regexp.QuoteMeta(bisectPythonCommand),
			"someBisectStage", "/tmp/sysroot_bisect"); err != nil {
			t.Error(err)
		}
	})
}

func TestForwardStdOutAndStdErrAndExitCodeFromBisect(t *testing.T) {
	withBisectTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
			fmt.Fprint(stdout, "somemessage")
			fmt.Fprint(stderr, "someerror")
			return newExitCodeError(23)
		}
		exitCode := callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc))
		if exitCode != 23 {
			t.Errorf("unexpected exit code. Got: %d", exitCode)
		}
		if ctx.stdoutString() != "somemessage" {
			t.Errorf("stdout was not forwarded. Got: %s", ctx.stdoutString())
		}
		if ctx.stderrString() != "someerror" {
			t.Errorf("stderr was not forwarded. Got: %s", ctx.stderrString())
		}
	})
}

func TestForwardGeneralErrorFromBisect(t *testing.T) {
	withBisectTestContext(t, func(ctx *testContext) {
		ctx.cmdMock = func(cmd *command, 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, "someerror") {
			t.Errorf("unexpected error. Got: %s", stderr)
		}
	})
}

func withBisectTestContext(t *testing.T, work func(ctx *testContext)) {
	withTestContext(t, func(ctx *testContext) {
		// Disable comparing to the old wrapper as that calls the bisect_driver
		// directly from python, and the new wrapper calls it via a separate
		// sub command.
		ctx.cfg.oldWrapperPath = ""
		ctx.env = []string{"BISECT_STAGE=xyz"}
		work(ctx)
	})
}