Skip to content

Commit

Permalink
fix: Adds default path for data flag
Browse files Browse the repository at this point in the history
Add default value as 'data' for the data flag. Updated data loading function to not throw an error if it is using the default data path and it is missing.

Fixes open-policy-agent#800
  • Loading branch information
George committed Jul 20, 2024
1 parent 2275d3f commit 9135c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
cmd.Flags().StringSliceP("policy", "p", []string{"policy"}, "Path to the Rego policy files directory")
cmd.Flags().StringSliceP("update", "u", []string{}, "A list of URLs can be provided to the update flag, which will download before the tests run")
cmd.Flags().StringSliceP("namespace", "n", []string{"main"}, "Test policies in a specific namespace")
cmd.Flags().StringSliceP("data", "d", []string{}, "A list of paths from which data for the rego policies will be recursively loaded")
cmd.Flags().StringSliceP("data", "d", []string{"data"}, "A list of paths from which data for the rego policies will be recursively loaded")

cmd.Flags().StringSlice("proto-file-dirs", []string{}, "A list of directories containing Protocol Buffer definitions")
cmd.Flags().Bool("tls", true, "Use TLS to access the registry")
Expand Down
8 changes: 7 additions & 1 deletion policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package policy
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -131,7 +133,11 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
})
if err != nil {
return nil, fmt.Errorf("filter data paths: %w", err)
if errors.Is(err.(loader.Errors)[0], fs.ErrNotExist) && len(dataPaths) == 1 && dataPaths[0] == "data" {
allDocumentPaths = []string{}
} else {
return nil, fmt.Errorf("sldkjf filter data paths: %s", err)
}
}

documents, err := loader.NewFileLoader().All(allDocumentPaths)
Expand Down

0 comments on commit 9135c04

Please sign in to comment.