2021-02-15 11:51:48 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/alecthomas/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeMatchesVersionFile(t *testing.T) {
|
2021-11-15 02:35:45 -08:00
|
|
|
nodeVersion := version{
|
|
|
|
Full: "1.2.3",
|
|
|
|
Major: "1",
|
|
|
|
Minor: "2",
|
|
|
|
Patch: "3",
|
|
|
|
}
|
2021-02-15 11:51:48 -08:00
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected bool
|
|
|
|
RCVersion string
|
|
|
|
}{
|
2021-11-15 02:35:45 -08:00
|
|
|
{Case: "no file context", Expected: true, RCVersion: ""},
|
|
|
|
{Case: "version match", Expected: true, RCVersion: "1.2.3"},
|
|
|
|
{Case: "version mismatch", Expected: false, RCVersion: "3.2.1"},
|
|
|
|
{Case: "version match in other format", Expected: true, RCVersion: "v1.2.3"},
|
|
|
|
{Case: "version match without patch", Expected: true, RCVersion: "1.2"},
|
|
|
|
{Case: "version match without patch in other format", Expected: true, RCVersion: "v1.2"},
|
|
|
|
{Case: "version match without minor", Expected: true, RCVersion: "1"},
|
|
|
|
{Case: "version match without minor in other format", Expected: true, RCVersion: "v1"},
|
2021-02-15 11:51:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getFileContent", ".nvmrc").Return(tc.RCVersion)
|
2021-11-25 23:54:16 -08:00
|
|
|
|
2021-02-15 11:51:48 -08:00
|
|
|
node := &node{
|
2021-12-03 11:19:57 -08:00
|
|
|
language: language{
|
|
|
|
env: env,
|
|
|
|
version: nodeVersion,
|
2021-02-15 11:51:48 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case)
|
|
|
|
}
|
|
|
|
}
|
2021-04-17 04:47:28 -07:00
|
|
|
|
|
|
|
func TestNodeInContext(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
HasYarn bool
|
|
|
|
hasNPM bool
|
|
|
|
hasDefault bool
|
|
|
|
PkgMgrEnabled bool
|
|
|
|
ExpectedString string
|
|
|
|
}{
|
|
|
|
{Case: "no package manager file", ExpectedString: "", PkgMgrEnabled: true},
|
|
|
|
{Case: "yarn", HasYarn: true, ExpectedString: "yarn", PkgMgrEnabled: true},
|
|
|
|
{Case: "npm", hasNPM: true, ExpectedString: "npm", PkgMgrEnabled: true},
|
|
|
|
{Case: "default", hasDefault: true, ExpectedString: "npm", PkgMgrEnabled: true},
|
|
|
|
{Case: "disabled", HasYarn: true, ExpectedString: "", PkgMgrEnabled: false},
|
2021-04-17 22:16:38 -07:00
|
|
|
{Case: "yarn and npm", HasYarn: true, hasNPM: true, ExpectedString: "yarn", PkgMgrEnabled: true},
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("hasFiles", "yarn.lock").Return(tc.HasYarn)
|
|
|
|
env.On("hasFiles", "package-lock.json").Return(tc.hasNPM)
|
|
|
|
env.On("hasFiles", "package.json").Return(tc.hasDefault)
|
|
|
|
node := &node{
|
2021-12-03 11:19:57 -08:00
|
|
|
language: language{
|
2021-04-17 04:47:28 -07:00
|
|
|
env: env,
|
2021-11-26 01:37:33 -08:00
|
|
|
props: map[Property]interface{}{
|
|
|
|
YarnIcon: "yarn",
|
|
|
|
NPMIcon: "npm",
|
|
|
|
DisplayPackageManager: tc.PkgMgrEnabled,
|
2021-04-17 04:47:28 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
node.loadContext()
|
2021-12-04 04:06:05 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, node.PackageManagerIcon, tc.Case)
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
|
|
|
}
|