Skip to content

Commit 18bd642

Browse files
committed
🧹 add test for shell resource sorting function
1 parent 3cbe8fa commit 18bd642

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

cli/shell/completer.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ type Suggestion struct {
2222

2323
// Completer is an auto-complete helper for the shell
2424
type Completer struct {
25-
schema resources.ResourcesSchema
26-
features cnquery.Features
27-
connectedProviders []string
25+
schema resources.ResourcesSchema
26+
features cnquery.Features
27+
sortFn func(a, b *llx.Documentation) int
2828
}
2929

3030
// NewCompleter creates a new Mondoo completer object
3131
func NewCompleter(schema resources.ResourcesSchema, features cnquery.Features, connectedProviders []string) *Completer {
32+
sortFn := byProviderSortFn(connectedProviders)
33+
3234
return &Completer{
33-
schema: schema,
34-
features: features,
35-
connectedProviders: connectedProviders,
35+
schema: schema,
36+
features: features,
37+
sortFn: sortFn,
3638
}
3739
}
3840

@@ -61,16 +63,7 @@ func (c *Completer) Complete(text string) []Suggestion {
6163
bundle, _ := mqlc.Compile(text, nil, mqlc.NewConfig(c.schema, c.features))
6264
if bundle != nil && len(bundle.Suggestions) > 0 {
6365
// reorder suggestions to put the ones from connected providers first
64-
slices.SortFunc(bundle.Suggestions, func(a, b *llx.Documentation) int {
65-
aConnected := stringx.Contains(c.connectedProviders, a.Provider)
66-
bConnected := stringx.Contains(c.connectedProviders, b.Provider)
67-
if aConnected && !bConnected {
68-
return -1
69-
} else if !aConnected && bConnected {
70-
return 1
71-
}
72-
return 0
73-
})
66+
slices.SortFunc(bundle.Suggestions, c.sortFn)
7467

7568
// add suggestions from the compiler
7669
for i := range bundle.Suggestions {
@@ -84,3 +77,16 @@ func (c *Completer) Complete(text string) []Suggestion {
8477

8578
return suggestions
8679
}
80+
81+
func byProviderSortFn(connectedProviders []string) func(a, b *llx.Documentation) int {
82+
return func(a, b *llx.Documentation) int {
83+
aConnected := stringx.Contains(connectedProviders, a.Provider)
84+
bConnected := stringx.Contains(connectedProviders, b.Provider)
85+
if aConnected && !bConnected {
86+
return -1
87+
} else if !aConnected && bConnected {
88+
return 1
89+
}
90+
return 0
91+
}
92+
}

cli/shell/completer_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package shell
5+
6+
import (
7+
"testing"
8+
9+
"go.mondoo.com/cnquery/v12/llx"
10+
)
11+
12+
func TestByProviderSortFn(t *testing.T) {
13+
connectedProviders := []string{"go.mondoo.com/cnquery/v9/providers/aws"}
14+
sortFn := byProviderSortFn(connectedProviders)
15+
16+
tests := []struct {
17+
docA *llx.Documentation
18+
docB *llx.Documentation
19+
expected int
20+
}{
21+
{
22+
docA: &llx.Documentation{Provider: "go.mondoo.com/cnquery/v9/providers/aws", Field: "a"},
23+
docB: &llx.Documentation{Provider: "gcp", Field: "b"},
24+
expected: -1,
25+
},
26+
{
27+
docA: &llx.Documentation{Provider: "go.mondoo.com/cnquery/v9/providers/azure", Field: "a"},
28+
docB: &llx.Documentation{Provider: "go.mondoo.com/cnquery/v9/providers/aws", Field: "a"},
29+
expected: 1,
30+
},
31+
{
32+
docA: &llx.Documentation{Provider: "go.mondoo.com/cnquery/v9/providers/gcp", Field: "b"},
33+
docB: &llx.Documentation{Provider: "go.mondoo.com/cnquery/v9/providers/gcp", Field: "a"},
34+
expected: 0,
35+
},
36+
}
37+
38+
for _, test := range tests {
39+
result := sortFn(test.docA, test.docB)
40+
if result != test.expected {
41+
t.Errorf("Expected %d, got %d for docs %+v and %+v", test.expected, result, test.docA, test.docB)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)