From 0aca9b814481efee6f7709803c682e8252a4eabe Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Sat, 26 Apr 2025 00:32:55 +0200 Subject: [PATCH] fix: post step failure is job failure (#85) --- pkg/runner/job_executor.go | 2 +- pkg/runner/runner_test.go | 23 +++++++++++++------ .../post-step-failure/action.yml | 4 ++++ .../post-step-failure/main.js | 0 .../post-step-failure/post.js | 2 ++ .../post-step-failure-is-job-failure/push.yml | 9 ++++++++ 6 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/action.yml create mode 100644 pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/main.js create mode 100644 pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/post.js create mode 100644 pkg/runner/testdata/post-step-failure-is-job-failure/push.yml diff --git a/pkg/runner/job_executor.go b/pkg/runner/job_executor.go index 509a1997..2ed7da69 100644 --- a/pkg/runner/job_executor.go +++ b/pkg/runner/job_executor.go @@ -92,7 +92,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo return nil })) - postExec := useStepLogger(rc, stepModel, stepStagePost, step.post()) + postExec := useStepLogger(rc, stepModel, stepStagePost, step.post().ThenError(setJobError)) if postExecutor != nil { // run the post executor in reverse order postExecutor = postExec.Finally(postExecutor) diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 209c9c15..252c56d7 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -371,13 +371,24 @@ func (factory *captureJobLoggerFactory) WithJobLogger() *log.Logger { return logger } -func TestPullFailureIsJobFailure(t *testing.T) { +func TestPullAndPostStepFailureIsJobFailure(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } - tables := []TestJobFileInfo{ - {workdir, "checkout", "push", "pull failure", map[string]string{"ubuntu-latest": "localhost:0000/missing:latest"}, secrets}, + defCache := &GoGitActionCache{ + path.Clean(path.Join(workdir, "cache")), + } + + mockCache := &mockCache{} + + tables := []struct { + TestJobFileInfo + ActionCache ActionCache + SetupResult string + }{ + {TestJobFileInfo{workdir, "checkout", "push", "pull failure", map[string]string{"ubuntu-latest": "localhost:0000/missing:latest"}, secrets}, defCache, "failure"}, + {TestJobFileInfo{workdir, "post-step-failure-is-job-failure", "push", "post failure", map[string]string{"ubuntu-latest": "-self-hosted"}, secrets}, mockCache, "success"}, } for _, table := range tables { @@ -392,9 +403,7 @@ func TestPullFailureIsJobFailure(t *testing.T) { if _, err := os.Stat(eventFile); err == nil { config.EventPath = eventFile } - config.ActionCache = &GoGitActionCache{ - path.Clean(path.Join(workdir, "cache")), - } + config.ActionCache = table.ActionCache logger := log.New() logger.SetOutput(&factory.buffer) @@ -413,7 +422,7 @@ func TestPullFailureIsJobFailure(t *testing.T) { hasJobResult = true } if val, ok := entry["stepResult"]; ok && !hasStepResult { - assert.Equal(t, "failure", val) + assert.Equal(t, table.SetupResult, val) hasStepResult = true } } diff --git a/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/action.yml b/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/action.yml new file mode 100644 index 00000000..8b4b3227 --- /dev/null +++ b/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/action.yml @@ -0,0 +1,4 @@ +runs: + using: node20 + main: main.js + post: post.js \ No newline at end of file diff --git a/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/main.js b/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/main.js new file mode 100644 index 00000000..e69de29b diff --git a/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/post.js b/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/post.js new file mode 100644 index 00000000..e2e1887b --- /dev/null +++ b/pkg/runner/testdata/post-step-failure-is-job-failure/post-step-failure/post.js @@ -0,0 +1,2 @@ +console.log('This is a post step failure test'); +process.exit(1); \ No newline at end of file diff --git a/pkg/runner/testdata/post-step-failure-is-job-failure/push.yml b/pkg/runner/testdata/post-step-failure-is-job-failure/push.yml new file mode 100644 index 00000000..da791764 --- /dev/null +++ b/pkg/runner/testdata/post-step-failure-is-job-failure/push.yml @@ -0,0 +1,9 @@ +name: basic +on: push + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ./post-step-failure-is-job-failure/post-step-failure \ No newline at end of file