xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/print_cmdline_flag_test.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"regexp"
10	"testing"
11)
12
13func TestRemovePrintCmdlineArg(t *testing.T) {
14	withTestContext(t, func(ctx *testContext) {
15		cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
16		if err := verifyArgCount(cmd, 0, "-print-cmdline"); err != nil {
17			t.Error(err)
18		}
19	})
20}
21
22func TestPrintCompilerCommand(t *testing.T) {
23	withTestContext(t, func(ctx *testContext) {
24		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
25		if matched, _ := regexp.MatchString(`cd '.*' && '.*/x86_64-cros-linux-gnu-gcc.real'.*'main.cc'`, ctx.stderrString()); !matched {
26			t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
27		}
28	})
29}
30
31func TestPrintNestedCommand(t *testing.T) {
32	withTestContext(t, func(ctx *testContext) {
33		// Note: -clang-syntax calls clang to check the syntax
34		ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", "-clang-syntax", mainCc)))
35		if matched, _ := regexp.MatchString(`cd '.*' && '.*usr/bin/clang'.*'main.cc'.*'-fsyntax-only'`, ctx.stderrString()); !matched {
36			t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
37		}
38	})
39}
40
41func TestPrintCmdWd(t *testing.T) {
42	withTestContext(t, func(ctx *testContext) {
43		printCmd(ctx, &command{
44			Path: "/somepath",
45		})
46		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath'\n", ctx.tempDir) {
47			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
48		}
49	})
50}
51
52func TestPrintCmdAbsolutePath(t *testing.T) {
53	withTestContext(t, func(ctx *testContext) {
54		printCmd(ctx, &command{
55			Path: "somepath",
56		})
57		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '%s/somepath'\n", ctx.tempDir, ctx.tempDir) {
58			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
59		}
60	})
61}
62
63func TestPrintCmdEnvUpdates(t *testing.T) {
64	withTestContext(t, func(ctx *testContext) {
65		printCmd(ctx, &command{
66			Path:       "/somepath",
67			EnvUpdates: []string{"a=b"},
68		})
69		if ctx.stderrString() != fmt.Sprintf("cd '%s' && env 'a=b' '/somepath'\n", ctx.tempDir) {
70			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
71		}
72	})
73}
74
75func TestPrintCmdArgs(t *testing.T) {
76	withTestContext(t, func(ctx *testContext) {
77		printCmd(ctx, &command{
78			Path: "/somepath",
79			Args: []string{"-a"},
80		})
81		if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath' '-a'\n", ctx.tempDir) {
82			t.Errorf("unexpected result. Got: %s", ctx.stderrString())
83		}
84	})
85}
86