Skip to content

Commit d20713d

Browse files
authored
Check if directory is empty before installation warning. (#1060)
1 parent 5a3a449 commit d20713d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/handlers/pathHandlers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ export function registerPathHandlers() {
5555
result.parentMissing = true;
5656
}
5757

58-
// Check if path exists
58+
// Check if path exists and is not an empty directory
5959
if (fs.existsSync(inputPath)) {
60-
result.exists = true;
60+
if (fs.statSync(inputPath).isDirectory()) {
61+
const contents = fs.readdirSync(inputPath);
62+
result.exists = contents.length > 0;
63+
} else {
64+
result.exists = true;
65+
}
6166
}
6267

6368
// Check if path is writable

tests/unit/handlers/pathHandler.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ const mockDiskSpace = (available: number) => {
7272
]);
7373
};
7474

75-
const mockFileSystem = ({ exists = true, writable = true } = {}) => {
75+
const mockFileSystem = ({ exists = true, writable = true, isDirectory = false, contentLength = 0 } = {}) => {
7676
vi.mocked(fs.existsSync).mockReturnValue(exists);
77+
vi.mocked(fs.statSync).mockReturnValue({
78+
isDirectory: () => isDirectory,
79+
} as unknown as fs.Stats);
80+
vi.mocked(fs.readdirSync).mockReturnValue(
81+
Array.from({ length: contentLength }, () => ({ name: 'mock-file' }) as fs.Dirent)
82+
);
7783
if (writable) {
7884
vi.mocked(fs.accessSync).mockReturnValue();
7985
} else {
@@ -126,6 +132,18 @@ describe('PathHandlers', () => {
126132
});
127133
});
128134

135+
it('does not exist if directory is empty', async () => {
136+
mockFileSystem({ exists: true, writable: true, contentLength: 0, isDirectory: true });
137+
138+
const result = await validateHandler({}, '/valid/path');
139+
expect(result).toEqual({
140+
isValid: true,
141+
exists: false,
142+
freeSpace: DEFAULT_FREE_SPACE,
143+
requiredSpace: REQUIRED_SPACE,
144+
});
145+
});
146+
129147
it('rejects path with insufficient disk space', async () => {
130148
mockFileSystem({ exists: true, writable: true });
131149
mockDiskSpace(LOW_FREE_SPACE);
@@ -152,7 +170,7 @@ describe('PathHandlers', () => {
152170
});
153171

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

157175
const result = await validateHandler({}, '/non/writable/path');
158176
expect(result).toEqual({

0 commit comments

Comments
 (0)