Skip to content

Commit

Permalink
feat: implement jest tests and add increment_build attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Thauan committed Jan 19, 2025
1 parent e2d4f41 commit 1d4a810
Show file tree
Hide file tree
Showing 9 changed files with 2,519 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/increment-pubspec-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ on:

jobs:
increment-version:
if: github.event.pull_request.merged == true || github.event_name == 'push'
if: |
(github.event_name == 'push' && github.event.head_commit.message != 'Merge pull request') ||
(github.event.pull_request.merged == true)
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test and Validate

on:
pull_request:
branches:
- main
- "chore/**"
- "feat/**"
- "fix/**"
- "docs/**"
- "refactor/**"
- "test/**"
push:
branches:
- main
- "chore/**"
- "feat/**"
- "fix/**"
- "docs/**"
- "refactor/**"
- "test/**"

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: "Increment Pubspec Version"
description: "Increments the version in pubspec.yaml based on PR labels or commit messages"
author: "Thauan (https://github.com/Thauan)"
inputs:
increment_build:
description: "Defines whether the build number should be incremented"
required: false
default: "true"
enable_on_commit:
description: "Enable the functionality on commit events"
required: false
Expand Down
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
moduleNameMapper: {
"^@src/(.*)$": "<rootDir>/src/$1",
},
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "ncc build src/index.ts -o dist"
"build": "ncc build src/index.ts -o dist",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^6.0.0",
"@types/jest": "^29.5.14",
"fs": "^0.0.1-security",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"yaml": "^2.7.0"
},
"devDependencies": {
Expand Down
275 changes: 275 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import { jest } from "@jest/globals";
import { getInput, setFailed, setOutput, warning } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { run, incrementVersion, getIncrementTypeFromCommits } from "./index";
import * as fs from "fs";
import * as yaml from "yaml";

jest.mock("@actions/core", () => ({
getInput: jest.fn(),
setFailed: jest.fn(),
setOutput: jest.fn(),
warning: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
error: jest.fn(),
}));

jest.mock("@actions/github", () => ({
context: {
payload: {
pull_request: undefined,
},
repo: {
owner: "owner",
repo: "repo",
},
},
getOctokit: jest.fn(),
}));

jest.mock("fs", () => ({
existsSync: jest.fn(),
readFileSync: jest.fn(),
writeFileSync: jest.fn(),
promises: {
readFile: jest.fn(),
writeFile: jest.fn(),
},
}));

jest.mock("child_process");

describe("Increment Pubspec Version Action", () => {
beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
jest.resetAllMocks();
});

it("should fail if it's not a Pull Request event", async () => {
context.eventName = "pull_request";
context.payload.pull_request = undefined;

await run();

expect(setFailed).toHaveBeenCalledWith("This event is not a Pull Request.");
});

it("should increment the version to patch when the 'patch' label is present in the PR", async () => {
(getInput as jest.Mock) = jest.fn().mockImplementation((input) => {
if (input === "increment_build") return "true";
return undefined;
});

context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "patch" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0+1" })
);

await run();

expect(getInput).toHaveBeenCalledWith("increment_build");
expect(setOutput).toHaveBeenCalledWith("new_version", "1.0.1+2");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 1.0.1+2"),
"utf8"
);
});

it("should increment the version to patch when the 'minor' label is present in the PR", async () => {
(getInput as jest.Mock) = jest.fn().mockImplementation((input) => {
if (input === "increment_build") return "true";
return undefined;
});

context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "minor" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0+20" })
);

await run();

expect(setOutput).toHaveBeenCalledWith("new_version", "1.1.0+21");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 1.1.0+21"),
"utf8"
);
});

it("should increment the version to patch when the 'major' label is present in the PR", async () => {
(getInput as jest.Mock) = jest.fn().mockImplementation((input) => {
if (input === "increment_build") return "true";
return undefined;
});

context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "major" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0+2" })
);

await run();

expect(setOutput).toHaveBeenCalledWith("new_version", "2.0.0+3");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 2.0.0+3"),
"utf8"
);
});

it("should increment the version to patch when the 'major' label is present in the PR", async () => {
context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "major" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0+2" })
);

await run();

expect(setOutput).toHaveBeenCalledWith("new_version", "2.0.0+2");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 2.0.0+2"),
"utf8"
);
});

it("should increment the version to patch when the 'major' when not contain build number, but increment_build is true", async () => {
(getInput as jest.Mock) = jest.fn().mockImplementation((input) => {
if (input === "increment_build") return "true";
return undefined;
});

context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "major" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0" })
);

await run();

expect(setOutput).toHaveBeenCalledWith("new_version", "2.0.0+1");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 2.0.0+1"),
"utf8"
);
});

it("should increment the version to patch when the 'major' when increment_build is false", async () => {
context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "major" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(
yaml.stringify({ version: "1.0.0" })
);

await run();

expect(setOutput).toHaveBeenCalledWith("new_version", "2.0.0");
expect(fs.writeFileSync).toHaveBeenCalledWith(
"./pubspec.yaml",
expect.stringContaining("version: 2.0.0"),
"utf8"
);
});

it("should add label to the pull request", async () => {
(getInput as jest.Mock).mockReturnValueOnce("gh-token-value");
(getInput as jest.Mock).mockReturnValueOnce("label-value");
(context as any).payload.pull_request = {
number: 1,
};

await run();

expect(getInput).toHaveBeenCalledWith("enable_on_commit");
expect(getOctokit).not.toHaveBeenCalled();
expect(setFailed).not.toHaveBeenCalled();
});

it("should fail if the pubspec.yaml file doesn't contain a version", async () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(yaml.stringify({}));

await incrementVersion(["patch"]);

expect(setFailed).toHaveBeenCalledWith(
"Current version not found in pubspec.yaml."
);
});

it("Should warn if there are no valid labels in the PR", async () => {
context.eventName = "pull_request";
context.payload.pull_request = {
number: 1,
labels: [{ name: "invalid-label" }],
};

await run();

expect(warning).toHaveBeenCalledWith(
"No valid labels found. No action will be taken."
);
});

it("Should return no increment if there are no keywords in the commits", async () => {
context.eventName = "push";
context.payload.commits = [{ message: "fix: atualiza documentação" }];

const incrementType = await getIncrementTypeFromCommits();

expect(incrementType).toBeNull();
});

it("Deve falhar se o arquivo pubspec.yaml não existir", async () => {
context.eventName = "pull_request";
context.payload.pull_request = {
number: 2,
labels: [{ name: "patch" }],
};

(fs.existsSync as jest.Mock).mockReturnValue(false);

await run();

expect(setFailed).toHaveBeenCalledWith("File ./pubspec.yaml not found.");
});
});
Loading

0 comments on commit 1d4a810

Please sign in to comment.