203 lines
6.5 KiB
TypeScript
203 lines
6.5 KiB
TypeScript
import { Pipeline } from './../pipelines/pipeline.entity';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
import { Project } from '../projects/project.entity';
|
|
import { ReposService } from './repos.service';
|
|
import { ConfigModule } from '@nestjs/config';
|
|
import { rm } from 'fs/promises';
|
|
import configuration from '../commons/config/configuration';
|
|
import { PipelineTask } from '../pipeline-tasks/pipeline-task.entity';
|
|
import { join } from 'path';
|
|
import { readFile } from 'fs/promises';
|
|
import { getLoggerToken, PinoLogger } from 'nestjs-pino';
|
|
import { Nack } from '@golevelup/nestjs-rabbitmq';
|
|
import { getInstanceName } from '../commons/utils/rabbit-mq';
|
|
|
|
const getTest1Project = () =>
|
|
({
|
|
id: '1',
|
|
sshUrl: 'ssh://gitea@git.ivanli.cc:7018/Fennec/test-1.git',
|
|
name: 'test-1',
|
|
} as Project);
|
|
|
|
describe('ReposService', () => {
|
|
let service: ReposService;
|
|
const repositoryMockFactory = jest.fn(() => ({
|
|
findOneOrFail: jest.fn(
|
|
(entity): Project => ({
|
|
...getTest1Project(),
|
|
...entity,
|
|
}),
|
|
),
|
|
}));
|
|
afterEach(async () => {
|
|
await rm(service.getWorkspaceRoot(getTest1Project()), {
|
|
recursive: true,
|
|
}).catch(() => undefined);
|
|
});
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
imports: [
|
|
ConfigModule.forRoot({
|
|
load: [configuration],
|
|
}),
|
|
],
|
|
providers: [
|
|
ReposService,
|
|
{
|
|
provide: getRepositoryToken(Project),
|
|
useFactory: repositoryMockFactory,
|
|
},
|
|
{
|
|
provide: getLoggerToken(ReposService.name),
|
|
useValue: new PinoLogger({}),
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<ReposService>(ReposService);
|
|
|
|
await rm(service.getWorkspaceRoot(getTest1Project()), {
|
|
recursive: true,
|
|
}).catch(() => undefined);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
it('getWorkspaceRoot', () => {
|
|
expect(service.getWorkspaceRoot(getTest1Project())).toBeDefined();
|
|
});
|
|
describe('listLogs', () => {
|
|
it('should be return logs', async () => {
|
|
const result = await service.listLogs({
|
|
project: getTest1Project(),
|
|
branch: 'master',
|
|
});
|
|
expect(result).toBeDefined();
|
|
}, 20_000);
|
|
});
|
|
describe('listBranch', () => {
|
|
it('should be return branches', async () => {
|
|
const result = await service.listBranches({ projectId: '1' });
|
|
expect(result).toBeDefined();
|
|
}, 20_000);
|
|
});
|
|
|
|
describe.skip('checkout', () => {
|
|
let task: PipelineTask;
|
|
let workspaceRoot: string;
|
|
beforeEach(() => {
|
|
const project = new Project();
|
|
const pipeline = new Pipeline();
|
|
task = new PipelineTask();
|
|
pipeline.project = project;
|
|
task.pipeline = pipeline;
|
|
project.id = 'pid';
|
|
project.name = 'pname';
|
|
pipeline.id = 'lid';
|
|
pipeline.name = 'pipeline';
|
|
task.id = 'tid';
|
|
task.commit = '123123hash';
|
|
workspaceRoot = service.getWorkspaceRootByTask(task);
|
|
});
|
|
|
|
it('should be checkout', async () => {
|
|
task.commit = '498c782685';
|
|
await service.checkout(task, workspaceRoot);
|
|
const filePath = join(workspaceRoot, 'README.md');
|
|
const text = await readFile(filePath, { encoding: 'utf-8' });
|
|
expect(text).toMatch(/Commit 1/gi);
|
|
}, 20_000);
|
|
it('should be checkout right commit', async () => {
|
|
task.commit = '7f7123fe5b';
|
|
await service.checkout(task, workspaceRoot);
|
|
const filePath = join(workspaceRoot, 'README.md');
|
|
const text = await readFile(filePath, { encoding: 'utf-8' });
|
|
expect(text).toMatch(/(?!Commit 1)/gi);
|
|
}, 20_000);
|
|
it('should be checkout right commit (复用)', async () => {
|
|
task.commit = '498c782685';
|
|
await service.checkout(task, workspaceRoot);
|
|
task.commit = '7f7123fe5b';
|
|
await service.checkout(task, workspaceRoot);
|
|
const filePath = join(workspaceRoot, 'README.md');
|
|
const text = await readFile(filePath, { encoding: 'utf-8' });
|
|
expect(text).toMatch(/(?!Commit 1)/gi);
|
|
}, 30_000);
|
|
});
|
|
|
|
describe('getWorkspaceRootByTask', () => {
|
|
it('should be return right path', () => {
|
|
const project = new Project();
|
|
const pipeline = new Pipeline();
|
|
const task = new PipelineTask();
|
|
pipeline.project = project;
|
|
task.pipeline = pipeline;
|
|
project.id = 'pid';
|
|
project.name = 'pname';
|
|
pipeline.id = 'lid';
|
|
pipeline.name = 'pipeline/\\-名称';
|
|
task.id = 'tid';
|
|
task.commit = '123123hash';
|
|
|
|
expect(service.getWorkspaceRootByTask(task)).toMatch(
|
|
/\/pname\/pipeline%2F%5C-%E5%90%8D%E7%A7%B0-123123hash$/,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('fetch', () => {
|
|
it('success', async () => {
|
|
const project = new Project();
|
|
const pipeline = new Pipeline();
|
|
pipeline.branch = 'test';
|
|
const fetch = jest.fn((_: any) => Promise.resolve());
|
|
pipeline.project = project;
|
|
const getGit = jest.spyOn(service, 'getGit').mockImplementation(() =>
|
|
Promise.resolve({
|
|
fetch,
|
|
} as any),
|
|
);
|
|
await expect(service.fetch(pipeline)).resolves.toEqual(getInstanceName());
|
|
expect(getGit).toBeCalledTimes(1);
|
|
expect(getGit.mock.calls[0]?.[0]).toEqual(project);
|
|
expect(fetch).toBeCalledTimes(1);
|
|
expect(fetch.mock.calls[0]?.[0]).toMatchObject([
|
|
'origin',
|
|
'test',
|
|
'--depth=100',
|
|
]);
|
|
});
|
|
it('failed a', async () => {
|
|
const project = new Project();
|
|
const pipeline = new Pipeline();
|
|
pipeline.branch = 'test';
|
|
const fetch = jest.fn((_: any) => Promise.resolve());
|
|
pipeline.project = project;
|
|
const getGit = jest
|
|
.spyOn(service, 'getGit')
|
|
.mockImplementation(() => Promise.reject('error'));
|
|
await expect(service.fetch(pipeline)).resolves.toMatchObject(new Nack());
|
|
expect(getGit).toBeCalledTimes(1);
|
|
expect(getGit.mock.calls[0]?.[0]).toEqual(project);
|
|
expect(fetch).toBeCalledTimes(0);
|
|
});
|
|
it('failed b', async () => {
|
|
const project = new Project();
|
|
const pipeline = new Pipeline();
|
|
pipeline.branch = 'test';
|
|
const fetch = jest.fn((_: any) => Promise.reject('error'));
|
|
pipeline.project = project;
|
|
const getGit = jest.spyOn(service, 'getGit').mockImplementation(() =>
|
|
Promise.resolve({
|
|
fetch,
|
|
} as any),
|
|
);
|
|
await expect(service.fetch(pipeline)).resolves.toMatchObject(new Nack());
|
|
expect(getGit).toBeCalledTimes(1);
|
|
expect(fetch).toBeCalledTimes(1);
|
|
});
|
|
});
|
|
});
|