Skip to content

Commit

Permalink
refactor(cocoapods): use utils.PackageID for ID (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen authored Aug 25, 2023
1 parent 40c1f85 commit df72a28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
17 changes: 6 additions & 11 deletions pkg/swift/cocoapods/parse.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cocoapods

import (
"fmt"
"sort"
"strings"

"golang.org/x/exp/maps"
Expand All @@ -14,16 +14,14 @@ import (
"github.com/aquasecurity/go-dep-parser/pkg/utils"
)

const idFormat = "%s/%s"

type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

type lockFile struct {
Pods []interface{} `yaml:"PODS"` // pod can be string or map[string]interface{}
Pods []any `yaml:"PODS"` // pod can be string or map[string]interface{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
Expand All @@ -38,7 +36,7 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
for _, pod := range lock.Pods {
switch p := pod.(type) {
case string: // dependency with version number
lib, err := parseDep(pod.(string))
lib, err := parseDep(p)
if err != nil {
log.Logger.Debug(err)
continue
Expand Down Expand Up @@ -74,14 +72,15 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
var dependsOn []string
// find versions for child dependencies
for _, childDep := range childDeps {
dependsOn = append(dependsOn, pkgID(childDep, parsedDeps[childDep].Version))
dependsOn = append(dependsOn, utils.PackageID(childDep, parsedDeps[childDep].Version))
}
deps = append(deps, types.Dependency{
ID: parsedDeps[dep].ID,
DependsOn: dependsOn,
})
}

sort.Sort(types.Dependencies(deps))
return utils.UniqueLibraries(maps.Values(parsedDeps)), deps, nil
}

Expand All @@ -100,14 +99,10 @@ func parseDep(dep string) (types.Library, error) {
name := ss[0]
version := strings.Trim(strings.TrimSpace(ss[1]), "()")
lib := types.Library{
ID: pkgID(name, version),
ID: utils.PackageID(name, version),
Name: name,
Version: version,
}

return lib, nil
}

func pkgID(name, version string) string {
return fmt.Sprintf(idFormat, name, version)
}
46 changes: 16 additions & 30 deletions pkg/swift/cocoapods/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cocoapods_test

import (
"os"
"sort"
"strings"
"testing"

"github.com/aquasecurity/go-dep-parser/pkg/swift/cocoapods"
Expand All @@ -24,49 +22,49 @@ func TestParse(t *testing.T) {
inputFile: "testdata/happy.lock",
wantLibs: []types.Library{
{
ID: "AppCenter/4.2.0",
Name: "AppCenter",
Version: "4.2.0",
},
{
ID: "AppCenter/Analytics/4.2.0",
ID: "AppCenter/Analytics@4.2.0",
Name: "AppCenter/Analytics",
Version: "4.2.0",
},
{
ID: "AppCenter/Core/4.2.0",
ID: "AppCenter/Core@4.2.0",
Name: "AppCenter/Core",
Version: "4.2.0",
},
{
ID: "AppCenter/Crashes/4.2.0",
ID: "AppCenter/Crashes@4.2.0",
Name: "AppCenter/Crashes",
Version: "4.2.0",
},
{
ID: "KeychainAccess/4.2.1",
ID: "AppCenter@4.2.0",
Name: "AppCenter",
Version: "4.2.0",
},
{
ID: "KeychainAccess@4.2.1",
Name: "KeychainAccess",
Version: "4.2.1",
},
},
wantDeps: []types.Dependency{
{
ID: "AppCenter/4.2.0",
ID: "AppCenter/Analytics@4.2.0",
DependsOn: []string{
"AppCenter/Analytics/4.2.0",
"AppCenter/Crashes/4.2.0",
"AppCenter/Core@4.2.0",
},
},
{
ID: "AppCenter/Analytics/4.2.0",
ID: "AppCenter/Crashes@4.2.0",
DependsOn: []string{
"AppCenter/Core/4.2.0",
"AppCenter/Core@4.2.0",
},
},
{
ID: "AppCenter/Crashes/4.2.0",
ID: "AppCenter@4.2.0",
DependsOn: []string{
"AppCenter/Core/4.2.0",
"AppCenter/Analytics@4.2.0",
"AppCenter/Crashes@4.2.0",
},
},
},
Expand All @@ -90,18 +88,6 @@ func TestParse(t *testing.T) {
gotLibs, gotDeps, err := cocoapods.NewParser().Parse(f)
require.NoError(t, err)

sort.Slice(gotLibs, func(i, j int) bool {
ret := strings.Compare(gotLibs[i].Name, gotLibs[j].Name)
if ret != 0 {
return ret < 0
}
return gotLibs[i].Version < gotLibs[j].Version
})

sort.Slice(gotDeps, func(i, j int) bool {
return gotDeps[i].ID < gotDeps[j].ID
})

assert.Equal(t, tt.wantLibs, gotLibs)
assert.Equal(t, tt.wantDeps, gotDeps)
})
Expand Down

0 comments on commit df72a28

Please sign in to comment.