oh-my-posh/src/segment_az_test.go

179 lines
4.3 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
2021-03-27 06:52:27 -07:00
func TestAzSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedEnabled bool
ExpectedString string
EnvSubName string
EnvSubID string
2021-05-21 10:56:31 -07:00
EnvSubAccount string
2021-03-27 06:52:27 -07:00
CliExists bool
CliSubName string
CliSubID string
2021-05-21 10:56:31 -07:00
CliSubAccount string
2021-03-27 06:52:27 -07:00
InfoSeparator string
DisplayID bool
DisplayName bool
2021-05-21 10:56:31 -07:00
DisplayAccount bool
2021-03-27 06:52:27 -07:00
}{
2021-05-21 10:56:31 -07:00
{
Case: "print only account",
ExpectedEnabled: true,
ExpectedString: "foobar",
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
CliSubAccount: "foobar",
InfoSeparator: "$",
DisplayID: false,
DisplayName: false,
DisplayAccount: true,
},
{
Case: "envvars present",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
ExpectedString: "foo$bar",
EnvSubName: "foo",
EnvSubID: "bar",
CliExists: false,
InfoSeparator: "$",
DisplayID: true,
DisplayName: true,
},
{
Case: "envvar name present",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
2021-05-21 10:56:31 -07:00
ExpectedString: "foo",
2021-03-27 06:52:27 -07:00
EnvSubName: "foo",
CliExists: false,
InfoSeparator: "$",
DisplayID: true,
DisplayName: true,
},
{
Case: "envvar id present",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
2021-05-21 10:56:31 -07:00
ExpectedString: "bar",
2021-03-27 06:52:27 -07:00
EnvSubID: "bar",
CliExists: false,
InfoSeparator: "$",
DisplayID: true,
DisplayName: true,
},
{
Case: "envvar account present",
ExpectedEnabled: true,
ExpectedString: "foobar",
EnvSubAccount: "foobar",
EnvSubID: "bar",
CliExists: false,
InfoSeparator: "$",
DisplayAccount: true,
},
{
Case: "cli not found",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: false,
2021-05-21 10:56:31 -07:00
ExpectedString: "",
2021-03-27 06:52:27 -07:00
CliExists: false,
InfoSeparator: "$",
DisplayID: true,
DisplayName: true,
},
{
Case: "cli contains data",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
ExpectedString: "foo$bar",
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
InfoSeparator: "$",
DisplayID: true,
DisplayName: true,
},
{
Case: "print only name",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
ExpectedString: "foo",
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
InfoSeparator: "$",
DisplayID: false,
DisplayName: true,
},
{
Case: "print only id",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: true,
ExpectedString: "bar",
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
InfoSeparator: "$",
DisplayID: true,
DisplayName: false,
},
{
Case: "print none",
2021-05-21 10:56:31 -07:00
ExpectedEnabled: true,
2021-03-27 06:52:27 -07:00
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
InfoSeparator: "$",
},
{
Case: "update needed",
ExpectedEnabled: true,
ExpectedString: updateMessage,
CliExists: true,
CliSubName: "Do you want to continue? (Y/n): Visual Studio Enterprise",
DisplayID: false,
DisplayName: true,
},
2021-05-21 10:56:31 -07:00
{
Case: "account info",
ExpectedEnabled: true,
ExpectedString: updateMessage,
CliExists: true,
CliSubName: "Do you want to continue? (Y/n): Visual Studio Enterprise",
DisplayID: false,
DisplayName: true,
DisplayAccount: true,
},
}
2021-03-27 06:52:27 -07:00
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getenv", "AZ_SUBSCRIPTION_NAME").Return(tc.EnvSubName)
env.On("getenv", "AZ_SUBSCRIPTION_ID").Return(tc.EnvSubID)
env.On("getenv", "AZ_SUBSCRIPTION_ACCOUNT").Return(tc.EnvSubAccount)
2021-03-27 06:52:27 -07:00
env.On("hasCommand", "az").Return(tc.CliExists)
2021-05-21 10:56:31 -07:00
env.On("runCommand", "az", []string{"account", "show", "--query=[name,id,user.name]", "-o=tsv"}).Return(
fmt.Sprintf("%s\n%s\n%s\n", tc.CliSubName, tc.CliSubID, tc.CliSubAccount),
nil,
)
2021-03-27 06:52:27 -07:00
props := &properties{
values: map[Property]interface{}{
2021-05-21 10:56:31 -07:00
SubscriptionInfoSeparator: tc.InfoSeparator,
DisplaySubscriptionID: tc.DisplayID,
DisplaySubscriptionName: tc.DisplayName,
DisplaySubscriptionAccount: tc.DisplayAccount,
2021-03-27 06:52:27 -07:00
},
}
2021-03-27 06:52:27 -07:00
az := &az{
env: env,
props: props,
}
assert.Equal(t, tc.ExpectedEnabled, az.enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, az.string(), tc.Case)
}
}