Skip to content

Commit

Permalink
Allow print calls in IR (open-policy-agent#5501)
Browse files Browse the repository at this point in the history
Since this was unset on the compiler for -t plan, print
calls got erased from the plans. Since this is useful for
debugging plan execution, we should allow print in this
context. If undesired, this can still be disabled via
the capabilities feature.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Jan 2, 2023
1 parent 455b9dc commit a13b623
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ func dobuild(params buildParams, args []string) error {
compiler = compiler.WithBundleVerificationKeyID(params.pubKeyID)
}

if params.target.String() == compile.TargetPlan {
compiler = compiler.WithEnablePrintStatements(true)
}

err = compiler.Build(context.Background())
if err != nil {
return err
Expand Down
67 changes: 67 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,73 @@ func TestBuildPlanWithPruneUnused(t *testing.T) {
})
}

func TestBuildPlanWithPrintStatements(t *testing.T) {

files := map[string]string{
"test.rego": `
package test
p { print("hello") }
`,
}

test.WithTempFS(files, func(root string) {
params := newBuildParams()
if err := params.target.Set("plan"); err != nil {
t.Fatal(err)
}
params.entrypoints.v = []string{"test"}
params.outputFile = path.Join(root, "bundle.tar.gz")

err := dobuild(params, []string{root})
if err != nil {
t.Fatal(err)
}

_, err = loader.NewFileLoader().AsBundle(params.outputFile)
if err != nil {
t.Fatal(err)
}

f, err := os.Open(params.outputFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()

gr, err := gzip.NewReader(f)
if err != nil {
t.Fatal(err)
}

tr := tar.NewReader(gr)
var found bool

for {
f, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
if f.Name == "/plan.json" {
found = true
plan, err := io.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(plan), "internal.print") {
t.Error("expected plan.json to contain reference to internal.print built-in function")
}
}
}

if !found {
t.Error("plan.json not found")
}
})
}

func TestBuildPlanWithRegoEntrypointAnnotations(t *testing.T) {

tests := []struct {
Expand Down

0 comments on commit a13b623

Please sign in to comment.