|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { homedir } from 'node:os'; |
| 4 | +import { expandHomePrefix, resolvePathFromCwd } from '../path.ts'; |
| 5 | + |
| 6 | +describe('expandHomePrefix', () => { |
| 7 | + it('expands a bare ~ to the home directory', () => { |
| 8 | + expect(expandHomePrefix('~')).toBe(homedir()); |
| 9 | + }); |
| 10 | + |
| 11 | + it('expands a leading ~/ to the home directory', () => { |
| 12 | + expect(expandHomePrefix('~/foo/bar')).toBe(path.join(homedir(), 'foo/bar')); |
| 13 | + }); |
| 14 | + |
| 15 | + it('expands a leading ~\\ on Windows-style separators', () => { |
| 16 | + expect(expandHomePrefix('~\\foo\\bar')).toBe(path.join(homedir(), 'foo\\bar')); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns absolute paths unchanged', () => { |
| 20 | + expect(expandHomePrefix('/absolute/path')).toBe('/absolute/path'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns relative paths unchanged', () => { |
| 24 | + expect(expandHomePrefix('relative/path')).toBe('relative/path'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('does not expand ~user style prefixes', () => { |
| 28 | + expect(expandHomePrefix('~other/foo')).toBe('~other/foo'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not expand ~ embedded later in the path', () => { |
| 32 | + expect(expandHomePrefix('foo/~/bar')).toBe('foo/~/bar'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns an empty string unchanged', () => { |
| 36 | + expect(expandHomePrefix('')).toBe(''); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('resolvePathFromCwd', () => { |
| 41 | + it('expands a bare ~ to the home directory', () => { |
| 42 | + expect(resolvePathFromCwd('~')).toBe(homedir()); |
| 43 | + }); |
| 44 | + |
| 45 | + it('expands a leading ~/ under the home directory', () => { |
| 46 | + expect(resolvePathFromCwd('~/.foo/derivedData')).toBe(path.join(homedir(), '.foo/derivedData')); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns absolute paths unchanged', () => { |
| 50 | + expect(resolvePathFromCwd('/abs/path')).toBe('/abs/path'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('resolves relative paths against process.cwd() by default', () => { |
| 54 | + expect(resolvePathFromCwd('rel/path')).toBe(path.resolve(process.cwd(), 'rel/path')); |
| 55 | + }); |
| 56 | + |
| 57 | + it('resolves relative paths against an explicit cwd when provided', () => { |
| 58 | + expect(resolvePathFromCwd('rel/path', '/some/base')).toBe( |
| 59 | + path.resolve('/some/base', 'rel/path'), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not resolve absolute paths against an explicit cwd', () => { |
| 64 | + expect(resolvePathFromCwd('/abs/path', '/some/base')).toBe('/abs/path'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('does not expand ~user style prefixes', () => { |
| 68 | + expect(resolvePathFromCwd('~other/foo')).toBe(path.resolve(process.cwd(), '~other/foo')); |
| 69 | + }); |
| 70 | +}); |
0 commit comments