From d428e0add624c7bfdc49ea5d652e38299c8a4ea0 Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Sat, 22 Feb 2020 20:09:47 -0500 Subject: [PATCH 1/3] Update tests to check exit code --- __tests__/commandHandler.test.ts | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/__tests__/commandHandler.test.ts b/__tests__/commandHandler.test.ts index 6b9714ed..cd079b50 100644 --- a/__tests__/commandHandler.test.ts +++ b/__tests__/commandHandler.test.ts @@ -3,20 +3,14 @@ import { join } from "path"; import nock from "nock"; import * as core from "@actions/core"; -jest.mock("@actions/core", () => { - return { - debug: jest.fn(), - setFailed: jest.fn(), - setOutput: jest.fn(), - }; -}); - import { context } from "@actions/github"; import { CommandHandler } from "../src/commandHandler"; describe("commandHandler", () => { beforeEach(() => { + jest.spyOn(core, "setOutput"); + context.action = "run1"; context.actor = "test-user"; context.eventName = "issue_comment"; @@ -25,6 +19,10 @@ describe("commandHandler", () => { context.workflow = "Issue comments"; }); + afterEach(() => { + delete process.exitCode; + }); + describe("process", () => { it("should return false when incorrect action", async () => { context.payload = require(join(__dirname, "payloads", "edited.json")); @@ -39,6 +37,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); + expect(process.exitCode).toBe(core.ExitCode.Failure); }); it("should return false when no slash command in comment", async () => { @@ -54,6 +53,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); + expect(process.exitCode).toBe(core.ExitCode.Failure); }); it("should return false when incorrect slash command", async () => { @@ -69,6 +69,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); + expect(process.exitCode).toBe(core.ExitCode.Failure); }); it("should return false when correct slash command but incorrect repo access", async () => { @@ -90,6 +91,7 @@ describe("commandHandler", () => { .reply(200, { permission: "read" }); await expect(commandHandler.process()).resolves.toBe(false); + expect(process.exitCode).toBe(core.ExitCode.Failure); expect(scoped.isDone()).toBe(true); }); @@ -115,6 +117,7 @@ describe("commandHandler", () => { .reply(201); await expect(commandHandler.process()).resolves.toBe(true); + expect(process.exitCode).toBeUndefined(); expect(permissionScope.isDone()).toBe(true); expect(reactionScope.isDone()).toBe(false); @@ -141,6 +144,7 @@ describe("commandHandler", () => { .reply(201); await expect(commandHandler.process()).resolves.toBe(true); + expect(process.exitCode).toBeUndefined(); expect(permissionScope.isDone()).toBe(true); expect(reactionScope.isDone()).toBe(true); @@ -165,6 +169,7 @@ describe("commandHandler", () => { .reply(200, { permission: "write" }); await expect(commandHandler.process()).resolves.toBe(true); + expect(process.exitCode).toBeUndefined(); expect(scoped.isDone()).toBe(true); @@ -187,6 +192,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(true); + expect(process.exitCode).toBeUndefined(); }); it("returns 'true' when action is 'edited' and allowEdits is 'true'", () => { @@ -202,6 +208,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(true); + expect(process.exitCode).toBeUndefined(); }); it("returns 'false' when action is 'edited' and allowEdits is 'false'", () => { @@ -217,6 +224,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(false); + expect(process.exitCode).toBe(core.ExitCode.Failure); }); it("returns 'false' when action is 'deleted'", () => { @@ -232,6 +240,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(false); + expect(process.exitCode).toBeUndefined(); }); it("returns 'false' when action is unknown", () => { @@ -247,6 +256,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(false); + expect(process.exitCode).toBeUndefined(); }); }); @@ -271,6 +281,7 @@ describe("commandHandler", () => { expect(scoped.isDone()).toBe(true); expect(result).toBe("admin"); + expect(process.exitCode).toBeUndefined(); }); }); @@ -290,6 +301,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.createReaction(123)).resolves.toBe(false); + expect(process.exitCode).toBeUndefined(); }); it("creates reaction if enabled", async () => { @@ -307,6 +319,7 @@ describe("commandHandler", () => { .reply(201); await expect(commandHandler.createReaction(123)).resolves.toBe(true); + expect(process.exitCode).toBeUndefined(); expect(scoped.isDone()).toBe(true); }); From 16ef1e3132546e4064bf8e12230dbbb72f37ebc8 Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Thu, 13 Feb 2020 13:52:14 -0500 Subject: [PATCH 2/3] Remove unused variable --- __tests__/commandHandler.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/__tests__/commandHandler.test.ts b/__tests__/commandHandler.test.ts index cd079b50..75fec7e2 100644 --- a/__tests__/commandHandler.test.ts +++ b/__tests__/commandHandler.test.ts @@ -73,8 +73,6 @@ describe("commandHandler", () => { }); it("should return false when correct slash command but incorrect repo access", async () => { - const mockedSetOutput = core.setOutput as jest.Mock; - context.payload = require(join(__dirname, "payloads", "created.json")); const commandHandler = new CommandHandler( From c20683a24e2042ab33f661a95d6bd8819dc0a7fe Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Thu, 13 Feb 2020 13:42:39 -0500 Subject: [PATCH 3/3] Use debug so the action doesn't fail the run --- __tests__/commandHandler.test.ts | 8 ++++---- src/commandHandler.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/__tests__/commandHandler.test.ts b/__tests__/commandHandler.test.ts index 75fec7e2..7d43d6d2 100644 --- a/__tests__/commandHandler.test.ts +++ b/__tests__/commandHandler.test.ts @@ -37,7 +37,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); - expect(process.exitCode).toBe(core.ExitCode.Failure); + expect(process.exitCode).toBeUndefined(); }); it("should return false when no slash command in comment", async () => { @@ -53,7 +53,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); - expect(process.exitCode).toBe(core.ExitCode.Failure); + expect(process.exitCode).toBeUndefined(); }); it("should return false when incorrect slash command", async () => { @@ -69,7 +69,7 @@ describe("commandHandler", () => { ); await expect(commandHandler.process()).resolves.toBe(false); - expect(process.exitCode).toBe(core.ExitCode.Failure); + expect(process.exitCode).toBeUndefined(); }); it("should return false when correct slash command but incorrect repo access", async () => { @@ -222,7 +222,7 @@ describe("commandHandler", () => { ); expect(commandHandler.shouldRunForAction()).toBe(false); - expect(process.exitCode).toBe(core.ExitCode.Failure); + expect(process.exitCode).toBeUndefined(); }); it("returns 'false' when action is 'deleted'", () => { diff --git a/src/commandHandler.ts b/src/commandHandler.ts index 9f5ab6e4..4e01a5ac 100644 --- a/src/commandHandler.ts +++ b/src/commandHandler.ts @@ -46,7 +46,7 @@ export class CommandHandler { const commandResults = this.command.checkComment(comment.body); if (!commandResults) { - setFailed("Comment didn't contain a valid slash command"); + debug("Comment didn't contain a valid slash command"); return false; } @@ -89,7 +89,7 @@ export class CommandHandler { return true; } - setFailed("Comment was edited and allow edits is disabled, no action to take"); + debug("Comment was edited and allow edits is disabled, no action to take"); return false; }