From b617a66ca9b1a0301a2f110543f6369103987c72 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Thu, 1 Sep 2022 18:21:39 +0300 Subject: [PATCH] Fix Nuget requestedBy calculation (#100) --- build/maven_test.go | 19 ++++++++++--------- build/utils/dotnet/solution/solution.go | 7 +++---- build/yarn.go | 12 ++---------- entities/buildinfo.go | 7 +++---- go.mod | 1 + go.sum | 2 ++ utils/fileutils.go | 3 ++- utils/utils.go | 9 --------- 8 files changed, 23 insertions(+), 37 deletions(-) diff --git a/build/maven_test.go b/build/maven_test.go index 924bbde7..b2ba93cf 100644 --- a/build/maven_test.go +++ b/build/maven_test.go @@ -52,15 +52,16 @@ func TestGenerateBuildInfoForMavenProject(t *testing.T) { mavenModule.SetMavenGoals("compile", "--no-transfer-progress") // Calculate build-info. err = mavenModule.CalcDependencies() - assert.NoError(t, err) - buildInfo, err := mavenBuild.ToBuildInfo() - assert.NoError(t, err) - // Check build-info results. - expectedModules := getExpectedMavenBuildInfo(t, filepath.Join(testdataDir, "maven", "expected_maven_buildinfo.json")).Modules - match, err := entities.IsEqualModuleSlices(buildInfo.Modules, expectedModules) - assert.NoError(t, err) - if !match { - testdatautils.PrintBuildInfoMismatch(t, expectedModules, buildInfo.Modules) + if assert.NoError(t, err) { + buildInfo, err := mavenBuild.ToBuildInfo() + assert.NoError(t, err) + // Check build-info results. + expectedModules := getExpectedMavenBuildInfo(t, filepath.Join(testdataDir, "maven", "expected_maven_buildinfo.json")).Modules + match, err := entities.IsEqualModuleSlices(buildInfo.Modules, expectedModules) + assert.NoError(t, err) + if !match { + testdatautils.PrintBuildInfoMismatch(t, expectedModules, buildInfo.Modules) + } } } diff --git a/build/utils/dotnet/solution/solution.go b/build/utils/dotnet/solution/solution.go index be3fc0a7..a2a8b666 100644 --- a/build/utils/dotnet/solution/solution.go +++ b/build/utils/dotnet/solution/solution.go @@ -114,10 +114,9 @@ func populateRequestedBy(parentDependency buildinfo.Dependency, dependenciesMap if childDep.NodeHasLoop() || len(childDep.RequestedBy) >= buildinfo.RequestedByMaxLength { continue } - for _, parentRequestedBy := range parentDependency.RequestedBy { - childRequestedBy := append([]string{parentDependency.Id}, parentRequestedBy...) - childDep.RequestedBy = append(childDep.RequestedBy, childRequestedBy) - } + // Update RequestedBy field from parent's RequestedBy. + childDep.UpdateRequestedBy(parentDependency.Id, parentDependency.RequestedBy) + // Run recursive call on child dependencies populateRequestedBy(*childDep, dependenciesMap, childrenMap) } diff --git a/build/yarn.go b/build/yarn.go index 6e721a5f..a3d5c9f2 100644 --- a/build/yarn.go +++ b/build/yarn.go @@ -8,6 +8,7 @@ import ( "github.com/jfrog/build-info-go/entities" "github.com/jfrog/build-info-go/utils" "github.com/jfrog/gofrog/version" + "golang.org/x/exp/slices" "os" "os/exec" "strings" @@ -96,7 +97,7 @@ func (ym *YarnModule) appendDependencyRecursively(yarnDependency *buildutils.Yar id := yarnDependency.Name() + ":" + yarnDependency.Details.Version // To avoid infinite loops in case of circular dependencies, the dependency won't be added if it's already in pathToRoot - if stringsSliceContains(pathToRoot, id) { + if slices.Contains(pathToRoot, id) { return nil } @@ -192,12 +193,3 @@ func runYarnCommand(executablePath, srcPath string, args ...string) error { } return err } - -func stringsSliceContains(slice []string, str string) bool { - for _, element := range slice { - if element == str { - return true - } - } - return false -} diff --git a/entities/buildinfo.go b/entities/buildinfo.go index 9254722d..6d1ea4b0 100644 --- a/entities/buildinfo.go +++ b/entities/buildinfo.go @@ -2,6 +2,7 @@ package entities import ( "github.com/jfrog/build-info-go/utils/compareutils" + "golang.org/x/exp/slices" "regexp" "strings" "time" @@ -528,10 +529,8 @@ func (d *Dependency) UpdateRequestedBy(parentId string, parentRequestedBy [][]st func (d *Dependency) NodeHasLoop() bool { for _, requestedBy := range d.RequestedBy { - for _, depId := range requestedBy { - if depId == d.Id { - return true - } + if slices.Contains(requestedBy, d.Id) { + return true } } return false diff --git a/go.mod b/go.mod index 4305bd47..9d3b860a 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/stretchr/testify v1.8.0 github.com/urfave/cli/v2 v2.11.2 github.com/xeipuuv/gojsonschema v1.2.0 + golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 ) require ( diff --git a/go.sum b/go.sum index 4b7ab39a..2eb6878d 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17 github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 h1:tnebWN09GYg9OLPss1KXj8txwZc6X6uMr6VFdcGNbHw= +golang.org/x/exp v0.0.0-20220827204233-334a2380cb91/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/utils/fileutils.go b/utils/fileutils.go index adc1e953..c97518d1 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/json" "fmt" + "golang.org/x/exp/slices" "io" "io/ioutil" "net/http" @@ -412,7 +413,7 @@ func CopyDir(fromPath, toPath string, includeDirs bool, excludeNames []string) e for _, v := range files { // Skip if excluded - if IsStringInSlice(filepath.Base(v), excludeNames) { + if slices.Contains(excludeNames, filepath.Base(v)) { continue } diff --git a/utils/utils.go b/utils/utils.go index 453cfe1c..203aa5bd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -11,12 +11,3 @@ func GetRegExp(regex string) (*regexp.Regexp, error) { } return regExp, nil } - -func IsStringInSlice(str string, strings []string) bool { - for _, v := range strings { - if v == str { - return true - } - } - return false -}