Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/api/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package api

import (
"testing"
"time"

"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -211,3 +213,11 @@ func Test_APIClient_ActivityInvalidJSON(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), slackerror.ErrUnableToParseJSON)
}

func Test_Activity_CreatedPretty(t *testing.T) {
// Created is in microseconds
createdMicroseconds := int64(1700000000000000)
activity := Activity{Created: createdMicroseconds}
expected := time.Unix(createdMicroseconds/1000000, 0).Format("2006-01-02 15:04:05")
assert.Equal(t, expected, activity.CreatedPretty())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐞 praise: Pretty time at L222!

}
9 changes: 9 additions & 0 deletions internal/api/workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
package api

import (
"fmt"
"testing"

"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_TriggerCreateOrUpdateError_Error(t *testing.T) {
err := &TriggerCreateOrUpdateError{
Err: fmt.Errorf("something went wrong"),
}
assert.Equal(t, "something went wrong", err.Error())
}

func TestClient_WorkflowsTriggerCreate(t *testing.T) {
var inputs = make(map[string]*Input)
inputs["test"] = &Input{Value: "val"}
Expand Down
18 changes: 18 additions & 0 deletions internal/pkg/create/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Template_GetSubdir(t *testing.T) {
t.Run("default is empty string", func(t *testing.T) {
tmpl := Template{}
assert.Equal(t, "", tmpl.GetSubdir())
})
t.Run("returns value set by SetSubdir", func(t *testing.T) {
tmpl := Template{}
tmpl.SetSubdir("subpath")
assert.Equal(t, "subpath", tmpl.GetSubdir())
})
}

func Test_Template_SetSubdir(t *testing.T) {
tmpl := Template{}
tmpl.SetSubdir("custom/dir")
assert.Equal(t, "custom/dir", tmpl.GetSubdir())
}

Comment on lines +26 to +43
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: It makes me feel good to see these have some tests added.

func TestTemplate_ResolveTemplateURL(t *testing.T) {
tests := map[string]struct {
url string
Expand Down
28 changes: 28 additions & 0 deletions internal/shared/types/app_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ func Test_RawJSON_UnmarshalJSON(t *testing.T) {
}
}

func Test_RawJSON_MarshalJSON(t *testing.T) {
tests := map[string]struct {
rawJSON RawJSON
expected string
}{
"marshals JSONData when present": {
rawJSON: func() RawJSON {
raw := json.RawMessage(`{"name":"foo"}`)
return RawJSON{JSONData: &raw}
}(),
Comment on lines +57 to +60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: TIL about an anonymous function as a way to populate a variable in our test tables. It's actually handy, although it can be difficult to read when overused.

expected: `{"name":"foo"}`,
},
"marshals from Data when JSONData is nil": {
rawJSON: RawJSON{Data: &yaml.MapSlice{
{Key: "name", Value: "bar"},
}},
expected: `{"name":"bar"}`,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result, err := tc.rawJSON.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, tc.expected, string(result))
})
}
}

func Test_RawJSON_UnmarshalYAML(t *testing.T) {
rawJSON := RawJSON{Data: &yaml.MapSlice{
{Key: "name", Value: "foo"},
Expand Down
112 changes: 112 additions & 0 deletions internal/shared/types/slack_yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2022-2026 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 types

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_SlackYaml_hasValidIconPath(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

threat: I'm coming for you SlackYaml. Not today. Not tomorrow. But one day, I'm coming for you and giving you a new name.

tests := map[string]struct {
icon string
setup func(t *testing.T, dir string)
expected bool
}{
"valid custom icon path returns true": {
icon: "custom/icon.png",
setup: func(t *testing.T, dir string) {
require.NoError(t, os.MkdirAll(filepath.Join(dir, "custom"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "custom", "icon.png"), []byte("img"), 0o644))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎁 praise: Exciting tests to find for this YAML package to get in meantimes!

},
expected: true,
},
"invalid custom icon path returns false": {
icon: "missing/icon.png",
setup: func(t *testing.T, dir string) {},
expected: false,
},
"no icon with default assets/icon.png present returns true": {
icon: "",
setup: func(t *testing.T, dir string) {
require.NoError(t, os.MkdirAll(filepath.Join(dir, "assets"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "assets", "icon.png"), []byte("img"), 0o644))
},
expected: true,
},
"no icon and no default returns true": {
icon: "",
setup: func(t *testing.T, dir string) {},
expected: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 thought: It stinks we aren't using aefero here... I'm curious if this might be check better kept for callsites in upcoming change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we have a few places where we use t.TempDir() instead of afero. FWIW I have a personal TODO to loopback and switch these to afero, if possible, after we hit 70% test coverage 😄

tc.setup(t, dir)

origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(origDir)) }()

sy := &SlackYaml{Icon: tc.icon}
assert.Equal(t, tc.expected, sy.hasValidIconPath())
})
}
}

func Test_SlackYaml_Verify(t *testing.T) {
tests := map[string]struct {
icon string
setup func(t *testing.T, dir string)
expectErr bool
}{
"valid icon returns nil error": {
icon: "icon.png",
setup: func(t *testing.T, dir string) {
require.NoError(t, os.WriteFile(filepath.Join(dir, "icon.png"), []byte("img"), 0o644))
},
expectErr: false,
},
"invalid icon returns error": {
icon: "missing.png",
setup: func(t *testing.T, dir string) {},
expectErr: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
tc.setup(t, dir)

origDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(origDir)) }()

sy := &SlackYaml{Icon: tc.icon}
if tc.expectErr {
assert.Error(t, sy.Verify())
} else {
assert.NoError(t, sy.Verify())
}
})
}
}
Loading
Loading