Skip to content

Commit

Permalink
fix: nil check for config output (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinygrasshopper authored Dec 9, 2024
1 parent 294862e commit 55446da
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Output struct {
// EffectivePaths returns a slice of effective configured output paths, whether
// a single or multiple output paths have been configured.
func (o *Output) ResolvePaths() []string {
if o == nil {
return []string{}
}
if o.Path != "" {
return []string{o.Path}
}
Expand Down
45 changes: 45 additions & 0 deletions config/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package config_test

import (
"reflect"
"testing"

"github.com/snyk/vervet/v8/config"
)

func TestOutput_ResolvePaths(t *testing.T) {
tests := []struct {
name string
subject *config.Output
want []string
}{
{
name: "nil",
subject: nil,
want: []string{},
},
{
name: "returns path if exists",
subject: &config.Output{
Path: "path",
Paths: []string{"path1", "path2"},
},
want: []string{"path"},
},
{
name: "return paths if path is empty",
subject: &config.Output{
Path: "",
Paths: []string{"path1", "path2"},
},
want: []string{"path1", "path2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.subject.ResolvePaths(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ResolvePaths() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/cmd/backstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func processCatalog(ctx *cli.Context, w io.Writer) error {
for _, apiName := range apiNames {
apiConf := proj.APIs[apiName]
outputPaths := apiConf.Output.ResolvePaths()
if len(outputPaths) == 0 {
log.Printf("API %q has no output paths, this command will have no effect", apiName)
continue
}
for _, outputPath := range outputPaths {
outputPath = filepath.Join(projectDir, outputPath)
if matchPath(outputPath) {
Expand Down

0 comments on commit 55446da

Please sign in to comment.