oh-my-posh/src/segment_ruby_test.go

110 lines
3.7 KiB
Go
Raw Normal View History

2020-10-25 08:32:14 -07:00
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRuby(t *testing.T) {
cases := []struct {
2021-02-03 10:33:05 -08:00
Case string
2020-10-25 08:32:14 -07:00
ExpectedString string
ExpectedEnabled bool
HasRbenv bool
HasRvmprompt bool
HasChruby bool
HasAsdf bool
HasRuby bool
Version string
HasRubyFiles bool
HasRakeFile bool
HasGemFile bool
DisplayVersion bool
}{
2021-02-03 10:33:05 -08:00
{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},
2020-10-25 08:32:14 -07:00
{
2021-02-03 10:33:05 -08:00
Case: "Version with chruby",
2020-10-25 08:32:14 -07:00
ExpectedString: "ruby-2.6.3",
ExpectedEnabled: true,
DisplayVersion: true,
HasRubyFiles: true,
HasChruby: true,
Version: ` * ruby-2.6.3
ruby-1.9.3-p392
jruby-1.7.0
rubinius-2.0.0-rc1`,
},
{
2021-02-03 10:33:05 -08:00
Case: "Version with chruby line 2",
2020-10-25 08:32:14 -07:00
ExpectedString: "ruby-1.9.3-p392",
ExpectedEnabled: true,
DisplayVersion: true,
HasRubyFiles: true,
HasChruby: true,
Version: ` ruby-2.6.3
* ruby-1.9.3-p392
jruby-1.7.0
rubinius-2.0.0-rc1`,
},
{
2021-02-03 10:33:05 -08:00
Case: "Version with asdf",
2020-10-25 08:32:14 -07:00
ExpectedString: "2.6.3",
ExpectedEnabled: true,
DisplayVersion: true,
HasRubyFiles: true,
HasAsdf: true,
2021-02-15 23:36:37 -08:00
Version: "ruby 2.6.3 /Users/jan/Projects/oh-my-posh/.tool-versions",
2020-10-25 08:32:14 -07:00
},
{
2021-02-03 10:33:05 -08:00
Case: "Version with asdf not set",
2020-10-25 08:32:14 -07:00
ExpectedString: "",
2021-02-03 10:33:05 -08:00
ExpectedEnabled: true,
2020-10-25 08:32:14 -07:00
DisplayVersion: true,
HasRubyFiles: true,
HasAsdf: true,
Version: "ruby ______ No version set. Run \"asdf <global|shell|local> ruby <version>\"",
},
2021-02-03 10:33:05 -08:00
{
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]",
},
2020-10-25 08:32:14 -07:00
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("hasCommand", "rbenv").Return(tc.HasRbenv)
env.On("runCommand", "rbenv", []string{"version-name"}).Return(tc.Version, nil)
env.On("hasCommand", "rvm-prompt").Return(tc.HasRvmprompt)
env.On("runCommand", "rvm-prompt", []string{"i", "v", "g"}).Return(tc.Version, nil)
env.On("hasCommand", "chruby").Return(tc.HasChruby)
env.On("runCommand", "chruby", []string(nil)).Return(tc.Version, nil)
env.On("hasCommand", "asdf").Return(tc.HasAsdf)
env.On("runCommand", "asdf", []string{"current", "ruby"}).Return(tc.Version, nil)
env.On("hasCommand", "ruby").Return(tc.HasRuby)
env.On("runCommand", "ruby", []string{"--version"}).Return(tc.Version, nil)
env.On("hasFiles", "*.rb").Return(tc.HasRubyFiles)
env.On("hasFiles", "Rakefile").Return(tc.HasRakeFile)
env.On("hasFiles", "Gemfile").Return(tc.HasGemFile)
env.On("getcwd", nil).Return("/usr/home/project")
env.On("homeDir", nil).Return("/usr/home")
2021-11-26 01:37:33 -08:00
var props properties = map[Property]interface{}{
DisplayVersion: tc.DisplayVersion,
2020-10-25 08:32:14 -07:00
}
2021-02-03 10:33:05 -08:00
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))
2020-10-25 08:32:14 -07:00
}
}