Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cli/actions/remoteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ export const copyOutputToCurrentDirectory = async (
const sourcePath = path.resolve(sourceDir, outputFileName);
const targetPath = path.resolve(targetDir, outputFileName);

// Skip copy if source and target are the same
// This can happen when an absolute path is specified for the output file
if (sourcePath === targetPath) {
logger.trace(`Source and target are the same (${sourcePath}), skipping copy`);
return;
}

try {
logger.trace(`Copying output file from: ${sourcePath} to: ${targetPath}`);

Expand Down
30 changes: 29 additions & 1 deletion tests/cli/actions/remoteAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('remoteAction functions', () => {
});

describe('copyOutputToCurrentDirectory', () => {
test('should copy output file', async () => {
test('should copy output file when source and target are different', async () => {
const sourceDir = '/source/dir';
const targetDir = '/target/dir';
const fileName = 'output.txt';
Expand All @@ -217,6 +217,34 @@ describe('remoteAction functions', () => {
expect(fs.copyFile).toHaveBeenCalledWith(path.resolve(sourceDir, fileName), path.resolve(targetDir, fileName));
});

test('should skip copy when source and target are the same', async () => {
const sourceDir = '/tmp/dir';
const targetDir = '/tmp/dir';
const fileName = 'output.txt';

vi.mocked(fs.copyFile).mockResolvedValue();

await copyOutputToCurrentDirectory(sourceDir, targetDir, fileName);

// Should not call copyFile when source and target are the same
expect(fs.copyFile).not.toHaveBeenCalled();
});

test('should skip copy when absolute path resolves to same location', async () => {
const sourceDir = '/tmp/repomix-123';
const targetDir = process.cwd();
const absolutePath = '/tmp/my_private_dir/output.xml';

vi.mocked(fs.copyFile).mockResolvedValue();

await copyOutputToCurrentDirectory(sourceDir, targetDir, absolutePath);

// When absolute path is used, both source and target resolve to the same path
// path.resolve('/tmp/repomix-123', '/tmp/my_private_dir/output.xml') -> '/tmp/my_private_dir/output.xml'
// path.resolve(process.cwd(), '/tmp/my_private_dir/output.xml') -> '/tmp/my_private_dir/output.xml'
expect(fs.copyFile).not.toHaveBeenCalled();
});

test('should throw error when copy fails', async () => {
const sourceDir = '/source/dir';
const targetDir = '/target/dir';
Expand Down
Loading