Skip to content

Commit 84c5948

Browse files
committed
test: improve package manager install lock coverage
1 parent f010a30 commit 84c5948

1 file changed

Lines changed: 75 additions & 29 deletions

File tree

libraries/rush-lib/src/logic/test/InstallHelpers.test.ts

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,61 @@ describe('InstallHelpers', () => {
8181

8282
describe(InstallHelpers.ensureLocalPackageManagerAsync.name, () => {
8383
const tempFolderPath: string = `${__dirname}/temp/${InstallHelpers.name}`;
84+
const packageManager: 'pnpm' = 'pnpm';
85+
const packageManagerVersion: string = '10.27.0';
86+
87+
function getRushGlobalFolder(): RushGlobalFolder {
88+
return {
89+
path: `${tempFolderPath}/rush-global`,
90+
nodeSpecificPath: `${tempFolderPath}/rush-global/node-${process.version}`
91+
} as RushGlobalFolder;
92+
}
93+
94+
function getRushConfiguration(): RushConfiguration {
95+
return {
96+
commonRushConfigFolder: `${tempFolderPath}/common/config/rush`,
97+
commonTempFolder: `${tempFolderPath}/common/temp`,
98+
packageManager,
99+
packageManagerToolVersion: packageManagerVersion
100+
} as RushConfiguration;
101+
}
102+
103+
function getPackageManagerToolFolder(rushGlobalFolder: RushGlobalFolder): string {
104+
return path.join(rushGlobalFolder.nodeSpecificPath, `${packageManager}-${packageManagerVersion}`);
105+
}
106+
107+
async function writeInstalledPackageManagerAsync(rushGlobalFolder: RushGlobalFolder): Promise<void> {
108+
const packageManagerToolFolder: string = getPackageManagerToolFolder(rushGlobalFolder);
109+
110+
JsonFile.save(
111+
{
112+
dependencies: {
113+
[packageManager]: packageManagerVersion
114+
},
115+
description: 'Temporary file generated by the Rush tool',
116+
name: `${packageManager}-local-install`,
117+
private: true,
118+
version: '0.0.0'
119+
},
120+
path.join(packageManagerToolFolder, 'package.json'),
121+
{ ensureFolderExists: true }
122+
);
123+
124+
JsonFile.save(
125+
{
126+
name: packageManager,
127+
version: packageManagerVersion
128+
},
129+
path.join(packageManagerToolFolder, 'node_modules', packageManager, 'package.json'),
130+
{ ensureFolderExists: true }
131+
);
132+
133+
const packageManagerBinFolder: string = path.join(packageManagerToolFolder, 'node_modules', '.bin');
134+
FileSystem.ensureFolder(packageManagerBinFolder);
135+
FileSystem.writeFile(path.join(packageManagerBinFolder, packageManager), '');
136+
137+
await new LastInstallFlag(packageManagerToolFolder, { node: process.versions.node }).createAsync();
138+
}
84139

85140
beforeEach(() => {
86141
FileSystem.ensureEmptyFolder(tempFolderPath);
@@ -92,56 +147,47 @@ describe('InstallHelpers', () => {
92147
});
93148

94149
it('does not acquire the global lock when the package manager is already installed', async () => {
95-
const rushGlobalFolder: RushGlobalFolder = {
96-
path: `${tempFolderPath}/rush-global`,
97-
nodeSpecificPath: `${tempFolderPath}/rush-global/node-${process.version}`
98-
} as RushGlobalFolder;
99-
const packageManagerToolFolder: string = path.join(rushGlobalFolder.nodeSpecificPath, 'pnpm-10.27.0');
100-
await new LastInstallFlag(packageManagerToolFolder, { node: process.versions.node }).createAsync();
150+
const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder();
151+
await writeInstalledPackageManagerAsync(rushGlobalFolder);
101152

102-
const lockAcquireSpy: jest.SpyInstance = jest.spyOn(LockFile, 'acquireAsync');
103-
const rushConfiguration: RushConfiguration = {
104-
commonRushConfigFolder: `${tempFolderPath}/common/config/rush`,
105-
commonTempFolder: `${tempFolderPath}/common/temp`,
106-
packageManager: 'pnpm',
107-
packageManagerToolVersion: '10.27.0'
108-
} as RushConfiguration;
153+
const lockAcquireSpy: jest.SpyInstance = jest
154+
.spyOn(LockFile, 'acquireAsync')
155+
.mockResolvedValue(false as unknown as LockFile);
156+
const installSpy: jest.SpyInstance = jest
157+
.spyOn(Utilities, 'installPackageInDirectoryAsync')
158+
.mockRejectedValue(new Error('The package manager should already be installed.'));
159+
const rushConfiguration: RushConfiguration = getRushConfiguration();
109160

110161
await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true);
111162

112163
expect(lockAcquireSpy).not.toHaveBeenCalled();
164+
expect(installSpy).not.toHaveBeenCalled();
113165
expect(FileSystem.exists(`${rushConfiguration.commonTempFolder}/pnpm-local`)).toEqual(true);
114166
});
115167

116168
it('acquires the global lock if an install lock file is present', async () => {
117-
const rushGlobalFolder: RushGlobalFolder = {
118-
path: `${tempFolderPath}/rush-global`,
119-
nodeSpecificPath: `${tempFolderPath}/rush-global/node-${process.version}`
120-
} as RushGlobalFolder;
121-
const packageManagerToolFolder: string = path.join(rushGlobalFolder.nodeSpecificPath, 'pnpm-10.27.0');
122-
await new LastInstallFlag(packageManagerToolFolder, { node: process.versions.node }).createAsync();
123-
FileSystem.writeFile(`${rushGlobalFolder.nodeSpecificPath}/pnpm-10.27.0#123.lock`, '');
169+
const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder();
170+
await writeInstalledPackageManagerAsync(rushGlobalFolder);
171+
FileSystem.writeFile(
172+
path.join(rushGlobalFolder.nodeSpecificPath, `${packageManager}-${packageManagerVersion}#123.lock`),
173+
''
174+
);
124175

125-
const releaseAsync: jest.Mock = jest.fn();
176+
const releaseLockMock: jest.Mock = jest.fn();
126177
const lockAcquireSpy: jest.SpyInstance = jest.spyOn(LockFile, 'acquireAsync').mockResolvedValue({
127178
dirtyWhenAcquired: true,
128-
release: releaseAsync
179+
release: releaseLockMock
129180
} as unknown as LockFile);
130181
const installSpy: jest.SpyInstance = jest
131182
.spyOn(Utilities, 'installPackageInDirectoryAsync')
132183
.mockResolvedValue();
133-
const rushConfiguration: RushConfiguration = {
134-
commonRushConfigFolder: `${tempFolderPath}/common/config/rush`,
135-
commonTempFolder: `${tempFolderPath}/common/temp`,
136-
packageManager: 'pnpm',
137-
packageManagerToolVersion: '10.27.0'
138-
} as RushConfiguration;
184+
const rushConfiguration: RushConfiguration = getRushConfiguration();
139185

140186
await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true);
141187

142188
expect(lockAcquireSpy).toHaveBeenCalledTimes(1);
143189
expect(installSpy).toHaveBeenCalledTimes(1);
144-
expect(releaseAsync).toHaveBeenCalledTimes(1);
190+
expect(releaseLockMock).toHaveBeenCalledTimes(1);
145191
expect(FileSystem.exists(`${rushConfiguration.commonTempFolder}/pnpm-local`)).toEqual(true);
146192
});
147193
});

0 commit comments

Comments
 (0)