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 <ryanbeltran@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
diff --git a/compiler_wrapper/gomacc_flag_test.go b/compiler_wrapper/gomacc_flag_test.go
index d7b2b0b..e1dc33e 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 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 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 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 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)
+	})
+}