|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { discover } from './discovery'; |
| 10 | +import { MockHost } from './testing/mock-host'; |
| 11 | + |
| 12 | +describe('discover', () => { |
| 13 | + it('should find a lockfile in the starting directory', async () => { |
| 14 | + const host = new MockHost({ |
| 15 | + '/project': ['package-lock.json'], |
| 16 | + }); |
| 17 | + const result = await discover(host, '/project'); |
| 18 | + expect(result).toBe('npm'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should find a lockfile in a parent directory', async () => { |
| 22 | + const host = new MockHost({ |
| 23 | + '/project': ['yarn.lock'], |
| 24 | + '/project/subdir': [], |
| 25 | + }); |
| 26 | + const result = await discover(host, '/project/subdir'); |
| 27 | + expect(result).toBe('yarn'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return null if no lockfile is found up to the root', async () => { |
| 31 | + const host = new MockHost({ |
| 32 | + '/': [], |
| 33 | + '/project': [], |
| 34 | + '/project/subdir': [], |
| 35 | + }); |
| 36 | + const result = await discover(host, '/project/subdir'); |
| 37 | + expect(result).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should apply precedence when multiple lockfiles are found', async () => { |
| 41 | + const host = new MockHost({ |
| 42 | + '/project': ['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock'], |
| 43 | + }); |
| 44 | + // pnpm should have the highest precedence according to the descriptor. |
| 45 | + const result = await discover(host, '/project'); |
| 46 | + expect(result).toBe('pnpm'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should stop searching at a .git boundary', async () => { |
| 50 | + const host = new MockHost({ |
| 51 | + '/': ['yarn.lock'], |
| 52 | + '/project/.git': true, // .git is mocked as a directory. |
| 53 | + '/project/subdir': [], |
| 54 | + }); |
| 55 | + const result = await discover(host, '/project/subdir'); |
| 56 | + expect(result).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should stop searching at the filesystem root', async () => { |
| 60 | + const host = new MockHost({ |
| 61 | + '/': [], |
| 62 | + }); |
| 63 | + const result = await discover(host, '/'); |
| 64 | + expect(result).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle file system errors during readdir gracefully', async () => { |
| 68 | + const host = new MockHost({}); |
| 69 | + host.readdir = () => Promise.reject(new Error('Permission denied')); |
| 70 | + |
| 71 | + const result = await discover(host, '/project'); |
| 72 | + expect(result).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle file system errors during stat gracefully', async () => { |
| 76 | + const host = new MockHost({ '/project': ['.git'] }); |
| 77 | + host.stat = () => Promise.reject(new Error('Permission denied')); |
| 78 | + |
| 79 | + // The error on stat should prevent it from finding the .git dir and thus it will continue to the root. |
| 80 | + const result = await discover(host, '/project'); |
| 81 | + expect(result).toBeNull(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should prioritize the closest lockfile, regardless of precedence', async () => { |
| 85 | + const host = new MockHost({ |
| 86 | + '/project': ['pnpm-lock.yaml'], // Higher precedence |
| 87 | + '/project/subdir': ['package-lock.json'], // Lower precedence |
| 88 | + }); |
| 89 | + const result = await discover(host, '/project/subdir'); |
| 90 | + // Should find 'npm' and stop, not continue to find 'pnpm'. |
| 91 | + expect(result).toBe('npm'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should find a lockfile in the git root directory', async () => { |
| 95 | + const host = new MockHost({ |
| 96 | + '/project': ['yarn.lock'], |
| 97 | + '/project/.git': true, |
| 98 | + '/project/subdir': [], |
| 99 | + }); |
| 100 | + const result = await discover(host, '/project/subdir'); |
| 101 | + expect(result).toBe('yarn'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should discover the alternate npm lockfile name', async () => { |
| 105 | + const host = new MockHost({ |
| 106 | + '/project': ['npm-shrinkwrap.json'], |
| 107 | + }); |
| 108 | + const result = await discover(host, '/project'); |
| 109 | + expect(result).toBe('npm'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should discover the alternate bun lockfile name', async () => { |
| 113 | + const host = new MockHost({ |
| 114 | + '/project': ['bun.lockb'], |
| 115 | + }); |
| 116 | + const result = await discover(host, '/project'); |
| 117 | + expect(result).toBe('bun'); |
| 118 | + }); |
| 119 | +}); |
0 commit comments