package main import ( "testing" "github.com/stretchr/testify/assert" ) const ( universion = "1.3.307" uni = "*.uni" corn = "*.corn" ) type languageArgs struct { version string extensions []string enabledExtensions []string commands []*cmd enabledCommands []string versionURLTemplate string expectedError error properties map[Property]interface{} matchesVersionFile matchesVersionFile } 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 { env.On("hasCommand", command.executable).Return(args.hasvalue(command.executable, args.enabledCommands)) env.On("runCommand", command.executable, command.args).Return(args.version, args.expectedError) } for _, extension := range args.extensions { env.On("hasFiles", extension).Return(args.hasvalue(extension, args.enabledExtensions)) } props := &properties{ values: args.properties, } l := &language{ props: props, env: env, extensions: args.extensions, commands: args.commands, versionURLTemplate: args.versionURLTemplate, matchesVersionFile: args.matchesVersionFile, } return l } func TestLanguageFilesFoundButNoCommandAndVersionAndDisplayVersion(t *testing.T) { args := &languageArgs{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, }, }, extensions: []string{uni}, enabledExtensions: []string{uni}, } lang := bootStrapLanguageTest(args) assert.True(t, lang.enabled()) assert.Equal(t, "", lang.string(), "unicorn is not available") } func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testing.T) { props := map[Property]interface{}{ DisplayVersion: false, } args := &languageArgs{ 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{ 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") } func TestLanguageDisabledNoFiles(t *testing.T) { args := &languageArgs{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, }, }, extensions: []string{uni}, enabledExtensions: []string{}, enabledCommands: []string{"unicorn"}, } lang := bootStrapLanguageTest(args) assert.False(t, lang.enabled(), "no files in the current directory") } func TestLanguageEnabledOneExtensionFound(t *testing.T) { args := &languageArgs{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, regex: "(?P.*)", }, }, extensions: []string{uni, corn}, enabledExtensions: []string{uni}, enabledCommands: []string{"unicorn"}, version: universion, } 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{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, regex: "(?P.*)", }, }, extensions: []string{uni, corn}, enabledExtensions: []string{corn}, enabledCommands: []string{"unicorn"}, version: universion, } 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{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: "(?P.*)", }, { executable: "corn", args: []string{"--version"}, regex: "(?P.*)", }, }, extensions: []string{uni, corn}, enabledExtensions: []string{corn}, enabledCommands: []string{"corn"}, version: universion, } 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{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, regex: "(?P.*)", }, }, extensions: []string{uni, corn}, enabledExtensions: []string{uni, corn}, enabledCommands: []string{"unicorn"}, version: universion, } 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) { props := map[Property]interface{}{ DisplayVersion: false, } args := &languageArgs{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, regex: "(?P.*)", }, }, 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.string(), "unicorn is available and uni and corn files are found") } func TestLanguageEnabledMissingCommand(t *testing.T) { props := map[Property]interface{}{ DisplayVersion: false, } 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.string(), "unicorn is available and uni and corn files are found") } func TestLanguageEnabledNoVersionData(t *testing.T) { props := map[Property]interface{}{ DisplayVersion: true, } args := &languageArgs{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: `(?:Python (?P((?P[0-9]+).(?P[0-9]+).(?P[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.string()) } func TestLanguageEnabledMissingCommandCustomText(t *testing.T) { expected := "missing" props := map[Property]interface{}{ 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.string(), "unicorn is available and uni and corn files are found") } func TestLanguageEnabledMissingCommandCustomTextHideError(t *testing.T) { props := map[Property]interface{}{ MissingCommandText: "missing", DisplayError: false, } 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.string()) } func TestLanguageEnabledCommandExitCode(t *testing.T) { expected := 200 args := &languageArgs{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, }, extensions: []string{uni, corn}, enabledExtensions: []string{uni, corn}, enabledCommands: []string{"uni"}, version: universion, 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) { props := map[Property]interface{}{ EnableHyperlink: true, } args := &languageArgs{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, { executable: "corn", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, }, versionURLTemplate: "[%s](https://unicor.org/doc/%s.%s.%s)", extensions: []string{uni, corn}, enabledExtensions: []string{corn}, enabledCommands: []string{"corn"}, version: universion, properties: props, } 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) { props := map[Property]interface{}{ EnableHyperlink: true, } args := &languageArgs{ 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, properties: props, } lang := bootStrapLanguageTest(args) assert.True(t, lang.enabled()) assert.Equal(t, "err parsing info from corn with 1.3.307", lang.string()) } func TestLanguageHyperlinkEnabledLessParamInTemplate(t *testing.T) { props := map[Property]interface{}{ EnableHyperlink: true, } args := &languageArgs{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, { executable: "corn", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, }, versionURLTemplate: "[%s](https://unicor.org/doc/%s)", extensions: []string{uni, corn}, enabledExtensions: []string{corn}, enabledCommands: []string{"corn"}, version: universion, properties: props, } 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) { props := map[Property]interface{}{ EnableHyperlink: true, } args := &languageArgs{ commands: []*cmd{ { executable: "uni", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, { executable: "corn", args: []string{"--version"}, regex: `(?P((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)))`, }, }, versionURLTemplate: "[%s](https://unicor.org/doc/%s.%s.%s.%s)", extensions: []string{uni, corn}, enabledExtensions: []string{corn}, enabledCommands: []string{"corn"}, version: universion, properties: props, } lang := bootStrapLanguageTest(args) assert.True(t, lang.enabled()) assert.Equal(t, "1.3.307", lang.string()) } func TestLanguageVersionMismatch(t *testing.T) { cases := []struct { Case string Enabled bool Mismatch bool ExpectedColor string ColorBackground bool }{ {Case: "Disabled", Enabled: false}, {Case: "Mismatch - Foreground color", Enabled: true, Mismatch: true, ExpectedColor: "#566777"}, {Case: "Mismatch - Background color", Enabled: true, Mismatch: true, ExpectedColor: "#566777", ColorBackground: true}, {Case: "No mismatch", Enabled: true, Mismatch: false}, } for _, tc := range cases { props := map[Property]interface{}{ EnableVersionMismatch: tc.Enabled, VersionMismatchColor: tc.ExpectedColor, ColorBackground: tc.ColorBackground, } var matchesVersionFile func() bool switch tc.Mismatch { case true: matchesVersionFile = func() bool { return false } default: matchesVersionFile = func() bool { return true } } args := &languageArgs{ commands: []*cmd{ { executable: "unicorn", args: []string{"--version"}, regex: "(?P.*)", }, }, extensions: []string{uni, corn}, enabledExtensions: []string{uni, corn}, enabledCommands: []string{"unicorn"}, version: universion, properties: props, matchesVersionFile: matchesVersionFile, } lang := bootStrapLanguageTest(args) assert.True(t, lang.enabled(), tc.Case) assert.Equal(t, universion, lang.string(), tc.Case) if tc.ColorBackground { assert.Equal(t, tc.ExpectedColor, lang.props.background, tc.Case) return } assert.Equal(t, tc.ExpectedColor, lang.props.foreground, tc.Case) } }