oh-my-posh/src/segments/nx_test.go

46 lines
1.3 KiB
Go
Raw Normal View History

2022-05-26 05:51:26 -07:00
package segments
import (
"oh-my-posh/environment"
"oh-my-posh/mock"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
2022-05-26 05:51:26 -07:00
)
func TestGetNodePackageVersion(t *testing.T) {
cases := []struct {
Case string
PackageJSON string
Version string
ShouldFail bool
NoFiles bool
}{
{Case: "14.1.5", Version: "14.1.5", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.1.5\"}"},
{Case: "14.0.0", Version: "14.0.0", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.0.0\"}"},
{Case: "no files", NoFiles: true, ShouldFail: true},
{Case: "bad data", ShouldFail: true, PackageJSON: "bad data"},
}
for _, tc := range cases {
var env = new(mock.MockedEnvironment)
// mock getVersion methods
env.On("Pwd").Return("posh")
path := filepath.Join("posh", "node_modules", "nx")
env.On("HasFilesInDir", path, "package.json").Return(!tc.NoFiles)
env.On("FileContent", filepath.Join(path, "package.json")).Return(tc.PackageJSON)
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string),
})
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything)
got := getNodePackageVersion(env, "nx")
2022-05-26 05:51:26 -07:00
if tc.ShouldFail {
assert.Empty(t, got, tc.Case)
2022-05-26 05:51:26 -07:00
return
}
assert.Equal(t, tc.Version, got, tc.Case)
}
}