Skip to content

Commit

Permalink
fixed PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Aug 3, 2023
1 parent 1abd246 commit c513827
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
27 changes: 12 additions & 15 deletions build/utils/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,17 @@ func buildYarnV1DependencyMap(packageInfo *PackageInfo, responseStr string) (dep
}

if depTree.Data.DepTree == nil {
err = errors.New("an error occurred while parsing dependencies - the dependencies tree received a null value in a mandatory field")
err = errors.New("received an empty tree field while parsing 'yarn list' results")
return
}
// NEW CODE

packNameToFullName := make(map[string]string)

// Initializing dependencies map without child dependencies for each dependency + creating a map that maps: package-name -> package-name@version
for _, curDependency := range depTree.Data.DepTree {
packageCleanName, packageVersion, err2 := splitNameAndVersion(curDependency.Name)
if err2 != nil {
err = errors.Join(err, err2)
return
packageCleanName, packageVersion, err := splitNameAndVersion(curDependency.Name)
if err != nil {
return nil, nil, err
}

dependenciesMap[curDependency.Name] = &YarnDependency{
Expand All @@ -188,10 +187,9 @@ func buildYarnV1DependencyMap(packageInfo *PackageInfo, responseStr string) (dep
dependency := *(dependenciesMap[curDependency.Name])

for _, subDep := range curDependency.Dependencies {
subDepName, _, err2 := splitNameAndVersion(subDep.DependencyName)
if err2 != nil {
err = errors.Join(err, err2)
return
subDepName, _, err := splitNameAndVersion(subDep.DependencyName)
if err != nil {
return nil, nil, err
}
dependency.Details.Dependencies = append(dependency.Details.Dependencies, YarnDependencyPointer{subDep.DependencyName, packNameToFullName[subDepName]})
}
Expand Down Expand Up @@ -286,16 +284,16 @@ func buildYarn1Root(packageInfo *PackageInfo, packNameToFullName *map[string]str
rootDeps = append(rootDeps, maps.Keys(packageInfo.OptionalDependencies)...)

for _, directDepName := range rootDeps {
rootDependency.Details.Dependencies = append(rootDependency.Details.Dependencies, YarnDependencyPointer{"", (*packNameToFullName)[directDepName]})
rootDependency.Details.Dependencies = append(rootDependency.Details.Dependencies, YarnDependencyPointer{Locator: (*packNameToFullName)[directDepName]})
}
return rootDependency
}

// splitNameAndVersion splits package name for package version for th following format ONLY: package-name@version
func splitNameAndVersion(packageFullName string) (packageCleanName string, packageVersion string, err2 error) {
func splitNameAndVersion(packageFullName string) (packageCleanName string, packageVersion string, err error) {
indexOfLastAt := strings.LastIndex(packageFullName, "@")
if indexOfLastAt == -1 {
err2 = errors.New("received package name of incorrect format (expected: package-name@version)")
err = errors.New("received package name of incorrect format (expected: package-name@version)")
return
}
packageCleanName = packageFullName[:indexOfLastAt]
Expand All @@ -314,7 +312,6 @@ type Yarn1DependencyTree struct {
type Yarn1DependencyDetails struct {
Name string `json:"name,omitempty"`
Dependencies []Yarn1DependencyPointer `json:"children,omitempty"`
Color string `json:"color"`
}

type Yarn1DependencyPointer struct {
Expand All @@ -333,7 +330,7 @@ func (yd *YarnDependency) Name() string {
if strings.Contains(yd.Value[1:], "@") {
atSignIndex := strings.Index(yd.Value[1:], "@") + 1
return yd.Value[:atSignIndex]
} else {
} else { // In some cases when using yarn V1 we encounter package names without their version (project's package name)
return yd.Value
}
}
Expand Down
11 changes: 5 additions & 6 deletions build/utils/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func checkGetYarnDependenciesUninstalled(t *testing.T, versionToSet string) {
pacInfo := PackageInfo{Name: "build-info-go-tests"}
_, _, err = GetYarnDependencies(executablePath, projectSrcPath, &pacInfo, &utils.NullLog{})
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "fetching dependencies failed since '"+pacInfo.Name+"' doesn't present in your lockfile\nPlease run 'yarn install' to update lockfile\n"))
assert.Contains(t, err.Error(), "fetching dependencies failed since '"+pacInfo.Name+"' doesn't present in your lockfile\nPlease run 'yarn install' to update lockfile\n")
}

func updateDirYarnVersion(executablePath string, srcPath string, versionToSet string) (err error) {
Expand Down Expand Up @@ -123,10 +123,9 @@ func checkGetYarnDependencies(t *testing.T, versionDir string, expectedLocators
assert.Len(t, dependenciesMap, 6)
for dependencyName, depInfo := range dependenciesMap {
var packageCleanName, packageVersion string
var err2 error
if dependencyName != root.Value {
packageCleanName, packageVersion, err2 = splitNameAndVersion(dependencyName)
assert.NoError(t, err2)
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) "))
}
Expand All @@ -140,8 +139,8 @@ func checkGetYarnDependencies(t *testing.T, versionDir string, expectedLocators
assert.NotNil(t, depInfo.Details.Dependencies)
subDependencies := []string{"loose-envify"}
for _, depPointer := range depInfo.Details.Dependencies {
packageName, _, err3 := splitNameAndVersion(depPointer.Locator)
assert.NoError(t, errors.Join(err2, err3))
packageName, _, err := splitNameAndVersion(depPointer.Locator)
assert.NoError(t, err)
assert.Contains(t, subDependencies, packageName)
}
case "xml":
Expand Down

0 comments on commit c513827

Please sign in to comment.