From 469f5d2edd85cd517eecf82766ce4b2ab5611122 Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Fri, 11 Jun 2021 11:58:05 -0700 Subject: compiler_wrapper: make goma testing use its own function this logic was repetitive, and we're adding more functions where we need it. BUG=b:190741226 TEST=go test Change-Id: I9736142ec59b22d58776d35357a7aa380365a228 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2956693 Reviewed-by: Ryan Beltran Tested-by: George Burgess --- compiler_wrapper/gomacc_flag_test.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/compiler_wrapper/gomacc_flag_test.go b/compiler_wrapper/gomacc_flag_test.go index d7b2b0b7..e1dc33ed 100644 --- a/compiler_wrapper/gomacc_flag_test.go +++ b/compiler_wrapper/gomacc_flag_test.go @@ -5,15 +5,13 @@ package main import ( + "os" "path" "testing" ) func TestCallGomaccIfEnvIsGivenAndValid(t *testing.T) { - withTestContext(t, func(ctx *testContext) { - gomaPath := path.Join(ctx.tempDir, "gomacc") - // Create a file so the gomacc path is valid. - ctx.writeFile(gomaPath, "") + withGomaccTestContext(t, func(ctx *testContext, gomaPath string) { ctx.env = []string{"GOMACC_PATH=" + gomaPath} cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc))) @@ -27,9 +25,11 @@ func TestCallGomaccIfEnvIsGivenAndValid(t *testing.T) { } func TestOmitGomaccIfEnvIsGivenButInvalid(t *testing.T) { - withTestContext(t, func(ctx *testContext) { - // Note: This path does not point to a valid file. - gomaPath := path.Join(ctx.tempDir, "gomacc") + withGomaccTestContext(t, func(ctx *testContext, gomaPath string) { + if err := os.Remove(gomaPath); err != nil { + t.Fatalf("failed removing fake goma file at %q: %v", gomaPath, err) + } + ctx.env = []string{"GOMACC_PATH=" + gomaPath} cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc))) @@ -40,10 +40,7 @@ func TestOmitGomaccIfEnvIsGivenButInvalid(t *testing.T) { } func TestCallGomaccIfArgIsGivenAndValid(t *testing.T) { - withTestContext(t, func(ctx *testContext) { - gomaPath := path.Join(ctx.tempDir, "gomacc") - // Create a file so the gomacc path is valid. - ctx.writeFile(gomaPath, "") + withGomaccTestContext(t, func(ctx *testContext, gomaPath string) { cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc, "--gomacc-path", gomaPath))) if err := verifyPath(cmd, gomaPath); err != nil { @@ -62,9 +59,11 @@ func TestCallGomaccIfArgIsGivenAndValid(t *testing.T) { } func TestOmitGomaccIfArgIsGivenButInvalid(t *testing.T) { - withTestContext(t, func(ctx *testContext) { - // Note: This path does not point to a valid file. - gomaPath := path.Join(ctx.tempDir, "gomacc") + withGomaccTestContext(t, func(ctx *testContext, gomaPath string) { + if err := os.Remove(gomaPath); err != nil { + t.Fatalf("failed removing fake goma file at %q: %v", gomaPath, err) + } + cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc, "--gomacc-path", gomaPath))) if err := verifyPath(cmd, gccX86_64+".real"); err != nil { @@ -92,3 +91,12 @@ func TestOmitGomaccByDefault(t *testing.T) { } }) } + +func withGomaccTestContext(t *testing.T, f func(*testContext, string)) { + withTestContext(t, func(ctx *testContext) { + gomaPath := path.Join(ctx.tempDir, "gomacc") + // Create a file so the gomacc path is valid. + ctx.writeFile(gomaPath, "") + f(ctx, gomaPath) + }) +} -- cgit v1.2.3