Skip to content

Commit

Permalink
feat(node): version mismatch color
Browse files Browse the repository at this point in the history
resolves #378
  • Loading branch information
JanDeDobbeleer committed Feb 15, 2021
1 parent c71c5e8 commit 0da40e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/docs/segment-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ Display the currently active node version.
- display_mode: `string` - determines when the segment is displayed
- `always`: The segment is always displayed
- `files`: The segment is only displayed when `*.js`, `*.ts`, or `package.json` files are present (default)
- enable_version_mismatch: `boolean` - color the segment when the version in `.nvmrc` doesn't match the
returned node version
- color_background: `boolean` - color the background or foreground for `version_mismatch_color` - defaults to `false`
- version_mismatch_color: `string` [color][colors] - the color to use for `enable_version_mismatch` - defaults to
segment's background or foreground color
8 changes: 6 additions & 2 deletions src/segment_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (n *node) init(props *properties, env environmentInfo) {
n.language = &language{
env: env,
props: props,
extensions: []string{"*.js", "*.ts", "package.json", ".nvm"},
extensions: []string{"*.js", "*.ts", "package.json", ".nvmrc"},
commands: []*cmd{
{
executable: "node",
Expand All @@ -30,5 +30,9 @@ func (n *node) enabled() bool {
}

func (n *node) matchesVersionFile() bool {
return true
fileVersion := n.language.env.getFileContent(".nvmrc")
if len(fileVersion) == 0 {
return true
}
return fileVersion == n.language.activeCommand.version.full
}
36 changes: 36 additions & 0 deletions src/segment_node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"testing"

"github.com/alecthomas/assert"
)

func TestNodeMatchesVersionFile(t *testing.T) {
cases := []struct {
Case string
Expected bool
RCVersion string
Version string
}{
{Case: "no file context", Expected: true, RCVersion: "", Version: "durp"},
{Case: "version match", Expected: true, RCVersion: "durp", Version: "durp"},
{Case: "version mismatch", Expected: false, RCVersion: "werp", Version: "durp"},
}

for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getFileContent", ".nvmrc").Return(tc.RCVersion)
node := &node{
language: &language{
env: env,
activeCommand: &cmd{
version: &version{
full: tc.Version,
},
},
},
}
assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case)
}
}

0 comments on commit 0da40e6

Please sign in to comment.