aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/clang_flags_test.go
diff options
context:
space:
mode:
authorTobias Bosch <tbosch@google.com>2019-06-24 09:31:39 -0700
committerTobias Bosch <tbosch@google.com>2019-06-28 19:00:23 +0000
commit900dbc92800d8fc927905db29cb302461054cf97 (patch)
treec74ecbfb01ee4ee679090139596aef3546f19d9b /compiler_wrapper/clang_flags_test.go
parent739e6abb2cd03b60e579df31ad55870a4a00260a (diff)
downloadtoolchain-utils-900dbc92800d8fc927905db29cb302461054cf97.tar.gz
Introduce infrastructure for calling and testing nested
commands, error messages and exit codes. Also: - implements the -Xclang-path= flag as use case of calling a nested command. - adds tests for forwarding errors, comparing against the old wrapper, and exit codes. - captures the source locations of errors in error messages. - compares exit codes of new wrapper and old wrapper. BUG=chromium:773875 TEST=unit test Change-Id: I919e58091d093d68939809f676f799a68ec7a34e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1676833 Reviewed-by: George Burgess <gbiv@chromium.org> Tested-by: Tobias Bosch <tbosch@google.com>
Diffstat (limited to 'compiler_wrapper/clang_flags_test.go')
-rw-r--r--compiler_wrapper/clang_flags_test.go85
1 files changed, 70 insertions, 15 deletions
diff --git a/compiler_wrapper/clang_flags_test.go b/compiler_wrapper/clang_flags_test.go
index f50757fa..07efb89e 100644
--- a/compiler_wrapper/clang_flags_test.go
+++ b/compiler_wrapper/clang_flags_test.go
@@ -1,7 +1,11 @@
package main
import (
+ "errors"
+ "fmt"
+ "io"
"path/filepath"
+ "strings"
"testing"
)
@@ -16,8 +20,8 @@ func TestClangBasename(t *testing.T) {
}
for _, tt := range tests {
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
- ctx.newCommand(tt.in, "-noccache", mainCc)))
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(tt.in, mainCc)))
if err := verifyPath(cmd, tt.out); err != nil {
t.Error(err)
}
@@ -28,8 +32,8 @@ func TestClangBasename(t *testing.T) {
func TestClangPathGivenClangEnv(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
ctx.env = []string{"CLANG=/a/b/clang"}
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
- ctx.newCommand(clangX86_64, "-noccache", mainCc)))
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(clangX86_64, mainCc)))
if err := verifyPath(cmd, "/a/b/clang"); err != nil {
t.Error(err)
}
@@ -39,8 +43,8 @@ func TestClangPathGivenClangEnv(t *testing.T) {
func TestAbsoluteClangPathBasedOnRootPath(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
ctx.cfg.rootRelPath = "somepath"
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
- ctx.newCommand(filepath.Join(ctx.tempDir, clangX86_64), "-noccache", mainCc)))
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(filepath.Join(ctx.tempDir, clangX86_64), mainCc)))
if err := verifyPath(cmd, filepath.Join(ctx.tempDir, "somepath/usr/bin/clang")); err != nil {
t.Error(err)
}
@@ -50,14 +54,65 @@ func TestAbsoluteClangPathBasedOnRootPath(t *testing.T) {
func TestRelativeClangPathBasedOnRootPath(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
ctx.cfg.rootRelPath = "somepath"
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
- ctx.newCommand(clangX86_64, "-noccache", mainCc)))
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(clangX86_64, mainCc)))
if err := verifyPath(cmd, "somepath/usr/bin/clang"); err != nil {
t.Error(err)
}
})
}
+func TestUseXclangPathAndCalcResourceDirByNestedClangCall(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cfg.rootRelPath = "somepath"
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount > 1 {
+ return nil
+ }
+ if err := verifyPath(cmd, "somepath/usr/bin/clang"); err != nil {
+ t.Error(err)
+ }
+ if err := verifyArgOrder(cmd, "--print-resource-dir"); err != nil {
+ t.Error(err)
+ }
+ fmt.Fprint(stdout, "someResourcePath")
+ return nil
+ }
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(clangX86_64, "-Xclang-path=somedir", mainCc)))
+ if err := verifyPath(cmd, "somedir/clang"); err != nil {
+ t.Error(err)
+ }
+ if err := verifyArgOrder(cmd, "-resource-dir=someResourcePath",
+ "--gcc-toolchain=/usr", mainCc); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
+func TestXclangPathFailIfNestedClangCallFails(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ fmt.Fprint(stderr, "someclangerror")
+ return errors.New("someerror")
+ }
+ stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(clangX86_64, "-Xclang-path=somedir", mainCc)))
+ if err := verifyInternalError(stderr); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(stderr, "clang") {
+ t.Errorf("could not find compiler path on stderr. Got: %s", stderr)
+ }
+ if !strings.Contains(stderr, "someerror") {
+ t.Errorf("could not find original error on stderr. Got: %s", stderr)
+ }
+ if !strings.Contains(stderr, "someclangerror") {
+ t.Errorf("stderr was not forwarded. Got: %s", stderr)
+ }
+ })
+}
+
func TestConvertGccToClangFlags(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
var tests = []struct {
@@ -73,7 +128,7 @@ func TestConvertGccToClangFlags(t *testing.T) {
}
for _, tt := range tests {
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand(clangX86_64, tt.in, mainCc)))
if err := verifyArgCount(cmd, 0, tt.in); err != nil {
t.Error(err)
@@ -110,7 +165,7 @@ func TestFilterUnsupportedClangFlags(t *testing.T) {
}
for _, tt := range tests {
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand(tt.compiler, tt.flag, mainCc)))
if err := verifyArgCount(cmd, tt.expectedCount, tt.flag); err != nil {
t.Error(err)
@@ -129,7 +184,7 @@ func TestClangArchFlags(t *testing.T) {
{"./x86_64-cros-linux-gnu-clang", []string{mainCc, "-target", "x86_64-cros-linux-gnu"}},
}
for _, tt := range tests {
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand(tt.compiler, mainCc)))
if err := verifyArgOrder(cmd, tt.flags...); err != nil {
t.Error(err)
@@ -143,7 +198,7 @@ func TestClangLinkerPathProbesBinariesOnPath(t *testing.T) {
linkerPath := filepath.Join(ctx.tempDir, "a/b/c")
ctx.writeFile(filepath.Join(linkerPath, "x86_64-cros-linux-gnu-ld"), "")
ctx.env = []string{"PATH=nonExistantPath:" + linkerPath}
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand("./x86_64-cros-linux-gnu-clang", mainCc)))
if err := verifyArgOrder(cmd, "-Ba/b/c"); err != nil {
t.Error(err)
@@ -159,7 +214,7 @@ func TestClangLinkerPathEvaluatesSymlinksForBinariesOnPath(t *testing.T) {
ctx.symlink(realLinkerPath, linkedLinkerPath)
ctx.env = []string{"PATH=nonExistantPath:" + filepath.Dir(linkedLinkerPath)}
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand("./x86_64-cros-linux-gnu-clang", mainCc)))
if err := verifyArgOrder(cmd, "-Ba/original/path"); err != nil {
t.Error(err)
@@ -169,7 +224,7 @@ func TestClangLinkerPathEvaluatesSymlinksForBinariesOnPath(t *testing.T) {
func TestClangFallbackLinkerPathRelativeToRootDir(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand(clangX86_64, mainCc)))
if err := verifyArgOrder(cmd, "-Bbin"); err != nil {
t.Error(err)
@@ -180,7 +235,7 @@ func TestClangFallbackLinkerPathRelativeToRootDir(t *testing.T) {
func TestClangLinkerPathRelativeToRootDir(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
ctx.cfg.rootRelPath = "somepath"
- cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg,
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
ctx.newCommand(clangX86_64, mainCc)))
if err := verifyArgOrder(cmd, "-Bsomepath/bin"); err != nil {
t.Error(err)