Skip to content

Commit e999939

Browse files
authored
fix: resolved path issues on Windows in tests (#12)
1 parent 7617499 commit e999939

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

tests/main.test.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ project/
9393
const [nodes, basePath] = parseAsciiTree(asciiTree);
9494
const validation = validateTree(nodes, basePath);
9595
expect(validation.isValid).toBe(false);
96-
expect(validation.errors).toContain('Duplicate path found: project/src/index.js');
96+
expect(validation.errors).toContain(`Duplicate path found: ${path.join('project', 'src', 'index.js')}`);
9797
});
9898

9999
test('should detect invalid characters in names', () => {
@@ -107,7 +107,7 @@ project/
107107
const [nodes, basePath] = parseAsciiTree(asciiTree);
108108
const validation = validateTree(nodes, basePath);
109109
expect(validation.isValid).toBe(false);
110-
expect(validation.errors).toContain('Invalid characters in name: project/src/file:name.js');
110+
expect(validation.errors).toContain(`Invalid characters in name: ${path.join('project', 'src', 'file:name.js')}`);
111111
});
112112

113113
test('should warn about empty directories', () => {
@@ -120,7 +120,7 @@ project/
120120
const [nodes, basePath] = parseAsciiTree(asciiTree);
121121
const validation = validateTree(nodes, basePath);
122122
expect(validation.isValid).toBe(true);
123-
expect(validation.warnings).toContain('Empty directory: project/src/empty');
123+
expect(validation.warnings).toContain(`Empty directory: ${path.join('project', 'src', 'empty')}`);
124124
});
125125

126126
test('should validate paths with special characters in directory names', () => {
@@ -287,37 +287,61 @@ dry-run-project/
287287
});
288288

289289
test('should handle file system permission errors', async () => {
290+
// Enable test mode to ensure errors are properly caught
291+
enableTestMode();
292+
290293
const asciiTree = `
291294
project/
292295
└── file.js
293296
`;
294297
const readOnlyDir = path.join(testOutputDir, 'readonly');
295298
fs.mkdirSync(readOnlyDir, { recursive: true });
296-
fs.chmodSync(readOnlyDir, 0o444); // Read-only permissions
297-
298-
await expect(generate(asciiTree, readOnlyDir))
299-
.rejects
300-
.toThrow();
301-
302-
fs.chmodSync(readOnlyDir, 0o777); // Restore permissions
299+
300+
// Create a file in the directory to make it non-empty
301+
fs.writeFileSync(path.join(readOnlyDir, 'existing.txt'), 'test');
302+
303+
// Mock fs.promises.mkdir to simulate permission error
304+
const mkdirSpy = jest.spyOn(fs.promises, 'mkdir')
305+
.mockImplementation(() => Promise.reject(new Error('EACCES: permission denied')));
306+
307+
try {
308+
await expect(generate(asciiTree, readOnlyDir))
309+
.rejects
310+
.toThrow(/EACCES/);
311+
} finally {
312+
// Clean up
313+
mkdirSpy.mockRestore();
314+
if (fs.existsSync(readOnlyDir)) {
315+
fs.rmSync(readOnlyDir, { recursive: true, force: true });
316+
}
317+
}
303318
});
304319

305320
test('should handle invalid output directory', async () => {
321+
// Enable test mode to ensure errors are properly caught
322+
enableTestMode();
323+
306324
const asciiTree = `
307325
project/
308326
└── file.js
309327
`;
310328
const invalidPath = path.join(testOutputDir, 'nonexistent', 'subdir');
311-
// Create parent directory but make it read-only
312-
fs.mkdirSync(path.dirname(invalidPath), { recursive: true });
313-
fs.chmodSync(path.dirname(invalidPath), 0o444);
314-
315-
await expect(generate(asciiTree, invalidPath))
316-
.rejects
317-
.toThrow();
318-
319-
// Restore permissions
320-
fs.chmodSync(path.dirname(invalidPath), 0o777);
329+
330+
// Mock fs.promises.mkdir to simulate permission error
331+
const mkdirSpy = jest.spyOn(fs.promises, 'mkdir')
332+
.mockImplementation(() => Promise.reject(new Error('EACCES: permission denied')));
333+
334+
try {
335+
await expect(generate(asciiTree, invalidPath))
336+
.rejects
337+
.toThrow(/EACCES/);
338+
} finally {
339+
// Clean up
340+
mkdirSpy.mockRestore();
341+
if (fs.existsSync(path.dirname(invalidPath))) {
342+
fs.rmSync(path.dirname(invalidPath), { recursive: true, force: true });
343+
}
344+
}
321345
});
322346

323347
test('should handle paths with special characters in directory names', async () => {

0 commit comments

Comments
 (0)