Skip to content

Commit 9dc0b88

Browse files
committed
tests
1 parent faeb01c commit 9dc0b88

4 files changed

Lines changed: 159 additions & 5 deletions

File tree

cmd/manifest/validate_test.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type ManifestValidatePkgMock struct {
3737
}
3838

3939
func (m *ManifestValidatePkgMock) ManifestValidate(ctx context.Context, clients *shared.ClientFactory, app types.App, token string) (bool, slackerror.Warnings, error) {
40-
m.Called(ctx, clients, app, token)
41-
return true, nil, nil
40+
args := m.Called(ctx, clients, app, token)
41+
return args.Bool(0), nil, args.Error(2)
4242
}
4343

4444
func TestManifestValidateCommand(t *testing.T) {
@@ -63,7 +63,7 @@ func TestManifestValidateCommand(t *testing.T) {
6363
manifestValidatePkgMock := new(ManifestValidatePkgMock)
6464
manifestValidateFunc = manifestValidatePkgMock.ManifestValidate
6565

66-
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
66+
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil, nil)
6767
err := cmd.ExecuteContext(ctx)
6868
if err != nil {
6969
assert.Fail(t, "cmd.Execute had unexpected error")
@@ -138,7 +138,7 @@ func TestManifestValidateCommand_HandleMissingAppInstallError_OneUserAuth(t *tes
138138
// Mock the manifest validate package
139139
manifestValidatePkgMock := new(ManifestValidatePkgMock)
140140
manifestValidateFunc = manifestValidatePkgMock.ManifestValidate
141-
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
141+
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil, nil)
142142

143143
// Should execute without error
144144
err := cmd.ExecuteContext(ctx)
@@ -200,14 +200,38 @@ func TestManifestValidateCommand_HandleMissingAppInstallError_MoreThanOneUserAut
200200
// Mock the manifest validate package
201201
manifestValidatePkgMock := new(ManifestValidatePkgMock)
202202
manifestValidateFunc = manifestValidatePkgMock.ManifestValidate
203-
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
203+
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(true, nil, nil)
204204

205205
// Should execute without error
206206
err := cmd.ExecuteContext(ctx)
207207
require.NoError(t, err)
208208
clientsMock.Auth.AssertCalled(t, "SetSelectedAuth", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
209209
}
210210

211+
func TestManifestValidateCommand_InvalidManifest(t *testing.T) {
212+
ctx := slackcontext.MockContext(t.Context())
213+
clientsMock := shared.NewClientsMock()
214+
clientsMock.AddDefaultMocks()
215+
216+
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
217+
clients.SDKConfig = hooks.NewSDKConfigMock()
218+
})
219+
220+
cmd := NewValidateCommand(clients)
221+
testutil.MockCmdIO(clients.IO, cmd)
222+
223+
appSelectMock := prompts.NewAppSelectMock()
224+
appSelectPromptFunc = appSelectMock.AppSelectPrompt
225+
appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly).Return(prompts.SelectedApp{}, nil)
226+
227+
manifestValidatePkgMock := new(ManifestValidatePkgMock)
228+
manifestValidateFunc = manifestValidatePkgMock.ManifestValidate
229+
manifestValidatePkgMock.On("ManifestValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(false, nil, nil)
230+
231+
err := cmd.ExecuteContext(ctx)
232+
require.NoError(t, err)
233+
}
234+
211235
func TestManifestValidateCommand_HandleOtherErrors(t *testing.T) {
212236
// Create mocks
213237
ctx := slackcontext.MockContext(t.Context())

internal/iostreams/printer_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,40 @@ func Test_PrintWarning(t *testing.T) {
143143
}
144144
}
145145

