-
Notifications
You must be signed in to change notification settings - Fork 697
Expand file tree
/
Copy pathInstallHelpers.test.ts
More file actions
194 lines (166 loc) · 7.28 KB
/
Copy pathInstallHelpers.test.ts
File metadata and controls
194 lines (166 loc) · 7.28 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { FileSystem, type IPackageJson, JsonFile, LockFile } from '@rushstack/node-core-library';
import { StringBufferTerminalProvider, Terminal } from '@rushstack/terminal';
import { TestUtilities } from '@rushstack/heft-config-file';
import { InstallHelpers } from '../installManager/InstallHelpers';
import { RushConfiguration } from '../../api/RushConfiguration';
import { LastInstallFlag } from '../../api/LastInstallFlag';
import type { RushGlobalFolder } from '../../api/RushGlobalFolder';
import { Utilities } from '../../utilities/Utilities';
describe('InstallHelpers', () => {
describe('generateCommonPackageJson', () => {
const originalJsonFileSave = JsonFile.save;
const mockJsonFileSave: jest.Mock = jest.fn();
let terminal: Terminal;
let terminalProvider: StringBufferTerminalProvider;
beforeAll(() => {
JsonFile.save = mockJsonFileSave;
});
beforeEach(() => {
terminalProvider = new StringBufferTerminalProvider();
terminal = new Terminal(terminalProvider);
});
afterEach(() => {
expect(
terminalProvider.getAllOutputAsChunks({
normalizeSpecialCharacters: true,
asLines: true
})
).toMatchSnapshot('Terminal Output');
mockJsonFileSave.mockClear();
});
afterAll(() => {
JsonFile.save = originalJsonFileSave;
});
it('generates correct package json with pnpm configurations', () => {
const RUSH_JSON_FILENAME: string = `${__dirname}/pnpmConfig/rush.json`;
const rushConfiguration: RushConfiguration =
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);
InstallHelpers.generateCommonPackageJson(
rushConfiguration,
rushConfiguration.defaultSubspace,
undefined,
terminal
);
const packageJson: IPackageJson = mockJsonFileSave.mock.calls[0][0];
expect(TestUtilities.stripAnnotations(packageJson)).toEqual(
expect.objectContaining({
pnpm: {
overrides: {
foo: '^2.0.0', // <-- unsupportedPackageJsonSettings.pnpm.override.foo
quux: 'npm:@myorg/quux@^1.0.0',
'bar@^2.1.0': '3.0.0',
'qar@1>zoo': '2'
},
packageExtensions: {
'react-redux': {
peerDependencies: {
'react-dom': '*'
}
}
},
neverBuiltDependencies: ['fsevents', 'level'],
onlyBuiltDependencies: ['esbuild', 'playwright'],
pnpmFutureFeature: true
}
})
);
});
});
describe(InstallHelpers.ensureLocalPackageManagerAsync.name, () => {
const tempFolderPath: string = `${__dirname}/temp/${InstallHelpers.name}`;
const packageManager: 'pnpm' = 'pnpm';
const packageManagerVersion: string = '10.27.0';
function getRushGlobalFolder(): RushGlobalFolder {
return {
path: `${tempFolderPath}/rush-global`,
nodeSpecificPath: `${tempFolderPath}/rush-global/node-${process.version}`
} as RushGlobalFolder;
}
function getRushConfiguration(): RushConfiguration {
return {
commonRushConfigFolder: `${tempFolderPath}/common/config/rush`,
commonTempFolder: `${tempFolderPath}/common/temp`,
packageManager,
packageManagerToolVersion: packageManagerVersion
} as RushConfiguration;
}
function getPackageManagerToolFolder(rushGlobalFolder: RushGlobalFolder): string {
return path.join(rushGlobalFolder.nodeSpecificPath, `${packageManager}-${packageManagerVersion}`);
}
async function writeInstalledPackageManagerAsync(rushGlobalFolder: RushGlobalFolder): Promise<void> {
const packageManagerToolFolder: string = getPackageManagerToolFolder(rushGlobalFolder);
JsonFile.save(
{
dependencies: {
[packageManager]: packageManagerVersion
},
description: 'Temporary file generated by the Rush tool',
name: `${packageManager}-local-install`,
private: true,
version: '0.0.0'
},
path.join(packageManagerToolFolder, 'package.json'),
{ ensureFolderExists: true }
);
JsonFile.save(
{
name: packageManager,
version: packageManagerVersion
},
path.join(packageManagerToolFolder, 'node_modules', packageManager, 'package.json'),
{ ensureFolderExists: true }
);
const packageManagerBinFolder: string = path.join(packageManagerToolFolder, 'node_modules', '.bin');
FileSystem.ensureFolder(packageManagerBinFolder);
FileSystem.writeFile(path.join(packageManagerBinFolder, packageManager), '');
await new LastInstallFlag(packageManagerToolFolder, { node: process.versions.node }).createAsync();
}
beforeEach(() => {
FileSystem.ensureEmptyFolder(tempFolderPath);
});
afterEach(() => {
FileSystem.deleteFolder(tempFolderPath);
jest.restoreAllMocks();
});
it('does not acquire the global lock when the package manager is already installed', async () => {
const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder();
await writeInstalledPackageManagerAsync(rushGlobalFolder);
const lockAcquireSpy: jest.SpyInstance = jest
.spyOn(LockFile, 'acquireAsync')
.mockResolvedValue(false as unknown as LockFile);
const installSpy: jest.SpyInstance = jest
.spyOn(Utilities, 'installPackageInDirectoryAsync')
.mockRejectedValue(new Error('The package manager should already be installed.'));
const rushConfiguration: RushConfiguration = getRushConfiguration();
await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true);
expect(lockAcquireSpy).not.toHaveBeenCalled();
expect(installSpy).not.toHaveBeenCalled();
expect(FileSystem.exists(`${rushConfiguration.commonTempFolder}/pnpm-local`)).toEqual(true);
});
it('acquires the global lock if an install lock file is present', async () => {
const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder();
await writeInstalledPackageManagerAsync(rushGlobalFolder);
FileSystem.writeFile(
path.join(rushGlobalFolder.nodeSpecificPath, `${packageManager}-${packageManagerVersion}#123.lock`),
''
);
const releaseLockMock: jest.Mock = jest.fn();
const lockAcquireSpy: jest.SpyInstance = jest.spyOn(LockFile, 'acquireAsync').mockResolvedValue({
dirtyWhenAcquired: true,
release: releaseLockMock
} as unknown as LockFile);
const installSpy: jest.SpyInstance = jest
.spyOn(Utilities, 'installPackageInDirectoryAsync')
.mockResolvedValue();
const rushConfiguration: RushConfiguration = getRushConfiguration();
await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true);
expect(lockAcquireSpy).toHaveBeenCalledTimes(1);
expect(installSpy).toHaveBeenCalledTimes(1);
expect(releaseLockMock).toHaveBeenCalledTimes(1);
expect(FileSystem.exists(`${rushConfiguration.commonTempFolder}/pnpm-local`)).toEqual(true);
});
});
});