path.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // pathUtils.test.ts
  2. import { describe, expect, it } from 'vitest';
  3. import { toPosixPath } from './path';
  4. describe('toPosixPath', () => {
  5. // 测试 Windows 风格路径到 POSIX 风格路径的转换
  6. it('converts Windows-style paths to POSIX paths', () => {
  7. const windowsPath = String.raw`C:\Users\Example\file.txt`;
  8. const expectedPosixPath = 'C:/Users/Example/file.txt';
  9. expect(toPosixPath(windowsPath)).toBe(expectedPosixPath);
  10. });
  11. // 确认 POSIX 风格路径不会被改变
  12. it('leaves POSIX-style paths unchanged', () => {
  13. const posixPath = '/home/user/file.txt';
  14. expect(toPosixPath(posixPath)).toBe(posixPath);
  15. });
  16. // 测试带有多个分隔符的路径
  17. it('converts paths with mixed separators', () => {
  18. const mixedPath = String.raw`C:/Users\Example\file.txt`;
  19. const expectedPosixPath = 'C:/Users/Example/file.txt';
  20. expect(toPosixPath(mixedPath)).toBe(expectedPosixPath);
  21. });
  22. // 测试空字符串
  23. it('handles empty strings', () => {
  24. const emptyPath = '';
  25. expect(toPosixPath(emptyPath)).toBe('');
  26. });
  27. // 测试仅包含分隔符的路径
  28. it('handles path with only separators', () => {
  29. const separatorsPath = '\\\\\\';
  30. const expectedPosixPath = '///';
  31. expect(toPosixPath(separatorsPath)).toBe(expectedPosixPath);
  32. });
  33. // 测试不包含任何分隔符的路径
  34. it('handles path without separators', () => {
  35. const noSeparatorPath = 'file.txt';
  36. expect(toPosixPath(noSeparatorPath)).toBe('file.txt');
  37. });
  38. // 测试以分隔符结尾的路径
  39. it('handles path ending with a separator', () => {
  40. const endingSeparatorPath = 'C:\\Users\\Example\\';
  41. const expectedPosixPath = 'C:/Users/Example/';
  42. expect(toPosixPath(endingSeparatorPath)).toBe(expectedPosixPath);
  43. });
  44. // 测试以分隔符开头的路径
  45. it('handles path starting with a separator', () => {
  46. const startingSeparatorPath = String.raw`\Users\Example`;
  47. const expectedPosixPath = '/Users/Example';
  48. expect(toPosixPath(startingSeparatorPath)).toBe(expectedPosixPath);
  49. });
  50. // 测试包含非法字符的路径
  51. it('handles path with invalid characters', () => {
  52. const invalidCharsPath = String.raw`C:\Us*?ers\Ex<ample>|file.txt`;
  53. const expectedPosixPath = 'C:/Us*?ers/Ex<ample>|file.txt';
  54. expect(toPosixPath(invalidCharsPath)).toBe(expectedPosixPath);
  55. });
  56. });