preserve --from order (#4350)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2025-11-07 10:17:10 -05:00 committed by GitHub
parent 8a22d394ed
commit 199394934d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 7 deletions

View File

@ -87,8 +87,8 @@ func runCatalogerList(opts *catalogerListOptions) error {
} }
func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task) (string, error) { func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task) (string, error) {
defaultCatalogers := options.Flatten(opts.DefaultCatalogers) defaultCatalogers := options.FlattenAndSort(opts.DefaultCatalogers)
selectCatalogers := options.Flatten(opts.SelectCatalogers) selectCatalogers := options.FlattenAndSort(opts.SelectCatalogers)
selectedTaskGroups, selectionEvidence, err := task.SelectInGroups( selectedTaskGroups, selectionEvidence, err := task.SelectInGroups(
allTaskGroups, allTaskGroups,
cataloging.NewSelectionRequest(). cataloging.NewSelectionRequest().

View File

@ -284,10 +284,10 @@ func (cfg *Catalog) PostLoad() error {
cfg.From = Flatten(cfg.From) cfg.From = Flatten(cfg.From)
cfg.Catalogers = Flatten(cfg.Catalogers) cfg.Catalogers = FlattenAndSort(cfg.Catalogers)
cfg.DefaultCatalogers = Flatten(cfg.DefaultCatalogers) cfg.DefaultCatalogers = FlattenAndSort(cfg.DefaultCatalogers)
cfg.SelectCatalogers = Flatten(cfg.SelectCatalogers) cfg.SelectCatalogers = FlattenAndSort(cfg.SelectCatalogers)
cfg.Enrich = Flatten(cfg.Enrich) cfg.Enrich = FlattenAndSort(cfg.Enrich)
// for backwards compatibility // for backwards compatibility
cfg.DefaultCatalogers = append(cfg.DefaultCatalogers, cfg.Catalogers...) cfg.DefaultCatalogers = append(cfg.DefaultCatalogers, cfg.Catalogers...)
@ -312,6 +312,11 @@ func Flatten(commaSeparatedEntries []string) []string {
out = append(out, strings.TrimSpace(s)) out = append(out, strings.TrimSpace(s))
} }
} }
return out
}
func FlattenAndSort(commaSeparatedEntries []string) []string {
out := Flatten(commaSeparatedEntries)
sort.Strings(out) sort.Strings(out)
return out return out
} }

View File

@ -79,6 +79,98 @@ func TestCatalog_PostLoad(t *testing.T) {
} }
} }
func TestFlatten(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
}{
{
name: "preserves order of comma-separated values",
input: []string{"registry,docker,oci-dir"},
expected: []string{"registry", "docker", "oci-dir"},
},
{
name: "preserves order across multiple entries",
input: []string{"registry,docker", "oci-dir"},
expected: []string{"registry", "docker", "oci-dir"},
},
{
name: "trims whitespace",
input: []string{" registry , docker ", " oci-dir "},
expected: []string{"registry", "docker", "oci-dir"},
},
{
name: "handles single value",
input: []string{"registry"},
expected: []string{"registry"},
},
{
name: "handles empty input",
input: []string{},
expected: nil,
},
{
name: "preserves reverse alphabetical order",
input: []string{"zebra,yankee,xray"},
expected: []string{"zebra", "yankee", "xray"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Flatten(tt.input)
assert.Equal(t, tt.expected, got)
})
}
}
func TestFlattenAndSort(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
}{
{
name: "sorts comma-separated values",
input: []string{"registry,docker,oci-dir"},
expected: []string{"docker", "oci-dir", "registry"},
},
{
name: "sorts across multiple entries",
input: []string{"registry,docker", "oci-dir"},
expected: []string{"docker", "oci-dir", "registry"},
},
{
name: "trims whitespace and sorts",
input: []string{" registry , docker ", " oci-dir "},
expected: []string{"docker", "oci-dir", "registry"},
},
{
name: "handles single value",
input: []string{"registry"},
expected: []string{"registry"},
},
{
name: "handles empty input",
input: []string{},
expected: nil,
},
{
name: "sorts reverse alphabetical order",
input: []string{"zebra,yankee,xray"},
expected: []string{"xray", "yankee", "zebra"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FlattenAndSort(tt.input)
assert.Equal(t, tt.expected, got)
})
}
}
func Test_enrichmentEnabled(t *testing.T) { func Test_enrichmentEnabled(t *testing.T) {
tests := []struct { tests := []struct {
directives string directives string
@ -139,7 +231,7 @@ func Test_enrichmentEnabled(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.directives, func(t *testing.T) { t.Run(test.directives, func(t *testing.T) {
got := enrichmentEnabled(Flatten([]string{test.directives}), test.test) got := enrichmentEnabled(FlattenAndSort([]string{test.directives}), test.test)
assert.Equal(t, test.expected, got) assert.Equal(t, test.expected, got)
}) })
} }