Skip to content

Commit

Permalink
feat: version file mismatch indication
Browse files Browse the repository at this point in the history
relates to #378
  • Loading branch information
JanDeDobbeleer committed Feb 7, 2021
1 parent 4cb94d0 commit b26b337
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 38 deletions.
21 changes: 21 additions & 0 deletions src/segment_language.go
Expand Up @@ -10,6 +10,8 @@ type loadContext func()

type inContext func() bool

type matchesVersionFile func() bool

type version struct {
full string
major string
Expand Down Expand Up @@ -68,6 +70,7 @@ type language struct {
exitCode int
loadContext loadContext
inContext inContext
matchesVersionFile matchesVersionFile
}

const (
Expand All @@ -83,6 +86,10 @@ const (
DisplayModeContext string = "context"
// MissingCommandText sets the text to display when the command is not present in the system
MissingCommandText Property = "missing_command_text"
// VersionMismatchColor displays empty string by default
VersionMismatchColor Property = "version_mismatch_color"
// EnableVersionMismatch displays empty string by default
EnableVersionMismatch Property = "enable_version_mismatch"
)

func (l *language) string() string {
Expand All @@ -98,6 +105,9 @@ func (l *language) string() string {
if l.props.getBool(EnableHyperlink, false) {
return l.activeCommand.buildVersionURL(l.versionURLTemplate)
}
if l.props.getBool(EnableVersionMismatch, false) {
l.setVersionFileMismatch()
}
return l.activeCommand.version.full
}

Expand Down Expand Up @@ -166,3 +176,14 @@ func (l *language) inLanguageContext() bool {
}
return l.inContext()
}

func (l *language) setVersionFileMismatch() {
if l.matchesVersionFile == nil || l.matchesVersionFile() {
return
}
if l.props.getBool(ColorBackground, false) {
l.props.background = l.props.getColor(VersionMismatchColor, l.props.background)
return
}
l.props.foreground = l.props.getColor(VersionMismatchColor, l.props.foreground)
}
137 changes: 99 additions & 38 deletions src/segment_language_test.go
Expand Up @@ -14,16 +14,14 @@ const (

type languageArgs struct {
version string
displayVersion bool
displayMode string
extensions []string
enabledExtensions []string
commands []*cmd
enabledCommands []string
missingCommandText string
versionURLTemplate string
enableHyperlink bool
expectedError error
properties map[Property]interface{}
matchesVersionFile matchesVersionFile
}

func (l *languageArgs) hasvalue(value string, list []string) bool {
Expand All @@ -45,21 +43,15 @@ func bootStrapLanguageTest(args *languageArgs) *language {
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,
},
}
if args.missingCommandText != "" {
props.values[MissingCommandText] = args.missingCommandText
values: args.properties,
}
l := &language{
props: props,
env: env,
extensions: args.extensions,
commands: args.commands,
versionURLTemplate: args.versionURLTemplate,
matchesVersionFile: args.matchesVersionFile,
}
return l
}
Expand All @@ -74,14 +66,16 @@ func TestLanguageFilesFoundButNoCommandAndVersionAndDisplayVersion(t *testing.T)
},
extensions: []string{uni},
enabledExtensions: []string{uni},
displayVersion: true,
}
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{
{
Expand All @@ -91,7 +85,7 @@ func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testin
},
extensions: []string{uni},
enabledExtensions: []string{uni},
displayVersion: false,
properties: props,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled(), "unicorn is not available")
Expand Down Expand Up @@ -123,7 +117,6 @@ func TestLanguageDisabledNoFiles(t *testing.T) {
extensions: []string{uni},
enabledExtensions: []string{},
enabledCommands: []string{"unicorn"},
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.False(t, lang.enabled(), "no files in the current directory")
Expand All @@ -142,7 +135,6 @@ func TestLanguageEnabledOneExtensionFound(t *testing.T) {
enabledExtensions: []string{uni},
enabledCommands: []string{"unicorn"},
version: universion,
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
Expand All @@ -162,7 +154,6 @@ func TestLanguageEnabledSecondExtensionFound(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"unicorn"},
version: universion,
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
Expand All @@ -187,7 +178,6 @@ func TestLanguageEnabledSecondCommand(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
Expand All @@ -207,14 +197,16 @@ func TestLanguageEnabledAllExtensionsFound(t *testing.T) {
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"unicorn"},
version: universion,
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) {
props := map[Property]interface{}{
DisplayVersion: false,
}
args := &languageArgs{
commands: []*cmd{
{
Expand All @@ -227,40 +219,46 @@ func TestLanguageEnabledNoVersion(t *testing.T) {
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"unicorn"},
version: universion,
displayVersion: false,
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,
displayVersion: false,
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 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,
missingCommandText: "missing",
displayVersion: true,
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, args.missingCommandText, lang.string(), "unicorn is available and uni and corn files are found")
assert.Equal(t, expected, lang.string(), "unicorn is available and uni and corn files are found")
}

func TestLanguageEnabledCommandExitCode(t *testing.T) {
Expand All @@ -277,7 +275,6 @@ func TestLanguageEnabledCommandExitCode(t *testing.T) {
enabledExtensions: []string{uni, corn},
enabledCommands: []string{"uni"},
version: universion,
displayVersion: true,
expectedError: &commandError{exitCode: expected},
}
lang := bootStrapLanguageTest(args)
Expand All @@ -287,6 +284,9 @@ func TestLanguageEnabledCommandExitCode(t *testing.T) {
}

func TestLanguageHyperlinkEnabled(t *testing.T) {
props := map[Property]interface{}{
EnableHyperlink: true,
}
args := &languageArgs{
commands: []*cmd{
{
Expand All @@ -305,15 +305,17 @@ func TestLanguageHyperlinkEnabled(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
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{
{
Expand All @@ -332,15 +334,17 @@ func TestLanguageHyperlinkEnabledWrongRegex(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
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{
{
Expand All @@ -359,15 +363,17 @@ func TestLanguageHyperlinkEnabledLessParamInTemplate(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
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{
{
Expand All @@ -386,10 +392,65 @@ func TestLanguageHyperlinkEnabledMoreParamInTemplate(t *testing.T) {
enabledExtensions: []string{corn},
enabledCommands: []string{"corn"},
version: universion,
displayVersion: true,
enableHyperlink: true,
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<version>.*)",
},
},
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)
}
}
5 changes: 5 additions & 0 deletions src/segment_node.go
Expand Up @@ -21,9 +21,14 @@ func (n *node) init(props *properties, env environmentInfo) {
},
},
versionURLTemplate: "[%[1]s](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V%[2]s.md#%[1]s)",
matchesVersionFile: n.matchesVersionFile,
}
}

func (n *node) enabled() bool {
return n.language.enabled()
}

func (n *node) matchesVersionFile() bool {
return true
}

0 comments on commit b26b337

Please sign in to comment.