|
| 1 | +import os from 'node:os'; |
1 | 2 | import test from 'ava'; |
2 | 3 | import esmock from 'esmock'; |
3 | 4 |
|
@@ -49,3 +50,53 @@ test('paths with regex replacement patterns', t => { |
49 | 50 | t.true(expandsTildePrefixWithHome('~/$1')); |
50 | 51 | t.true(expandsTildePrefixWithHome('~/$&')); |
51 | 52 | }); |
| 53 | + |
| 54 | +// Tests for ~user expansion |
| 55 | +test('~user expansion for current user', async t => { |
| 56 | + // Use the actual untildify function without mocking for this test |
| 57 | + const {default: realUntildify} = await import('./index.js'); |
| 58 | + const currentUser = os.userInfo().username; |
| 59 | + const currentHome = os.homedir(); |
| 60 | + |
| 61 | + // ~currentuser should expand to current user's home |
| 62 | + t.is(realUntildify(`~${currentUser}`), currentHome); |
| 63 | + t.is(realUntildify(`~${currentUser}/`), `${currentHome}/`); |
| 64 | + t.is(realUntildify(`~${currentUser}/dev`), `${currentHome}/dev`); |
| 65 | +}); |
| 66 | + |
| 67 | +test('~user expansion for other users', async t => { |
| 68 | + // Use the actual untildify function without mocking for this test |
| 69 | + const {default: realUntildify} = await import('./index.js'); |
| 70 | + |
| 71 | + // Other users should return unchanged (simplified implementation) |
| 72 | + t.is(realUntildify('~root'), '~root'); |
| 73 | + t.is(realUntildify('~root/'), '~root/'); |
| 74 | + t.is(realUntildify('~otheruser/test'), '~otheruser/test'); |
| 75 | +}); |
| 76 | + |
| 77 | +test('~user expansion for non-existent users', async t => { |
| 78 | + // Use the actual untildify function without mocking for this test |
| 79 | + const {default: realUntildify} = await import('./index.js'); |
| 80 | + |
| 81 | + // Non-existent users should return unchanged |
| 82 | + t.is(realUntildify('~nonexistentuser123'), '~nonexistentuser123'); |
| 83 | + t.is(realUntildify('~nonexistentuser123/'), '~nonexistentuser123/'); |
| 84 | + t.is(realUntildify('~nonexistentuser123/dev'), '~nonexistentuser123/dev'); |
| 85 | +}); |
| 86 | + |
| 87 | +test('~user expansion edge cases', async t => { |
| 88 | + // Use the actual untildify function without mocking for this test |
| 89 | + const {default: realUntildify} = await import('./index.js'); |
| 90 | + |
| 91 | + // Should not expand if not at the beginning |
| 92 | + t.is(realUntildify('foo~bar'), 'foo~bar'); |
| 93 | + t.is(realUntildify('/~user'), '/~user'); |
| 94 | + t.is(realUntildify('abc~def/ghi'), 'abc~def/ghi'); |
| 95 | + |
| 96 | + // Empty username after ~ (just ~ alone should still work) |
| 97 | + t.is(realUntildify('~'), os.homedir()); |
| 98 | + |
| 99 | + // Username containing special characters that don't typically appear in usernames |
| 100 | + t.is(realUntildify('~user!!invalid'), '~user!!invalid'); |
| 101 | + t.is(realUntildify('~user@host'), '~user@host'); |
| 102 | +}); |
0 commit comments