feat(node): export expected version

resolves #3576
This commit is contained in:
Jan De Dobbeleer 2023-03-13 07:49:09 +01:00 committed by Jan De Dobbeleer
parent 701d46cb73
commit c03ee35107
4 changed files with 39 additions and 30 deletions

View file

@ -20,7 +20,7 @@ type loadContext func()
type inContext func() bool
type getVersion func() (string, error)
type matchesVersionFile func() bool
type matchesVersionFile func() (string, bool)
type version struct {
Full string
@ -31,6 +31,7 @@ type version struct {
BuildMetadata string
URL string
Executable string
Expected string
}
type cmd struct {
@ -128,13 +129,19 @@ func (l *language) Enabled() bool {
enabled = l.hasLanguageFiles() || l.hasLanguageFolders() || l.inLanguageContext()
}
}
if !enabled || !l.props.GetBool(properties.FetchVersion, true) {
return enabled
}
err := l.setVersion()
if err != nil {
l.Error = err.Error()
}
if l.matchesVersionFile != nil {
l.version.Expected, l.Mismatch = l.matchesVersionFile()
}
return enabled
}

View file

@ -2,6 +2,7 @@ package segments
import (
"fmt"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
@ -46,12 +47,7 @@ func (n *Node) Init(props properties.Properties, env platform.Environment) {
}
func (n *Node) Enabled() bool {
if n.language.Enabled() {
n.Mismatch = !n.matchesVersionFile()
return true
}
return false
return n.language.Enabled()
}
func (n *Node) loadContext() {
@ -67,10 +63,10 @@ func (n *Node) loadContext() {
}
}
func (n *Node) matchesVersionFile() bool {
func (n *Node) matchesVersionFile() (string, bool) {
fileVersion := n.language.env.FileContent(".nvmrc")
if len(fileVersion) == 0 {
return true
return "", true
}
re := fmt.Sprintf(
@ -80,5 +76,7 @@ func (n *Node) matchesVersionFile() bool {
n.language.version.Patch,
)
return regex.MatchString(re, fileVersion)
version := strings.TrimPrefix(fileVersion, "v")
return version, regex.MatchString(re, fileVersion)
}

View file

@ -19,16 +19,17 @@ func TestNodeMatchesVersionFile(t *testing.T) {
cases := []struct {
Case string
Expected bool
ExpectedVersion string
RCVersion string
}{
{Case: "no file context", Expected: true, RCVersion: ""},
{Case: "version match", Expected: true, RCVersion: "1.2.3"},
{Case: "version mismatch", Expected: false, RCVersion: "3.2.1"},
{Case: "version match in other format", Expected: true, RCVersion: "v1.2.3"},
{Case: "version match without patch", Expected: true, RCVersion: "1.2"},
{Case: "version match without patch in other format", Expected: true, RCVersion: "v1.2"},
{Case: "version match without minor", Expected: true, RCVersion: "1"},
{Case: "version match without minor in other format", Expected: true, RCVersion: "v1"},
{Case: "version match", Expected: true, ExpectedVersion: "1.2.3", RCVersion: "1.2.3"},
{Case: "version mismatch", Expected: false, ExpectedVersion: "3.2.1", RCVersion: "3.2.1"},
{Case: "version match in other format", Expected: true, ExpectedVersion: "1.2.3", RCVersion: "v1.2.3"},
{Case: "version match without patch", Expected: true, ExpectedVersion: "1.2", RCVersion: "1.2"},
{Case: "version match without patch in other format", Expected: true, ExpectedVersion: "1.2", RCVersion: "v1.2"},
{Case: "version match without minor", Expected: true, ExpectedVersion: "1", RCVersion: "1"},
{Case: "version match without minor in other format", Expected: true, ExpectedVersion: "1", RCVersion: "v1"},
}
for _, tc := range cases {
@ -41,7 +42,9 @@ func TestNodeMatchesVersionFile(t *testing.T) {
version: nodeVersion,
},
}
assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case)
version, match := node.matchesVersionFile()
assert.Equal(t, tc.Expected, match, tc.Case)
assert.Equal(t, tc.ExpectedVersion, version, tc.Case)
}
}

View file

@ -24,7 +24,7 @@ Display the currently active [Node.js][node-js] version.
## Properties
| Name | Type | Description |
| ----------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ----------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` |
| `fetch_version` | `boolean` | display the Node.js version - defaults to `true` |
| `missing_command_text` | `string` | text to display when the command is missing - defaults to empty |
@ -56,6 +56,7 @@ Display the currently active [Node.js][node-js] version.
| `.Error` | `string` | error encountered when fetching the version string |
| `.PackageManagerIcon` | `string` | the Yarn or NPM icon when setting `fetch_package_manager` to `true` |
| `.Mismatch` | `boolean` | if the version in `.nvmrc` matches with `.Full` |
| `.Expected` | `string` | the expected version set in `.nvmrc` |
[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates