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'; 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, }, ], }).compile(); service = module.get(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$/, ); }); }); });