1+ import { exec } from 'child_process' ;
2+
13import { describe , it , expect , vi , beforeEach } from 'vitest' ;
2- import { checkGitCli , GitCliCheckResult } from './gitCliCheck' ;
4+
5+ import { checkGitCli } from './gitCliCheck' ;
36
47// Mock the child_process module
58vi . mock ( 'child_process' , ( ) => ( {
@@ -23,88 +26,113 @@ vi.mock('util', () => ({
2326 } ) ,
2427} ) ) ;
2528
26- // Import the mocked modules
27- import { exec } from 'child_process' ;
28-
2929describe ( 'gitCliCheck' , ( ) => {
3030 const mockExec = exec as unknown as vi . Mock ;
31-
31+
3232 beforeEach ( ( ) => {
3333 mockExec . mockReset ( ) ;
3434 } ) ;
35-
35+
3636 it ( 'should return all true when git and gh are available and authenticated' , async ( ) => {
3737 // Mock successful responses
38- mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
39- if ( cmd === 'git --version' ) {
40- callback ( null , { stdout : 'git version 2.30.1' } ) ;
41- } else if ( cmd === 'gh --version' ) {
42- callback ( null , { stdout : 'gh version 2.0.0' } ) ;
43- } else if ( cmd === 'gh auth status' ) {
44- callback ( null , { stdout : 'Logged in to github.com as username' } ) ;
45- }
46- } ) ;
47-
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+
4853 const result = await checkGitCli ( ) ;
49-
54+
5055 expect ( result . gitAvailable ) . toBe ( true ) ;
5156 expect ( result . ghAvailable ) . toBe ( true ) ;
5257 expect ( result . ghAuthenticated ) . toBe ( true ) ;
5358 expect ( result . errors ) . toHaveLength ( 0 ) ;
5459 } ) ;
55-
60+
5661 it ( 'should detect when git is not available' , async ( ) => {
57- mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
58- if ( cmd === 'git --version' ) {
59- callback ( new Error ( 'Command not found' ) ) ;
60- } else if ( cmd === 'gh --version' ) {
61- callback ( null , { stdout : 'gh version 2.0.0' } ) ;
62- } else if ( cmd === 'gh auth status' ) {
63- callback ( null , { stdout : 'Logged in to github.com as username' } ) ;
64- }
65- } ) ;
66-
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+
6777 const result = await checkGitCli ( ) ;
68-
78+
6979 expect ( result . gitAvailable ) . toBe ( false ) ;
7080 expect ( result . ghAvailable ) . toBe ( true ) ;
7181 expect ( result . ghAuthenticated ) . toBe ( true ) ;
72- expect ( result . errors ) . toContain ( 'Git CLI is not available. Please install git.' ) ;
82+ expect ( result . errors ) . toContain (
83+ 'Git CLI is not available. Please install git.' ,
84+ ) ;
7385 } ) ;
74-
86+
7587 it ( 'should detect when gh is not available' , async ( ) => {
76- mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
77- if ( cmd === 'git --version' ) {
78- callback ( null , { stdout : 'git version 2.30.1' } ) ;
79- } else if ( cmd === 'gh --version' ) {
80- callback ( new Error ( 'Command not found' ) ) ;
81- }
82- } ) ;
83-
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+
84101 const result = await checkGitCli ( ) ;
85-
102+
86103 expect ( result . gitAvailable ) . toBe ( true ) ;
87104 expect ( result . ghAvailable ) . toBe ( false ) ;
88105 expect ( result . ghAuthenticated ) . toBe ( false ) ;
89- expect ( result . errors ) . toContain ( 'GitHub CLI is not available. Please install gh CLI.' ) ;
106+ expect ( result . errors ) . toContain (
107+ 'GitHub CLI is not available. Please install gh CLI.' ,
108+ ) ;
90109 } ) ;
91-
110+
92111 it ( 'should detect when gh is not authenticated' , async ( ) => {
93- mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
94- if ( cmd === 'git --version' ) {
95- callback ( null , { stdout : 'git version 2.30.1' } ) ;
96- } else if ( cmd === 'gh --version' ) {
97- callback ( null , { stdout : 'gh version 2.0.0' } ) ;
98- } else if ( cmd === 'gh auth status' ) {
99- callback ( new Error ( 'You are not logged into any GitHub hosts' ) ) ;
100- }
101- } ) ;
102-
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+
103129 const result = await checkGitCli ( ) ;
104-
130+
105131 expect ( result . gitAvailable ) . toBe ( true ) ;
106132 expect ( result . ghAvailable ) . toBe ( true ) ;
107133 expect ( result . ghAuthenticated ) . toBe ( false ) ;
108- expect ( result . errors ) . toContain ( 'GitHub CLI is not authenticated. Please run "gh auth login".' ) ;
134+ expect ( result . errors ) . toContain (
135+ 'GitHub CLI is not authenticated. Please run "gh auth login".' ,
136+ ) ;
109137 } ) ;
110- } ) ;
138+ } ) ;
0 commit comments