From e9e7ffdb7cbdf163a62104f3d92a80381f5e1ae4 Mon Sep 17 00:00:00 2001 From: lnu Date: Tue, 29 Dec 2020 19:49:15 +0100 Subject: [PATCH] fix: align dotnet segment with other languages --- src/segment_dotnet.go | 53 ++++++++++++------------------------ src/segment_dotnet_test.go | 11 ++++---- src/segment_language.go | 35 ++++++++++++++++++++---- src/segment_language_test.go | 5 ++-- 4 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/segment_dotnet.go b/src/segment_dotnet.go index 550ec43a160a..e0b06f401849 100644 --- a/src/segment_dotnet.go +++ b/src/segment_dotnet.go @@ -1,14 +1,7 @@ package main -import ( - "errors" -) - type dotnet struct { - props *properties - env environmentInfo - activeVersion string - unsupportedVersion bool + language *language } const ( @@ -18,41 +11,29 @@ const ( ) func (d *dotnet) string() string { - if d.unsupportedVersion { - return d.props.getString(UnsupportedDotnetVersionIcon, "\u2327") - } + version := d.language.string() - if d.props.getBool(DisplayVersion, true) { - return d.activeVersion + // Exit code 145 is a special indicator that dotnet + // ran, but the current project config settings specify + // use of an SDK that isn't installed. + if d.language.exitCode == 145 { + return d.language.props.getString(UnsupportedDotnetVersionIcon, "\u2327") } - return "" + return version } func (d *dotnet) init(props *properties, env environmentInfo) { - d.props = props - d.env = env + d.language = &language{ + env: env, + props: props, + commands: []string{"dotnet"}, + versionParam: "--version", + extensions: []string{"*.cs", "*.vb", "*.sln", "*.csproj", "*.vbproj"}, + versionRegex: `(?P[0-9]+.[0-9]+.[0-9]+)`, + } } func (d *dotnet) enabled() bool { - if !d.env.hasCommand("dotnet") { - return false - } - - output, err := d.env.runCommand("dotnet", "--version") - if err == nil { - d.activeVersion = output - return true - } - - // Exit code 145 is a special indicator that dotnet - // ran, but the current project config settings specify - // use of an SDK that isn't installed. - var exerr *commandError - if errors.As(err, &exerr) && exerr.exitCode == 145 { - d.unsupportedVersion = true - return true - } - - return false + return d.language.enabled() } diff --git a/src/segment_dotnet_test.go b/src/segment_dotnet_test.go index 9172cdad1d53..dc07b79da1c0 100644 --- a/src/segment_dotnet_test.go +++ b/src/segment_dotnet_test.go @@ -23,17 +23,18 @@ func bootStrapDotnetTest(args *dotnetArgs) *dotnet { } else { env.On("runCommand", "dotnet", []string{"--version"}).Return(args.version, nil) } + + env.On("hasFiles", "*.cs").Return(true) + env.On("getPathSeperator", nil).Return("") props := &properties{ values: map[Property]interface{}{ DisplayVersion: args.displayVersion, UnsupportedDotnetVersionIcon: args.unsupportedIcon, }, } - a := &dotnet{ - env: env, - props: props, - } - return a + dotnet := &dotnet{} + dotnet.init(props, env) + return dotnet } func TestEnabledDotnetNotFound(t *testing.T) { diff --git a/src/segment_language.go b/src/segment_language.go index 6540e039ae2e..c2eb277ac1a1 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -1,13 +1,17 @@ package main +import "errors" + type language struct { props *properties env environmentInfo extensions []string commands []string + executable string versionParam string versionRegex string version string + exitCode int } const ( @@ -22,6 +26,7 @@ const ( ) func (l *language) string() string { + // call getVersion if displayVersion set in config if l.props.getBool(DisplayVersion, true) && l.getVersion() { return l.version } @@ -29,6 +34,11 @@ func (l *language) string() string { } func (l *language) enabled() bool { + // check if one of the defined commands exists in the system + if !l.hasCommand() { + return false + } + displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext) switch displayMode { @@ -43,6 +53,7 @@ func (l *language) enabled() bool { } } +// isInContext will return true at least one file matching the extensions is found func (l *language) isInContext() bool { for i, extension := range l.extensions { if l.env.hasFiles(extension) { @@ -56,20 +67,32 @@ func (l *language) isInContext() bool { return true } +// getVersion returns the version an exit code returned by the exexutable func (l *language) getVersion() bool { - var executable string + versionInfo, err := l.env.runCommand(l.executable, l.versionParam) + var exerr *commandError + if err == nil { + values := findNamedRegexMatch(l.versionRegex, versionInfo) + l.exitCode = 0 + l.version = values["version"] + } else { + errors.As(err, &exerr) + l.exitCode = exerr.exitCode + l.version = "" + } + return true +} + +// hasCommand checks if one of the commands exists and set it as executablr +func (l *language) hasCommand() bool { for i, command := range l.commands { if l.env.hasCommand(command) { - executable = command + l.executable = command break } if i == len(l.commands)-1 { return false } } - versionInfo, _ := l.env.runCommand(executable, l.versionParam) - values := findNamedRegexMatch(l.versionRegex, versionInfo) - l.version = values["version"] - return true } diff --git a/src/segment_language_test.go b/src/segment_language_test.go index a1793ca36f0c..f42fb0d9f534 100644 --- a/src/segment_language_test.go +++ b/src/segment_language_test.go @@ -68,8 +68,7 @@ func TestLanguageFilesFoundButNoCommandAndVersion(t *testing.T) { displayVersion: true, } lang := bootStrapLanguageTest(args) - assert.True(t, lang.enabled()) - assert.Equal(t, "", lang.string()) + assert.False(t, lang.enabled(), "unicorn is not available") } func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) { @@ -80,7 +79,7 @@ func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) { enabledExtensions: []string{uni}, } lang := bootStrapLanguageTest(args) - assert.True(t, lang.enabled(), "unicorn is not available") + assert.False(t, lang.enabled(), "unicorn is not available") } func TestLanguageDisabledNoFiles(t *testing.T) {