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