oh-my-posh/src/segment_language_test.go

396 lines
11 KiB
Go
Raw Normal View History

2020-11-14 11:04:04 -08:00
package main
import (
"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
displayVersion bool
displayMode string
extensions []string
enabledExtensions []string
2021-02-03 10:11:32 -08:00
commands []*cmd
enabledCommands []string
missingCommandText string
2021-02-03 10:11:32 -08:00
versionURLTemplate string
enableHyperlink bool
2021-02-03 10:11:32 -08:00
expectedError error
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(MockedEnvironment)
for _, command := range args.commands {
2021-02-03 10:11:32 -08:00
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))
}
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
DisplayMode: args.displayMode,
EnableHyperlink: args.enableHyperlink,
2020-11-14 11:04:04 -08:00
},
}
if args.missingCommandText != "" {
props.values[MissingCommandTextProperty] = args.missingCommandText
}
2020-11-14 11:04:04 -08:00
l := &language{
2021-02-03 10:11:32 -08:00
props: props,
env: env,
extensions: args.extensions,
commands: args.commands,
versionURLTemplate: args.versionURLTemplate,
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},
displayVersion: true,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, MissingCommandText, lang.string(), "unicorn is not available")
2020-11-14 11:04:04 -08:00
}
func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(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},
displayVersion: false,
}
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"},
displayVersion: true,
2020-11-14 11:04:04 -08:00
}
lang := bootStrapLanguageTest(args)
assert.False(t, lang.enabled(), "no files in the current directory")
}
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,
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, universion, lang.string(), "unicorn is available and uni files are found")
}
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
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, universion, lang.string(), "unicorn is available and corn files are found")
}
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
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, universion, lang.string(), "unicorn is available and corn files are found")
}
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
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, universion, lang.string(), "unicorn is available and uni and corn files are found")
}
func TestLanguageEnabledNoVersion(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
displayVersion: false,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found")
}
func TestLanguageEnabledMissingCommand(t *testing.T) {
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,
displayVersion: false,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found")
}
func TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
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,
missingCommandText: "missing",
2021-02-03 10:11:32 -08:00
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, args.missingCommandText, lang.string(), "unicorn is available and uni and corn files are found")
}
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,
displayVersion: true,
2021-02-03 10:11:32 -08:00
expectedError: &commandError{exitCode: expected},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "err executing uni with [--version]", lang.string())
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]+)))`,
},
},
versionURLTemplate: "[%s](https://unicor.org/doc/%s.%s.%s)",
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "[1.3.307](https://unicor.org/doc/1.3.307)", lang.string())
}
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`,
},
},
versionURLTemplate: "[%s](https://unicor.org/doc/%s.%s.%s)",
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
2021-02-03 10:11:32 -08:00
assert.Equal(t, "err parsing info from corn with 1.3.307", lang.string())
}
func TestLanguageHyperlinkEnabledLessParamInTemplate(t *testing.T) {
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]+)))`,
},
{
executable: "corn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "[%s](https://unicor.org/doc/%s)",
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "[1.3.307](https://unicor.org/doc/1)", lang.string())
}
func TestLanguageHyperlinkEnabledMoreParamInTemplate(t *testing.T) {
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]+)))`,
},
{
executable: "corn",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "[%s](https://unicor.org/doc/%s.%s.%s.%s)",
extensions: []string{uni, corn},
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "1.3.307", lang.string())
}