Skip to content

Commit

Permalink
delete strings.HasPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondkmp committed Oct 15, 2024
1 parent dbedea0 commit 96c2bc2
Show file tree
Hide file tree
Showing 4 changed files with 726 additions and 290 deletions.
17 changes: 16 additions & 1 deletion pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,24 @@ func (t *Collector) readDependencyTree(dependency *Dependency) error {
return nil
}

func IsParentPath(parentPath, childPath string) bool {
parentPath = filepath.Clean(parentPath)
childPath = filepath.Clean(childPath)

relPath, err := filepath.Rel(parentPath, childPath)
if err != nil {
return false
}

if relPath == "." || strings.HasPrefix(relPath, "..") {
return false
}
return true
}

func (t *Collector) writeToParentConflicDependency(d *Dependency) {
for p := d.parent; p != t.rootDependency; p = p.parent {
if strings.HasPrefix(d.dir, p.dir) {
if IsParentPath(p.dir, d.dir) {
if p.conflictDependency == nil {
p.conflictDependency = make(map[string]*Dependency)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/node-modules/nodeModuleCollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ func TestReadDependencyTreeForTar(t *testing.T) {
r := lo.FlatMap(lo.Values(collector.NodeModuleDirToDependencyMap), func(it *map[string]*Dependency, i int) []string {
return lo.Keys(*it)
})
g.Expect(len(r)).To(Equal(46))
g.Expect(len(r)).To(Equal(97))

g.Expect(collector.HoiestedDependencyMap["tar"].dir).To(Equal(filepath.Join(dir, "node_modules/tar")))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["minipass"].Version).To(Equal("7.1.2"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["minizlib"].Version).To(Equal("3.0.1"))

g.Expect(collector.HoiestedDependencyMap["archiver-utils"].dir).To(Equal(filepath.Join(dir, "node_modules/archiver-utils")))
g.Expect(collector.HoiestedDependencyMap["archiver-utils"].Version).To(Equal("5.0.2"))
}

func TestReadDependencyTreeForYarn(t *testing.T) {
Expand Down
Loading

0 comments on commit 96c2bc2

Please sign in to comment.