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 Integration Test #150

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN yarn install --production
COPY --from=build /app/dist ./dist
RUN npm link

ENTRYPOINT ["/app/dist/app.js"]
ENTRYPOINT ["/app/dist/index.js"]
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module.exports = {
roots: ['<rootDir>/src'],
testEnvironment: 'node',
roots: ['<rootDir>/src', '<rootDir>/tests'],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
moduleNameMapper: {
axios: 'axios/dist/node/axios.cjs',
},
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepublishOnly": "yarn build"
},
"bin": {
"codecoach": "dist/app.js"
"codecoach": "dist/index.js"
},
"files": [
"dist/**/*"
Expand All @@ -34,13 +34,15 @@
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"axios": "^1.4.0",
"eslint": "^7.15.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-sonarjs": "^0.5.0",
"jest": "^26.4.0",
"nodemon": "^2.0.6",
"prettier": "^2.2.1",
"testcontainers": "^9.12.0",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"typescript": "^4.1.2"
Expand Down
7 changes: 7 additions & 0 deletions src/CodeCoachError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class CodeCoachError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, CodeCoachError.prototype);
this.name = 'CodeCoachError';
}
}
19 changes: 7 additions & 12 deletions src/Config/Config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BuildLogFile } from './@types';
import { ConfigParser } from './Config';

