Skip to content

Commit 12f3158

Browse files
committed
test(filesystem): add tests for paths containing tilde character
Add comprehensive test coverage to verify the filesystem server correctly handles paths containing ~ as a literal character in folder names. This is a common naming convention to force folders to sort to the top of directory listings (e.g., ~MyFolder). The existing code already handles these paths correctly: - expandHome() only expands ~/ and ~ (home directory shorthand) - ~MyFolder and /path/to/~MyFolder are treated as literal paths Adds tests for: - expandHome with ~ as literal folder prefix - Server startup with directories containing ~ in name - Various edge cases (multiple tildes, tilde at end, etc.) Fixes modelcontextprotocol#3412
1 parent f424458 commit 12f3158

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/filesystem/__tests__/path-utils.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ describe('Path Utilities', () => {
225225
it('leaves other paths unchanged', () => {
226226
expect(expandHome('C:/test')).toBe('C:/test');
227227
});
228+
229+
it('treats ~MyFolder as literal path, not home directory expansion', () => {
230+
// Paths like ~MyFolder should NOT be expanded to home directory
231+
// This is a common convention to sort folders to top of directory listings
232+
expect(expandHome('~MyFolder')).toBe('~MyFolder');
233+
expect(expandHome('/path/to/~MyFolder')).toBe('/path/to/~MyFolder');
234+
expect(expandHome('/Volumes/Drive/Projects/~MyFolder')).toBe('/Volumes/Drive/Projects/~MyFolder');
235+
});
236+
237+
it('handles edge cases with tilde in different positions', () => {
238+
// Tilde in middle of path should be preserved
239+
expect(expandHome('/path/~MyFolder/subdir')).toBe('/path/~MyFolder/subdir');
240+
// Multiple tildes
241+
expect(expandHome('~Folder1/~Folder2')).toBe('~Folder1/~Folder2');
242+
// Tilde at end
243+
expect(expandHome('/path/file~')).toBe('/path/file~');
244+
});
228245
});
229246

230247
describe('WSL path handling (issue #2795 fix)', () => {

‎src/filesystem/__tests__/startup-validation.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,30 @@ describe('Startup Directory Validation', () => {
9797
// Should still start with the valid directory
9898
expect(result.stderr).toContain('Secure MCP Filesystem Server running on stdio');
9999
});
100+
101+
it('should handle directories with tilde (~) prefix in name', async () => {
102+
// Create a directory with ~ prefix (common convention to sort folders to top)
103+
const tildeDir = path.join(testDir, '~MyFolder');
104+
await fs.mkdir(tildeDir, { recursive: true });
105+
106+
const result = await spawnServer([tildeDir]);
107+
108+
// Server should start successfully (not crash)
109+
expect(result.stderr).toContain('Secure MCP Filesystem Server running on stdio');
110+
expect(result.stderr).not.toContain('Error:');
111+
expect(result.exitCode).not.toBe(1);
112+
});
113+
114+
it('should handle paths containing tilde (~) character in middle of path', async () => {
115+
// Create nested directories with ~ in the name
116+
const nestedTildeDir = path.join(testDir, 'Projects', '~Important');
117+
await fs.mkdir(nestedTildeDir, { recursive: true });
118+
119+
const result = await spawnServer([nestedTildeDir]);
120+
121+
// Server should start successfully (not crash)
122+
expect(result.stderr).toContain('Secure MCP Filesystem Server running on stdio');
123+
expect(result.stderr).not.toContain('Error:');
124+
expect(result.exitCode).not.toBe(1);
125+
});
100126
});

0 commit comments

Comments
 (0)