aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/print_cmdline_flag_test.go
blob: 8f6fc226e756a472f265ba79d347ace6da4211ec (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
// 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 (
	"fmt"
	"regexp"
	"testing"
)

func TestRemovePrintCmdlineArg(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
		if err := verifyArgCount(cmd, 0, "-print-cmdline"); err != nil {
			t.Error(err)
		}
	})
}

func TestPrintCompilerCommand(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
		if matched, _ := regexp.MatchString(`cd '.*' && '.*/x86_64-cros-linux-gnu-gcc.real'.*'main.cc'`, ctx.stderrString()); !matched {
			t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
		}
	})
}

func TestPrintNestedCommand(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		// Note: -clang-syntax calls clang to check the syntax
		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", "-clang-syntax", mainCc)))
		if matched, _ := regexp.MatchString(`cd '.*' && '.*usr/bin/clang'.*'main.cc'.*'-fsyntax-only'`, ctx.stderrString()); !matched {
			t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
		}
	})
}

func TestPrintCmdWd(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		printCmd(ctx, &command{
			Path: "/somepath",
		})
		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath'\n", ctx.tempDir) {
			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
		}
	})
}

func TestPrintCmdAbsolutePath(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		printCmd(ctx, &command{
			Path: "somepath",
		})
		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '%s/somepath'\n", ctx.tempDir, ctx.tempDir) {
			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
		}
	})
}

func TestPrintCmdEnvUpdates(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		printCmd(ctx, &command{
			Path:       "/somepath",
			EnvUpdates: []string{"a=b"},
		})
		if ctx.stderrString() != fmt.Sprintf("cd '%s' && env 'a=b' '/somepath'\n", ctx.tempDir) {
			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
		}
	})
}

func TestPrintCmdArgs(t *testing.T) {
	withTestContext(t, func(ctx *testContext) {
		printCmd(ctx, &command{
			Path: "/somepath",
			Args: []string{"-a"},
		})
		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath' '-a'\n", ctx.tempDir) {
			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
		}
	})
}