|
|
|
@@ -3,46 +3,46 @@ import * as smartpath from '../ts_iso/index.js';
|
|
|
|
|
|
|
|
|
|
tap.test('pathJoin - should join path segments correctly', async () => {
|
|
|
|
|
// Basic path joining
|
|
|
|
|
expect(smartpath.pathJoin('path', 'to', 'file.txt')).toEqual('path/to/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('/path', 'to', 'file.txt')).toEqual('/path/to/file.txt');
|
|
|
|
|
expect(smartpath.join('path', 'to', 'file.txt')).toEqual('path/to/file.txt');
|
|
|
|
|
expect(smartpath.join('/path', 'to', 'file.txt')).toEqual('/path/to/file.txt');
|
|
|
|
|
|
|
|
|
|
// Windows paths
|
|
|
|
|
expect(smartpath.pathJoin('C:', 'Users', 'test')).toEqual('C:\\Users\\test');
|
|
|
|
|
expect(smartpath.pathJoin('C:\\Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
expect(smartpath.join('C:', 'Users', 'test')).toEqual('C:\\Users\\test');
|
|
|
|
|
expect(smartpath.join('C:\\Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
|
|
|
|
|
// Empty segments
|
|
|
|
|
expect(smartpath.pathJoin('path', '', 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('', 'path', 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.join('path', '', 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.join('', 'path', 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
|
|
|
|
|
// No segments
|
|
|
|
|
expect(smartpath.pathJoin()).toEqual('');
|
|
|
|
|
expect(smartpath.pathJoin('')).toEqual('');
|
|
|
|
|
expect(smartpath.join()).toEqual('');
|
|
|
|
|
expect(smartpath.join('')).toEqual('');
|
|
|
|
|
|
|
|
|
|
// Single segment
|
|
|
|
|
expect(smartpath.pathJoin('path')).toEqual('path');
|
|
|
|
|
expect(smartpath.pathJoin('/path')).toEqual('/path');
|
|
|
|
|
expect(smartpath.join('path')).toEqual('path');
|
|
|
|
|
expect(smartpath.join('/path')).toEqual('/path');
|
|
|
|
|
|
|
|
|
|
// Multiple separators
|
|
|
|
|
expect(smartpath.pathJoin('path/', '/to/', '/file.txt')).toEqual('path/to/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('path\\\\', '\\\\to\\\\', '\\\\file.txt')).toEqual('path\\to\\file.txt');
|
|
|
|
|
expect(smartpath.join('path/', '/to/', '/file.txt')).toEqual('path/to/file.txt');
|
|
|
|
|
expect(smartpath.join('path\\\\', '\\\\to\\\\', '\\\\file.txt')).toEqual('path\\to\\file.txt');
|
|
|
|
|
|
|
|
|
|
// Root paths
|
|
|
|
|
expect(smartpath.pathJoin('/')).toEqual('/');
|
|
|
|
|
expect(smartpath.pathJoin('/', 'path')).toEqual('/path');
|
|
|
|
|
expect(smartpath.join('/')).toEqual('/');
|
|
|
|
|
expect(smartpath.join('/', 'path')).toEqual('/path');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('pathJoin - should handle file:// URLs', async () => {
|
|
|
|
|
// Unix file URLs
|
|
|
|
|
expect(smartpath.pathJoin('file:///home/user', 'documents', 'file.txt')).toEqual('/home/user/documents/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('file:///home/user/', 'documents')).toEqual('/home/user/documents');
|
|
|
|
|
expect(smartpath.join('file:///home/user', 'documents', 'file.txt')).toEqual('/home/user/documents/file.txt');
|
|
|
|
|
expect(smartpath.join('file:///home/user/', 'documents')).toEqual('/home/user/documents');
|
|
|
|
|
|
|
|
|
|
// Windows file URLs
|
|
|
|
|
expect(smartpath.pathJoin('file:///C:/Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('file:///D:/Projects/', 'app')).toEqual('D:\\Projects\\app');
|
|
|
|
|
expect(smartpath.join('file:///C:/Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
expect(smartpath.join('file:///D:/Projects/', 'app')).toEqual('D:\\Projects\\app');
|
|
|
|
|
|
|
|
|
|
// Mixed file URL and path
|
|
|
|
|
expect(smartpath.pathJoin('file:///home/user', '../test')).toEqual('/home/user/../test');
|
|
|
|
|
expect(smartpath.pathJoin('documents', 'file:///home/user/file.txt')).toEqual('documents/home/user/file.txt');
|
|
|
|
|
expect(smartpath.join('file:///home/user', '../test')).toEqual('/home/user/../test');
|
|
|
|
|
expect(smartpath.join('documents', 'file:///home/user/file.txt')).toEqual('documents/home/user/file.txt');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('fileUrlToPath - should convert file URLs to paths', async () => {
|
|
|
|
@@ -110,36 +110,36 @@ tap.test('dirname - should extract directory from paths and URLs', async () => {
|
|
|
|
|
|
|
|
|
|
tap.test('edge cases - should handle edge cases correctly', async () => {
|
|
|
|
|
// Non-string values filtered out
|
|
|
|
|
expect(smartpath.pathJoin('path', null as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('path', undefined as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('path', 123 as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.join('path', null as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.join('path', undefined as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
expect(smartpath.join('path', 123 as any, 'file.txt')).toEqual('path/file.txt');
|
|
|
|
|
|
|
|
|
|
// Very long paths
|
|
|
|
|
const longSegment = 'a'.repeat(100);
|
|
|
|
|
const result = smartpath.pathJoin(longSegment, longSegment, 'file.txt');
|
|
|
|
|
const result = smartpath.join(longSegment, longSegment, 'file.txt');
|
|
|
|
|
expect(result).toEqual(`${longSegment}/${longSegment}/file.txt`);
|
|
|
|
|
|
|
|
|
|
// Unicode characters
|
|
|
|
|
expect(smartpath.pathJoin('path', '文件夹', 'файл.txt')).toEqual('path/文件夹/файл.txt');
|
|
|
|
|
expect(smartpath.join('path', '文件夹', 'файл.txt')).toEqual('path/文件夹/файл.txt');
|
|
|
|
|
expect(smartpath.fileUrlToPath('file:///home/用户/文件.txt')).toEqual('/home/用户/文件.txt');
|
|
|
|
|
expect(smartpath.pathToFileUrl('/home/用户/文件.txt')).toEqual('file:///home/%E7%94%A8%E6%88%B7/%E6%96%87%E4%BB%B6.txt');
|
|
|
|
|
|
|
|
|
|
// Backslashes in Unix-style paths (should be preserved)
|
|
|
|
|
expect(smartpath.pathJoin('/home/user', 'path\\with\\backslashes')).toEqual('/home/user/path\\with\\backslashes');
|
|
|
|
|
expect(smartpath.join('/home/user', 'path\\with\\backslashes')).toEqual('/home/user/path\\with\\backslashes');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('cross-platform behavior - should detect separators correctly', async () => {
|
|
|
|
|
// Should detect Windows paths and use backslashes
|
|
|
|
|
expect(smartpath.pathJoin('C:\\Users', 'test')).toEqual('C:\\Users\\test');
|
|
|
|
|
expect(smartpath.pathJoin('D:', 'Projects', 'app')).toEqual('D:\\Projects\\app');
|
|
|
|
|
expect(smartpath.join('C:\\Users', 'test')).toEqual('C:\\Users\\test');
|
|
|
|
|
expect(smartpath.join('D:', 'Projects', 'app')).toEqual('D:\\Projects\\app');
|
|
|
|
|
|
|
|
|
|
// Should detect Unix paths and use forward slashes
|
|
|
|
|
expect(smartpath.pathJoin('/home', 'user')).toEqual('/home/user');
|
|
|
|
|
expect(smartpath.pathJoin('/var', 'log', 'app.log')).toEqual('/var/log/app.log');
|
|
|
|
|
expect(smartpath.join('/home', 'user')).toEqual('/home/user');
|
|
|
|
|
expect(smartpath.join('/var', 'log', 'app.log')).toEqual('/var/log/app.log');
|
|
|
|
|
|
|
|
|
|
// Mixed paths - first segment determines separator
|
|
|
|
|
expect(smartpath.pathJoin('C:\\Users', 'test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
expect(smartpath.pathJoin('/home', 'user\\documents')).toEqual('/home/user\\documents');
|
|
|
|
|
expect(smartpath.join('C:\\Users', 'test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
|
|
|
|
|
expect(smartpath.join('/home', 'user\\documents')).toEqual('/home/user\\documents');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tap.start();
|