2020-11-14 11:04:04 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-10 03:14:43 -08:00
|
|
|
universion = "1.3.307"
|
2020-11-14 11:04:04 -08:00
|
|
|
uni = "*.uni"
|
|
|
|
corn = "*.corn"
|
|
|
|
)
|
|
|
|
|
|
|
|
type languageArgs struct {
|
2020-12-27 23:33:58 -08:00
|
|
|
version string
|
|
|
|
displayVersion bool
|
|
|
|
displayMode string
|
|
|
|
extensions []string
|
|
|
|
enabledExtensions []string
|
2021-02-03 10:11:32 -08:00
|
|
|
commands []*cmd
|
2020-12-27 23:33:58 -08:00
|
|
|
enabledCommands []string
|
|
|
|
missingCommandText string
|
2021-02-03 10:11:32 -08:00
|
|
|
versionURLTemplate string
|
2021-01-10 03:14:43 -08:00
|
|
|
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{}{
|
2021-01-10 03:14:43 -08:00
|
|
|
DisplayVersion: args.displayVersion,
|
|
|
|
DisplayMode: args.displayMode,
|
|
|
|
EnableHyperlink: args.enableHyperlink,
|
2020-11-14 11:04:04 -08:00
|
|
|
},
|
|
|
|
}
|
2020-12-27 23:33:58 -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
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:33:58 -08:00
|
|
|
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},
|
2020-12-21 11:16:33 -08:00
|
|
|
displayVersion: true,
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
|
|
|
lang := bootStrapLanguageTest(args)
|
2021-01-14 23:08:56 -08:00
|
|
|
assert.True(t, lang.enabled())
|
|
|
|
assert.Equal(t, MissingCommandText, lang.string(), "unicorn is not available")
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
|
|
|
|
2020-12-27 23:33:58 -08:00
|
|
|
func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testing.T) {
|
|
|
|
args := &languageArgs{
|
2021-02-03 10:11:32 -08:00
|
|
|
commands: []*cmd{
|
|
|
|
{
|
|
|
|
executable: "unicorn",
|
|
|
|
args: []string{"--version"},
|
|
|
|
},
|
|
|
|
},
|
2020-12-27 23:33:58 -08:00
|
|
|
extensions: []string{uni},
|
|
|
|
enabledExtensions: []string{uni},
|
|
|
|
displayVersion: false,
|
|
|
|
}
|
|
|
|
lang := bootStrapLanguageTest(args)
|
|
|
|
assert.True(t, lang.enabled(), "unicorn is not available")
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:16:33 -08:00
|
|
|
func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) {
|
|
|
|
args := &languageArgs{
|
2021-02-03 10:11:32 -08:00
|
|
|
commands: []*cmd{
|
|
|
|
{
|
|
|
|
executable: "unicorn",
|
|
|
|
args: []string{"--version"},
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 11:16:33 -08:00
|
|
|
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")
|
|
|
|
}
|
2020-12-27 23:33:58 -08:00
|
|
|
|
|
|
|
func TestLanguageEnabledMissingCommand(t *testing.T) {
|
|
|
|
args := &languageArgs{
|
2021-02-03 10:11:32 -08:00
|
|
|
commands: []*cmd{},
|
2020-12-27 23:33:58 -08:00
|
|
|
extensions: []string{uni, corn},
|
|
|
|
enabledExtensions: []string{uni, corn},
|
2021-02-03 10:11:32 -08:00
|
|
|
enabledCommands: []string{"unicorn"},
|
|
|
|
version: universion,
|
2020-12-27 23:33:58 -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 TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
|
|
|
|
args := &languageArgs{
|
2021-02-03 10:11:32 -08:00
|
|
|
commands: []*cmd{},
|
2020-12-27 23:33:58 -08:00
|
|
|
extensions: []string{uni, corn},
|
|
|
|
enabledExtensions: []string{uni, corn},
|
2021-02-03 10:11:32 -08:00
|
|
|
enabledCommands: []string{"unicorn"},
|
|
|
|
version: universion,
|
2020-12-27 23:33:58 -08:00
|
|
|
missingCommandText: "missing",
|
2021-02-03 10:11:32 -08:00
|
|
|
displayVersion: true,
|
2020-12-27 23:33:58 -08:00
|
|
|
}
|
|
|
|
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-01-10 03:14:43 -08:00
|
|
|
|
2021-02-03 10:11:32 -08:00
|
|
|
func TestLanguageEnabledCommandExitCode(t *testing.T) {
|
|
|
|
expected := 200
|
2021-01-10 03:14:43 -08:00
|
|
|
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]+)))`,
|
|
|
|
},
|
|
|
|
},
|
2021-01-10 03:14:43 -08:00
|
|
|
extensions: []string{uni, corn},
|
2021-02-03 10:11:32 -08:00
|
|
|
enabledExtensions: []string{uni, corn},
|
|
|
|
enabledCommands: []string{"uni"},
|
2021-01-10 03:14:43 -08:00
|
|
|
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,
|
2021-01-10 03:14:43 -08:00
|
|
|
}
|
|
|
|
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,
|
2021-01-10 03:14:43 -08:00
|
|
|
}
|
|
|
|
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())
|
2021-01-10 03:14:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-01-10 03:14:43 -08:00
|
|
|
}
|
|
|
|
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,
|
2021-01-10 03:14:43 -08:00
|
|
|
}
|
|
|
|
lang := bootStrapLanguageTest(args)
|
|
|
|
assert.True(t, lang.enabled())
|
|
|
|
assert.Equal(t, "1.3.307", lang.string())
|
|
|
|
}
|