Skip to content

Commit

Permalink
chore: catch emptycfg slice and non-string targets
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Bressi <[email protected]>
  • Loading branch information
puffitos committed Dec 19, 2023
1 parent 709ceb6 commit 33c2363
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (s *Sparrow) updateCheckTargets(cfg any) any {
return nil
}

// check if map with targets
checkCfg, ok := cfg.(map[string]any)
if !ok {
return checkCfg
Expand All @@ -159,13 +160,21 @@ func (s *Sparrow) updateCheckTargets(cfg any) any {
return checkCfg
}

// Check if targets is a slice
actuali, ok := checkCfg["targets"].([]any)
if !ok {
return checkCfg
}
if len(actuali) == 0 {
return checkCfg
}

// convert to string slice
var actual []string
for _, v := range actuali {
if _, ok := v.(string); !ok {
return checkCfg
}
actual = append(actual, v.(string))
}
var urls []string
Expand Down
30 changes: 30 additions & 0 deletions pkg/sparrow/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,36 @@ func TestSparrow_updateCheckTargets(t *testing.T) {
"targets": "not a slice",
},
},
{
name: "config with empty targets",
config: map[string]any{
"targets": []any{},
},
globalTargets: gt,
expected: map[string]any{
"targets": []any{},
},
},
{
name: "config with non string target slice",
config: map[string]any{
"targets": []any{1, 2, 3},
},
globalTargets: gt,
expected: map[string]any{
"targets": []any{1, 2, 3},
},
},
{
name: "config with mixed target slice",
config: map[string]any{
"targets": []any{"https://gitlab.com", 1, 3},
},
globalTargets: gt,
expected: map[string]any{
"targets": []any{"https://gitlab.com", 1, 3},
},
},
{
name: "config with targets",
config: map[string]any{
Expand Down

0 comments on commit 33c2363

Please sign in to comment.