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