-
-
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.
- Loading branch information
Showing
5 changed files
with
237 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import path from 'node:path'; | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { printCompletion, printSecurityCheck, printSummary, printTopFiles } from '../../src/cli/cliPrint.js'; | ||
import type { SuspiciousFileResult } from '../../src/core/security/securityCheck.js'; | ||
import { logger } from '../../src/shared/logger.js'; | ||
import { createMockConfig, isWindows } from '../testing/testUtils.js'; | ||
|
||
vi.mock('../../src/shared/logger'); | ||
vi.mock('picocolors', () => ({ | ||
default: { | ||
white: (str: string) => `WHITE:${str}`, | ||
dim: (str: string) => `DIM:${str}`, | ||
green: (str: string) => `GREEN:${str}`, | ||
yellow: (str: string) => `YELLOW:${str}`, | ||
red: (str: string) => `RED:${str}`, | ||
cyan: (str: string) => `CYAN:${str}`, | ||
}, | ||
})); | ||
|
||
describe('cliPrint', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
describe('printSummary', () => { | ||
test('should print summary with suspicious files and security check enabled', () => { | ||
const config = createMockConfig({ | ||
security: { enableSecurityCheck: true }, | ||
}); | ||
const suspiciousFiles: SuspiciousFileResult[] = [ | ||
{ filePath: 'suspicious.txt', messages: ['Contains sensitive data'] }, | ||
]; | ||
|
||
printSummary(10, 1000, 200, 'output.txt', suspiciousFiles, config); | ||
|
||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('1 suspicious file(s) detected and excluded')); | ||
}); | ||
|
||
test('should print summary with security check disabled', () => { | ||
const config = createMockConfig({ | ||
security: { enableSecurityCheck: false }, | ||
}); | ||
|
||
printSummary(10, 1000, 200, 'output.txt', [], config); | ||
|
||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Security check disabled')); | ||
}); | ||
}); | ||
|
||
describe('printSecurityCheck', () => { | ||
test('should skip printing when security check is disabled', () => { | ||
const config = createMockConfig({ | ||
security: { enableSecurityCheck: false }, | ||
}); | ||
|
||
printSecurityCheck('/root', [], config); | ||
expect(logger.log).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should print message when no suspicious files found', () => { | ||
const config = createMockConfig({ | ||
security: { enableSecurityCheck: true }, | ||
}); | ||
|
||
printSecurityCheck('/root', [], config); | ||
|
||
expect(logger.log).toHaveBeenCalledWith('WHITE:🔎 Security Check:'); | ||
expect(logger.log).toHaveBeenCalledWith('DIM:──────────────────'); | ||
expect(logger.log).toHaveBeenCalledWith('GREEN:✔ WHITE:No suspicious files detected.'); | ||
}); | ||
|
||
test.runIf(!isWindows)('should print details of suspicious files when found', () => { | ||
const config = createMockConfig({ | ||
security: { enableSecurityCheck: true }, | ||
}); | ||
const suspiciousFiles: SuspiciousFileResult[] = [ | ||
{ | ||
filePath: path.join('/root', 'config/secrets.txt'), | ||
messages: ['Contains API key', 'Contains password'], | ||
}, | ||
]; | ||
|
||
printSecurityCheck('/root', suspiciousFiles, config); | ||
|
||
expect(logger.log).toHaveBeenCalledWith('YELLOW:1 suspicious file(s) detected and excluded from the output:'); | ||
expect(logger.log).toHaveBeenCalledWith('WHITE:1. WHITE:config/secrets.txt'); | ||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Contains API key')); | ||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Contains password')); | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('Please review these files for potential sensitive information.'), | ||
); | ||
}); | ||
}); | ||
|
||
describe('printTopFiles', () => { | ||
test('should print top files sorted by character count', () => { | ||
const fileCharCounts = { | ||
'src/index.ts': 1000, | ||
'src/utils.ts': 500, | ||
'README.md': 2000, | ||
}; | ||
const fileTokenCounts = { | ||
'src/index.ts': 200, | ||
'src/utils.ts': 100, | ||
'README.md': 400, | ||
}; | ||
|
||
printTopFiles(fileCharCounts, fileTokenCounts, 2); | ||
|
||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Top 2 Files')); | ||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('README.md')); | ||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('src/index.ts')); | ||
expect(logger.log).not.toHaveBeenCalledWith(expect.stringContaining('src/utils.ts')); | ||
}); | ||
|
||
test('should handle empty file list', () => { | ||
printTopFiles({}, {}, 5); | ||
|
||
expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Top 5 Files')); | ||
}); | ||
}); | ||
|
||
describe('printCompletion', () => { | ||
test('should print completion message', () => { | ||
printCompletion(); | ||
|
||
expect(logger.log).toHaveBeenCalledWith('GREEN:🎉 All Done!'); | ||
expect(logger.log).toHaveBeenCalledWith('WHITE:Your repository has been successfully packed.'); | ||
}); | ||
}); | ||
}); |
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,49 +1,87 @@ | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { getGlobalDirectory } from '../../src/config/globalDirectory.js'; | ||
import { isLinux, isMac, isWindows } from '../testing/testUtils.js'; | ||
|
||
vi.mock('node:os'); | ||
|
||
describe('globalDirectory', () => { | ||
describe('getGlobalDirectory', () => { | ||
const originalPlatform = process.platform; | ||
const originalEnv = process.env; | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
process.env = {}; | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
test.runIf(isWindows)('should return correct path for Windows', () => { | ||
vi.mocked(os.platform).mockReturnValue('win32'); | ||
vi.mocked(os.homedir).mockReturnValue('C:\\Users\\TestUser'); | ||
process.env.LOCALAPPDATA = 'C:\\Users\\TestUser\\AppData\\Local'; | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('C:\\Users\\TestUser\\AppData\\Local', 'Repomix')); | ||
afterEach(() => { | ||
Object.defineProperty(process, 'platform', { value: originalPlatform }); | ||
process.env = originalEnv; | ||
}); | ||
|
||
test.runIf(isWindows)('should use homedir if LOCALAPPDATA is not set on Windows', () => { | ||
vi.mocked(os.platform).mockReturnValue('win32'); | ||
vi.mocked(os.homedir).mockReturnValue('C:\\Users\\TestUser'); | ||
process.env.LOCALAPPDATA = undefined; | ||
describe('Windows platform', () => { | ||
test('should use LOCALAPPDATA when available', () => { | ||
Object.defineProperty(process, 'platform', { value: 'win32' }); | ||
process.env.LOCALAPPDATA = 'C:\\Users\\Test\\AppData\\Local'; | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('C:\\Users\\Test\\AppData\\Local', 'Repomix')); | ||
}); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('C:\\Users\\TestUser', 'AppData', 'Local', 'Repomix')); | ||
test('should fall back to homedir when LOCALAPPDATA is not available', () => { | ||
Object.defineProperty(process, 'platform', { value: 'win32' }); | ||
process.env.LOCALAPPDATA = undefined; | ||
vi.mocked(os.homedir).mockReturnValue('C:\\Users\\Test'); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('C:\\Users\\Test', 'AppData', 'Local', 'Repomix')); | ||
}); | ||
}); | ||
|
||
test.runIf(isLinux)('should use XDG_CONFIG_HOME on Unix systems if set', () => { | ||
vi.mocked(os.platform).mockReturnValue('linux'); | ||
process.env.XDG_CONFIG_HOME = '/custom/config'; | ||
describe('Unix platforms', () => { | ||
test('should use XDG_CONFIG_HOME when available', () => { | ||
Object.defineProperty(process, 'platform', { value: 'linux' }); | ||
process.env.XDG_CONFIG_HOME = '/custom/config'; | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('/custom/config', 'repomix')); | ||
}); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('/custom/config', 'repomix')); | ||
test('should fall back to ~/.config on Linux', () => { | ||
Object.defineProperty(process, 'platform', { value: 'linux' }); | ||
process.env.XDG_CONFIG_HOME = undefined; | ||
vi.mocked(os.homedir).mockReturnValue('/home/test'); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('/home/test', '.config', 'repomix')); | ||
}); | ||
|
||
test('should fall back to ~/.config on macOS', () => { | ||
Object.defineProperty(process, 'platform', { value: 'darwin' }); | ||
process.env.XDG_CONFIG_HOME = undefined; | ||
vi.mocked(os.homedir).mockReturnValue('/Users/test'); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('/Users/test', '.config', 'repomix')); | ||
}); | ||
}); | ||
|
||
test.runIf(isMac)('should use ~/.config on Unix systems if XDG_CONFIG_HOME is not set', () => { | ||
vi.mocked(os.platform).mockReturnValue('darwin'); | ||
vi.mocked(os.homedir).mockReturnValue('/Users/TestUser'); | ||
process.env.XDG_CONFIG_HOME = undefined; | ||
describe('Edge cases', () => { | ||
test('should handle empty homedir', () => { | ||
Object.defineProperty(process, 'platform', { value: 'linux' }); | ||
process.env.XDG_CONFIG_HOME = undefined; | ||
vi.mocked(os.homedir).mockReturnValue(''); | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('', '.config', 'repomix')); | ||
}); | ||
|
||
test('should handle unusual XDG_CONFIG_HOME paths', () => { | ||
Object.defineProperty(process, 'platform', { value: 'linux' }); | ||
process.env.XDG_CONFIG_HOME = '////multiple///slashes///'; | ||
|
||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('/Users/TestUser', '.config', 'repomix')); | ||
const result = getGlobalDirectory(); | ||
expect(result).toBe(path.join('////multiple///slashes///', 'repomix')); | ||
}); | ||
}); | ||
}); |