Skip to content

Commit

Permalink
added documentations and test
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Oct 30, 2023
1 parent 66c0ccd commit 839a635
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
10 changes: 7 additions & 3 deletions build/utils/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ func runNpmLsWithoutNodeModules(executablePath, srcPath string, npmListParams Np
return data, nil
}

func installPackageLock(executablePath, srcPath string, npmInstallCommandArgs []string, npmArgs []string, log utils.Log, npmVersion *version.Version) error {
func installPackageLock(executablePath, srcPath string, npmInstallCommandArgs, npmArgs []string, log utils.Log, npmVersion *version.Version) error {
if npmVersion.AtLeast("6.0.0") {
npmArgs = append(npmArgs, "--package-lock-only")
// Adding 'install' command flags provided by the user, if such were provided in previous steps of the flow, and making sure no duplicates will be inserted
npmArgs = append(npmArgs, filterUniqueArgs(npmInstallCommandArgs, npmArgs)...)
// Installing package-lock to generate the dependencies map.
_, _, err := RunNpmCmd(executablePath, srcPath, AppendNpmCommand(npmArgs, "install"), log)
Expand All @@ -168,7 +169,7 @@ func installPackageLock(executablePath, srcPath string, npmInstallCommandArgs []
return errors.New("it looks like you’re using version " + npmVersion.GetVersion() + " of the npm client. Versions below 6.0.0 require running `npm install` before running this command")
}

// filters out all args from argsToFilter that already in existingArgs. In addition, filters out npm install command and leave only flags within the final returned args
// Filters out all args from argsToFilter that already in existingArgs. In addition, filters out npm install command and leave only flags within the final returned args
func filterUniqueArgs(argsToFilter []string, existingArgs []string) []string {
var filteredArgs []string
for _, arg := range argsToFilter {
Expand Down Expand Up @@ -210,7 +211,10 @@ func GetNpmVersion(executablePath string, log utils.Log) (*version.Version, erro
}

type NpmTreeDepListParam struct {
Args []string
// Required for 'install' and 'ls' commands that might be executed while building NPM dependency tree
Args []string
// 'Install' command args provided by the user. These are optional arguments, which are addable only from certain entry points.
// These arguments might be used while building NPM dependency tree, that might require running 'npm install...'
InstallCommandArgs []string
// Ignore the node_modules folder if exists, using the '--package-lock-only' flag
IgnoreNodeModules bool
Expand Down
41 changes: 37 additions & 4 deletions build/utils/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestReadPackageInfo(t *testing.T) {
return
}

tests := []struct {
testcases := []struct {
json string
pi *PackageInfo
}{
Expand All @@ -35,7 +35,7 @@ func TestReadPackageInfo(t *testing.T) {
&PackageInfo{Name: "build-info-go-tests", Version: "1.0.0", Scope: "@jfrog"}},
{`{}`, &PackageInfo{}},
}
for _, test := range tests {
for _, test := range testcases {
t.Run(test.json, func(t *testing.T) {
packInfo, err := ReadPackageInfo([]byte(test.json), npmVersion)
assert.NoError(t, err)
Expand Down Expand Up @@ -94,14 +94,14 @@ func TestReadPackageInfoFromPackageJsonIfExistErr(t *testing.T) {
}

func TestGetDeployPath(t *testing.T) {
tests := []struct {
testcases := []struct {
expectedPath string
pi *PackageInfo
}{
{`build-info-go-tests/-/build-info-go-tests-1.0.0.tgz`, &PackageInfo{Name: "build-info-go-tests", Version: "1.0.0", Scope: ""}},
{`@jfrog/build-info-go-tests/-/build-info-go-tests-1.0.0.tgz`, &PackageInfo{Name: "build-info-go-tests", Version: "1.0.0", Scope: "@jfrog"}},
}
for _, test := range tests {
for _, test := range testcases {
t.Run(test.expectedPath, func(t *testing.T) {
assert.Equal(t, test.expectedPath, test.pi.GetDeployPath())
})
Expand Down Expand Up @@ -430,3 +430,36 @@ func validateDependencies(t *testing.T, projectPath string, npmArgs []string) {
// Asserting there is at least one dependency.
assert.Greater(t, len(dependencies), 0, "Error: dependencies are not found!")
}

func TestFilterUniqueArgs(t *testing.T) {
var testcases = []struct {
argsToFilter []string
alreadyExists []string
expectedResult []string
}{
{
argsToFilter: []string{"install"},
alreadyExists: []string{},
expectedResult: nil,
},
{
argsToFilter: []string{"install", "--flagA"},
alreadyExists: []string{"--flagA"},
expectedResult: nil,
},
{
argsToFilter: []string{"install", "--flagA", "--flagB"},
alreadyExists: []string{"--flagA"},
expectedResult: []string{"--flagB"},
},
{
argsToFilter: []string{"install", "--flagA", "--flagB"},
alreadyExists: []string{"--flagA", "--flagC"},
expectedResult: []string{"--flagB"},
},
}

for _, testcase := range testcases {
assert.Equal(t, testcase.expectedResult, filterUniqueArgs(testcase.argsToFilter, testcase.alreadyExists))
}
}

0 comments on commit 839a635

Please sign in to comment.