|
| 1 | +import { exec } from 'child_process'; |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 4 | + |
| 5 | +import { checkGitCli } from './gitCliCheck'; |
| 6 | + |
| 7 | +// Mock the child_process module |
| 8 | +vi.mock('child_process', () => ({ |
| 9 | + exec: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock the util module |
| 13 | +vi.mock('util', () => ({ |
| 14 | + promisify: vi.fn((fn) => { |
| 15 | + return (cmd: string) => { |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + fn(cmd, (error: Error | null, result: { stdout: string }) => { |
| 18 | + if (error) { |
| 19 | + reject(error); |
| 20 | + } else { |
| 21 | + resolve(result); |
| 22 | + } |
| 23 | + }); |
| 24 | + }); |
| 25 | + }; |
| 26 | + }), |
| 27 | +})); |
| 28 | + |
| 29 | +describe('gitCliCheck', () => { |
| 30 | + const mockExec = exec as unknown as vi.Mock; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + mockExec.mockReset(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return all true when git and gh are available and authenticated', async () => { |
| 37 | + // Mock successful responses |
| 38 | + mockExec.mockImplementation( |
| 39 | + ( |
| 40 | + cmd: string, |
| 41 | + callback: (error: Error | null, result: { stdout: string }) => void, |
| 42 | + ) => { |
| 43 | + if (cmd === 'git --version') { |
| 44 | + callback(null, { stdout: 'git version 2.30.1' }); |
| 45 | + } else if (cmd === 'gh --version') { |
| 46 | + callback(null, { stdout: 'gh version 2.0.0' }); |
| 47 | + } else if (cmd === 'gh auth status') { |
| 48 | + callback(null, { stdout: 'Logged in to github.com as username' }); |
| 49 | + } |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + const result = await checkGitCli(); |
| 54 | + |
| 55 | + expect(result.gitAvailable).toBe(true); |
| 56 | + expect(result.ghAvailable).toBe(true); |
| 57 | + expect(result.ghAuthenticated).toBe(true); |
| 58 | + expect(result.errors).toHaveLength(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should detect when git is not available', async () => { |
| 62 | + mockExec.mockImplementation( |
| 63 | + ( |
| 64 | + cmd: string, |
| 65 | + callback: (error: Error | null, result: { stdout: string }) => void, |
| 66 | + ) => { |
| 67 | + if (cmd === 'git --version') { |
| 68 | + callback(new Error('Command not found'), { stdout: '' }); |
| 69 | + } else if (cmd === 'gh --version') { |
| 70 | + callback(null, { stdout: 'gh version 2.0.0' }); |
| 71 | + } else if (cmd === 'gh auth status') { |
| 72 | + callback(null, { stdout: 'Logged in to github.com as username' }); |
| 73 | + } |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + const result = await checkGitCli(); |
| 78 | + |
| 79 | + expect(result.gitAvailable).toBe(false); |
| 80 | + expect(result.ghAvailable).toBe(true); |
| 81 | + expect(result.ghAuthenticated).toBe(true); |
| 82 | + expect(result.errors).toContain( |
| 83 | + 'Git CLI is not available. Please install git.', |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should detect when gh is not available', async () => { |
| 88 | + mockExec.mockImplementation( |
| 89 | + ( |
| 90 | + cmd: string, |
| 91 | + callback: (error: Error | null, result: { stdout: string }) => void, |
| 92 | + ) => { |
| 93 | + if (cmd === 'git --version') { |
| 94 | + callback(null, { stdout: 'git version 2.30.1' }); |
| 95 | + } else if (cmd === 'gh --version') { |
| 96 | + callback(new Error('Command not found'), { stdout: '' }); |
| 97 | + } |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + const result = await checkGitCli(); |
| 102 | + |
| 103 | + expect(result.gitAvailable).toBe(true); |
| 104 | + expect(result.ghAvailable).toBe(false); |
| 105 | + expect(result.ghAuthenticated).toBe(false); |
| 106 | + expect(result.errors).toContain( |
| 107 | + 'GitHub CLI is not available. Please install gh CLI.', |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should detect when gh is not authenticated', async () => { |
| 112 | + mockExec.mockImplementation( |
| 113 | + ( |
| 114 | + cmd: string, |
| 115 | + callback: (error: Error | null, result: { stdout: string }) => void, |
| 116 | + ) => { |
| 117 | + if (cmd === 'git --version') { |
| 118 | + callback(null, { stdout: 'git version 2.30.1' }); |
| 119 | + } else if (cmd === 'gh --version') { |
| 120 | + callback(null, { stdout: 'gh version 2.0.0' }); |
| 121 | + } else if (cmd === 'gh auth status') { |
| 122 | + callback(new Error('You are not logged into any GitHub hosts'), { |
| 123 | + stdout: '', |
| 124 | + }); |
| 125 | + } |
| 126 | + }, |
| 127 | + ); |
| 128 | + |
| 129 | + const result = await checkGitCli(); |
| 130 | + |
| 131 | + expect(result.gitAvailable).toBe(true); |
| 132 | + expect(result.ghAvailable).toBe(true); |
| 133 | + expect(result.ghAuthenticated).toBe(false); |
| 134 | + expect(result.errors).toContain( |
| 135 | + 'GitHub CLI is not authenticated. Please run "gh auth login".', |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
0 commit comments