-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathinfo_test.go
More file actions
176 lines (172 loc) · 7.37 KB
/
info_test.go
File metadata and controls
176 lines (172 loc) · 7.37 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2022-2025 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manifest
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/slackapi/slack-cli/internal/app"
"github.com/slackapi/slack-cli/internal/config"
"github.com/slackapi/slack-cli/internal/experiment"
"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/prompts"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/style"
"github.com/slackapi/slack-cli/test/testutil"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestInfoCommand(t *testing.T) {
testutil.TableTestCommand(t, testutil.CommandTests{
"errors when the source is project and app id is set": {
CmdArgs: []string{"--source", "local", "--app", "A0001"},
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
cf.SDKConfig = hooks.NewSDKConfigMock()
},
ExpectedError: slackerror.New(slackerror.ErrMismatchedFlags).
WithMessage("The \"--source\" flag must be \"remote\" when using \"--app\""),
},
"errors when the source is an unexpected value": {
CmdArgs: []string{"--source", "paper"},
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
cf.SDKConfig = hooks.NewSDKConfigMock()
cm.HookExecutor.On("Execute", mock.Anything, mock.Anything).Return("", nil)
},
ExpectedError: slackerror.New(slackerror.ErrInvalidFlag).
WithMessage("The \"--source\" flag must be \"local\" or \"remote\""),
},
"gathers the --source local from the get-manifest hook": {
CmdArgs: []string{"--source", "local"},
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
manifestMock := &app.ManifestMockObject{}
manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{
AppManifest: types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app001",
},
},
}, nil)
cf.AppClient().Manifest = manifestMock
cf.SDKConfig = hooks.NewSDKConfigMock()
},
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
mockManifest := types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app001",
},
}
manifest, err := json.MarshalIndent(mockManifest, "", " ")
require.NoError(t, err)
assert.Equal(t, string(manifest)+"\n", cm.GetStdoutOutput())
},
},
"gathers the --source remote from the apps.manifest.export method": {
CmdArgs: []string{"--source", "remote"},
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
appSelectMock := prompts.NewAppSelectMock()
appSelectPromptFunc = appSelectMock.AppSelectPrompt
appSelectMock.On("AppSelectPrompt").Return(
prompts.SelectedApp{
App: types.App{AppID: "A001"},
Auth: types.SlackAuth{Token: "xapp"}}, nil)
manifestMock := &app.ManifestMockObject{}
manifestMock.On("GetManifestRemote", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{
AppManifest: types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app002",
},
},
}, nil)
cf.AppClient().Manifest = manifestMock
cf.SDKConfig = hooks.NewSDKConfigMock()
},
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
mockManifest := types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app002",
},
}
manifest, err := json.MarshalIndent(mockManifest, "", " ")
require.NoError(t, err)
assert.Equal(t, string(manifest)+"\n", cm.GetStdoutOutput())
},
},
"gathers manifest.source from project configurations with the bolt experiment": {
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
appSelectMock := prompts.NewAppSelectMock()
appSelectPromptFunc = appSelectMock.AppSelectPrompt
appSelectMock.On("AppSelectPrompt").Return(
prompts.SelectedApp{
App: types.App{AppID: "A004"},
Auth: types.SlackAuth{Token: "xapp"}}, nil)
cm.IO.AddDefaultMocks()
cm.Os.AddDefaultMocks()
cf.SDKConfig.WorkingDirectory = "."
manifestMock := &app.ManifestMockObject{}
manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{
AppManifest: types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app002",
},
},
}, nil)
cf.AppClient().Manifest = manifestMock
cm.Config.ExperimentsFlag = append(cm.Config.ExperimentsFlag, string(experiment.BoltFrameworks))
cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug)
mockProjectConfig := config.NewProjectConfigMock()
mockProjectConfig.On("GetManifestSource", mock.Anything).Return(config.ManifestSourceLocal, nil)
cm.Config.ProjectConfig = mockProjectConfig
},
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
mockManifest := types.AppManifest{
DisplayInformation: types.DisplayInformation{
Name: "app002",
},
}
manifest, err := json.MarshalIndent(mockManifest, "", " ")
require.NoError(t, err)
assert.Equal(t, string(manifest)+"\n", cm.GetStdoutOutput())
},
},
"errors if project manifest source is remote with the bolt experiment": {
ExpectedError: slackerror.New(slackerror.ErrInvalidManifestSource).
WithMessage(`Cannot get manifest info from the "%s" source`, config.ManifestSourceRemote).
WithRemediation("%s", strings.Join([]string{
fmt.Sprintf("Find the current manifest on app settings: %s", style.LinkText("https://api.slack.com/apps")),
fmt.Sprintf("Set \"manifest.source\" to \"%s\" in \"%s\" to continue", config.ManifestSourceLocal, filepath.Join(".slack", "config.json")),
fmt.Sprintf("Read about manifest sourcing with %s", style.Commandf("manifest info --help", false)),
}, "\n")),
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
cf.SDKConfig.WorkingDirectory = "."
cm.IO.AddDefaultMocks()
cm.Os.AddDefaultMocks()
cm.Config.ExperimentsFlag = append(cm.Config.ExperimentsFlag, string(experiment.BoltFrameworks))
cm.Config.LoadExperiments(ctx, cm.IO.PrintDebug)
mockProjectConfig := config.NewProjectConfigMock()
mockProjectConfig.On("GetManifestSource", mock.Anything).
Return(config.ManifestSource(config.ManifestSourceRemote), nil)
cm.Config.ProjectConfig = mockProjectConfig
},
},
}, func(clients *shared.ClientFactory) *cobra.Command {
return NewInfoCommand(clients)
})
}