repo-helpers.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { faker } from '@faker-js/faker';
  2. import childProcess from 'child_process';
  3. import logger from '../../config/logger/logger';
  4. import { getConfig } from '../../core/config/TipiConfig';
  5. import { cloneRepo, updateRepo } from '../repo-helpers';
  6. jest.mock('child_process');
  7. beforeEach(async () => {
  8. jest.resetModules();
  9. jest.resetAllMocks();
  10. });
  11. describe('Test: updateRepo', () => {
  12. it('Should run update script', async () => {
  13. const log = jest.spyOn(logger, 'info');
  14. const spy = jest.spyOn(childProcess, 'execFile');
  15. const url = faker.internet.url();
  16. const stdout = faker.random.words();
  17. // @ts-ignore
  18. spy.mockImplementation((_path, _args, _, cb) => {
  19. // @ts-ignore
  20. if (cb) cb(null, stdout, null);
  21. });
  22. await updateRepo(url);
  23. expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/scripts/git.sh`, ['update', url], {}, expect.any(Function));
  24. expect(log).toHaveBeenCalledWith(`Update result: ${stdout}`);
  25. });
  26. it('Should throw and log error if script failed', async () => {
  27. const url = faker.internet.url();
  28. const log = jest.spyOn(logger, 'error');
  29. const spy = jest.spyOn(childProcess, 'execFile');
  30. const randomWord = faker.random.word();
  31. // @ts-ignore
  32. spy.mockImplementation((_path, _args, _, cb) => {
  33. // @ts-ignore
  34. if (cb) cb(randomWord, null, null);
  35. });
  36. try {
  37. await updateRepo(url);
  38. } catch (e) {
  39. expect(e).toBe(randomWord);
  40. expect(log).toHaveBeenCalledWith(`Error updating repo: ${randomWord}`);
  41. }
  42. });
  43. });
  44. describe('Test: cloneRepo', () => {
  45. it('Should run clone script', async () => {
  46. const log = jest.spyOn(logger, 'info');
  47. const spy = jest.spyOn(childProcess, 'execFile');
  48. const url = faker.internet.url();
  49. const stdout = faker.random.words();
  50. // @ts-ignore
  51. spy.mockImplementation((_path, _args, _, cb) => {
  52. // @ts-ignore
  53. if (cb) cb(null, stdout, null);
  54. });
  55. await cloneRepo(url);
  56. expect(spy).toHaveBeenCalledWith(`${getConfig().rootFolder}/scripts/git.sh`, ['clone', url], {}, expect.any(Function));
  57. expect(log).toHaveBeenCalledWith(`Clone result ${stdout}`);
  58. });
  59. it('Should throw and log error if script failed', async () => {
  60. const url = faker.internet.url();
  61. const log = jest.spyOn(logger, 'error');
  62. const spy = jest.spyOn(childProcess, 'execFile');
  63. const randomWord = faker.random.word();
  64. // @ts-ignore
  65. spy.mockImplementation((_path, _args, _, cb) => {
  66. // @ts-ignore
  67. if (cb) cb(randomWord, null, null);
  68. });
  69. try {
  70. await cloneRepo(url);
  71. } catch (e) {
  72. expect(e).toBe(randomWord);
  73. expect(log).toHaveBeenCalledWith(`Error cloning repo: ${randomWord}`);
  74. }
  75. });
  76. });