-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from yamadashy/refact/remote-action
refactor(core): Git Command Module Extraction
- Loading branch information
Showing
6 changed files
with
150 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { exec } from 'node:child_process'; | ||
import { promisify } from 'node:util'; | ||
import { logger } from '../../shared/logger.js'; | ||
|
||
const execAsync = promisify(exec); | ||
|
||
export const isGitInstalled = async ( | ||
deps = { | ||
execAsync, | ||
}, | ||
) => { | ||
try { | ||
const result = await deps.execAsync('git --version'); | ||
return !result.stderr; | ||
} catch (error) { | ||
logger.trace('Git is not installed:', (error as Error).message); | ||
return false; | ||
} | ||
}; | ||
|
||
export const execGitShallowClone = async ( | ||
url: string, | ||
directory: string, | ||
deps = { | ||
execAsync, | ||
}, | ||
) => { | ||
await deps.execAsync(`git clone --depth 1 ${url} ${directory}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { execGitShallowClone, isGitInstalled } from '../../../src/core/file/gitCommand.js'; | ||
import { logger } from '../../../src/shared/logger.js'; | ||
|
||
vi.mock('../../../src/shared/logger'); | ||
|
||
describe('gitCommand', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
describe('isGitInstalled', () => { | ||
test('should return true when git is installed', async () => { | ||
const mockExecAsync = vi.fn().mockResolvedValue({ stdout: 'git version 2.34.1', stderr: '' }); | ||
|
||
const result = await isGitInstalled({ execAsync: mockExecAsync }); | ||
|
||
expect(result).toBe(true); | ||
expect(mockExecAsync).toHaveBeenCalledWith('git --version'); | ||
}); | ||
|
||
test('should return false when git command fails', async () => { | ||
const mockExecAsync = vi.fn().mockRejectedValue(new Error('Command not found: git')); | ||
|
||
const result = await isGitInstalled({ execAsync: mockExecAsync }); | ||
|
||
expect(result).toBe(false); | ||
expect(mockExecAsync).toHaveBeenCalledWith('git --version'); | ||
expect(logger.trace).toHaveBeenCalledWith('Git is not installed:', 'Command not found: git'); | ||
}); | ||
|
||
test('should return false when git command returns stderr', async () => { | ||
const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: 'git: command not found' }); | ||
|
||
const result = await isGitInstalled({ execAsync: mockExecAsync }); | ||
|
||
expect(result).toBe(false); | ||
expect(mockExecAsync).toHaveBeenCalledWith('git --version'); | ||
}); | ||
}); | ||
|
||
describe('execGitShallowClone', () => { | ||
test('should execute git clone with correct parameters', async () => { | ||
const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); | ||
const url = 'https://github.com/user/repo.git'; | ||
const directory = '/tmp/repo'; | ||
|
||
await execGitShallowClone(url, directory, { execAsync: mockExecAsync }); | ||
|
||
expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); | ||
}); | ||
|
||
test('should throw error when git clone fails', async () => { | ||
const mockExecAsync = vi.fn().mockRejectedValue(new Error('Authentication failed')); | ||
const url = 'https://github.com/user/repo.git'; | ||
const directory = '/tmp/repo'; | ||
|
||
await expect(execGitShallowClone(url, directory, { execAsync: mockExecAsync })).rejects.toThrow( | ||
'Authentication failed', | ||
); | ||
|
||
expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters