123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import { faker } from '@faker-js/faker';
- import childProcess from 'child_process';
- import logger from '../../config/logger/logger';
- import { getConfig } from '../../core/config/TipiConfig';
- import { cloneRepo, updateRepo } from '../repo-helpers';
- jest.mock('child_process');
- beforeEach(async () => {
- jest.resetModules();
- jest.resetAllMocks();
- });
- describe('Test: updateRepo', () => {
- it('Should run update script', async () => {
- const log = jest.spyOn(logger, 'info');
- const spy = jest.spyOn(childProcess, 'execFile');
- const url = faker.internet.url();
- const stdout = faker.random.words();
- // @ts-ignore
- spy.mockImplementation((_path, _args, _, cb) => {
- // @ts-ignore
- if (cb) cb(null, stdout, null);
- });
- await updateRepo(url);
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/scripts/git.sh`, ['update', url], {}, expect.any(Function));
- expect(log).toHaveBeenCalledWith(`Update result: ${stdout}`);
- });
- it('Should throw and log error if script failed', async () => {
- const url = faker.internet.url();
- const log = jest.spyOn(logger, 'error');
- const spy = jest.spyOn(childProcess, 'execFile');
- const randomWord = faker.random.word();
- // @ts-ignore
- spy.mockImplementation((_path, _args, _, cb) => {
- // @ts-ignore
- if (cb) cb(randomWord, null, null);
- });
- try {
- await updateRepo(url);
- } catch (e) {
- expect(e).toBe(randomWord);
- expect(log).toHaveBeenCalledWith(`Error updating repo: ${randomWord}`);
- }
- });
- });
- describe('Test: cloneRepo', () => {
- it('Should run clone script', async () => {
- const log = jest.spyOn(logger, 'info');
- const spy = jest.spyOn(childProcess, 'execFile');
- const url = faker.internet.url();
- const stdout = faker.random.words();
- // @ts-ignore
- spy.mockImplementation((_path, _args, _, cb) => {
- // @ts-ignore
- if (cb) cb(null, stdout, null);
- });
- await cloneRepo(url);
- expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/scripts/git.sh`, ['clone', url], {}, expect.any(Function));
- expect(log).toHaveBeenCalledWith(`Clone result ${stdout}`);
- });
- it('Should throw and log error if script failed', async () => {
- const url = faker.internet.url();
- const log = jest.spyOn(logger, 'error');
- const spy = jest.spyOn(childProcess, 'execFile');
- const randomWord = faker.random.word();
- // @ts-ignore
- spy.mockImplementation((_path, _args, _, cb) => {
- // @ts-ignore
- if (cb) cb(randomWord, null, null);
- });
- try {
- await cloneRepo(url);
- } catch (e) {
- expect(e).toBe(randomWord);
- expect(log).toHaveBeenCalledWith(`Error cloning repo: ${randomWord}`);
- }
- });
- });
|