aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/errors_test.go
blob: 096ae373e0b71fdd441af00968469124eb447d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"errors"
	"fmt"
	"syscall"
	"testing"
)

func TestNewErrorwithSourceLocfMessage(t *testing.T) {
	err := newErrorwithSourceLocf("a%sc", "b")
	if err.Error() != "errors_test.go:15: abc" {
		t.Errorf("Error message incorrect. Got: %s", err.Error())
	}
}

func TestWrapErrorwithSourceLocfMessage(t *testing.T) {
	cause := errors.New("someCause")
	err := wrapErrorwithSourceLocf(cause, "a%sc", "b")
	if err.Error() != "errors_test.go:23: abc: someCause" {
		t.Errorf("Error message incorrect. Got: %s", err.Error())
	}
}

func TestNewUserErrorf(t *testing.T) {
	err := newUserErrorf("a%sc", "b")
	if err.Error() != "abc" {
		t.Errorf("Error message incorrect. Got: %s", err.Error())
	}
}

func TestSubprocessOk(t *testing.T) {
	exitCode, err := wrapSubprocessErrorWithSourceLoc(nil, nil)
	if exitCode != 0 {
		t.Errorf("unexpected exit code. Got: %d", exitCode)
	}
	if err != nil {
		t.Errorf("unexpected error. Got: %s", err)
	}
}

func TestSubprocessExitCodeError(t *testing.T) {
	exitCode, err := wrapSubprocessErrorWithSourceLoc(nil, newExitCodeError(23))
	if exitCode != 23 {
		t.Errorf("unexpected exit code. Got: %d", exitCode)
	}
	if err != nil {
		t.Errorf("unexpected error. Got: %s", err)
	}
}

func TestSubprocessCCacheError(t *testing.T) {
	_, err := wrapSubprocessErrorWithSourceLoc(&command{Path: "/usr/bin/ccache"}, syscall.ENOENT)
	if _, ok := err.(userError); !ok {
		t.Errorf("unexpected error type. Got: %T", err)
	}
	if err.Error() != "ccache not found under /usr/bin/ccache. Please install it" {
		t.Errorf("unexpected error message. Got: %s", err)
	}
}

func TestSubprocessGeneralError(t *testing.T) {
	cmd := &command{Path: "somepath"}
	_, err := wrapSubprocessErrorWithSourceLoc(cmd, errors.New("someerror"))
	if err.Error() != fmt.Sprintf("errors_test.go:68: failed to execute %#v: someerror", cmd) {
		t.Errorf("Error message incorrect. Got: %s", err.Error())
	}
}