xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/errors_test.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li// Copyright 2019 The ChromiumOS Authors
2*760c253cSXin Li// Use of this source code is governed by a BSD-style license that can be
3*760c253cSXin Li// found in the LICENSE file.
4*760c253cSXin Li
5*760c253cSXin Lipackage main
6*760c253cSXin Li
7*760c253cSXin Liimport (
8*760c253cSXin Li	"errors"
9*760c253cSXin Li	"fmt"
10*760c253cSXin Li	"syscall"
11*760c253cSXin Li	"testing"
12*760c253cSXin Li)
13*760c253cSXin Li
14*760c253cSXin Lifunc TestNewErrorwithSourceLocfMessage(t *testing.T) {
15*760c253cSXin Li	err := newErrorwithSourceLocf("a%sc", "b")
16*760c253cSXin Li	if err.Error() != "errors_test.go:15: abc" {
17*760c253cSXin Li		t.Errorf("Error message incorrect. Got: %s", err.Error())
18*760c253cSXin Li	}
19*760c253cSXin Li}
20*760c253cSXin Li
21*760c253cSXin Lifunc TestWrapErrorwithSourceLocfMessage(t *testing.T) {
22*760c253cSXin Li	cause := errors.New("someCause")
23*760c253cSXin Li	err := wrapErrorwithSourceLocf(cause, "a%sc", "b")
24*760c253cSXin Li	if err.Error() != "errors_test.go:23: abc: someCause" {
25*760c253cSXin Li		t.Errorf("Error message incorrect. Got: %s", err.Error())
26*760c253cSXin Li	}
27*760c253cSXin Li}
28*760c253cSXin Li
29*760c253cSXin Lifunc TestNewUserErrorf(t *testing.T) {
30*760c253cSXin Li	err := newUserErrorf("a%sc", "b")
31*760c253cSXin Li	if err.Error() != "abc" {
32*760c253cSXin Li		t.Errorf("Error message incorrect. Got: %s", err.Error())
33*760c253cSXin Li	}
34*760c253cSXin Li}
35*760c253cSXin Li
36*760c253cSXin Lifunc TestSubprocessOk(t *testing.T) {
37*760c253cSXin Li	exitCode, err := wrapSubprocessErrorWithSourceLoc(nil, nil)
38*760c253cSXin Li	if exitCode != 0 {
39*760c253cSXin Li		t.Errorf("unexpected exit code. Got: %d", exitCode)
40*760c253cSXin Li	}
41*760c253cSXin Li	if err != nil {
42*760c253cSXin Li		t.Errorf("unexpected error. Got: %s", err)
43*760c253cSXin Li	}
44*760c253cSXin Li}
45*760c253cSXin Li
46*760c253cSXin Lifunc TestSubprocessExitCodeError(t *testing.T) {
47*760c253cSXin Li	exitCode, err := wrapSubprocessErrorWithSourceLoc(nil, newExitCodeError(23))
48*760c253cSXin Li	if exitCode != 23 {
49*760c253cSXin Li		t.Errorf("unexpected exit code. Got: %d", exitCode)
50*760c253cSXin Li	}
51*760c253cSXin Li	if err != nil {
52*760c253cSXin Li		t.Errorf("unexpected error. Got: %s", err)
53*760c253cSXin Li	}
54*760c253cSXin Li}
55*760c253cSXin Li
56*760c253cSXin Lifunc TestSubprocessCCacheError(t *testing.T) {
57*760c253cSXin Li	_, err := wrapSubprocessErrorWithSourceLoc(&command{Path: "/usr/bin/ccache"}, syscall.ENOENT)
58*760c253cSXin Li	if _, ok := err.(userError); !ok {
59*760c253cSXin Li		t.Errorf("unexpected error type. Got: %T", err)
60*760c253cSXin Li	}
61*760c253cSXin Li	if err.Error() != "ccache not found under /usr/bin/ccache. Please install it" {
62*760c253cSXin Li		t.Errorf("unexpected error message. Got: %s", err)
63*760c253cSXin Li	}
64*760c253cSXin Li}
65*760c253cSXin Li
66*760c253cSXin Lifunc TestSubprocessGeneralError(t *testing.T) {
67*760c253cSXin Li	cmd := &command{Path: "somepath"}
68*760c253cSXin Li	_, err := wrapSubprocessErrorWithSourceLoc(cmd, errors.New("someerror"))
69*760c253cSXin Li	if err.Error() != fmt.Sprintf("errors_test.go:68: failed to execute %#v: someerror", cmd) {
70*760c253cSXin Li		t.Errorf("Error message incorrect. Got: %s", err.Error())
71*760c253cSXin Li	}
72*760c253cSXin Li}
73