Skip to content

Commit

Permalink
refactor: use error for no-summary + no-table format
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Feb 4, 2025
1 parent d37ad35 commit 786e704
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
3 changes: 1 addition & 2 deletions pkg/flag/report_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ func (f *ReportFlagGroup) ToOptions() (ReportOptions, error) {

// "--so-summary" option is available only with "--format table".
if noSummaryTable && format != types.FormatTable {
noSummaryTable = false
log.Warn(`"--no-summary-table" can be used only with "--format table".`)
return ReportOptions{}, xerrors.New(`"--no-summary-table" can be used only with "--format table".`)
}

cs, err := loadComplianceTypes(f.Compliance.Value())
Expand Down
29 changes: 14 additions & 15 deletions pkg/flag/report_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
name string
fields fields
want flag.ReportOptions
wantErr string
wantLogs []string
}{
{
Expand Down Expand Up @@ -116,20 +117,6 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
ListAllPkgs: true,
},
},
{
name: "invalid option combination: --no-summary-table with --format json",
fields: fields{
format: "json",
noSummaryTable: true,
},
wantLogs: []string{
`"--no-summary-table" can be used only with "--format table".`,
},
want: flag.ReportOptions{
Format: "json",
NoSummaryTable: false,
},
},
{
name: "happy path with output plugin args",
fields: fields{
Expand Down Expand Up @@ -175,6 +162,14 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
Severities: []dbTypes.Severity{dbTypes.SeverityLow},
},
},
{
name: "invalid option combination: --no-summary-table with --format json",
fields: fields{
format: "json",
noSummaryTable: true,
},
wantErr: `"--no-summary-table" can be used only with "--format table".`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -219,7 +214,11 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
}

got, err := f.ToOptions()
require.NoError(t, err)
if tt.wantErr != "" {
require.Contains(t, err.Error(), tt.wantErr)
return
}

assert.EqualExportedValuesf(t, tt.want, got, "ToOptions()")

// Assert log messages
Expand Down

0 comments on commit 786e704

Please sign in to comment.