refactor: align ruby with language

This commit is contained in:
Jan De Dobbeleer 2021-02-03 19:33:05 +01:00 committed by Jan De Dobbeleer
parent c43cbac284
commit 1ba666fe97
2 changed files with 64 additions and 55 deletions

View file

@ -1,56 +1,53 @@
package main
type ruby struct {
props *properties
env environmentInfo
version string
language *language
}
func (r *ruby) string() string {
if r.props.getBool(DisplayVersion, true) {
return r.version
version := r.language.string()
// asdf default non-set version
if version == "______" {
return ""
}
return ""
return version
}
func (r *ruby) init(props *properties, env environmentInfo) {
r.props = props
r.env = env
r.language = &language{
env: env,
props: props,
extensions: []string{"*.rb", "Rakefile", "Gemfile"},
commands: []*cmd{
{
executable: "rbenv",
args: []string{"version-name"},
regex: `(?P<version>.+)`,
},
{
executable: "rvm-prompt",
args: []string{"i", "v", "g"},
regex: `(?P<version>.+)`,
},
{
executable: "chruby",
args: []string(nil),
regex: `\* (?P<version>.+)\n`,
},
{
executable: "asdf",
args: []string{"current", "ruby"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
{
executable: "ruby",
args: []string{"--version"},
regex: `ruby\s+(?P<version>[^\s]+)\s+`,
},
},
}
}
func (r *ruby) enabled() bool {
if !r.env.hasFiles("*.rb") && !r.env.hasFiles("Rakefile") && !r.env.hasFiles("Gemfile") {
return false
}
if !r.props.getBool(DisplayVersion, true) {
return true
}
r.version = r.getVersion()
return r.version != ""
}
func (r *ruby) getVersion() string {
options := []struct {
Command string
Args []string
Regex string
}{
{Command: "rbenv", Args: []string{"version-name"}, Regex: `(?P<version>.+)`},
{Command: "rvm-prompt", Args: []string{"i", "v", "g"}, Regex: `(?P<version>.+)`},
{Command: "chruby", Args: []string(nil), Regex: `\* (?P<version>.+)\n`},
{Command: "asdf", Args: []string{"current", "ruby"}, Regex: `ruby\s+(?P<version>[^\s_]+)\s+`},
{Command: "ruby", Args: []string{"--version"}, Regex: `ruby\s+(?P<version>[^\s_]+)\s+`},
}
for _, option := range options {
if !r.env.hasCommand(option.Command) {
continue
}
version, _ := r.env.runCommand(option.Command, option.Args...)
match := findNamedRegexMatch(option.Regex, version)
if match["version"] == "" {
continue
}
return match["version"]
}
return ""
return r.language.enabled()
}

View file

@ -9,6 +9,7 @@ import (
func TestRuby(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
ExpectedEnabled bool
HasRbenv bool
@ -22,13 +23,14 @@ func TestRuby(t *testing.T) {
HasGemFile bool
DisplayVersion bool
}{
{ExpectedString: "", ExpectedEnabled: false},
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRubyFiles: true},
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRakeFile: true},
{ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasGemFile: true},
{ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true, HasGemFile: true},
{ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true},
{Case: "No files", ExpectedString: "", ExpectedEnabled: false},
{Case: "Ruby files", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRubyFiles: true},
{Case: "Rakefile", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasRakeFile: true},
{Case: "Gemfile", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: false, HasGemFile: true},
{Case: "Gemfile with version", ExpectedString: "", ExpectedEnabled: true, DisplayVersion: true, HasGemFile: true},
{Case: "No files with version", ExpectedString: "", ExpectedEnabled: false, DisplayVersion: true},
{
Case: "Version with chruby",
ExpectedString: "ruby-2.6.3",
ExpectedEnabled: true,
DisplayVersion: true,
@ -40,6 +42,7 @@ func TestRuby(t *testing.T) {
rubinius-2.0.0-rc1`,
},
{
Case: "Version with chruby line 2",
ExpectedString: "ruby-1.9.3-p392",
ExpectedEnabled: true,
DisplayVersion: true,
@ -51,6 +54,7 @@ func TestRuby(t *testing.T) {
rubinius-2.0.0-rc1`,
},
{
Case: "Version with asdf",
ExpectedString: "2.6.3",
ExpectedEnabled: true,
DisplayVersion: true,
@ -59,13 +63,23 @@ func TestRuby(t *testing.T) {
Version: "ruby 2.6.3 /Users/jan/Projects/oh-my-posh3/.tool-versions",
},
{
Case: "Version with asdf not set",
ExpectedString: "",
ExpectedEnabled: false,
ExpectedEnabled: true,
DisplayVersion: true,
HasRubyFiles: true,
HasAsdf: true,
Version: "ruby ______ No version set. Run \"asdf <global|shell|local> ruby <version>\"",
},
{
Case: "Version with ruby",
ExpectedString: "2.6.3",
ExpectedEnabled: true,
DisplayVersion: true,
HasRubyFiles: true,
HasRuby: true,
Version: "ruby 2.6.3 (2019-04-16 revision 67580) [universal.x86_64-darwin20]",
},
}
for _, tc := range cases {
env := new(MockedEnvironment)
@ -87,11 +101,9 @@ func TestRuby(t *testing.T) {
DisplayVersion: tc.DisplayVersion,
},
}
ruby := &ruby{
env: env,
props: props,
}
assert.Equal(t, tc.ExpectedEnabled, ruby.enabled(), fmt.Sprintf("Failed in case: %+v", tc))
assert.Equal(t, tc.ExpectedString, ruby.string(), fmt.Sprintf("Failed in case: %+v", tc))
ruby := &ruby{}
ruby.init(props, env)
assert.Equal(t, tc.ExpectedEnabled, ruby.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, ruby.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}