Skip to content

Commit

Permalink
build: fix npm package conflicts (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegocam authored Oct 7, 2024
1 parent e59b907 commit 0f39279
Show file tree
Hide file tree
Showing 9 changed files with 1,031 additions and 2,568 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
node_modules
.vscode
145 changes: 51 additions & 94 deletions lib/handle-pull-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,78 @@
import mockDate from "mockdate";
import timezoneMock from "timezone-mock";
import { describe, test, expect, vi, afterAll, beforeAll } from "vitest";
import { mockProcessStdout } from "vitest-mock-process";
import { describe, test, expect, afterEach, beforeEach, vi } from "vitest";
import {
generatePullRequestWebhook,
cleanupWebhooksFolder,
setupWebhooksFolder,
} from "../test/utils";
import handlePullRequest from "./handle-pull-request";
import * as comment from "./comment";
import stdMocks from "std-mocks";

timezoneMock.register("UTC");
mockDate.set("2022-06-10T00:00:00.000Z");

describe("handlePullRequest", () => {
beforeAll(() => {
setupWebhooksFolder();
});
beforeEach(() => {
stdMocks.use();
setupWebhooksFolder();
});

afterAll(() => {
cleanupWebhooksFolder();
});
afterEach(() => {
stdMocks.restore();
cleanupWebhooksFolder();
});

describe("handlePullRequest", () => {
test("closed pull request", async () => {
const mockStdout = mockProcessStdout();
const eventPath = generatePullRequestWebhook({ state: "closed" });
process.env.GITHUB_EVENT_PATH = eventPath;

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request closed for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
["Pull request already closed, ignoring\n"],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request closed for https://github.com/gr2m/merge-schedule-action/pull/2\n",
"Pull request already closed, ignoring\n",
]);
});

test("fork pull request", async () => {
const mockStdout = mockProcessStdout();
const eventPath = generatePullRequestWebhook({ fork: true });
process.env.GITHUB_EVENT_PATH = eventPath;

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
["::error::Setting a scheduled merge is not allowed from forks\n"],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
"::error::Setting a scheduled merge is not allowed from forks\n",
]);
});

test("no schedule command", async () => {
const mockStdout = mockProcessStdout();
const eventPath = generatePullRequestWebhook();
process.env.GITHUB_EVENT_PATH = eventPath;

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
["No /schedule command found\n"],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
"No /schedule command found\n",
]);
});

test("no schedule command with previous commit", async () => {
const mockStdout = mockProcessStdout();
const eventPath = generatePullRequestWebhook({ number: 3 });
process.env.GITHUB_EVENT_PATH = eventPath;

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/3\n",
],
["No /schedule command found\n"],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/3\n",
"No /schedule command found\n",
]);
});

test("invalid date", async () => {
const mockStdout = mockProcessStdout();
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule bad-date",
Expand All @@ -92,25 +81,20 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
[`Schedule date found: "bad-date"\n`],
[
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Schedule date found: "bad-date"\n`,
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(`
":x: **Merge Schedule**
\\"bad-date\\" is not a valid date
"bad-date" is not a valid date
<!-- Merge Schedule Pull Request Comment -->"
`);
});

test("date in the past", async () => {
const mockStdout = mockProcessStdout();
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-08",
Expand All @@ -119,14 +103,10 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
[`Schedule date found: "2022-06-08"\n`],
[
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Schedule date found: "2022-06-08"\n`,
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(`
Expand All @@ -137,7 +117,6 @@ describe("handlePullRequest", () => {
});

test("date in the past - custom time zone", async () => {
const mockStdout = mockProcessStdout();
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-08",
Expand All @@ -147,14 +126,10 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
[`Schedule date found: "2022-06-08"\n`],
[
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Schedule date found: "2022-06-08"\n`,
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(`
Expand All @@ -165,7 +140,6 @@ describe("handlePullRequest", () => {
});

test("schedule merge", async () => {
const mockStdout = mockProcessStdout();
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-12",
Expand All @@ -174,14 +148,10 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
[`Schedule date found: "2022-06-12"\n`],
[
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Schedule date found: "2022-06-12"\n`,
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(`
Expand All @@ -192,7 +162,6 @@ describe("handlePullRequest", () => {
});

test("schedule merge with previous commit", async () => {
const mockStdout = mockProcessStdout();
const updateComment = vi.spyOn(comment, "updateComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-12",
Expand All @@ -202,14 +171,10 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/3\n",
],
[`Schedule date found: "2022-06-12"\n`],
[
`Comment updated: https://github.com/gr2m/merge-schedule-action/issues/3#issuecomment-31\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/3\n",
`Schedule date found: "2022-06-12"\n`,
`Comment updated: https://github.com/gr2m/merge-schedule-action/issues/3#issuecomment-31\n`,
]);
expect(updateComment.mock.calls).toHaveLength(1);
expect(updateComment.mock.calls[0][2]).toMatchInlineSnapshot(`
Expand All @@ -220,7 +185,6 @@ describe("handlePullRequest", () => {
});

test("schedule merge with previous commit already up to date", async () => {
const mockStdout = mockProcessStdout();
const updateComment = vi.spyOn(comment, "updateComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule 2022-06-12",
Expand All @@ -230,18 +194,15 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/4\n",
],
[`Schedule date found: "2022-06-12"\n`],
["Comment already up to date\n"],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/4\n",
`Schedule date found: "2022-06-12"\n`,
"Comment already up to date\n",
]);
expect(updateComment.mock.calls).toHaveLength(0);
});

test("schedule merge without date", async () => {
const mockStdout = mockProcessStdout();
const createComment = vi.spyOn(comment, "createComment");
const eventPath = generatePullRequestWebhook({
body: "Pull request body\n/schedule",
Expand All @@ -250,13 +211,9 @@ describe("handlePullRequest", () => {

await handlePullRequest();

expect(mockStdout.mock.calls).toEqual([
[
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
],
[
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
],
expect(stdMocks.flush().stdout).toEqual([
"Handling pull request opened for https://github.com/gr2m/merge-schedule-action/pull/2\n",
`Comment created: https://github.com/gr2m/merge-schedule-action/issues/2#issuecomment-22\n`,
]);
expect(createComment.mock.calls).toHaveLength(1);
expect(createComment.mock.calls[0][2]).toMatchInlineSnapshot(`
Expand Down
Loading

0 comments on commit 0f39279

Please sign in to comment.