|
| 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