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
9 changes: 7 additions & 2 deletions src/handlers/pathHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ export function registerPathHandlers() {
result.parentMissing = true;
}

// Check if path exists
// Check if path exists and is not an empty directory
if (fs.existsSync(inputPath)) {
result.exists = true;
if (fs.statSync(inputPath).isDirectory()) {
const contents = fs.readdirSync(inputPath);
result.exists = contents.length > 0;
} else {
result.exists = true;
}
}

// Check if path is writable
Expand Down
22 changes: 20 additions & 2 deletions tests/unit/handlers/pathHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ const mockDiskSpace = (available: number) => {
]);
};

const mockFileSystem = ({ exists = true, writable = true } = {}) => {
const mockFileSystem = ({ exists = true, writable = true, isDirectory = false, contentLength = 0 } = {}) => {
vi.mocked(fs.existsSync).mockReturnValue(exists);
vi.mocked(fs.statSync).mockReturnValue({
isDirectory: () => isDirectory,
} as unknown as fs.Stats);
vi.mocked(fs.readdirSync).mockReturnValue(
Array.from({ length: contentLength }, () => ({ name: 'mock-file' }) as fs.Dirent)
);
if (writable) {
vi.mocked(fs.accessSync).mockReturnValue();
} else {
Expand Down Expand Up @@ -126,6 +132,18 @@ describe('PathHandlers', () => {
});
});

it('does not exist if directory is empty', async () => {
mockFileSystem({ exists: true, writable: true, contentLength: 0, isDirectory: true });

const result = await validateHandler({}, '/valid/path');
expect(result).toEqual({
isValid: true,
exists: false,
freeSpace: DEFAULT_FREE_SPACE,
requiredSpace: REQUIRED_SPACE,
});
});

it('rejects path with insufficient disk space', async () => {
mockFileSystem({ exists: true, writable: true });
mockDiskSpace(LOW_FREE_SPACE);
Expand All @@ -152,7 +170,7 @@ describe('PathHandlers', () => {
});

it('rejects non-writable path', async () => {
mockFileSystem({ exists: true, writable: false });
mockFileSystem({ exists: true, writable: false, isDirectory: true, contentLength: 1 });

const result = await validateHandler({}, '/non/writable/path');
expect(result).toEqual({
Expand Down
Loading