• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"testing"
9)
10
11func TestCallCCacheGivenConfig(t *testing.T) {
12	withCCacheEnabledTestContext(t, func(ctx *testContext) {
13		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
14			ctx.newCommand(gccX86_64, mainCc)))
15		if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil {
16			t.Error(err)
17		}
18		if err := verifyArgOrder(cmd, gccX86_64+".real", mainCc); err != nil {
19			t.Error(err)
20		}
21	})
22}
23
24func TestNotCallCCacheGivenConfig(t *testing.T) {
25	withTestContext(t, func(ctx *testContext) {
26		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
27			ctx.newCommand(gccX86_64, mainCc)))
28		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
29			t.Error(err)
30		}
31	})
32}
33
34func TestNotCallCCacheGivenConfigAndNoCCacheArg(t *testing.T) {
35	withCCacheEnabledTestContext(t, func(ctx *testContext) {
36		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
37			ctx.newCommand(gccX86_64, "-noccache", mainCc)))
38		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
39			t.Error(err)
40		}
41		if err := verifyArgCount(cmd, 0, "-noccache"); err != nil {
42			t.Error(err)
43		}
44	})
45}
46
47func TestSetCacheDir(t *testing.T) {
48	withCCacheEnabledTestContext(t, func(ctx *testContext) {
49		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
50			ctx.newCommand(gccX86_64, mainCc)))
51		if err := verifyEnvUpdate(cmd, "CCACHE_DIR=/var/cache/distfiles/ccache"); err != nil {
52			t.Error(err)
53		}
54	})
55}
56
57func TestSetCacheUmask(t *testing.T) {
58	withCCacheEnabledTestContext(t, func(ctx *testContext) {
59		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
60			ctx.newCommand(gccX86_64, mainCc)))
61		if err := verifyEnvUpdate(cmd, "CCACHE_UMASK=002"); err != nil {
62			t.Error(err)
63		}
64	})
65}
66
67func TestUpdateSandboxRewriteWithValue(t *testing.T) {
68	withCCacheEnabledTestContext(t, func(ctx *testContext) {
69		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
70			ctx.newCommand(gccX86_64, mainCc)))
71		if err := verifyNoEnvUpdate(cmd, "SANDBOX_WRITE"); err != nil {
72			t.Error(err)
73		}
74
75		ctx.env = []string{"SANDBOX_WRITE=xyz"}
76		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
77			ctx.newCommand(gccX86_64, mainCc)))
78		if err := verifyEnvUpdate(cmd,
79			"SANDBOX_WRITE=xyz:/var/cache/distfiles/ccache"); err != nil {
80			t.Error(err)
81		}
82	})
83}
84
85func TestUpdateSandboxRewriteWithoutValue(t *testing.T) {
86	withCCacheEnabledTestContext(t, func(ctx *testContext) {
87		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
88			ctx.newCommand(gccX86_64, mainCc)))
89		if err := verifyNoEnvUpdate(cmd, "SANDBOX_WRITE"); err != nil {
90			t.Error(err)
91		}
92
93		ctx.env = []string{"SANDBOX_WRITE="}
94		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
95			ctx.newCommand(gccX86_64, mainCc)))
96		if err := verifyEnvUpdate(cmd,
97			"SANDBOX_WRITE=:/var/cache/distfiles/ccache"); err != nil {
98			t.Error(err)
99		}
100	})
101}
102
103func TestClearCCacheDisableWithValue(t *testing.T) {
104	withCCacheEnabledTestContext(t, func(ctx *testContext) {
105		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
106			ctx.newCommand(gccX86_64, mainCc)))
107		if err := verifyNoEnvUpdate(cmd, "CCACHE_DISABLE"); err != nil {
108			t.Error(err)
109		}
110
111		ctx.env = []string{"CCACHE_DISABLE=true"}
112		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
113			ctx.newCommand(gccX86_64, mainCc)))
114		if err := verifyEnvUpdate(cmd, "CCACHE_DISABLE="); err != nil {
115			t.Error(err)
116		}
117	})
118}
119
120func TestClearCCacheDisableWithoutValue(t *testing.T) {
121	withCCacheEnabledTestContext(t, func(ctx *testContext) {
122		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
123			ctx.newCommand(gccX86_64, mainCc)))
124		if err := verifyNoEnvUpdate(cmd, "CCACHE_DISABLE"); err != nil {
125			t.Error(err)
126		}
127
128		ctx.env = []string{"CCACHE_DISABLE="}
129		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
130			ctx.newCommand(gccX86_64, mainCc)))
131		if err := verifyEnvUpdate(cmd, "CCACHE_DISABLE="); err != nil {
132			t.Error(err)
133		}
134	})
135}
136
137func TestAddCCacheCpp2FlagForClang(t *testing.T) {
138	withCCacheEnabledTestContext(t, func(ctx *testContext) {
139		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
140			ctx.newCommand(clangX86_64, mainCc)))
141		if err := verifyEnvUpdate(cmd, "CCACHE_CPP2=yes"); err != nil {
142			t.Error(err)
143		}
144	})
145}
146
147func TestOmitCCacheCpp2FlagForGcc(t *testing.T) {
148	withCCacheEnabledTestContext(t, func(ctx *testContext) {
149		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
150			ctx.newCommand(gccX86_64, mainCc)))
151		if err := verifyNoEnvUpdate(cmd, "CCACHE_CPP2"); err != nil {
152			t.Error(err)
153		}
154	})
155}
156
157func withCCacheEnabledTestContext(t *testing.T, work func(ctx *testContext)) {
158	withTestContext(t, func(ctx *testContext) {
159		ctx.cfg.useCCache = true
160		work(ctx)
161	})
162}
163