|
| 1 | +package gitlabci |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGetOwnerAndRepo(t *testing.T) { |
| 11 | + testcases := []struct { |
| 12 | + name string |
| 13 | + setup func() |
| 14 | + expectedOwner string |
| 15 | + expectedRepo string |
| 16 | + expectedError string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "CI_PROJECT_NAMESPACE and CI_PROJECT_NAME is set as expected", |
| 20 | + setup: func() { |
| 21 | + os.Setenv("CI_PROJECT_NAMESPACE", "foo-bar") |
| 22 | + os.Setenv("CI_PROJECT_NAME", "my-awesome-repo") |
| 23 | + }, |
| 24 | + expectedOwner: "foo-bar", |
| 25 | + expectedRepo: "my-awesome-repo", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "CI_PROJECT_NAMESPACE is not set", |
| 29 | + setup: func() { |
| 30 | + os.Unsetenv("CI_PROJECT_NAMESPACE") |
| 31 | + os.Setenv("CI_PROJECT_NAME", "my-awesome-repo") |
| 32 | + }, |
| 33 | + expectedError: `env CI_PROJECT_NAMESPACE not set`, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "CI_PROJECT_NAME is not set", |
| 37 | + setup: func() { |
| 38 | + os.Setenv("CI_PROJECT_NAMESPACE", "foo-bar") |
| 39 | + os.Unsetenv("CI_PROJECT_NAME") |
| 40 | + }, |
| 41 | + expectedError: `env CI_PROJECT_NAME not set`, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + p := &Provider{} |
| 46 | + for _, tc := range testcases { |
| 47 | + t.Run(tc.name, func(t *testing.T) { |
| 48 | + os.Clearenv() |
| 49 | + |
| 50 | + if tc.setup != nil { |
| 51 | + tc.setup() |
| 52 | + } |
| 53 | + |
| 54 | + owner, repo, err := p.GetOwnerAndRepo() |
| 55 | + |
| 56 | + assert.Equal(t, tc.expectedOwner, owner) |
| 57 | + assert.Equal(t, tc.expectedRepo, repo) |
| 58 | + assertError(t, tc.expectedError, err) |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestGetActionActor(t *testing.T) { |
| 64 | + testcases := []struct { |
| 65 | + name string |
| 66 | + setup func() |
| 67 | + expectedActor string |
| 68 | + expectedError string |
| 69 | + }{ |
| 70 | + { |
| 71 | + name: "env GITLAB_USER_LOGIN is set as expected", |
| 72 | + setup: func() { |
| 73 | + os.Setenv("GITLAB_USER_LOGIN", "foo-bar") |
| 74 | + }, |
| 75 | + expectedActor: "foo-bar", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "env GITLAB_USER_LOGIN is not set", |
| 79 | + expectedError: "env GITLAB_USER_LOGIN not set", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + p := &Provider{} |
| 84 | + for _, tc := range testcases { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + os.Clearenv() |
| 87 | + |
| 88 | + if tc.setup != nil { |
| 89 | + tc.setup() |
| 90 | + } |
| 91 | + |
| 92 | + actor, err := p.GetActor() |
| 93 | + assert.Equal(t, tc.expectedActor, actor) |
| 94 | + assertError(t, tc.expectedError, err) |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestGetTag(t *testing.T) { |
| 100 | + testcases := []struct { |
| 101 | + name string |
| 102 | + setup func() |
| 103 | + expectedTag string |
| 104 | + expectedError string |
| 105 | + }{ |
| 106 | + { |
| 107 | + name: "env CI_COMMIT_TAG is setup", |
| 108 | + setup: func() { |
| 109 | + os.Setenv("CI_COMMIT_TAG", "v5.0.0") |
| 110 | + }, |
| 111 | + expectedTag: "v5.0.0", |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "CI_COMMIT_TAG is not set", |
| 115 | + setup: func() { |
| 116 | + os.Unsetenv("CI_COMMIT_TAG") |
| 117 | + }, |
| 118 | + expectedError: `CI_COMMIT_TAG env variable not found`, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + p := &Provider{} |
| 123 | + for _, tc := range testcases { |
| 124 | + t.Run(tc.name, func(t *testing.T) { |
| 125 | + os.Clearenv() |
| 126 | + |
| 127 | + if tc.setup != nil { |
| 128 | + tc.setup() |
| 129 | + } |
| 130 | + |
| 131 | + tag, err := p.GetTag() |
| 132 | + assert.Equal(t, tc.expectedTag, tag) |
| 133 | + assertError(t, tc.expectedError, err) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func assertError(t *testing.T, expectedError string, err error) { |
| 139 | + if expectedError == "" { |
| 140 | + assert.Nil(t, err) |
| 141 | + } |
| 142 | + |
| 143 | + if expectedError != "" { |
| 144 | + assert.NotNil(t, err) |
| 145 | + if err != nil { |
| 146 | + assert.Equal(t, expectedError, err.Error()) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestGetWorkingDirectory(t *testing.T) { |
| 152 | + testcases := []struct { |
| 153 | + name string |
| 154 | + setup func() |
| 155 | + expectedDir string |
| 156 | + }{ |
| 157 | + { |
| 158 | + name: "env CI_PROJECT_DIR is setup", |
| 159 | + setup: func() { |
| 160 | + os.Setenv("CI_PROJECT_DIR", "/builds/foo-bar/my-awesome-repo") |
| 161 | + }, |
| 162 | + expectedDir: "/builds/foo-bar/my-awesome-repo", |
| 163 | + }, { |
| 164 | + name: "env CI_PROJECT_DIR is setup", |
| 165 | + setup: func() { |
| 166 | + os.Setenv("CI_PROJECT_DIR", "/builds/foo-bar/my-awesome-repo") |
| 167 | + os.Setenv("INPUT_WORKDIR", "/my-workdir") |
| 168 | + }, |
| 169 | + expectedDir: "/my-workdir", |
| 170 | + }, { |
| 171 | + name: "env CI_PROJECT_DIR is not set", |
| 172 | + setup: func() { |
| 173 | + os.Unsetenv("CI_PROJECT_DIR") |
| 174 | + }, |
| 175 | + expectedDir: "", |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + p := &Provider{} |
| 180 | + for _, tc := range testcases { |
| 181 | + t.Run(tc.name, func(t *testing.T) { |
| 182 | + os.Clearenv() |
| 183 | + |
| 184 | + if tc.setup != nil { |
| 185 | + tc.setup() |
| 186 | + } |
| 187 | + |
| 188 | + dir := p.GetWorkDirectory() |
| 189 | + assert.Equal(t, tc.expectedDir, dir) |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestGetTemplateFile(t *testing.T) { |
| 195 | + testcases := []struct { |
| 196 | + name string |
| 197 | + setup func() |
| 198 | + expectFile string |
| 199 | + }{ |
| 200 | + { |
| 201 | + name: "env INPUT_KREW_TEMPLATE_FILE is setup", |
| 202 | + setup: func() { |
| 203 | + os.Setenv("INPUT_KREW_TEMPLATE_FILE", "my-awesome-plugin.yaml") |
| 204 | + }, |
| 205 | + expectFile: "my-awesome-plugin.yaml", |
| 206 | + }, { |
| 207 | + name: "env INPUT_KREW_TEMPLATE_FILE is not set", |
| 208 | + setup: func() { |
| 209 | + os.Unsetenv("INPUT_KREW_TEMPLATE_FILE") |
| 210 | + }, |
| 211 | + expectFile: ".krew.yaml", |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + p := &Provider{} |
| 216 | + for _, tc := range testcases { |
| 217 | + t.Run(tc.name, func(t *testing.T) { |
| 218 | + os.Clearenv() |
| 219 | + |
| 220 | + if tc.setup != nil { |
| 221 | + tc.setup() |
| 222 | + } |
| 223 | + |
| 224 | + dir := p.GetTemplateFile() |
| 225 | + assert.Equal(t, tc.expectFile, dir) |
| 226 | + }) |
| 227 | + } |
| 228 | +} |
0 commit comments