forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpackage-manager_spec.ts
More file actions
116 lines (94 loc) · 4.22 KB
/
package-manager_spec.ts
File metadata and controls
116 lines (94 loc) · 4.22 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
112
113
114
115
116
/**
* @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 { MANIFEST_FIELDS, PackageManager } from './package-manager';
import { SUPPORTED_PACKAGE_MANAGERS } from './package-manager-descriptor';
import { MockHost } from './testing/mock-host';
describe('PackageManager', () => {
let host: MockHost;
let runCommandSpy: jasmine.Spy;
const descriptor = SUPPORTED_PACKAGE_MANAGERS['npm'];
beforeEach(() => {
host = new MockHost();
runCommandSpy = spyOn(host, 'runCommand').and.resolveTo({ stdout: '1.2.3', stderr: '' });
});
describe('getRegistryManifest', () => {
it('should quote complex range specifiers when required by the host', async () => {
// Simulate a quoting host
Object.assign(host, { requiresQuoting: true });
const pm = new PackageManager(host, '/tmp', descriptor);
const manifest = { name: 'foo', version: '1.0.0' };
runCommandSpy.and.resolveTo({ stdout: JSON.stringify(manifest), stderr: '' });
await pm.getRegistryManifest('foo', '>=1.0.0 <2.0.0');
expect(runCommandSpy).toHaveBeenCalledWith(
descriptor.binary,
[...descriptor.getManifestCommand, '"foo@>=1.0.0 <2.0.0"', ...MANIFEST_FIELDS],
jasmine.anything(),
);
});
it('should NOT quote complex range specifiers when not required by the host', async () => {
// Simulate a non-quoting host
Object.assign(host, { requiresQuoting: false });
const pm = new PackageManager(host, '/tmp', descriptor);
const manifest = { name: 'foo', version: '1.0.0' };
runCommandSpy.and.resolveTo({ stdout: JSON.stringify(manifest), stderr: '' });
await pm.getRegistryManifest('foo', '>=1.0.0 <2.0.0');
expect(runCommandSpy).toHaveBeenCalledWith(
descriptor.binary,
[...descriptor.getManifestCommand, 'foo@>=1.0.0 <2.0.0', ...MANIFEST_FIELDS],
jasmine.anything(),
);
});
});
describe('getVersion', () => {
it('should fetch the version from the package manager if not cached', async () => {
const pm = new PackageManager(host, '/tmp', descriptor);
const version = await pm.getVersion();
expect(version).toBe('1.2.3');
expect(runCommandSpy).toHaveBeenCalledWith(
descriptor.binary,
descriptor.versionCommand,
jasmine.objectContaining({ cwd: '/tmp' }),
);
});
it('should cache the version after the first fetch', async () => {
const pm = new PackageManager(host, '/tmp', descriptor);
await pm.getVersion();
await pm.getVersion();
expect(runCommandSpy).toHaveBeenCalledTimes(1);
});
it('should use the version provided in the constructor', async () => {
const pm = new PackageManager(host, '/tmp', descriptor, { version: '4.5.6' });
const version = await pm.getVersion();
expect(version).toBe('4.5.6');
expect(runCommandSpy).not.toHaveBeenCalled();
});
});
describe('initializationError', () => {
it('should throw initializationError when running commands', async () => {
const error = new Error('Not installed');
const pm = new PackageManager(host, '/tmp', descriptor, { initializationError: error });
expect(() => pm.ensureInstalled()).toThrow(error);
await expectAsync(pm.getVersion()).toBeRejectedWith(error);
await expectAsync(pm.install()).toBeRejectedWith(error);
await expectAsync(pm.add('foo', 'none', false, false, false)).toBeRejectedWith(error);
});
it('should not throw initializationError for operations that do not require the binary', async () => {
const error = new Error('Not installed');
const pm = new PackageManager(host, '/tmp', descriptor, { initializationError: error });
// Mock readFile for getManifest directory case
spyOn(host, 'readFile').and.resolveTo('{"name": "foo", "version": "1.0.0"}');
// Should not throw
const manifest = await pm.getManifest({
type: 'directory',
fetchSpec: '/tmp/foo',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
expect(manifest).toEqual({ name: 'foo', version: '1.0.0' });
});
});
});