forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoinstaller.test.ts
More file actions
105 lines (90 loc) · 3.68 KB
/
Autoinstaller.test.ts
File metadata and controls
105 lines (90 loc) · 3.68 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import './mockRushCommandLineParser';
import { FileSystem } from '@rushstack/node-core-library';
import { Autoinstaller } from '../../logic/Autoinstaller';
import { InstallHelpers } from '../../logic/installManager/InstallHelpers';
import { RushConstants } from '../../logic/RushConstants';
import { Utilities } from '../../utilities/Utilities';
import {
getCommandLineParserInstanceAsync,
isolateEnvironmentConfigurationForTests,
type IEnvironmentConfigIsolation
} from './TestUtils';
describe(Autoinstaller.name, () => {
let _envIsolation: IEnvironmentConfigIsolation;
beforeEach(() => {
_envIsolation = isolateEnvironmentConfigurationForTests();
});
afterEach(() => {
_envIsolation.restore();
jest.restoreAllMocks();
});
it('moves an existing node_modules folder into the Rush recycler before reinstalling', async () => {
const { parser, repoPath, spawnMock } = await getCommandLineParserInstanceAsync(
'pluginWithBuildCommandRepo',
'update'
);
const autoinstallerPath: string = `${repoPath}/common/autoinstallers/plugins`;
const nodeModulesFolder: string = `${autoinstallerPath}/${RushConstants.nodeModulesFolderName}`;
const staleFilePath: string = `${nodeModulesFolder}/stale-package/index.js`;
const recyclerFolder: string = `${parser.rushConfiguration.commonTempFolder}/${RushConstants.rushRecyclerFolderName}`;
await FileSystem.writeFileAsync(staleFilePath, 'stale', {
ensureFolderExists: true
});
let recyclerEntriesBefore: Set<string>;
try {
recyclerEntriesBefore = new Set(await FileSystem.readFolderItemNamesAsync(recyclerFolder));
} catch (error) {
if (FileSystem.isNotExistError(error)) {
recyclerEntriesBefore = new Set();
} else {
throw error;
}
}
jest.spyOn(InstallHelpers, 'ensureLocalPackageManagerAsync').mockResolvedValue(undefined);
jest.spyOn(Utilities, 'syncNpmrc').mockImplementation(() => undefined);
jest
.spyOn(Utilities, 'executeCommandAsync')
.mockImplementation(async (options: Parameters<typeof Utilities.executeCommandAsync>[0]) => {
await FileSystem.ensureFolderAsync(
`${options.workingDirectory}/${RushConstants.nodeModulesFolderName}`
);
});
const autoinstaller: Autoinstaller = new Autoinstaller({
autoinstallerName: 'plugins',
rushConfiguration: parser.rushConfiguration,
rushGlobalFolder: parser.rushGlobalFolder
});
await autoinstaller.prepareAsync();
const recyclerEntriesAfter: string[] = (await FileSystem.readFolderItemNamesAsync(recyclerFolder)).filter(
(entry: string) => !recyclerEntriesBefore.has(entry)
);
expect(recyclerEntriesAfter).toHaveLength(1);
await expect(
FileSystem.existsAsync(`${recyclerFolder}/${recyclerEntriesAfter[0]}/stale-package/index.js`)
).resolves.toBe(true);
await expect(FileSystem.existsAsync(staleFilePath)).resolves.toBe(false);
await expect(FileSystem.existsAsync(`${nodeModulesFolder}/rush-autoinstaller.flag`)).resolves.toBe(true);
if (process.platform === 'win32') {
expect(spawnMock).toHaveBeenCalledWith(
'cmd.exe',
expect.arrayContaining(['/c']),
expect.objectContaining({
detached: true,
stdio: 'ignore',
windowsVerbatimArguments: true
})
);
} else {
expect(spawnMock).toHaveBeenCalledWith(
'rm',
expect.arrayContaining(['-rf']),
expect.objectContaining({
detached: true,
stdio: 'ignore'
})
);
}
});
});