Skip to content

Commit cbbbce0

Browse files
committed
verbose toggle prints when on
when the verbose flag is given then it prints trace information. when the verbose flag is false, then it does not print trace information this fixes issue: #5
1 parent 8acdb5a commit cbbbce0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

pkg/commands/eval.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ func (s *EvalCommand) Execute(args []string) error {
5050
}
5151

5252
func (s *EvalCommand) setDefaults() {
53-
s.Writer = new(bytes.Buffer)
54-
if s.Verbose {
53+
if s.Writer == nil {
5554
s.Writer = os.Stdout
5655
}
5756

57+
if !s.Verbose {
58+
s.Writer = new(bytes.Buffer)
59+
}
60+
5861
if s.Namespace == "" {
5962
s.Namespace = "main"
6063
}

pkg/commands/eval_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestEvalCommand(t *testing.T) {
1818
policy string
1919
failsWith error
2020
skip bool
21+
verbose bool
2122
}{
2223
{
2324
name: "invalid policy path given",
@@ -130,6 +131,22 @@ func TestEvalCommand(t *testing.T) {
130131
policy: "testdata/policy/individuals/no_passing_valid.rego",
131132
failsWith: commands.PolicyFailure,
132133
},
134+
{
135+
name: "verbosity on success should print trace information",
136+
template: "testdata/templates",
137+
values: []string{"testdata/values.yml", "testdata/added_values.yml"},
138+
policy: "testdata/policy/individuals/multiple_values.rego",
139+
failsWith: nil,
140+
verbose: true,
141+
},
142+
{
143+
name: "verbosity on failure should print trace information",
144+
template: "testdata/templates",
145+
values: []string{"testdata/values.yml"},
146+
policy: "testdata/policy/individuals/no_passing_valid.rego",
147+
failsWith: commands.PolicyFailure,
148+
verbose: true,
149+
},
133150
} {
134151
t.Run(tt.name, func(t *testing.T) {
135152
if tt.skip {
@@ -142,12 +159,21 @@ func TestEvalCommand(t *testing.T) {
142159
Template: tt.template,
143160
Policy: tt.policy,
144161
Values: tt.values,
162+
Verbose: tt.verbose,
145163
}
146164
err := evalCmd.Execute([]string{})
147165
if err != nil && !errors.Is(err, tt.failsWith) {
148166
t.Errorf("expected error:\n%v\ngot:\n%v", tt.failsWith, err)
149167
}
150168

169+
if !tt.verbose && stdOut.Len() > 0 {
170+
t.Errorf("when verbose is off this should always be empty, but it contains %v bytes", stdOut.Len())
171+
}
172+
173+
if tt.verbose && stdOut.Len() == 0 {
174+
t.Errorf("we expected to print verbose trace information, but it is empty")
175+
}
176+
151177
if err == nil && tt.failsWith != nil {
152178
t.Errorf("expected a failing policy %w but no failures found", tt.failsWith)
153179
}

pkg/commands/util.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ func getQueryList(policy string) map[string]int {
203203
}
204204

205205
func evalPolicyOnInput(writer io.Writer, policy string, namespace string, input interface{}) error {
206-
bufWriter := new(bytes.Buffer)
207206
testResults := make(map[string]bool)
208207
ctx := context.Background()
209208
var results rego.ResultSet
@@ -244,9 +243,9 @@ func evalPolicyOnInput(writer io.Writer, policy string, namespace string, input
244243

245244
if len(resultSet) > 0 {
246245
results = append(results, resultSet...)
247-
topdown.PrettyTrace(bufWriter, *buf)
248-
fmt.Fprint(writer, bufWriter.String())
249246
}
247+
248+
topdown.PrettyTrace(writer, *buf)
250249
}
251250

252251
if len(queryList) <= 0 {

0 commit comments

Comments
 (0)