146+
func Test_PrintInfo(t *testing.T) {
147+
tests := map[string]struct {
148+
format string
149+
arguments []any
150+
expected string
151+
}{
152+
"prints a formatted info to stdout": {
153+
format: "hello %s - noon is %d",
154+
arguments: []any{"world", 12},
155+
expected: "hello world - noon is 12\n",
156+
},
157+
"prints unformatted info to stdout": {
158+
format: "something happened",
159+
expected: "something happened\n",
160+
},
161+
}
162+
for name, tc := range tests {
163+
t.Run(name, func(t *testing.T) {
164+
ctx := slackcontext.MockContext(t.Context())
165+
fsMock := slackdeps.NewFsMock()
166+
osMock := slackdeps.NewOsMock()
167+
osMock.AddDefaultMocks()
168+
config := config.NewConfig(fsMock, osMock)
169+
io := NewIOStreams(config, fsMock, osMock)
170+
stdoutBuffer := bytes.Buffer{}
171+
stdoutLogger := log.Logger{}
172+
stdoutLogger.SetOutput(&stdoutBuffer)
173+
io.Stdout = &stdoutLogger
174+
io.PrintInfo(ctx, false, tc.format, tc.arguments...)
175+
assert.Equal(t, tc.expected, stdoutBuffer.String())
176+
})
177+
}
178+
}
179+
146180
func Test_IOStreams_PrintTrace(t *testing.T) {
147181
tests := map[string]struct {
148182
traceID string

internal/pkg/apps/install_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,3 +1681,54 @@ func TestSetAppEnvironmentTokens(t *testing.T) {
16811681
})
16821682
}
16831683
}
1684+
1685+
func TestContinueDespiteWarning(t *testing.T) {
1686+
tests := map[string]struct {
1687+
warnings slackerror.Warnings
1688+
confirmPrompt bool
1689+
expectedResult bool
1690+
}{
1691+
"user confirms breaking change": {
1692+
warnings: slackerror.Warnings{
1693+
{Code: "breaking_change", Message: "something changed"},
1694+
},
1695+
confirmPrompt: true,
1696+
expectedResult: true,
1697+
},
1698+
"user declines breaking change": {
1699+
warnings: slackerror.Warnings{
1700+
{Code: "breaking_change", Message: "something changed"},
1701+
},
1702+
confirmPrompt: false,
1703+
expectedResult: false,
1704+
},
1705+
"non-breaking warning continues without prompt": {
1706+
warnings: slackerror.Warnings{
1707+
{Code: "some_warning", Message: "just a warning"},
1708+
},
1709+
expectedResult: true,
1710+
},
1711+
}
1712+
for name, tc := range tests {
1713+
t.Run(name, func(t *testing.T) {
1714+
ctx := slackcontext.MockContext(t.Context())
1715+
clientsMock := shared.NewClientsMock()
1716+
clientsMock.IO.AddDefaultMocks()
1717+
output := &bytes.Buffer{}
1718+
clientsMock.IO.Stdout.SetOutput(output)
1719+
stderr := &bytes.Buffer{}
1720+
clientsMock.IO.Stderr.SetOutput(stderr)
1721+
clientsMock.IO.On(
1722+
"ConfirmPrompt",
1723+
mock.Anything,
1724+
"Confirm changes?",
1725+
false,
1726+
).Return(tc.confirmPrompt, nil)
1727+
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
1728+
1729+
result, err := continueDespiteWarning(ctx, clients, tc.warnings)
1730+
require.NoError(t, err)
1731+
assert.Equal(t, tc.expectedResult, result)
1732+
})
1733+
}
1734+
}

internal/pkg/platform/activity_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,51 @@ func Test_prettifyActivity(t *testing.T) {
7474
`{"some":"data"}`,
7575
},
7676
},
77+
"warn level activity should contain the message": {
78+
activity: api.Activity{
79+
TraceID: "w123",
80+
Level: types.WARN,
81+
EventType: "unknown",
82+
ComponentID: "w789",
83+
Payload: map[string]interface{}{
84+
"some": "warning",
85+
},
86+
Created: 1686939542,
87+
},
88+
expectedResults: []string{
89+
`{"some":"warning"}`,
90+
},
91+
},
92+
"error level activity should contain the message": {
93+
activity: api.Activity{
94+
TraceID: "e123",
95+
Level: types.ERROR,
96+
EventType: "unknown",
97+
ComponentID: "e789",
98+
Payload: map[string]interface{}{
99+
"some": "error",
100+
},
101+
Created: 1686939542,
102+
},
103+
expectedResults: []string{
104+
`{"some":"error"}`,
105+
},
106+
},
107+
"fatal level activity should contain the message": {
108+
activity: api.Activity{
109+
TraceID: "f123",
110+
Level: types.FATAL,
111+
EventType: "unknown",
112+
ComponentID: "f789",
113+
Payload: map[string]interface{}{
114+
"some": "fatal",
115+
},
116+
Created: 1686939542,
117+
},
118+
expectedResults: []string{
119+
`{"some":"fatal"}`,
120+
},
121+
},
77122
}
78123
for name, tc := range tests {
79124
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)