Skip to content

Commit

Permalink
prevent against nil-based panics
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Sep 18, 2023
1 parent a9adf64 commit 675fd28
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ type ArgocdMatcher struct {
func NewArgocdMatcher(vcsToArgoMap pkg.VcsToArgoMap, repo *repo.Repo) *ArgocdMatcher {
log.Debug().Msgf("looking for %s repos", repo.CloneURL)
repoApps := vcsToArgoMap.GetAppsInRepo(repo.CloneURL)
log.Debug().Msgf("found %d apps", repoApps.Count())
if repoApps == nil {
log.Debug().Msg("found no apps")
} else {
log.Debug().Msgf("found %d apps", repoApps.Count())
}

return &ArgocdMatcher{
appsDirectory: repoApps,
}
}

func (a *ArgocdMatcher) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) {
if a.appsDirectory == nil {
return AffectedItems{}, nil
}

appsSlice := a.appsDirectory.FindAppsBasedOnChangeList(changeList)
return AffectedItems{Applications: appsSlice}, nil
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/affected_apps/argocd_matcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package affected_apps

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/zapier/kubechecks/pkg"
repo2 "github.com/zapier/kubechecks/pkg/repo"
)

func TestCreateNewMatcherWithNilVcsMap(t *testing.T) {
// setup
var (
vcsMap pkg.VcsToArgoMap
repo repo2.Repo
)

// run test
matcher := NewArgocdMatcher(vcsMap, &repo)

// verify results
require.Nil(t, matcher.appsDirectory)
}

func TestFindAffectedAppsWithNilAppsDirectory(t *testing.T) {
// setup
var (
ctx = context.TODO()
changeList = []string{"/go.mod"}
)

matcher := ArgocdMatcher{}
items, err := matcher.AffectedApps(ctx, changeList)

// verify results
require.NoError(t, err)
require.Len(t, items.Applications, 0)
require.Len(t, items.ApplicationSets, 0)
}

0 comments on commit 675fd28

Please sign in to comment.