Skip to content

Commit

Permalink
handled edge case in splitNameAndVersion and added test for this func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
eranturgeman committed Aug 3, 2023
1 parent 9570f56 commit 64da880
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
3 changes: 2 additions & 1 deletion build/utils/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ func buildYarn1Root(packageInfo *PackageInfo, packNameToFullName *map[string]str
return rootDependency
}

// splitNameAndVersion splits package name for package version for th following format ONLY: package-name@version
// splitNameAndVersion splits package name for package version for th following formats ONLY: package-name@version, package-name@npm:version
func splitNameAndVersion(packageFullName string) (packageCleanName string, packageVersion string, err error) {
packageFullName = strings.Replace(packageFullName, "npm:", "", 1)
indexOfLastAt := strings.LastIndex(packageFullName, "@")
if indexOfLastAt == -1 {
err = errors.New("received package name of incorrect format (expected: package-name@version)")
Expand Down
42 changes: 25 additions & 17 deletions build/utils/yarn_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package utils

import (
"bytes"
"errors"
"github.com/jfrog/build-info-go/utils"
"github.com/stretchr/testify/assert"
"os"
Expand Down Expand Up @@ -76,21 +74,8 @@ func checkGetYarnDependenciesUninstalled(t *testing.T, versionToSet string) {

func updateDirYarnVersion(executablePath string, srcPath string, versionToSet string) (err error) {
command := exec.Command(executablePath, "set", "version", versionToSet)

command.Dir = srcPath
outBuffer := bytes.NewBuffer([]byte{})
command.Stdout = outBuffer
errBuffer := bytes.NewBuffer([]byte{})
command.Stderr = errBuffer
err = command.Run()

if err != nil {
// urfave/cli (aka codegangsta) exits when an ExitError is returned, so if it's an ExitError we'll convert it to a regular error.
if _, ok := err.(*exec.ExitError); ok {
err = errors.New(err.Error())
}
return
}
return
}

Expand Down Expand Up @@ -127,7 +112,7 @@ func checkGetYarnDependencies(t *testing.T, versionDir string, expectedLocators
packageCleanName, packageVersion, err = splitNameAndVersion(dependencyName)
assert.NoError(t, err)
if packageCleanName == "" || packageVersion == "" {
assert.NoError(t, errors.New("got an empty dependency name/version or in incorrect format (expected: package-name@version) "))
t.Error("got an empty dependency name/version or in incorrect format (expected: package-name@version) ")
}
} else {
packageCleanName = root.Value
Expand Down Expand Up @@ -161,7 +146,7 @@ func checkGetYarnDependencies(t *testing.T, versionDir string, expectedLocators
assert.Contains(t, expectedLocators, dependency.Locator)
}
default:
assert.NoError(t, errors.New("package "+dependencyName+" should not be inside the dependencies map"))
t.Error("package '" + dependencyName + "' should not be inside the dependencies map")
}
}
}
Expand All @@ -181,3 +166,26 @@ func TestYarnDependency_Name(t *testing.T) {
assert.Equal(t, testCase.packageExpectedName, yarnDep.Name())
}
}

func TestSplitNameAndVersion(t *testing.T) {
testCases := []struct {
packageFullName string
expectedName string
expectedVersion string
}{
{"[email protected]", "json", "1.2.3"},
{"@babel/[email protected]", "@babel/highlight", "7.14.0"},
{"json@npm:1.2.3", "json", "1.2.3"},
{"@babel/highlight@npm:7.14.0", "@babel/highlight", "7.14.0"},
}
for _, testCase := range testCases {
packageCleanName, packageVersion, err := splitNameAndVersion(testCase.packageFullName)
assert.NoError(t, err)
assert.Equal(t, testCase.expectedName, packageCleanName)
assert.Equal(t, testCase.expectedVersion, packageVersion)
}

incorrectFormatPackageName := "json:1.2.3"
_, _, err := splitNameAndVersion(incorrectFormatPackageName)
assert.Error(t, err)
}

0 comments on commit 64da880

Please sign in to comment.