Skip to content

Commit a39db24

Browse files
author
nikos.nikolakakis
committed
Add --group flag to override group name for stdin input
- Add --group CLI flag to test command for custom group naming - Override FileName from '-' to custom group name when --group is provided - Enables better identification of policy violations in CI/CD workflows - Add unit tests for group flag functionality Fixes #1095
1 parent 2a509fe commit a39db24

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

internal/commands/test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
115115
"junit-hide-message",
116116
"quiet",
117117
"tls",
118+
"group",
118119
}
119120
for _, name := range flagNames {
120121
if err := viper.BindPFlag(name, cmd.Flags().Lookup(name)); err != nil {
@@ -196,6 +197,7 @@ func NewTestCommand(ctx context.Context) *cobra.Command {
196197

197198
cmd.Flags().StringSlice("proto-file-dirs", []string{}, "A list of directories containing Protocol Buffer definitions")
198199
cmd.Flags().Bool("tls", true, "Use TLS to access the registry")
200+
cmd.Flags().String("group", "", "Override the group name for stdin input (used for output formatting)")
199201

200202
return &cmd
201203
}

runner/test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type TestRunner struct {
3535
Combine bool
3636
Quiet bool
3737
Output string
38+
Group string
3839
}
3940

4041
// Run executes the TestRunner, verifying all Rego policies against the given
@@ -110,6 +111,15 @@ func (t *TestRunner) Run(ctx context.Context, fileList []string) (output.CheckRe
110111
}
111112
}
112113

114+
// Override group name for stdin input if --group flag is provided
115+
if t.Group != "" {
116+
for i := range results {
117+
if results[i].FileName == "-" {
118+
results[i].FileName = t.Group
119+
}
120+
}
121+
}
122+
113123
return results, nil
114124
}
115125

runner/test_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package runner
2+
3+
import (
4+
"testing"
5+
6+
"github.com/open-policy-agent/conftest/output"
7+
)
8+
9+
func TestTestRunner_GroupFlag(t *testing.T) {
10+
// Create a mock TestRunner with Group set
11+
runner := TestRunner{
12+
Group: "my-custom-group",
13+
}
14+
15+
// Create mock results with stdin filename
16+
results := output.CheckResults{
17+
{
18+
FileName: "-",
19+
Namespace: "main",
20+
Failures: []output.Result{{Message: "test failure"}},
21+
},
22+
{
23+
FileName: "regular-file.yaml",
24+
Namespace: "main",
25+
Failures: []output.Result{{Message: "another failure"}},
26+
},
27+
}
28+
29+
// Test the group name override logic
30+
if runner.Group != "" {
31+
for i := range results {
32+
if results[i].FileName == "-" {
33+
results[i].FileName = runner.Group
34+
}
35+
}
36+
}
37+
38+
// Verify the stdin result was updated
39+
if results[0].FileName != "my-custom-group" {
40+
t.Errorf("Expected stdin filename to be overridden to 'my-custom-group', got '%s'", results[0].FileName)
41+
}
42+
43+
// Verify the regular file result was not changed
44+
if results[1].FileName != "regular-file.yaml" {
45+
t.Errorf("Expected regular filename to remain 'regular-file.yaml', got '%s'", results[1].FileName)
46+
}
47+
}
48+
49+
func TestTestRunner_GroupFlagEmpty(t *testing.T) {
50+
// Create a mock TestRunner without Group set
51+
runner := TestRunner{
52+
Group: "",
53+
}
54+
55+
// Create mock results with stdin filename
56+
results := output.CheckResults{
57+
{
58+
FileName: "-",
59+
Namespace: "main",
60+
Failures: []output.Result{{Message: "test failure"}},
61+
},
62+
}
63+
64+
// Test the group name override logic
65+
if runner.Group != "" {
66+
for i := range results {
67+
if results[i].FileName == "-" {
68+
results[i].FileName = runner.Group
69+
}
70+
}
71+
}
72+
73+
// Verify the stdin result was NOT updated
74+
if results[0].FileName != "-" {
75+
t.Errorf("Expected stdin filename to remain '-', got '%s'", results[0].FileName)
76+
}
77+
}

0 commit comments

Comments
 (0)