Skip to content

Commit

Permalink
refactor: move ignore file check to report flag handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaist committed Oct 2, 2024
1 parent f0d051d commit cc2e22a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 6 additions & 0 deletions pkg/flag/report_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/mattn/go-shellwords"
"github.com/samber/lo"
"github.com/spf13/viper"
"golang.org/x/xerrors"

dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/result"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
xstrings "github.com/aquasecurity/trivy/pkg/x/strings"
)

Expand Down Expand Up @@ -238,6 +240,10 @@ func (f *ReportFlagGroup) ToOptions() (ReportOptions, error) {
}
}

if viper.IsSet(f.IgnoreFile.ConfigName) && !fsutils.FileExists(f.IgnoreFile.Value()) {
return ReportOptions{}, xerrors.Errorf("ignore file not found: %s", f.IgnoreFile.Value())
}

return ReportOptions{
Format: format,
ReportFormat: f.ReportFormat.Value(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/flag/report_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,16 @@ func TestReportFlagGroup_ToOptions(t *testing.T) {
assert.Equal(t, tt.wantLogs, out.Messages(), tt.name)
})
}

t.Run("Error on non existing ignore file", func(t *testing.T) {
t.Cleanup(viper.Reset)

setValue(flag.IgnoreFileFlag.ConfigName, string("doesntexist"))
f := &flag.ReportFlagGroup{
IgnoreFile: flag.IgnoreFileFlag.Clone(),
}

_, err := f.ToOptions()
assert.ErrorContains(t, err, "ignore file not found: doesntexist")
})
}
17 changes: 1 addition & 16 deletions pkg/result/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,30 +1016,15 @@ func TestFilter(t *testing.T) {
})
}

ignoreFile := tt.args.ignoreFile
if ignoreFile == "" {
ignoreFile = result.DefaultIgnoreFile
}
err := result.Filter(ctx, tt.args.report, result.FilterOptions{
Severities: tt.args.severities,
VEXSources: vexSources,
IgnoreStatuses: tt.args.ignoreStatuses,
IgnoreFile: ignoreFile,
IgnoreFile: tt.args.ignoreFile,
PolicyFile: tt.args.policyFile,
})
require.NoError(t, err)
assert.Equal(t, tt.want, tt.args.report)
})
}

t.Run("Error on existent ignore file", func(t *testing.T) {
fakeTime := time.Date(2020, 8, 10, 7, 28, 17, 958601, time.UTC)
ctx := clock.With(context.Background(), fakeTime)
test := tests[0]

err := result.Filter(ctx, test.args.report, result.FilterOptions{
IgnoreFile: "invalid",
})
assert.ErrorContains(t, err, "invalid error: invalid does not exist")
})
}
3 changes: 0 additions & 3 deletions pkg/result/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ func (c *IgnoreConfig) MatchLicense(licenseID, filePath string) *IgnoreFinding {
func ParseIgnoreFile(ctx context.Context, ignoreFile string) (IgnoreConfig, error) {
var conf IgnoreConfig
if _, err := os.Stat(ignoreFile); errors.Is(err, fs.ErrNotExist) {
if ignoreFile != DefaultIgnoreFile {
return IgnoreConfig{}, xerrors.Errorf("%s does not exist", ignoreFile)
}
// .trivyignore doesn't necessarily exist
return IgnoreConfig{}, nil
} else if filepath.Ext(ignoreFile) == ".yml" || filepath.Ext(ignoreFile) == ".yaml" {
Expand Down

0 comments on commit cc2e22a

Please sign in to comment.