oh-my-posh/src/segment_nbgv_test.go

77 lines
2 KiB
Go
Raw Normal View History

2021-06-04 10:27:39 -07:00
package main
import (
"errors"
"testing"
"github.com/alecthomas/assert"
)
func TestNbgv(t *testing.T) {
cases := []struct {
Case string
ExpectedEnabled bool
ExpectedString string
Response string
HasNbgv bool
Template string
2021-06-04 10:27:39 -07:00
Error error
}{
{Case: "nbgv not installed"},
{Case: "nbgv installed, no version file", HasNbgv: true, Response: "{ \"VersionFileFound\": false }"},
{Case: "nbgv installed with version file", ExpectedEnabled: true, HasNbgv: true, Response: "{ \"VersionFileFound\": true }"},
{
Case: "invalid template",
ExpectedEnabled: true,
ExpectedString: "invalid template text",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true }",
Template: "{{ err }}",
2021-06-04 10:27:39 -07:00
},
{
Case: "command error",
HasNbgv: true,
Error: errors.New("oh noes"),
},
{
Case: "invalid json",
HasNbgv: true,
Response: "><<<>>>",
},
{
Case: "Version",
ExpectedEnabled: true,
ExpectedString: "bump",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true, \"Version\": \"bump\" }",
Template: "{{ .Version }}",
2021-06-04 10:27:39 -07:00
},
{
Case: "AssemblyVersion",
ExpectedEnabled: true,
ExpectedString: "bump",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true, \"AssemblyVersion\": \"bump\" }",
Template: "{{ .AssemblyVersion }}",
2021-06-04 10:27:39 -07:00
},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("HasCommand", "nbgv").Return(tc.HasNbgv)
env.On("RunCommand", "nbgv", []string{"get-version", "--format=json"}).Return(tc.Response, tc.Error)
2021-06-04 10:27:39 -07:00
nbgv := &nbgv{
env: env,
props: properties{},
2021-06-04 10:27:39 -07:00
}
enabled := nbgv.enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if tc.Template == "" {
tc.Template = nbgv.template()
}
2021-06-04 10:27:39 -07:00
if enabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, nbgv), tc.Case)
2021-06-04 10:27:39 -07:00
}
}
}