88 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
/*
 | 
						|
* MIT License
 | 
						|
*
 | 
						|
* Copyright (c) 2022 urfave/cli maintainers
 | 
						|
*
 | 
						|
* Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
						|
* of this software and associated documentation files (the "Software"), to deal
 | 
						|
* in the Software without restriction, including without limitation the rights
 | 
						|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
						|
* copies of the Software, and to permit persons to whom the Software is
 | 
						|
* furnished to do so, subject to the following conditions:
 | 
						|
*
 | 
						|
* The above copyright notice and this permission notice shall be included in all
 | 
						|
* copies or substantial portions of the Software.
 | 
						|
*
 | 
						|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
						|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
						|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
						|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
						|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
						|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
						|
* SOFTWARE.
 | 
						|
 */
 | 
						|
package errors
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"reflect"
 | 
						|
	"runtime"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
)
 | 
						|
 | 
						|
var (
 | 
						|
	wd, _        = os.Getwd()
 | 
						|
	lastExitCode = 0
 | 
						|
	fakeOsExiter = func(rc int) {
 | 
						|
		lastExitCode = rc
 | 
						|
	}
 | 
						|
)
 | 
						|
 | 
						|
func expect(t *testing.T, a any, b any) {
 | 
						|
	_, fn, line, _ := runtime.Caller(1)
 | 
						|
	fn = strings.Replace(fn, wd+"/", "", -1)
 | 
						|
 | 
						|
	require.Equalf(t, a, b, "(%s:%d) Expected %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a))
 | 
						|
}
 | 
						|
 | 
						|
func TestHandleExitCoder_nil(t *testing.T) {
 | 
						|
	exitCode := 0
 | 
						|
	called := false
 | 
						|
 | 
						|
	OsExiter = func(rc int) {
 | 
						|
		if !called {
 | 
						|
			exitCode = rc
 | 
						|
			called = true
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	defer func() { OsExiter = fakeOsExiter }()
 | 
						|
 | 
						|
	HandleExitCoder(nil)
 | 
						|
 | 
						|
	expect(t, exitCode, 0)
 | 
						|
	expect(t, called, false)
 | 
						|
}
 | 
						|
 | 
						|
func TestHandleExitCoder_ExitCoder(t *testing.T) {
 | 
						|
	exitCode := 0
 | 
						|
	called := false
 | 
						|
 | 
						|
	OsExiter = func(rc int) {
 | 
						|
		if !called {
 | 
						|
			exitCode = rc
 | 
						|
			called = true
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	defer func() { OsExiter = fakeOsExiter }()
 | 
						|
 | 
						|
	HandleExitCoder(NewExitError("galactic perimeter breach", 9))
 | 
						|
 | 
						|
	expect(t, exitCode, 9)
 | 
						|
	expect(t, called, true)
 | 
						|
}
 |