Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests #5

Merged
merged 9 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jest.jestCommandLine": "yarn jest",
"jest.rootPath": "src"
}
22 changes: 15 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31693,13 +31693,13 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const artifacts_1 = __nccwpck_require__(1870);
const tags_1 = __nccwpck_require__(7816);
const create_git_1 = __nccwpck_require__(6704);
const temporary_branch_1 = __nccwpck_require__(2786);
const create_git_1 = __nccwpck_require__(6704);
const configuration_1 = __nccwpck_require__(5778);
async function main() {
const configuration = new configuration_1.Configuration(core.getInput.bind(core));
const git = (0, create_git_1.createGit)();
const tags = new tags_1.Tags();
const tags = new tags_1.Tags(git);
const artifacts = new artifacts_1.Artifacts(git, tags, configuration);
const temporaryBranch = new temporary_branch_1.TemporaryBranch(git);
Promise.resolve()
Expand Down Expand Up @@ -31797,7 +31797,9 @@ class Artifacts {
async push() {
const pushingResult = await this.git.push();
const messages = pushingResult.remoteMessages.all.join('\n');
messages && core.info(`Pushed artifacts with messages: ${messages}`);
if (messages) {
core.info(`Pushed artifacts with messages: ${messages}`);
}
}
}
exports.Artifacts = Artifacts;
Expand Down Expand Up @@ -31835,17 +31837,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Tags = void 0;
const create_git_1 = __nccwpck_require__(6704);
const core = __importStar(__nccwpck_require__(2186));
class Tags {
git;
tags = [];
git = (0, create_git_1.createGit)();
constructor(git) {
this.git = git;
}
async collect() {
this.tags = (await this.git.tags(['--contains'])).all;
core.info(`Collecting tags: ${this.toString()}`);
}
async move() {
core.info(`Moving tags: ${this.toString()}`);
if (!this.tags.length) {
return;
}
await this.remove();
await this.create();
}
Expand All @@ -31868,7 +31875,9 @@ class Tags {
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
pushInfo(result, message) {
const messages = result.remoteMessages.all.join('\n');
messages && core.info(`${message}: ${messages}`);
if (messages) {
core.info(`${message}: ${messages}`);
}
}
}
exports.Tags = Tags;
Expand Down Expand Up @@ -31911,7 +31920,6 @@ class TemporaryBranch {
git;
constructor(git) {
this.git = git;
this.git = git;
}
async create() {
const _isDetached = await this.isDetached();
Expand Down
12 changes: 12 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
clearMocks: true,
preset: 'ts-jest',
moduleDirectories: ['node_modules'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'^@model/(.*)$': '<rootDir>/src/model/$1',
},
setupFilesAfterEnv: ['<rootDir>/tests/setup-tests.ts'],
maxWorkers: 8,
};
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
"simple-git": "~3.22.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@total-typescript/shoehorn": "^0.1.0",
"@types/node": "~20.11.25",
"@typescript-eslint/eslint-plugin": "~7.1.1",
"@typescript-eslint/parser": "~7.1.1",
"@vercel/ncc": "~0.38.1",
"eslint": "~8.57.0",
"husky": "~9.0.11",
"jest": "^29.7.0",
"prettier": "~3.2.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "~5.3.0"
},
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as core from '@actions/core';
import { Artifacts } from './model/artifacts';
import { Tags } from './model/tags';
import { Artifacts } from '@model/artifacts';
import { Tags } from '@model/tags';
import { TemporaryBranch } from '@model/temporary-branch';
import { createGit } from './create-git';
import { TemporaryBranch } from './model/temporary-branch';
import { Configuration } from './configuration';

async function main(): Promise<void> {
const configuration = new Configuration(core.getInput.bind(core));

const git = createGit();
const tags = new Tags();
const tags = new Tags(git);
const artifacts = new Artifacts(git, tags, configuration);
const temporaryBranch = new TemporaryBranch(git);

Expand Down
7 changes: 5 additions & 2 deletions src/model/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SimpleGit } from 'simple-git';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import type { Tags } from './tags';
import type { Configuration } from '../configuration';
import type { Configuration } from '@/configuration';
widoz marked this conversation as resolved.
Show resolved Hide resolved

export class Artifacts {
constructor(
Expand Down Expand Up @@ -59,6 +59,9 @@ export class Artifacts {
private async push(): Promise<void> {
const pushingResult = await this.git.push();
const messages = pushingResult.remoteMessages.all.join('\n');
messages && core.info(`Pushed artifacts with messages: ${messages}`);

if (messages) {
core.info(`Pushed artifacts with messages: ${messages}`);
}
}
}
14 changes: 11 additions & 3 deletions src/model/tags.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { PushResult, SimpleGit } from 'simple-git';
import { createGit } from '../create-git';
import * as core from '@actions/core';

export class Tags {
private tags: Array<string> = [];
private readonly git: SimpleGit = createGit();

constructor(private readonly git: Readonly<SimpleGit>) {}

public async collect(): Promise<void> {
this.tags = (await this.git.tags(['--contains'])).all;
Expand All @@ -13,6 +13,11 @@ export class Tags {

public async move(): Promise<void> {
core.info(`Moving tags: ${this.toString()}`);

if (!this.tags.length) {
return;
}

await this.remove();
await this.create();
}
Expand Down Expand Up @@ -40,6 +45,9 @@ export class Tags {
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
private pushInfo(result: Readonly<PushResult>, message: string): void {
const messages = result.remoteMessages.all.join('\n');
messages && core.info(`${message}: ${messages}`);

if (messages) {
core.info(`${message}: ${messages}`);
}
}
}
4 changes: 1 addition & 3 deletions src/model/temporary-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import type { PushResult, SimpleGit } from 'simple-git';
import * as core from '@actions/core';

export class TemporaryBranch {
constructor(private readonly git: Readonly<SimpleGit>) {
this.git = git;
}
constructor(private readonly git: Readonly<SimpleGit>) {}

async create(): Promise<void> {
const _isDetached = await this.isDetached();
Expand Down
Empty file added tests/setup-tests.ts
Empty file.
83 changes: 83 additions & 0 deletions tests/unit/model/artifacts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { SimpleGit } from 'simple-git';
import type { Tags } from '@model/tags';
import type { getInput } from '@actions/core';

import { it, jest, describe, expect } from '@jest/globals';
import { exec } from '@actions/exec';
import { fromPartial } from '@total-typescript/shoehorn';
import { Configuration } from '@/configuration';
import { Artifacts } from '@model/artifacts';

jest.mock('@actions/exec', () => ({
__esModule: true,
exec: jest.fn(),
}));

jest.mock('@model/tags', () => ({
__esModule: true,
Tags: {
collect: jest.fn(),
move: jest.fn(),
},
}));

describe('Artifacts', () => {
it('Compile the assets and Deploy when finished', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push: jest.fn(() =>
Promise.resolve({
remoteMessages: {
all: [''],
},
})
),
});
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });

jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));

const artifacts = new Artifacts(git, tags, configuration());

await artifacts.update();

expect(jest.mocked(exec)).toHaveBeenNthCalledWith(1, 'yarn build');
expect(jest.mocked(exec)).toHaveBeenNthCalledWith(2, 'git add -f ./build/*');
});

it('Throw an error when failing to compile', async () => {
jest.mocked(exec).mockImplementation(async () => Promise.resolve(1));
const artifacts = new Artifacts(
fromPartial<SimpleGit>({}),
fromPartial<Tags>({}),
configuration()
);
await expect(artifacts.update()).rejects.toThrow(
'Failed creating artifacts: Failing to compile artifacts. Process exited with non-zero code.'
);
});

it('Throw an error when failing to git-add', async () => {
jest.mocked(exec).mockImplementation(async (command) => (command === 'yarn build' ? 0 : 1));
const artifacts = new Artifacts(
fromPartial<SimpleGit>({}),
fromPartial<Tags>({}),
configuration()
);
await expect(artifacts.update()).rejects.toThrow(
'Failed creating artifacts: Failing to git-add the artifacts build. Process exited with non-zero code.'
);
});
widoz marked this conversation as resolved.
Show resolved Hide resolved
});

function configuration(): Configuration {
return new Configuration(stubGetInput());
}

function stubGetInput(): typeof getInput {
return jest.fn((name: string): string => {
return name === 'command' ? 'yarn build' : './build';
});
}
48 changes: 48 additions & 0 deletions tests/unit/model/tags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { SimpleGit } from 'simple-git';

import { describe, it, expect, jest } from '@jest/globals';
import { fromPartial } from '@total-typescript/shoehorn';
import { Tags } from '@model/tags';

describe('Tags', () => {
it('Can move the collected tags', async () => {
const git = fromPartial<SimpleGit>({
tags: jest.fn(() => Promise.resolve({ all: ['tag1', 'tag2'] })),
tag: jest.fn(),
addTag: jest.fn(),
push: jest.fn(() => Promise.resolve({ remoteMessages: { all: [''] } })),
pushTags: jest.fn(() => Promise.resolve({ remoteMessages: { all: [''] } })),
});

const tags = new Tags(git);

await tags.collect();
await tags.move();

expect(git.tag).toHaveBeenCalledWith(['-d', 'tag1', 'tag2']);
expect(git.push).toHaveBeenCalledWith(['--delete', 'origin', 'tag1', 'tag2']);
expect(git.addTag).toHaveBeenCalledWith('tag1');
expect(git.addTag).toHaveBeenCalledWith('tag2');
expect(git.pushTags).toHaveBeenCalled();
});

it('Do not move tags if there are no collected tags', async () => {
const git = fromPartial<SimpleGit>({
tags: jest.fn(() => Promise.resolve({ all: [] })),
tag: jest.fn(),
addTag: jest.fn(),
push: jest.fn(),
pushTags: jest.fn(),
});

const tags = new Tags(git);

await tags.collect();
await tags.move();

expect(git.tag).not.toHaveBeenCalled();
expect(git.push).not.toHaveBeenCalled();
expect(git.addTag).not.toHaveBeenCalled();
expect(git.pushTags).not.toHaveBeenCalled();
});
});
Loading