oh-my-posh/src/segment_exit_test.go

91 lines
1.9 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExitWriterEnabled(t *testing.T) {
cases := []struct {
ExitCode int
Expected bool
}{
{ExitCode: 102, Expected: true},
{ExitCode: 0, Expected: false},
{ExitCode: -1, Expected: true},
2019-03-13 04:14:30 -07:00
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("lastErrorCode").Return(tc.ExitCode)
e := &exit{
2022-01-01 11:08:08 -08:00
env: env,
props: properties{},
}
assert.Equal(t, tc.Expected, e.enabled())
2019-03-13 04:14:30 -07:00
}
}
func TestGetMeaningFromExitCode(t *testing.T) {
errorMap := make(map[int]string)
errorMap[1] = "ERROR"
errorMap[2] = "USAGE"
errorMap[126] = "NOPERM"
errorMap[127] = "NOTFOUND"
errorMap[129] = "SIGHUP"
errorMap[130] = "SIGINT"
errorMap[131] = "SIGQUIT"
errorMap[132] = "SIGILL"
errorMap[133] = "SIGTRAP"
errorMap[134] = "SIGIOT"
errorMap[135] = "SIGBUS"
errorMap[136] = "SIGFPE"
errorMap[137] = "SIGKILL"
errorMap[138] = "SIGUSR1"
errorMap[139] = "SIGSEGV"
errorMap[140] = "SIGUSR2"
errorMap[141] = "SIGPIPE"
errorMap[142] = "SIGALRM"
errorMap[143] = "SIGTERM"
errorMap[144] = "SIGSTKFLT"
errorMap[145] = "SIGCHLD"
errorMap[146] = "SIGCONT"
errorMap[147] = "SIGSTOP"
errorMap[148] = "SIGTSTP"
errorMap[149] = "SIGTTIN"
errorMap[150] = "SIGTTOU"
errorMap[151] = "151"
errorMap[7000] = "7000"
for exitcode, want := range errorMap {
2021-11-14 04:39:00 -08:00
e := &exit{}
e.code = exitcode
2019-03-13 04:14:30 -07:00
assert.Equal(t, want, e.getMeaningFromExitCode())
}
}
2021-11-14 04:39:00 -08:00
func TestExitWriterTemplateString(t *testing.T) {
cases := []struct {
Case string
ExitCode int
Expected string
Template string
}{
{Case: "Only code", ExitCode: 129, Expected: "129", Template: "{{ .Code }}"},
}
2021-11-14 04:39:00 -08:00
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("lastErrorCode").Return(tc.ExitCode)
env.onTemplate()
2022-01-01 11:08:08 -08:00
props := properties{
2021-11-26 01:37:33 -08:00
SegmentTemplate: tc.Template,
2021-11-14 04:39:00 -08:00
}
e := &exit{
env: env,
props: props,
}
assert.Equal(t, tc.Expected, e.string(), tc.Case)
}
}