-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathlist_test.go
More file actions
94 lines (81 loc) · 2.56 KB
/
list_test.go
File metadata and controls
94 lines (81 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package aitools
import (
"testing"
"github.com/databricks/cli/libs/aitools/installer"
"github.com/databricks/cli/libs/cmdio"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListCommandExists(t *testing.T) {
cmd := NewListCmd()
assert.Equal(t, "list", cmd.Use)
}
func TestListCommandCallsListFn(t *testing.T) {
orig := listSkillsFn
t.Cleanup(func() { listSkillsFn = orig })
called := false
listSkillsFn = func(cmd *cobra.Command, scope string) error {
called = true
return nil
}
ctx := cmdio.MockDiscard(t.Context())
cmd := NewListCmd()
cmd.SetContext(ctx)
err := cmd.RunE(cmd, nil)
require.NoError(t, err)
assert.True(t, called)
}
func TestListCommandHasScopeFlags(t *testing.T) {
cmd := NewListCmd()
f := cmd.Flags().Lookup("project")
require.NotNil(t, f, "--project flag should exist (deprecated alias)")
assert.NotEmpty(t, f.Deprecated, "--project should be marked deprecated")
f = cmd.Flags().Lookup("global")
require.NotNil(t, f, "--global flag should exist (deprecated alias)")
assert.NotEmpty(t, f.Deprecated, "--global should be marked deprecated")
f = cmd.Flags().Lookup("scope")
require.NotNil(t, f, "--scope flag should exist")
}
func TestListScopeFlag(t *testing.T) {
tests := []struct {
name string
args []string
wantScope string
wantErr string
}{
{name: "scope project", args: []string{"--scope", "project"}, wantScope: installer.ScopeProject},
{name: "scope global", args: []string{"--scope", "global"}, wantScope: installer.ScopeGlobal},
{name: "scope both shows both", args: []string{"--scope", "both"}, wantScope: ""},
{name: "scope invalid", args: []string{"--scope", "all"}, wantErr: `invalid --scope "all"`},
{name: "legacy both flags together rejected", args: []string{"--project", "--global"}, wantErr: "cannot use --global and --project together"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
orig := listSkillsFn
t.Cleanup(func() { listSkillsFn = orig })
var gotScope string
called := false
listSkillsFn = func(_ *cobra.Command, scope string) error {
called = true
gotScope = scope
return nil
}
ctx := cmdio.MockDiscard(t.Context())
cmd := NewListCmd()
cmd.SetContext(ctx)
cmd.SetArgs(tt.args)
cmd.SilenceErrors = true
cmd.SilenceUsage = true
err := cmd.Execute()
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.True(t, called)
assert.Equal(t, tt.wantScope, gotScope)
})
}
}