|
3 | 3 | package forge_test |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "fmt" |
| 7 | + "math/rand/v2" |
6 | 8 | "os" |
7 | 9 | "path/filepath" |
8 | 10 | "strings" |
9 | 11 | "testing" |
10 | 12 |
|
11 | 13 | "github.com/frantjc/forge" |
12 | 14 | "github.com/frantjc/forge/githubactions" |
| 15 | + xos "github.com/frantjc/x/os" |
13 | 16 | "github.com/stretchr/testify/require" |
14 | 17 | "sigs.k8s.io/yaml" |
15 | 18 | ) |
@@ -45,19 +48,23 @@ func TestActionRunDocker(t *testing.T) { |
45 | 48 | func TestActionRunDockerNonzeroExitCode(t *testing.T) { |
46 | 49 | cr := Runtime(t) |
47 | 50 |
|
| 51 | + expected := rand.IntN(254) + 1 |
48 | 52 | uses := Uses(t, &githubactions.Metadata{ |
49 | 53 | Name: t.Name(), |
50 | 54 | Runs: &githubactions.MetadataRuns{ |
51 | 55 | Using: githubactions.RunsUsingDocker, |
52 | 56 | Image: "docker://public.ecr.aws/docker/library/alpine", |
53 | 57 | Entrypoint: "/bin/sh", |
54 | | - Args: []string{"-c", "exit 1"}, |
| 58 | + Args: []string{"-c", fmt.Sprintf("exit %d", expected)}, |
55 | 59 | }, |
56 | 60 | }) |
57 | 61 |
|
58 | 62 | action := &forge.Action{Uses: uses} |
59 | 63 |
|
60 | | - require.Error(t, action.Run(t.Context(), cr, forge.WithStreams(Streams(t)))) |
| 64 | + err := action.Run(t.Context(), cr, forge.WithStreams(Streams(t))) |
| 65 | + require.Error(t, err) |
| 66 | + require.ErrorIs(t, err, forge.ErrContainerExitedWithNonzeroExitCode) |
| 67 | + require.Equal(t, expected, xos.ErrorExitCode(err)) |
61 | 68 | } |
62 | 69 |
|
63 | 70 | func TestActionRunDockerWithEnv(t *testing.T) { |
@@ -147,78 +154,88 @@ func TestActionRunNode(t *testing.T) { |
147 | 154 | func TestActionRunSaveState(t *testing.T) { |
148 | 155 | cr := Runtime(t) |
149 | 156 |
|
| 157 | + k, v := "general", "kenobi" |
150 | 158 | uses := Uses(t, &githubactions.Metadata{ |
151 | 159 | Name: t.Name(), |
152 | 160 | Runs: &githubactions.MetadataRuns{ |
153 | 161 | Using: githubactions.RunsUsingDocker, |
154 | 162 | Image: "docker://public.ecr.aws/docker/library/alpine", |
155 | 163 | Entrypoint: "/bin/sh", |
156 | | - Args: []string{"-c", `printf 'general=kenobi\n' >> "$GITHUB_STATE"`}, |
| 164 | + Args: []string{"-c", fmt.Sprintf(`printf '%s=%s\n' >> "$%s"`, k, v, githubactions.EnvVarState)}, |
157 | 165 | }, |
158 | 166 | }) |
159 | 167 |
|
160 | 168 | gc := githubactions.NewGlobalContextFromEnv() |
161 | 169 | action := &forge.Action{Uses: uses, GlobalContext: gc} |
162 | 170 | require.NoError(t, action.Run(t.Context(), cr, forge.WithStreams(Streams(t)))) |
163 | | - require.Equal(t, "kenobi", gc.EnvContext["STATE_general"]) |
| 171 | + require.Equal(t, v, gc.EnvContext[githubactions.StateEnvVar(k)]) |
164 | 172 | } |
165 | 173 |
|
166 | 174 | func TestActionRunSetEnv(t *testing.T) { |
167 | 175 | cr := Runtime(t) |
168 | 176 |
|
| 177 | + k, v := "GENERAL", "kenobi" |
169 | 178 | uses := Uses(t, &githubactions.Metadata{ |
170 | 179 | Name: t.Name(), |
171 | 180 | Runs: &githubactions.MetadataRuns{ |
172 | 181 | Using: githubactions.RunsUsingDocker, |
173 | 182 | Image: "docker://public.ecr.aws/docker/library/alpine", |
174 | 183 | Entrypoint: "/bin/sh", |
175 | | - Args: []string{"-c", `printf 'GENERAL=kenobi\n' >> "$GITHUB_ENV"`}, |
| 184 | + Args: []string{"-c", fmt.Sprintf(`printf '%s=%s\n' >> "$%s"`, k, v, githubactions.EnvVarEnv)}, |
176 | 185 | }, |
177 | 186 | }) |
178 | 187 |
|
179 | 188 | gc := githubactions.NewGlobalContextFromEnv() |
180 | 189 | action := &forge.Action{Uses: uses, GlobalContext: gc} |
181 | 190 | require.NoError(t, action.Run(t.Context(), cr, forge.WithStreams(Streams(t)))) |
182 | | - require.Equal(t, "kenobi", gc.EnvContext["GENERAL"]) |
| 191 | + require.Equal(t, v, gc.EnvContext[k]) |
183 | 192 | } |
184 | 193 |
|
185 | 194 | func TestActionRunSetOutput(t *testing.T) { |
186 | 195 | cr := Runtime(t) |
187 | 196 |
|
| 197 | + k, v := "general", "kenobi" |
188 | 198 | uses := Uses(t, &githubactions.Metadata{ |
189 | 199 | Name: t.Name(), |
190 | 200 | Runs: &githubactions.MetadataRuns{ |
191 | 201 | Using: githubactions.RunsUsingDocker, |
192 | 202 | Image: "docker://public.ecr.aws/docker/library/alpine", |
193 | 203 | Entrypoint: "/bin/sh", |
194 | | - Args: []string{"-c", `printf 'general=kenobi\n' >> "$GITHUB_OUTPUT"`}, |
| 204 | + Args: []string{"-c", fmt.Sprintf(`printf '%s=%s\n' >> "$%s"`, k, v, githubactions.EnvVarOutput)}, |
195 | 205 | }, |
196 | 206 | }) |
197 | 207 |
|
198 | 208 | gc := githubactions.NewGlobalContextFromEnv() |
199 | 209 | action := &forge.Action{ID: "test", Uses: uses, GlobalContext: gc} |
200 | 210 | require.NoError(t, action.Run(t.Context(), cr, forge.WithStreams(Streams(t)))) |
201 | | - require.Equal(t, "kenobi", gc.StepsContext["test"].Outputs["general"]) |
| 211 | + require.Equal(t, v, gc.StepsContext[action.ID].Outputs[k]) |
202 | 212 | } |
203 | 213 |
|
204 | 214 | func TestActionRunGlobalContext(t *testing.T) { |
205 | 215 | cr := Runtime(t) |
206 | 216 |
|
| 217 | + gc := githubactions.NewGlobalContextFromEnv() |
| 218 | + gc.GitHubContext.Actor = "octocat" |
| 219 | + gc.GitHubContext.Repository = "octocat/hello-world" |
| 220 | + gc.GitHubContext.Ref = "refs/heads/main" |
207 | 221 | uses := Uses(t, &githubactions.Metadata{ |
208 | 222 | Name: "test-docker-global-context", |
209 | 223 | Runs: &githubactions.MetadataRuns{ |
210 | 224 | Using: githubactions.RunsUsingDocker, |
211 | 225 | Image: "docker://public.ecr.aws/docker/library/alpine", |
212 | 226 | Entrypoint: "/bin/sh", |
213 | | - Args: []string{"-c", `[ "$GITHUB_ACTOR" = "octocat" ] && [ "$GITHUB_REPOSITORY" = "octocat/hello-world" ] && [ "$GITHUB_REF" = "refs/heads/main" ]`}, |
| 227 | + Args: []string{ |
| 228 | + "-c", |
| 229 | + fmt.Sprintf( |
| 230 | + `[ "$%s" = "%s" ] && [ "$%s" = "%s" ] && [ "$%s" = "%s" ]`, |
| 231 | + githubactions.EnvVarActor, gc.GitHubContext.Actor, |
| 232 | + githubactions.EnvVarRepository, gc.GitHubContext.Repository, |
| 233 | + githubactions.EnvVarRef, gc.GitHubContext.Ref, |
| 234 | + ), |
| 235 | + }, |
214 | 236 | }, |
215 | 237 | }) |
216 | 238 |
|
217 | | - gc := githubactions.NewGlobalContextFromEnv() |
218 | | - gc.GitHubContext.Actor = "octocat" |
219 | | - gc.GitHubContext.Repository = "octocat/hello-world" |
220 | | - gc.GitHubContext.Ref = "refs/heads/main" |
221 | | - |
222 | 239 | action := &forge.Action{Uses: uses, GlobalContext: gc} |
223 | 240 | require.NoError(t, action.Run(t.Context(), cr, forge.WithStreams(Streams(t)))) |
224 | 241 | } |
|
0 commit comments