2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2021-06-04 10:27:39 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/mock"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2021-06-04 10:27:39 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/alecthomas/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNbgv(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
ExpectedEnabled bool
|
|
|
|
ExpectedString string
|
|
|
|
Response string
|
|
|
|
HasNbgv bool
|
2022-01-23 12:37:51 -08:00
|
|
|
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 }",
|
2022-01-23 12:37:51 -08:00
|
|
|
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\" }",
|
2022-01-23 12:37:51 -08:00
|
|
|
Template: "{{ .Version }}",
|
2021-06-04 10:27:39 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "AssemblyVersion",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
ExpectedString: "bump",
|
|
|
|
HasNbgv: true,
|
|
|
|
Response: "{ \"VersionFileFound\": true, \"AssemblyVersion\": \"bump\" }",
|
2022-01-23 12:37:51 -08:00
|
|
|
Template: "{{ .AssemblyVersion }}",
|
2021-06-04 10:27:39 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("HasCommand", "nbgv").Return(tc.HasNbgv)
|
|
|
|
env.On("RunCommand", "nbgv", []string{"get-version", "--format=json"}).Return(tc.Response, tc.Error)
|
2022-01-26 05:10:18 -08:00
|
|
|
nbgv := &Nbgv{
|
2022-01-23 12:37:51 -08:00
|
|
|
env: env,
|
2022-01-26 04:53:35 -08:00
|
|
|
props: properties.Map{},
|
2021-06-04 10:27:39 -07:00
|
|
|
}
|
2022-01-26 05:26:56 -08:00
|
|
|
enabled := nbgv.Enabled()
|
2021-06-04 10:27:39 -07:00
|
|
|
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
|
2022-01-23 12:37:51 -08:00
|
|
|
if tc.Template == "" {
|
2022-01-26 05:26:56 -08:00
|
|
|
tc.Template = nbgv.Template()
|
2022-01-23 12:37:51 -08:00
|
|
|
}
|
2021-06-04 10:27:39 -07:00
|
|
|
if enabled {
|
2022-01-23 12:37:51 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, nbgv), tc.Case)
|
2021-06-04 10:27:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|