oh-my-posh/src/segments/language_test.go

499 lines
14 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2020-11-14 11:04:04 -08:00
import (
"oh-my-posh/environment"
"oh-my-posh/mock"
"oh-my-posh/properties"
2020-11-14 11:04:04 -08:00
"testing"
"github.com/stretchr/testify/assert"
)
const (
universion = "1.3.307"
2020-11-14 11:04:04 -08:00
uni = "*.uni"
corn = "*.corn"
)
type languageArgs struct {
version string
extensions []string
enabledExtensions []string
2021-02-03 10:11:32 -08:00
commands []*cmd
enabledCommands []string
2021-02-03 10:11:32 -08:00
versionURLTemplate string
expectedError error
properties properties.Properties
matchesVersionFile matchesVersionFile
inHome bool
2020-11-14 11:04:04 -08:00
}
func (l *languageArgs) hasvalue(value string, list []string) bool {
for _, element := range list {
if element == value {
return true
}
}
return false
}
func bootStrapLanguageTest(args *languageArgs) *language {
env := new(mock.MockedEnvironment)
2020-11-14 11:04:04 -08:00
for _, command := range args.commands {
env.On("HasCommand", command.executable).Return(args.hasvalue(command.executable, args.enabledCommands))
env.On("RunCommand", command.executable, command.args).Return(args.version, args.expectedError)
2020-11-14 11:04:04 -08:00
}
for _, extension := range args.extensions {
env.On("HasFiles", extension).Return(args.hasvalue(extension, args.enabledExtensions))
2020-11-14 11:04:04 -08:00
}
home := "/usr/home"
cwd := "/usr/home/project"
if args.inHome {
cwd = home
}
env.On("Pwd").Return(cwd)
env.On("Home").Return(home)
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string),
})
2022-01-01 11:08:08 -08:00
if args.properties == nil {
args.properties = properties.Map{}
2022-01-01 11:08:08 -08:00
}
2020-11-14 11:04:04 -08:00
l := &language{
2021-11-26 01:37:33 -08:00
props: args.properties,
2021-02-03 10:11:32 -08:00
env: env,
extensions: args.extensions,
commands: args.commands,
versionURLTemplate: args.versionURLTemplate,
matchesVersionFile: args.matchesVersionFile,
2020-11-14 11:04:04 -08:00
}
return l
}
func TestLanguageFilesFoundButNoCommandAndVersionAndDisplayVersion(t *testing.T) {
2020-11-14 11:04:04 -08:00
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni},
enabledExtensions: []string{uni},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "", lang.Error, "unicorn is not available")
2020-11-14 11:04:04 -08:00
}
func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testing.T) {
props := properties.Map{
properties.FetchVersion: false,
}
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
},
},
extensions: []string{uni},
enabledExtensions: []string{uni},
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled(), "unicorn is not available")
}
func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
},
},
extensions: []string{uni},
enabledExtensions: []string{uni},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled(), "unicorn is not available")
}
2020-11-14 11:04:04 -08:00
func TestLanguageDisabledNoFiles(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
},
},
extensions: []string{uni},
enabledExtensions: []string{},
enabledCommands: []string{"unicorn"},
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.False(t, lang.Enabled(), "no files in the current directory")
2020-11-14 11:04:04 -08:00
}
func TestLanguageEnabledOneExtensionFound(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{uni},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"unicorn"},
2020-11-14 11:04:04 -08:00
version: universion,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, universion, lang.Full, "unicorn is available and uni files are found")
assert.Equal(t, "unicorn", lang.Executable, "unicorn was used")
2020-11-14 11:04:04 -08:00
}
func TestLanguageDisabledInHome(t *testing.T) {
args := &languageArgs{
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
extensions: []string{uni, corn},
enabledExtensions: []string{uni},
enabledCommands: []string{"unicorn"},
version: universion,
inHome: true,
}
lang := bootStrapLanguageTest(args)
assert.False(t, lang.Enabled())
}
2020-11-14 11:04:04 -08:00
func TestLanguageEnabledSecondExtensionFound(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"unicorn"},
version: universion,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, universion, lang.Full, "unicorn is available and corn files are found")
assert.Equal(t, "unicorn", lang.Executable, "unicorn was used")
2020-11-14 11:04:04 -08:00
}
func TestLanguageEnabledSecondCommand(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
{
executable: "corn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"corn"},
version: universion,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, universion, lang.Full, "unicorn is available and corn files are found")
assert.Equal(t, "corn", lang.Executable, "corn was used")
2020-11-14 11:04:04 -08:00
}
func TestLanguageEnabledAllExtensionsFound(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"unicorn"},
version: universion,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, universion, lang.Full, "unicorn is available and uni and corn files are found")
assert.Equal(t, "unicorn", lang.Executable, "unicorn was used")
2020-11-14 11:04:04 -08:00
}
func TestLanguageEnabledNoVersion(t *testing.T) {
props := properties.Map{
properties.FetchVersion: false,
}
2020-11-14 11:04:04 -08:00
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "unicorn",
args: []string{"--version"},
regex: "(?P<version>.*)",
},
},
2020-11-14 11:04:04 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"unicorn"},
version: universion,
properties: props,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "", lang.Full, "unicorn is available and uni and corn files are found")
assert.Equal(t, "", lang.Executable, "no version was found")
2020-11-14 11:04:04 -08:00
}
func TestLanguageEnabledMissingCommand(t *testing.T) {
props := properties.Map{
properties.FetchVersion: false,
}
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{},
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
2021-02-03 10:11:32 -08:00
enabledCommands: []string{"unicorn"},
version: universion,
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "", lang.Full, "unicorn is unavailable and uni and corn files are found")
assert.Equal(t, "", lang.Executable, "no executable was found")
}
func TestLanguageEnabledNoVersionData(t *testing.T) {
props := properties.Map{
properties.FetchVersion: true,
}
args := &languageArgs{
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"uni"},
version: "",
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "", lang.Full)
assert.Equal(t, "", lang.Executable, "no version was found")
}
func TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
expected := "missing"
props := properties.Map{
MissingCommandText: expected,
}
args := &languageArgs{
commands: []*cmd{},
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"unicorn"},
version: universion,
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, expected, lang.Error, "unicorn is available and uni and corn files are found")
}
func TestLanguageEnabledMissingCommandCustomTextHideError(t *testing.T) {
props := properties.Map{MissingCommandText: "missing"}
args := &languageArgs{
commands: []*cmd{},
extensions: []string{uni, corn},
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"unicorn"},
version: universion,
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "", lang.Full)
}
2021-02-03 10:11:32 -08:00
func TestLanguageEnabledCommandExitCode(t *testing.T) {
expected := 200
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
extensions: []string{uni, corn},
2021-02-03 10:11:32 -08:00
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"uni"},
version: universion,
expectedError: &environment.CommandError{ExitCode: expected},
2021-02-03 10:11:32 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "err executing uni with [--version]", lang.Error)
2021-02-03 10:11:32 -08:00
assert.Equal(t, expected, lang.exitCode)
}
func TestLanguageHyperlinkEnabled(t *testing.T) {
args := &languageArgs{
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
{
executable: "corn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
2022-01-23 09:48:55 -08:00
versionURLTemplate: "https://unicor.org/doc/{{ .Full }}",
2021-02-03 10:11:32 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
properties: properties.Map{},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
2022-01-23 09:48:55 -08:00
assert.Equal(t, "https://unicor.org/doc/1.3.307", lang.version.URL)
}
func TestLanguageHyperlinkEnabledWrongRegex(t *testing.T) {
args := &languageArgs{
2021-02-03 10:11:32 -08:00
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `wrong`,
},
{
executable: "corn",
args: []string{"--version"},
regex: `wrong`,
},
},
2022-01-23 09:48:55 -08:00
versionURLTemplate: "https://unicor.org/doc/{{ .Full }}",
2021-02-03 10:11:32 -08:00
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
properties: properties.Map{},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "err parsing info from corn with 1.3.307", lang.Error)
}
func TestLanguageEnabledInHome(t *testing.T) {
cases := []struct {
Case string
HomeEnabled bool
ExpectedEnabled bool
}{
{Case: "Always enabled", HomeEnabled: true, ExpectedEnabled: true},
{Case: "Context disabled", HomeEnabled: false, ExpectedEnabled: false},
}
for _, tc := range cases {
props := properties.Map{
HomeEnabled: tc.HomeEnabled,
}
args := &languageArgs{
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
properties: props,
inHome: true,
}
lang := bootStrapLanguageTest(args)
assert.Equal(t, tc.ExpectedEnabled, lang.Enabled(), tc.Case)
}
}
func TestLanguageInnerHyperlink(t *testing.T) {
args := &languageArgs{
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
versionURLTemplate: "https://uni.org/release/{{ .Full }}",
},
{
executable: "corn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
versionURLTemplate: "https://unicor.org/doc/{{ .Full }}",
},
},
versionURLTemplate: "This gets replaced with inner template",
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
properties: properties.Map{},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "https://unicor.org/doc/1.3.307", lang.version.URL)
}
func TestLanguageHyperlinkTemplatePropertyTakesPriority(t *testing.T) {
args := &languageArgs{
commands: []*cmd{
{
executable: "uni",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
versionURLTemplate: "https://uni.org/release/{{ .Full }}",
},
},
extensions: []string{uni},
enabledExtensions: []string{uni},
enabledCommands: []string{"uni"},
version: universion,
properties: properties.Map{
properties.VersionURLTemplate: "https://custom/url/template/{{ .Major }}.{{ .Minor }}",
},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.Enabled())
assert.Equal(t, "https://custom/url/template/1.3", lang.version.URL)
}