Skip to content

Commit

Permalink
add a truth table and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Dec 14, 2023
1 parent 0ba2ac8 commit ce724a4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/config/app_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ func (d *AppDirectory) FindAppsBasedOnChangeList(changeList []string, targetBran
log.Warn().Msgf("failed to find matched app named '%s'", appName)
continue
}
if app.TargetRevision == "HEAD" && targetBranch != "main" && targetBranch != "master" {
log.Debug().Msgf("target revision of %s is HEAD and '%s' is not main or master", appName, targetBranch)

if !shouldInclude(app, targetBranch) {
log.Debug().Msgf("target revision of %s is %s and does not match '%s'", appName, app.TargetRevision, targetBranch)
}
if app.TargetRevision == "HEAD" && (targetBranch != "main" && targetBranch != "master") {
continue
}

if app.TargetRevision != "" && app.TargetRevision != targetBranch {
log.Debug().Msgf("target revision of %s is %s and does not match '%s'", appName, app.TargetRevision, targetBranch)
continue
}
appsSlice = append(appsSlice, app)
Expand All @@ -116,6 +118,28 @@ func (d *AppDirectory) FindAppsBasedOnChangeList(changeList []string, targetBran
return appsSlice
}

func shouldInclude(app ApplicationStub, targetBranch string) bool {
if app.TargetRevision == "" {
return true
}

if app.TargetRevision == targetBranch {
return true
}

if app.TargetRevision == "HEAD" {
if targetBranch == "main" {
return true
}

if targetBranch == "master" {
return true
}
}

return false
}

func (d *AppDirectory) GetApps(filter func(stub ApplicationStub) bool) []ApplicationStub {
var result []ApplicationStub
for _, value := range d.appsMap {
Expand Down
47 changes: 47 additions & 0 deletions pkg/config/app_directory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"testing"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand Down Expand Up @@ -49,3 +50,49 @@ func TestPathsAreJoinedProperly(t *testing.T) {
"/test1/three.yaml": {"test-app"},
}, rad.appFiles)
}

func TestShouldInclude(t *testing.T) {
testcases := []struct {
vcsMergeTarget string
argocdAppBranch string
expected bool
}{
{
vcsMergeTarget: "some-branch",
argocdAppBranch: "some-branch",
expected: true,
},
{
vcsMergeTarget: "some-branch",
argocdAppBranch: "some-other-branch",
expected: false,
},
{
argocdAppBranch: "HEAD",
vcsMergeTarget: "main",
expected: true,
},
{
argocdAppBranch: "HEAD",
vcsMergeTarget: "master",
expected: true,
},
{
argocdAppBranch: "HEAD",
vcsMergeTarget: "other",
expected: false,
},
{
argocdAppBranch: "",
vcsMergeTarget: "branch",
expected: true,
},
}

for _, tc := range testcases {
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
actual := shouldInclude(ApplicationStub{TargetRevision: tc.argocdAppBranch}, tc.vcsMergeTarget)
assert.Equal(t, tc.expected, actual)
})
}
}

0 comments on commit ce724a4

Please sign in to comment.