const mockGitHubRepo = 'https://github.com/codeleague/codecoach.git';
const mockGitHubPr = 42;
Expand Down Expand Up @@ -70,8 +71,7 @@ describe('Config parsing Test', () => {
};

it('should be able to parse GitHub config provided by environment variables', async () => {
process.argv = GITHUB_ENV_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(GITHUB_ENV_ARGS);
expect(config.vcs).toBe('github');
expect(config.githubRepoUrl).toBe(mockGitHubRepo);
expect(config.githubPr).toBe(mockGitHubPr);
Expand All @@ -84,8 +84,7 @@ describe('Config parsing Test', () => {
});

it('should be able to parse GitHub config provided by file', async () => {
process.argv = GITHUB_FILE_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(GITHUB_FILE_ARGS);
expect(config.vcs).toBe('github');
expect(config.githubRepoUrl).toBe(mockGitHubRepo);
expect(config.githubPr).toBe(mockGitHubPr);
Expand All @@ -98,8 +97,7 @@ describe('Config parsing Test', () => {
});

it('should be able to parse GitLab config provided by environment variables', async () => {
process.argv = GITLAB_ENV_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(GITLAB_ENV_ARGS);
expect(config.vcs).toBe('gitlab');
expect(config.gitlabHost).toBe(mockGitLabHost);
expect(config.gitlabProjectId).toBe(mockGitLabProjectId);
Expand All @@ -113,8 +111,7 @@ describe('Config parsing Test', () => {
});

it('should be able to parse GitLab config provided by file', async () => {
process.argv = GITLAB_FILE_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(GITLAB_FILE_ARGS);
expect(config.vcs).toBe('gitlab');
expect(config.gitlabHost).toBe(mockGitLabHost);
expect(config.gitlabProjectId).toBe(mockGitLabProjectId);
Expand All @@ -128,16 +125,14 @@ describe('Config parsing Test', () => {
});

it('should be able to parse dryRun config provided by environment variables', async () => {
process.argv = DRYRUN_ENV_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(DRYRUN_ENV_ARGS);
expect(config.dryRun).toBe(true);

validateBuildLog(config.buildLogFile);
});

it('should be able to parse dryRun config provided by file', async () => {
process.argv = DRYRUN_FILE_ARGS;
const config = (await import('./Config')).configs;
const config = ConfigParser(DRYRUN_FILE_ARGS);
expect(config.dryRun).toBe(true);

validateBuildLog(config.buildLogFile);
Expand Down
6 changes: 3 additions & 3 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ and <cwd> is build root directory (optional (Will use current context as cwd)).
})
.strict()
.help()
.wrap(120)
.parse(process.argv.slice(1)) as ConfigArgument;
.wrap(120);

export const configs = args;
export const ConfigParser = (argv: string[]): ConfigArgument =>
args.parse(argv) as ConfigArgument;
2 changes: 1 addition & 1 deletion src/Config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { configs } from './Config';
export { ConfigParser } from './Config';
export * from './@types';
export * from './@enums';
54 changes: 35 additions & 19 deletions src/Provider/GitLab/GitLabMRService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import {
import * as Resources from '@gitbeaker/core';
import { Gitlab } from '@gitbeaker/rest';

import { configs } from '../../Config';

export class GitLabMRService implements IGitLabMRService {
private readonly projectId: number;
private readonly mrIid: number;
private readonly api: Resources.Gitlab;
private readonly gitlabHost: string;
private readonly gitlabProjectId: number;
private readonly gitlabMrIid: number;
private readonly gitlabToken: string;
private readonly api: Resource.Gitlab;

constructor() {
this.projectId = configs.gitlabProjectId;
this.mrIid = configs.gitlabMrIid;
constructor(
gitlabHost: string,
gitlabProjectId: number,
gitlabMrIid: number,
gitlabToken: string,
) {
this.gitlabHost = gitlabHost;
this.gitlabProjectId = gitlabProjectId;
this.gitlabMrIid = gitlabMrIid;
this.gitlabToken = gitlabToken;

this.api = new Gitlab({
host: configs.gitlabHost,
token: configs.gitlabToken,
host: this.gitlabHost,
token: this.gitlabToken,
});
}

Expand All @@ -43,9 +50,14 @@ export class GitLabMRService implements IGitLabMRService {
newLine: line.toString(),
};

await this.api.MergeRequestDiscussions.create(this.projectId, this.mrIid, body, {
position,
});
await this.api.MergeRequestDiscussions.create(
this.gitlabProjectId,
this.gitlabMrIid,
body,
{
position,
},
);
}

async getCurrentUserId(): Promise<number> {
Expand All @@ -54,20 +66,24 @@ export class GitLabMRService implements IGitLabMRService {
}

async listAllNotes(): Promise<MergeRequestNoteSchema[]> {
return await this.api.MergeRequestNotes.all(this.projectId, this.mrIid);
return await this.api.MergeRequestNotes.all(this.gitlabProjectId, this.gitlabMrIid);
}

async deleteNote(noteId: number): Promise<void> {
await this.api.MergeRequestNotes.remove(this.projectId, this.mrIid, noteId);
await this.api.MergeRequestNotes.remove(
this.gitlabProjectId,
this.gitlabMrIid,
noteId,
);
}

// github can do someone fancy shit here we cant
async createNote(note: string): Promise<void> {
await this.api.MergeRequestNotes.create(this.projectId, this.mrIid, note);
await this.api.MergeRequestNotes.create(this.gitlabProjectId, this.gitlabMrIid, note);
}

async diff(): Promise<Diff[]> {
const changes = await this.api.MergeRequests.allDiffs(this.projectId, this.mrIid);
const changes = await this.api.MergeRequests.allDiffs(this.gitlabProjectId, this.gitlabMrIid);

if (!changes) {
return [];
Expand All @@ -81,8 +97,8 @@ export class GitLabMRService implements IGitLabMRService {

async getLatestVersion(): Promise<MergeRequestDiffVersionsSchema> {
const versions = await this.api.MergeRequests.allDiffVersions(
this.projectId,
this.mrIid,
this.gitlabProjectId,
this.gitlabMrIid,
);
const collected = versions.filter((v) => v.state === 'collected');

Expand Down
101 changes: 101 additions & 0 deletions src/app.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { App } from './app';
import CodeCoachError from './CodeCoachError';
import { ConfigArgument } from './Config';
import { File } from './File';
import { VCSEngine } from './Provider/CommonVCS/VCSEngine';
import { GitLabAdapter } from './Provider/GitLab/GitLabAdapter';
import { GitLabMRService } from './Provider/GitLab/GitLabMRService';
import { GitHubAdapter } from './Provider/GitHub/GitHubAdapter';
import { GitHubPRService } from './Provider/GitHub/GitHubPRService';

jest.mock('./File');
jest.mock('./Provider/CommonVCS/VCSEngine');
jest.mock('./Provider/GitLab/GitLabAdapter');
jest.mock('./Provider/GitLab/GitLabMRService');
jest.mock('./Provider/GitHub/GitHubAdapter');
jest.mock('./Provider/GitHub/GitHubPRService');

const mockedVCSEngine = VCSEngine as jest.MockedClass<typeof VCSEngine>;
const mockedGitLabAdapter = GitLabAdapter as jest.MockedClass<typeof GitLabAdapter>;
const mockedGitLabMRService = GitLabMRService as jest.MockedClass<typeof GitLabMRService>;
const mockedGitHubAdapter = GitHubAdapter as jest.MockedClass<typeof GitHubAdapter>;
const mockedGitHubPRService = GitHubPRService as jest.MockedClass<typeof GitHubPRService>;

describe('App', () => {
it('should not require VCS when dry-run', async () => {
const mockedWriteFileHelper = jest.fn();
File.writeFileHelper = mockedWriteFileHelper;

const configs = ({ buildLogFile: [], dryRun: true } as unknown) as ConfigArgument;
const app = new App(configs);
await app.start();

expect(mockedWriteFileHelper).toBeCalled();
});

it('should throw "VCS adapter is not found" error when run without VCS', async () => {
const app = new App(({ buildLogFile: [] } as unknown) as ConfigArgument);
const fn = async () => await app.start();

await expect(fn).rejects.toThrowError(CodeCoachError);
await expect(fn).rejects.toThrowError('VCS adapter is not found');
});

it('should initialize GitLabAdapter and GitLabMRService correctly', async () => {
const vcsReportFn = jest.fn().mockResolvedValue(true);
mockedVCSEngine.mockImplementationOnce(() => {
return ({
report: vcsReportFn,
} as unknown) as VCSEngine;
});

const configs = ({
vcs: 'gitlab',
gitlabHost: 'https://gitlab.com',
gitlabProjectId: 1234,
gitlabMrIid: 99,
gitlabToken: 'fakegitlabtoken',
buildLogFile: [],
} as unknown) as ConfigArgument;

const app = new App(configs);
await app.start();

expect(vcsReportFn).toBeCalledTimes(1);
expect(mockedGitLabMRService).toBeCalledWith(
configs.gitlabHost,
configs.gitlabProjectId,
configs.gitlabMrIid,
configs.gitlabToken,
);
expect(mockedGitLabAdapter).toBeCalledTimes(1);
});

it('should initialize GitHubAdapter and GitHubPRService correctly', async () => {
const vcsReportFn = jest.fn().mockResolvedValue(true);
mockedVCSEngine.mockImplementationOnce(() => {
return ({
report: vcsReportFn,
} as unknown) as VCSEngine;
});

const configs = ({
vcs: 'github',
githubRepoUrl: 'https://github.com/codeleague/codecoach',
githubPr: 1234,
githubToken: 'fakegithubtoken',
buildLogFile: [],
} as unknown) as ConfigArgument;

const app = new App(configs);
await app.start();

expect(vcsReportFn).toBeCalledTimes(1);
expect(mockedGitHubPRService).toBeCalledWith(
configs.githubToken,
configs.githubRepoUrl,
configs.githubPr,
);
expect(mockedGitHubAdapter).toBeCalledTimes(1);
});
});
Loading