1// Copyright 2019 The Chromium OS Authors. All rights reserved.
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	"path"
9	"testing"
10)
11
12func TestOmitSysrootGivenUserDefinedSysroot(t *testing.T) {
13	withTestContext(t, func(ctx *testContext) {
14		runWithCompiler := func(compiler string) {
15			cmd := ctx.must(callCompiler(ctx, ctx.cfg,
16				ctx.newCommand(compiler, "--sysroot=/somepath", mainCc)))
17			if err := verifyArgOrder(cmd, "--sysroot=/somepath", mainCc); err != nil {
18				t.Error(err)
19			}
20			if err := verifyArgCount(cmd, 1, "--sysroot.*"); err != nil {
21				t.Error(err)
22			}
23		}
24
25		runWithCompiler(gccX86_64)
26		runWithCompiler(clangX86_64)
27	})
28}
29
30func TestSetSysrootFlagFromEnv(t *testing.T) {
31	withTestContext(t, func(ctx *testContext) {
32		ctx.env = []string{"SYSROOT=/envpath"}
33		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
34			ctx.newCommand(gccX86_64, mainCc)))
35		if err := verifyEnvUpdate(cmd, "SYSROOT="); err != nil {
36			t.Error(err)
37		}
38		if err := verifyArgOrder(cmd, "--sysroot=/envpath", mainCc); err != nil {
39			t.Error(err)
40		}
41	})
42}
43
44func TestClearEmptySysrootFlagInEnv(t *testing.T) {
45	withTestContext(t, func(ctx *testContext) {
46		ctx.env = []string{"SYSROOT="}
47		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
48			ctx.newCommand(gccX86_64, mainCc)))
49		if err := verifyEnvUpdate(cmd, "SYSROOT="); err != nil {
50			t.Error(err)
51		}
52		if err := verifyArgOrder(cmd, "--sysroot=.*/x86_64-cros-linux-gnu", mainCc); err != nil {
53			t.Error(err)
54		}
55	})
56}
57
58func TestSetSysrootRelativeToWrapperPath(t *testing.T) {
59	withTestContext(t, func(ctx *testContext) {
60		ctx.cfg.rootRelPath = "somepath"
61		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
62			ctx.newCommand(gccX86_64, mainCc)))
63		if err := verifyArgOrder(cmd,
64			"--sysroot="+ctx.tempDir+"/somepath/usr/x86_64-cros-linux-gnu", mainCc); err != nil {
65			t.Error(err)
66		}
67	})
68}
69
70func TestSetSysrootRelativeToSymlinkedWrapperPath(t *testing.T) {
71	withTestContext(t, func(ctx *testContext) {
72		ctx.cfg.rootRelPath = "somepath"
73		linkedWrapperPath := path.Join(ctx.tempDir, "a/linked/path/x86_64-cros-linux-gnu-gcc")
74		ctx.symlink(path.Join(ctx.tempDir, gccX86_64), linkedWrapperPath)
75
76		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
77			ctx.newCommand(linkedWrapperPath, mainCc)))
78		if err := verifyArgOrder(cmd,
79			"--sysroot="+ctx.tempDir+"/somepath/usr/x86_64-cros-linux-gnu", mainCc); err != nil {
80			t.Error(err)
81		}
82	})
83}
84