forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_spec.ts
More file actions
150 lines (123 loc) · 5.07 KB
/
cli_spec.ts
File metadata and controls
150 lines (123 loc) · 5.07 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @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 * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import type { InstalledPackage } from '../../package-managers';
import { supplementWithLocalDependencies } from './cli';
/**
* Creates a minimal on-disk fixture that simulates an npm workspace member:
*
* <projectRoot>/
* package.json ← Angular project manifest (workspace member)
* node_modules/
* <depName>/
* package.json ← installed package manifest
*/
async function createWorkspaceMemberFixture(options: {
projectDeps: Record<string, string>;
installedPackages: Array<{ name: string; version: string }>;
}): Promise<string> {
const projectRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'ng-update-spec-'));
// Write the Angular project's package.json
await fs.writeFile(
path.join(projectRoot, 'package.json'),
JSON.stringify({
name: 'test-app',
version: '0.0.0',
dependencies: options.projectDeps,
}),
);
// Write each installed package into node_modules
for (const pkg of options.installedPackages) {
// Support scoped packages like @angular/core
const pkgDir = path.join(projectRoot, 'node_modules', ...pkg.name.split('/'));
await fs.mkdir(pkgDir, { recursive: true });
await fs.writeFile(
path.join(pkgDir, 'package.json'),
JSON.stringify({ name: pkg.name, version: pkg.version }),
);
}
return projectRoot;
}
describe('supplementWithLocalDependencies', () => {
let tmpDir: string;
afterEach(async () => {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it('should add packages from the local package.json that are missing from the dependency map', async () => {
// Simulates an npm workspace member where `npm list` (run against the
// workspace root) did not return `@angular/core`, even though it is
// declared in the member's package.json and installed in node_modules.
tmpDir = await createWorkspaceMemberFixture({
projectDeps: { '@angular/core': '^21.0.0' },
installedPackages: [{ name: '@angular/core', version: '21.2.4' }],
});
const deps = new Map<string, InstalledPackage>();
await supplementWithLocalDependencies(deps, tmpDir);
expect(deps.has('@angular/core')).toBeTrue();
expect(deps.get('@angular/core')?.version).toBe('21.2.4');
});
it('should not overwrite a package that is already present in the dependency map', async () => {
tmpDir = await createWorkspaceMemberFixture({
projectDeps: { '@angular/core': '^21.0.0' },
installedPackages: [{ name: '@angular/core', version: '21.2.4' }],
});
// The package manager already returned a version for @angular/core.
const existingEntry: InstalledPackage = { name: '@angular/core', version: '21.0.0' };
const deps = new Map<string, InstalledPackage>([['@angular/core', existingEntry]]);
await supplementWithLocalDependencies(deps, tmpDir);
// The existing entry must not be overwritten.
expect(deps.get('@angular/core')).toBe(existingEntry);
expect(deps.get('@angular/core')?.version).toBe('21.0.0');
});
it('should skip packages that are declared in package.json but not installed in node_modules', async () => {
tmpDir = await createWorkspaceMemberFixture({
projectDeps: { 'not-installed': '^1.0.0' },
installedPackages: [],
});
const deps = new Map<string, InstalledPackage>();
await supplementWithLocalDependencies(deps, tmpDir);
// Package is not installed; should not be added.
expect(deps.has('not-installed')).toBeFalse();
});
it('should handle devDependencies and peerDependencies in addition to dependencies', async () => {
tmpDir = await createWorkspaceMemberFixture({
projectDeps: {},
installedPackages: [
{ name: 'rxjs', version: '7.8.2' },
{ name: 'zone.js', version: '0.15.0' },
],
});
// Write a package.json that uses devDependencies and peerDependencies.
await fs.writeFile(
path.join(tmpDir, 'package.json'),
JSON.stringify({
name: 'test-app',
version: '0.0.0',
devDependencies: { 'zone.js': '~0.15.0' },
peerDependencies: { rxjs: '~7.8.0' },
}),
);
const deps = new Map<string, InstalledPackage>();
await supplementWithLocalDependencies(deps, tmpDir);
expect(deps.has('zone.js')).toBeTrue();
expect(deps.get('zone.js')?.version).toBe('0.15.0');
expect(deps.has('rxjs')).toBeTrue();
expect(deps.get('rxjs')?.version).toBe('7.8.2');
});
it('should do nothing when the project root has no package.json', async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ng-update-spec-'));
const deps = new Map<string, InstalledPackage>();
// Should resolve without throwing.
await expectAsync(supplementWithLocalDependencies(deps, tmpDir)).toBeResolved();
expect(deps.size).toBe(0);
});
});