|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import test from "ava"; |
| 3 | +import sinon from "sinon"; |
| 4 | + |
| 5 | +import * as common from "./action-common"; |
| 6 | +import * as actionsUtil from "./actions-util"; |
| 7 | +import * as environment from "./environment"; |
| 8 | +import * as logging from "./logging"; |
| 9 | +import { ActionName } from "./status-report"; |
| 10 | +import * as statusReport from "./status-report"; |
| 11 | +import { |
| 12 | + getTestActionsEnv, |
| 13 | + getTestEnv, |
| 14 | + makeMacro, |
| 15 | + RecordingLogger, |
| 16 | + setupTests, |
| 17 | +} from "./testing-utils"; |
| 18 | +import { getErrorMessage } from "./util"; |
| 19 | + |
| 20 | +setupTests(test); |
| 21 | + |
| 22 | +interface RunInActionsTestOpts { |
| 23 | + runFn?: () => Promise<any>; |
| 24 | + expectedErrorMessage?: string; |
| 25 | + expectedTelemetryError?: string; |
| 26 | +} |
| 27 | + |
| 28 | +const runInActionsMacro = makeMacro({ |
| 29 | + exec: async (t, opts: RunInActionsTestOpts) => { |
| 30 | + const expectFailure = opts?.expectedErrorMessage !== undefined; |
| 31 | + |
| 32 | + const logger = new RecordingLogger(); |
| 33 | + const getActionsLogger = sinon |
| 34 | + .stub(logging, "getActionsLogger") |
| 35 | + .returns(logger); |
| 36 | + |
| 37 | + const env = getTestEnv(); |
| 38 | + const getEnv = sinon.stub(environment, "getEnv").returns(env); |
| 39 | + |
| 40 | + const actionsEnv = getTestActionsEnv(env); |
| 41 | + const getActionsEnv = sinon |
| 42 | + .stub(actionsUtil, "getActionsEnv") |
| 43 | + .returns(actionsEnv); |
| 44 | + |
| 45 | + const getJobUUID = sinon |
| 46 | + .stub(statusReport, "getJobUUID") |
| 47 | + .returns("test-job-uuid"); |
| 48 | + |
| 49 | + const setFailed = sinon.stub(core, "setFailed"); |
| 50 | + const sendUnhandledErrorStatusReport = sinon.stub( |
| 51 | + statusReport, |
| 52 | + "sendUnhandledErrorStatusReport", |
| 53 | + ); |
| 54 | + |
| 55 | + const name = ActionName.Init; |
| 56 | + const run = sinon.stub(); |
| 57 | + |
| 58 | + if (opts?.runFn) { |
| 59 | + run.callsFake(opts.runFn); |
| 60 | + } |
| 61 | + |
| 62 | + const transformTelemetryError = sinon |
| 63 | + .stub() |
| 64 | + .callsFake((err) => opts?.expectedTelemetryError ?? getErrorMessage(err)); |
| 65 | + const testAction: common.Action = { |
| 66 | + name, |
| 67 | + run, |
| 68 | + transformTelemetryError, |
| 69 | + }; |
| 70 | + |
| 71 | + await common.runInActions(testAction); |
| 72 | + |
| 73 | + // These always should have been called once. |
| 74 | + t.true(getActionsLogger.calledOnce); |
| 75 | + t.true(getEnv.calledOnce); |
| 76 | + t.true(getActionsEnv.calledOnce); |
| 77 | + |
| 78 | + const expectedActionState = { |
| 79 | + actions: actionsEnv, |
| 80 | + env, |
| 81 | + logger, |
| 82 | + name: ActionName.Init, |
| 83 | + }; |
| 84 | + |
| 85 | + t.true(getJobUUID.calledOnceWithExactly(sinon.match(expectedActionState))); |
| 86 | + t.true(run.calledOnceWithExactly(sinon.match(expectedActionState))); |
| 87 | + |
| 88 | + t.is(setFailed.calledOnce, expectFailure ?? false); |
| 89 | + t.is(sendUnhandledErrorStatusReport.calledOnce, expectFailure ?? false); |
| 90 | + |
| 91 | + if (expectFailure) { |
| 92 | + t.true( |
| 93 | + setFailed.calledOnceWithExactly( |
| 94 | + `${statusReport.getDisplayActionName(name)} action failed: ${opts?.expectedErrorMessage}`, |
| 95 | + ), |
| 96 | + ); |
| 97 | + t.true( |
| 98 | + sendUnhandledErrorStatusReport.calledOnceWithExactly( |
| 99 | + name, |
| 100 | + sinon.match.any, |
| 101 | + opts?.expectedTelemetryError ?? opts?.expectedErrorMessage, |
| 102 | + logger, |
| 103 | + ), |
| 104 | + ); |
| 105 | + } |
| 106 | + }, |
| 107 | + title: (providedTitle) => `runInActions - ${providedTitle}`, |
| 108 | +}); |
| 109 | + |
| 110 | +runInActionsMacro.serial("calls run", {}); |
| 111 | +runInActionsMacro.serial("handles run exceptions", { |
| 112 | + runFn: () => { |
| 113 | + throw new Error("Test failure"); |
| 114 | + }, |
| 115 | + expectedErrorMessage: "Test failure", |
| 116 | +}); |
| 117 | +runInActionsMacro.serial("transforms run exceptions", { |
| 118 | + runFn: () => { |
| 119 | + throw new Error("Test failure"); |
| 120 | + }, |
| 121 | + expectedErrorMessage: "Test failure", |
| 122 | + expectedTelemetryError: "Transformed failure message", |
| 123 | +}); |
0 commit comments