oh-my-posh/segment_node_test.go
Travis Illig 5844faa54d feat: dotnet segment for .NET SDK display
New segment for .NET SDK version (or unsupported version) display.

Includes update for handling command execution errors so segments
can act differently based on exit codes. Using a custom error
type to make it testable rather than passing the OS error directly
to the segment.
2020-10-16 11:39:01 -07:00

116 lines
2.7 KiB
Go
Executable file

package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type nodeArgs struct {
enabled bool
nodeVersion string
hasJS bool
hasTS bool
displayVersion bool
}
func bootStrapNodeTest(args *nodeArgs) *node {
env := new(MockedEnvironment)
env.On("hasCommand", "node").Return(args.enabled)
env.On("runCommand", "node", []string{"--version"}).Return(args.nodeVersion, nil)
env.On("hasFiles", "*.js").Return(args.hasJS)
env.On("hasFiles", "*.ts").Return(args.hasTS)
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
},
}
n := &node{
env: env,
props: props,
}
return n
}
func TestNodeWriterDisabled(t *testing.T) {
args := &nodeArgs{
enabled: false,
}
node := bootStrapNodeTest(args)
assert.False(t, node.enabled(), "node is not available")
}
func TestNodeWriterDisabledNoJSorTSFiles(t *testing.T) {
args := &nodeArgs{
enabled: true,
}
node := bootStrapNodeTest(args)
assert.False(t, node.enabled(), "no JS or TS files in the current directory")
}
func TestNodeEnabledJSFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasJS: true,
displayVersion: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and JS files are found")
}
func TestNodeEnabledTsFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasTS: true,
displayVersion: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and TS files are found")
}
func TestNodeEnabledJsAndTsFiles(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasJS: true,
hasTS: true,
displayVersion: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "node is available and JS and TS files are found")
}
func TestNodeEnabledNoVersion(t *testing.T) {
expected := ""
args := &nodeArgs{
enabled: true,
nodeVersion: "1.14",
hasJS: true,
displayVersion: false,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "we don't expect a version")
}
func TestNodeEnabledNodeVersion(t *testing.T) {
expected := "1.14"
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
hasJS: true,
displayVersion: true,
}
node := bootStrapNodeTest(args)
assert.True(t, node.enabled())
assert.Equal(t, expected, node.string(), "we expect a version")
}