Skip to content

Commit

Permalink
Allows priorities to be a regex or string
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and ryanmoran committed Feb 11, 2021
1 parent ac6e367 commit 3f81926
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 18 additions & 5 deletions draft/planner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package draft

import (
"reflect"
"regexp"
"sort"

Expand All @@ -14,7 +15,7 @@ func NewPlanner() Planner {
return Planner{}
}

func (p Planner) Resolve(name string, entries []packit.BuildpackPlanEntry, priorities []string) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry) {
func (p Planner) Resolve(name string, entries []packit.BuildpackPlanEntry, priorities []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry) {
var filteredEntries []packit.BuildpackPlanEntry
for _, e := range entries {
if e.Name == name {
Expand All @@ -36,12 +37,24 @@ func (p Planner) Resolve(name string, entries []packit.BuildpackPlanEntry, prior
rightPriority := -1

for index, match := range priorities {
if regexp.MustCompile(match).MatchString(left) {
leftPriority = len(priorities) - index - 1
if r, ok := match.(*regexp.Regexp); ok {
if r.MatchString(left) {
leftPriority = len(priorities) - index - 1
}
} else {
if reflect.DeepEqual(match, left) {
leftPriority = len(priorities) - index - 1
}
}

if regexp.MustCompile(match).MatchString(right) {
rightPriority = len(priorities) - index - 1
if r, ok := match.(*regexp.Regexp); ok {
if r.MatchString(right) {
rightPriority = len(priorities) - index - 1
}
} else {
if reflect.DeepEqual(match, right) {
rightPriority = len(priorities) - index - 1
}
}
}

Expand Down
19 changes: 14 additions & 5 deletions draft/planner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package draft_test

import (
"regexp"
"testing"

"github.com/paketo-buildpacks/packit"
Expand All @@ -16,13 +17,13 @@ func testPlanner(t *testing.T, context spec.G, it spec.S) {

planner draft.Planner

priorities []string
priorities []interface{}
)

it.Before(func() {
priorities = []string{
"^highest$",
"^lowest$",
priorities = []interface{}{
"highest",
"lowest",
}

planner = draft.NewPlanner()
Expand Down Expand Up @@ -194,6 +195,14 @@ func testPlanner(t *testing.T, context spec.G, it spec.S) {
})

context("there are entries matching a regexp", func() {
it.Before(func() {
priorities = []interface{}{
"buildpack.yml",
regexp.MustCompile(`^.*\.(cs)|(fs)|(vb)proj$`),
regexp.MustCompile(`^.*\.runtimeconfig\.json$`),
}
})

it("returns no entries", func() {
entry, entries := planner.Resolve("dotnet-runtime", []packit.BuildpackPlanEntry{
{
Expand All @@ -216,7 +225,7 @@ func testPlanner(t *testing.T, context spec.G, it spec.S) {
"version-source": "myapp.vbproj",
},
},
}, []string{`^buildpack\.yml$`, `^.*\.(cs)|(fs)|(vb)proj$`, `^.*\.runtimeconfig\.json$`})
}, priorities)

Expect(entry).To(Equal(packit.BuildpackPlanEntry{
Name: "dotnet-runtime",
Expand Down

0 comments on commit 3f81926

Please sign in to comment.