|
| 1 | +import { describe, expect, vi } from 'vitest'; |
| 2 | +import { type HistoryOptions, history } from '@code-pushup/core'; |
| 3 | +import { safeCheckout } from '@code-pushup/utils'; |
| 4 | +import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants'; |
| 5 | +import { yargsCli } from '../yargs-cli'; |
| 6 | +import { yargsHistoryCommandObject } from './history-command'; |
| 7 | + |
| 8 | +vi.mock('@code-pushup/core', async () => { |
| 9 | + const { |
| 10 | + MINIMAL_HISTORY_CONFIG_MOCK, |
| 11 | + }: typeof import('@code-pushup/test-utils') = await vi.importActual( |
| 12 | + '@code-pushup/test-utils', |
| 13 | + ); |
| 14 | + const core: object = await vi.importActual('@code-pushup/core'); |
| 15 | + return { |
| 16 | + ...core, |
| 17 | + history: vi |
| 18 | + .fn() |
| 19 | + .mockImplementation((options: HistoryOptions, commits: string[]) => |
| 20 | + commits.map(commit => `${commit}-report.json`), |
| 21 | + ), |
| 22 | + readRcByPath: vi.fn().mockResolvedValue(MINIMAL_HISTORY_CONFIG_MOCK), |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +vi.mock('@code-pushup/utils', async () => { |
| 27 | + const utils: object = await vi.importActual('@code-pushup/utils'); |
| 28 | + return { |
| 29 | + ...utils, |
| 30 | + safeCheckout: vi.fn(), |
| 31 | + getCurrentBranchOrTag: vi.fn().mockReturnValue('main'), |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +vi.mock('simple-git', async () => { |
| 36 | + const actual = await vi.importActual('simple-git'); |
| 37 | + return { |
| 38 | + ...actual, |
| 39 | + simpleGit: () => ({ |
| 40 | + log: ({ maxCount }: { maxCount: number } = { maxCount: 1 }) => |
| 41 | + Promise.resolve({ |
| 42 | + all: [ |
| 43 | + { hash: 'commit-6' }, |
| 44 | + { hash: 'commit-5' }, |
| 45 | + { hash: 'commit-4' }, |
| 46 | + { hash: 'commit-3' }, |
| 47 | + { hash: 'commit-2' }, |
| 48 | + { hash: 'commit-1' }, |
| 49 | + ].slice(-maxCount), |
| 50 | + }), |
| 51 | + }), |
| 52 | + }; |
| 53 | +}); |
| 54 | + |
| 55 | +describe('history-command', () => { |
| 56 | + it('should return the last 5 commits', async () => { |
| 57 | + await yargsCli(['history', '--config=/test/code-pushup.config.ts'], { |
| 58 | + ...DEFAULT_CLI_CONFIGURATION, |
| 59 | + commands: [yargsHistoryCommandObject()], |
| 60 | + }).parseAsync(); |
| 61 | + |
| 62 | + expect(history).toHaveBeenCalledWith( |
| 63 | + expect.objectContaining({ |
| 64 | + targetBranch: 'main', |
| 65 | + }), |
| 66 | + ['commit-1', 'commit-2', 'commit-3', 'commit-4', 'commit-5'], |
| 67 | + ); |
| 68 | + |
| 69 | + expect(safeCheckout).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should have 2 commits to crawl in history if maxCount is set to 2', async () => { |
| 73 | + await yargsCli( |
| 74 | + ['history', '--config=/test/code-pushup.config.ts', '--maxCount=2'], |
| 75 | + { |
| 76 | + ...DEFAULT_CLI_CONFIGURATION, |
| 77 | + commands: [yargsHistoryCommandObject()], |
| 78 | + }, |
| 79 | + ).parseAsync(); |
| 80 | + |
| 81 | + expect(history).toHaveBeenCalledWith(expect.any(Object), [ |
| 82 | + 'commit-1', |
| 83 | + 'commit-2', |
| 84 | + ]); |
| 85 | + |
| 86 | + expect(safeCheckout).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | +}); |
0 commit comments