Skip to content

Commit ad9ddd9

Browse files
authored
test: add units tests for untested functions in style, api, types, and create packages (#427)
test: increase test coverage for multiple packages Add tests for untested functions across style, api, shared/types, and pkg/create packages to increase overall coverage from 70.1% to 70.6%.
1 parent b079123 commit ad9ddd9

File tree

6 files changed

+362
-0
lines changed

6 files changed

+362
-0
lines changed

internal/api/activity_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package api
1616

1717
import (
1818
"testing"
19+
"time"
1920

2021
"github.com/slackapi/slack-cli/internal/shared/types"
2122
"github.com/slackapi/slack-cli/internal/slackcontext"
2223
"github.com/slackapi/slack-cli/internal/slackerror"
24+
"github.com/stretchr/testify/assert"
2325
"github.com/stretchr/testify/require"
2426
)
2527

@@ -211,3 +213,11 @@ func Test_APIClient_ActivityInvalidJSON(t *testing.T) {
211213
require.Error(t, err)
212214
require.Contains(t, err.Error(), slackerror.ErrUnableToParseJSON)
213215
}
216+
217+
func Test_Activity_CreatedPretty(t *testing.T) {
218+
// Created is in microseconds
219+
createdMicroseconds := int64(1700000000000000)
220+
activity := Activity{Created: createdMicroseconds}
221+
expected := time.Unix(createdMicroseconds/1000000, 0).Format("2006-01-02 15:04:05")
222+
assert.Equal(t, expected, activity.CreatedPretty())
223+
}

internal/api/workflows_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@
1515
package api
1616

1717
import (
18+
"fmt"
1819
"testing"
1920

2021
"github.com/slackapi/slack-cli/internal/shared/types"
2122
"github.com/slackapi/slack-cli/internal/slackcontext"
23+
"github.com/stretchr/testify/assert"
2224
"github.com/stretchr/testify/require"
2325
)
2426

27+
func Test_TriggerCreateOrUpdateError_Error(t *testing.T) {
28+
err := &TriggerCreateOrUpdateError{
29+
Err: fmt.Errorf("something went wrong"),
30+
}
31+
assert.Equal(t, "something went wrong", err.Error())
32+
}
33+
2534
func TestClient_WorkflowsTriggerCreate(t *testing.T) {
2635
var inputs = make(map[string]*Input)
2736
inputs["test"] = &Input{Value: "val"}

internal/pkg/create/template_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ import (
2323
"github.com/stretchr/testify/require"
2424
)
2525

26+
func Test_Template_GetSubdir(t *testing.T) {
27+
t.Run("default is empty string", func(t *testing.T) {
28+
tmpl := Template{}
29+
assert.Equal(t, "", tmpl.GetSubdir())
30+
})
31+
t.Run("returns value set by SetSubdir", func(t *testing.T) {
32+
tmpl := Template{}
33+
tmpl.SetSubdir("subpath")
34+
assert.Equal(t, "subpath", tmpl.GetSubdir())
35+
})
36+
}
37+
38+
func Test_Template_SetSubdir(t *testing.T) {
39+
tmpl := Template{}
40+
tmpl.SetSubdir("custom/dir")
41+
assert.Equal(t, "custom/dir", tmpl.GetSubdir())
42+
}
43+
2644
func TestTemplate_ResolveTemplateURL(t *testing.T) {
2745
tests := map[string]struct {
2846
url string

internal/shared/types/app_manifest_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ func Test_RawJSON_UnmarshalJSON(t *testing.T) {
4848
}
4949
}
5050

51+
func Test_RawJSON_MarshalJSON(t *testing.T) {
52+
tests := map[string]struct {
53+
rawJSON RawJSON
54+
expected string
55+
}{
56+
"marshals JSONData when present": {
57+
rawJSON: func() RawJSON {
58+
raw := json.RawMessage(`{"name":"foo"}`)
59+
return RawJSON{JSONData: &raw}
60+
}(),
61+
expected: `{"name":"foo"}`,
62+
},
63+
"marshals from Data when JSONData is nil": {
64+
rawJSON: RawJSON{Data: &yaml.MapSlice{
65+
{Key: "name", Value: "bar"},
66+
}},
67+
expected: `{"name":"bar"}`,
68+
},
69+
}
70+
for name, tc := range tests {
71+
t.Run(name, func(t *testing.T) {
72+
result, err := tc.rawJSON.MarshalJSON()
73+
require.NoError(t, err)
74+
assert.Equal(t, tc.expected, string(result))
75+
})
76+
}
77+
}
78+
5179
func Test_RawJSON_UnmarshalYAML(t *testing.T) {
5280
rawJSON := RawJSON{Data: &yaml.MapSlice{
5381
{Key: "name", Value: "foo"},
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2022-2026 Salesforce, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package types
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func Test_SlackYaml_hasValidIconPath(t *testing.T) {
27+
tests := map[string]struct {
28+
icon string
29+
setup func(t *testing.T, dir string)
30+
expected bool
31+
}{
32+
"valid custom icon path returns true": {
33+
icon: "custom/icon.png",
34+
setup: func(t *testing.T, dir string) {
35+
require.NoError(t, os.MkdirAll(filepath.Join(dir, "custom"), 0o755))
36+
require.NoError(t, os.WriteFile(filepath.Join(dir, "custom", "icon.png"), []byte("img"), 0o644))
37+
},
38+
expected: true,
39+
},
40+
"invalid custom icon path returns false": {
41+
icon: "missing/icon.png",
42+
setup: func(t *testing.T, dir string) {},
43+
expected: false,
44+
},
45+
"no icon with default assets/icon.png present returns true": {
46+
icon: "",
47+
setup: func(t *testing.T, dir string) {
48+
require.NoError(t, os.MkdirAll(filepath.Join(dir, "assets"), 0o755))
49+
require.NoError(t, os.WriteFile(filepath.Join(dir, "assets", "icon.png"), []byte("img"), 0o644))
50+
},
51+
expected: true,
52+
},
53+
"no icon and no default returns true": {
54+
icon: "",
55+
setup: func(t *testing.T, dir string) {},
56+
expected: true,
57+
},
58+
}
59+
for name, tc := range tests {
60+
t.Run(name, func(t *testing.T) {
61+
dir := t.TempDir()
62+
tc.setup(t, dir)
63+
64+
origDir, err := os.Getwd()
65+
require.NoError(t, err)
66+
require.NoError(t, os.Chdir(dir))
67+
defer func() { require.NoError(t, os.Chdir(origDir)) }()
68+
69+
sy := &SlackYaml{Icon: tc.icon}
70+
assert.Equal(t, tc.expected, sy.hasValidIconPath())
71+
})
72+
}
73+
}
74+
75+
func Test_SlackYaml_Verify(t *testing.T) {
76+
tests := map[string]struct {
77+
icon string
78+
setup func(t *testing.T, dir string)
79+
expectErr bool
80+
}{
81+
"valid icon returns nil error": {
82+
icon: "icon.png",
83+
setup: func(t *testing.T, dir string) {
84+
require.NoError(t, os.WriteFile(filepath.Join(dir, "icon.png"), []byte("img"), 0o644))
85+
},
86+
expectErr: false,
87+
},
88+
"invalid icon returns error": {
89+
icon: "missing.png",
90+
setup: func(t *testing.T, dir string) {},
91+
expectErr: true,
92+
},
93+
}
94+
for name, tc := range tests {
95+
t.Run(name, func(t *testing.T) {
96+
dir := t.TempDir()
97+
tc.setup(t, dir)
98+
99+
origDir, err := os.Getwd()
100+
require.NoError(t, err)
101+
require.NoError(t, os.Chdir(dir))
102+
defer func() { require.NoError(t, os.Chdir(origDir)) }()
103+
104+
sy := &SlackYaml{Icon: tc.icon}
105+
if tc.expectErr {
106+
assert.Error(t, sy.Verify())
107+
} else {
108+
assert.NoError(t, sy.Verify())
109+
}
110+
})
111+
}
112+
}

0 commit comments

Comments
 (0)