-
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve test coverage for core components
- Loading branch information
Showing
12 changed files
with
786 additions
and
82 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
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
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 |
---|---|---|
@@ -1,43 +1,88 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { formatGitUrl } from '../../../src/cli/actions/remoteAction.js'; | ||
import * as fs from 'node:fs/promises'; | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { | ||
cleanupTempDirectory, | ||
copyOutputToCurrentDirectory, | ||
createTempDirectory, | ||
formatGitUrl, | ||
} from '../../../src/cli/actions/remoteAction.js'; | ||
|
||
vi.mock('node:fs/promises'); | ||
vi.mock('node:child_process'); | ||
vi.mock('../../../src/cli/actions/defaultAction.js'); | ||
vi.mock('../../../src/shared/logger.js'); | ||
vi.mock('node:fs/promises'); | ||
vi.mock('node:os'); | ||
vi.mock('../../../src/shared/logger'); | ||
|
||
describe('remoteAction', () => { | ||
describe('remoteAction functions', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe('formatGitUrl', () => { | ||
it('should format GitHub shorthand correctly', () => { | ||
test('should convert GitHub shorthand to full URL', () => { | ||
expect(formatGitUrl('user/repo')).toBe('https://github.com/user/repo.git'); | ||
expect(formatGitUrl('user-name/repo-name')).toBe('https://github.com/user-name/repo-name.git'); | ||
expect(formatGitUrl('user_name/repo_name')).toBe('https://github.com/user_name/repo_name.git'); | ||
}); | ||
|
||
it('should add .git to HTTPS URLs if missing', () => { | ||
test('should handle HTTPS URLs', () => { | ||
expect(formatGitUrl('https://github.com/user/repo')).toBe('https://github.com/user/repo.git'); | ||
expect(formatGitUrl('https://github.com/user/repo.git')).toBe('https://github.com/user/repo.git'); | ||
}); | ||
|
||
it('should not modify URLs that are already correctly formatted', () => { | ||
expect(formatGitUrl('https://github.com/user/repo.git')).toBe('https://github.com/user/repo.git'); | ||
expect(formatGitUrl('[email protected]:user/repo.git')).toBe('[email protected]:user/repo.git'); | ||
test('should not modify SSH URLs', () => { | ||
const sshUrl = 'git@github.com:user/repo.git'; | ||
expect(formatGitUrl(sshUrl)).toBe(sshUrl); | ||
}); | ||
}); | ||
|
||
describe('createTempDirectory', () => { | ||
test('should create temporary directory', async () => { | ||
const mockTempDir = '/mock/temp/dir'; | ||
vi.mocked(os.tmpdir).mockReturnValue('/mock/temp'); | ||
vi.mocked(fs.mkdtemp).mockResolvedValue(mockTempDir); | ||
|
||
it('should not modify SSH URLs', () => { | ||
expect(formatGitUrl('[email protected]:user/repo.git')).toBe('[email protected]:user/repo.git'); | ||
const result = await createTempDirectory(); | ||
expect(result).toBe(mockTempDir); | ||
expect(fs.mkdtemp).toHaveBeenCalledWith(path.join('/mock/temp', 'repomix-')); | ||
}); | ||
}); | ||
|
||
describe('cleanupTempDirectory', () => { | ||
test('should cleanup directory', async () => { | ||
const mockDir = '/mock/temp/dir'; | ||
vi.mocked(fs.rm).mockResolvedValue(); | ||
|
||
await cleanupTempDirectory(mockDir); | ||
|
||
expect(fs.rm).toHaveBeenCalledWith(mockDir, { recursive: true, force: true }); | ||
}); | ||
}); | ||
|
||
describe('copyOutputToCurrentDirectory', () => { | ||
test('should copy output file', async () => { | ||
const sourceDir = '/source/dir'; | ||
const targetDir = '/target/dir'; | ||
const fileName = 'output.txt'; | ||
|
||
vi.mocked(fs.copyFile).mockResolvedValue(); | ||
|
||
await copyOutputToCurrentDirectory(sourceDir, targetDir, fileName); | ||
|
||
expect(fs.copyFile).toHaveBeenCalledWith(path.join(sourceDir, fileName), path.join(targetDir, fileName)); | ||
}); | ||
|
||
test('should throw error when copy fails', async () => { | ||
const sourceDir = '/source/dir'; | ||
const targetDir = '/target/dir'; | ||
const fileName = 'output.txt'; | ||
|
||
vi.mocked(fs.copyFile).mockRejectedValue(new Error('Permission denied')); | ||
|
||
it('should not modify URLs from other Git hosting services', () => { | ||
expect(formatGitUrl('https://gitlab.com/user/repo.git')).toBe('https://gitlab.com/user/repo.git'); | ||
expect(formatGitUrl('https://bitbucket.org/user/repo.git')).toBe('https://bitbucket.org/user/repo.git'); | ||
await expect(copyOutputToCurrentDirectory(sourceDir, targetDir, fileName)).rejects.toThrow( | ||
'Failed to copy output file', | ||
); | ||
}); | ||
}); | ||
}); |
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,118 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import * as defaultAction from '../../src/cli/actions/defaultAction.js'; | ||
import * as initAction from '../../src/cli/actions/initAction.js'; | ||
import * as remoteAction from '../../src/cli/actions/remoteAction.js'; | ||
import * as versionAction from '../../src/cli/actions/versionAction.js'; | ||
import { executeAction, run } from '../../src/cli/cliRun.js'; | ||
import type { RepomixConfigMerged } from '../../src/config/configSchema.js'; | ||
import type { PackResult } from '../../src/core/packager.js'; | ||
import { logger } from '../../src/shared/logger.js'; | ||
|
||
vi.mock('../../src/shared/logger', () => ({ | ||
logger: { | ||
log: vi.fn(), | ||
trace: vi.fn(), | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
success: vi.fn(), | ||
note: vi.fn(), | ||
setVerbose: vi.fn(), | ||
}, | ||
})); | ||
|
||
vi.mock('commander', () => ({ | ||
program: { | ||
description: vi.fn().mockReturnThis(), | ||
arguments: vi.fn().mockReturnThis(), | ||
option: vi.fn().mockReturnThis(), | ||
action: vi.fn().mockReturnThis(), | ||
parseAsync: vi.fn().mockResolvedValue(undefined), | ||
}, | ||
})); | ||
|
||
vi.mock('../../src/cli/actions/defaultAction'); | ||
vi.mock('../../src/cli/actions/initAction'); | ||
vi.mock('../../src/cli/actions/remoteAction'); | ||
vi.mock('../../src/cli/actions/versionAction'); | ||
|
||
describe('cliRun', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
|
||
vi.mocked(defaultAction.runDefaultAction).mockResolvedValue({ | ||
config: { | ||
cwd: process.cwd(), | ||
output: { | ||
filePath: 'repomix-output.txt', | ||
style: 'plain', | ||
topFilesLength: 5, | ||
showLineNumbers: false, | ||
removeComments: false, | ||
removeEmptyLines: false, | ||
copyToClipboard: false, | ||
}, | ||
include: [], | ||
ignore: { | ||
useGitignore: true, | ||
useDefaultPatterns: true, | ||
customPatterns: [], | ||
}, | ||
security: { | ||
enableSecurityCheck: true, | ||
}, | ||
} satisfies RepomixConfigMerged, | ||
packResult: { | ||
totalFiles: 0, | ||
totalCharacters: 0, | ||
totalTokens: 0, | ||
fileCharCounts: {}, | ||
fileTokenCounts: {}, | ||
suspiciousFilesResults: [], | ||
} satisfies PackResult, | ||
}); | ||
vi.mocked(initAction.runInitAction).mockResolvedValue(); | ||
vi.mocked(remoteAction.runRemoteAction).mockResolvedValue(); | ||
vi.mocked(versionAction.runVersionAction).mockResolvedValue(); | ||
}); | ||
|
||
test('should run without arguments', async () => { | ||
await expect(run()).resolves.not.toThrow(); | ||
}); | ||
|
||
describe('executeAction', () => { | ||
test('should execute default action when no special options provided', async () => { | ||
await executeAction('.', process.cwd(), {}); | ||
|
||
expect(defaultAction.runDefaultAction).toHaveBeenCalledWith('.', process.cwd(), expect.any(Object)); | ||
}); | ||
|
||
test('should enable verbose logging when verbose option is true', async () => { | ||
await executeAction('.', process.cwd(), { verbose: true }); | ||
|
||
expect(logger.setVerbose).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
test('should execute version action when version option is true', async () => { | ||
await executeAction('.', process.cwd(), { version: true }); | ||
|
||
expect(versionAction.runVersionAction).toHaveBeenCalled(); | ||
expect(defaultAction.runDefaultAction).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should execute init action when init option is true', async () => { | ||
await executeAction('.', process.cwd(), { init: true }); | ||
|
||
expect(initAction.runInitAction).toHaveBeenCalledWith(process.cwd(), false); | ||
expect(defaultAction.runDefaultAction).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should execute remote action when remote option is provided', async () => { | ||
await executeAction('.', process.cwd(), { remote: 'yamadashy/repomix' }); | ||
|
||
expect(remoteAction.runRemoteAction).toHaveBeenCalledWith('yamadashy/repomix', expect.any(Object)); | ||
expect(defaultAction.runDefaultAction).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
Oops, something went wrong.