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