Skip to content

Commit 975f92a

Browse files
committed
test: use local yarn instead of depending on global npm
1 parent b06ae55 commit 975f92a

7 files changed

Lines changed: 62 additions & 27 deletions

File tree

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { mkdir, writeFile } from 'fs/promises';
22
import { join } from 'path';
3-
import { getGlobalVariable } from '../utils/env';
3+
import { getGlobalVariable, setGlobalVariable } from '../utils/env';
4+
import { globalYarn } from '../utils/process';
45

56
/**
67
* Configure npm to use a unique sandboxed environment.
78
*/
89
export default async function () {
910
const tempRoot: string = getGlobalVariable('tmp-root');
1011
const npmModulesPrefix = join(tempRoot, 'npm-global');
12+
const yarnModuleCache = join(tempRoot, 'yarn-cache-folder');
13+
const yarnModulesPrefix = join(tempRoot, 'yarn-global');
1114
const npmRegistry: string = getGlobalVariable('package-registry');
1215
const npmrc = join(tempRoot, '.npmrc');
16+
const yarnrc = join(tempRoot, '.yarnrc');
1317

1418
// Configure npm to use the sandboxed npm globals and rc file
1519
// From this point onward all npm transactions use the "global" npm cache
1620
// isolated within this e2e test invocation.
1721
process.env.NPM_CONFIG_USERCONFIG = npmrc;
1822
process.env.NPM_CONFIG_PREFIX = npmModulesPrefix;
1923
process.env.NPM_CONFIG_REGISTRY = npmRegistry;
24+
process.env.YARN_CACHE_FOLDER = yarnModuleCache;
2025

2126
// Snapshot builds may contain versions that are not yet released (e.g., RC phase main branch).
2227
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
@@ -25,9 +30,18 @@ export default async function () {
2530
process.env['NPM_CONFIG_legacy_peer_deps'] = 'true';
2631
}
2732

28-
// Configure the registry and prefix used within the test sandbox
33+
// Configure the registry and create the prefix directories
2934
await writeFile(npmrc, `registry=${npmRegistry}\nprefix=${npmModulesPrefix}`);
35+
await writeFile(
36+
yarnrc,
37+
`registry ${npmRegistry}\nprefix ${yarnModulesPrefix}\ncache-folder ${yarnModuleCache}`,
38+
);
3039
await mkdir(npmModulesPrefix);
40+
await mkdir(yarnModulesPrefix);
41+
await mkdir(yarnModuleCache);
42+
43+
setGlobalVariable('npm-global', npmModulesPrefix);
44+
setGlobalVariable('yarn-global', yarnModulesPrefix);
3145

3246
console.log(` Using "${npmModulesPrefix}" as e2e test global npm cache.`);
3347
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { getGlobalVariable } from '../utils/env';
2-
import { execWithEnv, extractNpmEnv } from '../utils/process';
2+
import { globalYarn, extractNpmEnv } from '../utils/process';
33
import { isPrereleaseCli } from '../utils/project';
44

55
export default async function () {
66
const testRegistry: string = getGlobalVariable('package-registry');
7-
await execWithEnv(
8-
'npm',
7+
await globalYarn(
98
[
109
'run',
1110
'admin',
12-
'--',
1311
'publish',
1412
'--no-versionCheck',
1513
'--no-branchCheck',
@@ -18,10 +16,12 @@ export default async function () {
1816
isPrereleaseCli() ? 'next' : 'latest',
1917
],
2018
{
21-
...extractNpmEnv(),
22-
// Also set an auth token value for the local test registry which is required by npm 7+
23-
// even though it is never actually used.
24-
'NPM_CONFIG__AUTH': 'e2e-testing',
19+
env: {
20+
...extractNpmEnv(),
21+
// Also set an auth token value for the local test registry which is required by npm 7+
22+
// even though it is never actually used.
23+
'NPM_CONFIG__AUTH': 'e2e-testing',
24+
},
2525
},
2626
);
2727
}

tests/legacy-cli/e2e/setup/100-global-cli.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getGlobalVariable } from '../utils/env';
2-
import { silentNpm } from '../utils/process';
2+
import { globalYarn } from '../utils/process';
33

44
const NPM_VERSION = '7.24.0';
55
const YARN_VERSION = '1.22.18';
@@ -13,12 +13,12 @@ export default async function () {
1313
const testRegistry: string = getGlobalVariable('package-registry');
1414

1515
// Install global Angular CLI being tested, npm+yarn used by e2e tests.
16-
await silentNpm(
17-
'install',
18-
'--global',
16+
await globalYarn([
17+
'global',
18+
'add',
1919
`--registry=${testRegistry}`,
2020
'@angular/cli',
2121
`npm@${NPM_VERSION}`,
2222
`yarn@${YARN_VERSION}`,
23-
);
23+
]);
2424
}

tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
execAndCaptureError,
88
execAndWaitForOutputToMatch,
99
execWithEnv,
10+
globalYarn,
1011
silentNpm,
1112
} from '../../../utils/process';
1213

@@ -393,7 +394,7 @@ source <(ng completion script)
393394
await mockHome(async (home) => {
394395
try {
395396
// Temporarily uninstall the global CLI binary from the system.
396-
await silentNpm(['uninstall', '--global', '@angular/cli', `--registry=${testRegistry}`]);
397+
await globalYarn(['global', 'remove', '@angular/cli', `--registry=${testRegistry}`]);
397398

398399
// Setup a fake project directory with a local install of the CLI.
399400
const projectDir = path.join(home, 'project');
@@ -425,7 +426,7 @@ source <(ng completion script)
425426
}
426427
} finally {
427428
// Reinstall global CLI for remainder of the tests.
428-
await silentNpm(['install', '--global', '@angular/cli', `--registry=${testRegistry}`]);
429+
await globalYarn(['global', 'add', '@angular/cli', `--registry=${testRegistry}`]);
429430
}
430431
});
431432
}

tests/legacy-cli/e2e/tests/commands/completion/completion.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
execAndCaptureError,
77
execAndWaitForOutputToMatch,
88
execWithEnv,
9+
globalYarn,
910
silentNpm,
1011
} from '../../../utils/process';
1112

@@ -358,7 +359,7 @@ source <(ng completion script)
358359
await mockHome(async (home) => {
359360
try {
360361
// Temporarily uninstall the global CLI binary from the system.
361-
await silentNpm(['uninstall', '--global', '@angular/cli', `--registry=${testRegistry}`]);
362+
await globalYarn(['global', 'remove', '@angular/cli', `--registry=${testRegistry}`]);
362363

363364
// Setup a fake project directory with a local install of the CLI.
364365
const projectDir = path.join(home, 'project');
@@ -381,7 +382,7 @@ source <(ng completion script)
381382
}
382383
} finally {
383384
// Reinstall global CLI for remainder of the tests.
384-
await silentNpm(['install', '--global', '@angular/cli', `--registry=${testRegistry}`]);
385+
await globalYarn(['global', 'add', '@angular/cli', `--registry=${testRegistry}`]);
385386
}
386387
});
387388
}

tests/legacy-cli/e2e/tests/misc/npm-7.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { execSync } from 'child_process';
33
import { valid as validSemVer } from 'semver';
44
import { rimraf } from '../../utils/fs';
55
import { getActivePackageManager } from '../../utils/packages';
6-
import { execWithEnv, ng, npm } from '../../utils/process';
6+
import { execWithEnv, globalYarn, ng } from '../../utils/process';
77
import { isPrereleaseCli } from '../../utils/project';
88
import { expectToFail } from '../../utils/utils';
99

@@ -46,7 +46,7 @@ export default async function () {
4646

4747
try {
4848
// Install version >=7.5.6
49-
await npm('install', '--global', 'npm@>=7.5.6');
49+
await globalYarn(['global', 'add', 'npm@>=7.5.6']);
5050

5151
// Ensure `ng update` does not show npm warning
5252
const { stderr: stderrUpdate1 } = await ng('update', ...extraArgs);
@@ -55,7 +55,7 @@ export default async function () {
5555
}
5656

5757
// Install version <7.5.6
58-
await npm('install', '--global', 'npm@7.4.0');
58+
await globalYarn(['global', 'add', 'npm@7.4.0']);
5959

6060
// Ensure `ng add` shows npm warning
6161
const { stderr: stderrAdd } = await execWithEnv(
@@ -115,6 +115,6 @@ export default async function () {
115115
process.chdir(currentDirectory);
116116

117117
// Reset version back to initial version
118-
await npm('install', '--global', `npm@${initialVersion}`);
118+
await globalYarn(['global', 'add', `npm@${initialVersion}`]);
119119
}
120120
}

tests/legacy-cli/e2e/utils/process.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface ExecOptions {
1616
cwd?: string;
1717
}
1818

19-
const NPM_CONFIG_RE = /^npm_config_/i;
19+
const NPM_CONFIG_RE = /^(npm_config_|yarn_)/i;
2020

2121
let _processes: child_process.ChildProcess[] = [];
2222

@@ -322,6 +322,23 @@ export function silentYarn(...args: string[]) {
322322
return _exec({ silent: true }, 'yarn', args);
323323
}
324324

325+
const YARN = join(__dirname, '../../../../.yarn/releases/yarn-1.22.17.cjs');
326+
327+
export function globalYarn(
328+
args: string[],
329+
{ silent, env }: Pick<ExecOptions, 'silent' | 'env'> = {},
330+
) {
331+
// Use the yarn global cache when executing yarn
332+
const npmPrefix = process.env.NPM_CONFIG_PREFIX;
333+
try {
334+
process.env.NPM_CONFIG_PREFIX = getGlobalVariable('yarn-global');
335+
336+
return _exec({ silent: silent ?? false /*TODO: true*/, env }, 'node', [YARN, ...args]);
337+
} finally {
338+
process.env.NPM_CONFIG_PREFIX = npmPrefix;
339+
}
340+
}
341+
325342
export function npm(...args: string[]) {
326343
return _exec({}, 'npm', args);
327344
}
@@ -361,12 +378,14 @@ export async function launchTestProcess(entry: string, ...args: any[]) {
361378
// non angular-cli paths such as /usr/bin for generic commands.
362379
paths = paths.filter((p) => p.startsWith(tempRoot) || !p.includes('angular-cli'));
363380

364-
// Ensure the custom npm global bin is on the PATH
381+
// Ensure the custom npm and yarn global bin is on the PATH
365382
// https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
383+
paths.unshift(join(getGlobalVariable('yarn-global'), 'bin'));
384+
paths.unshift(join(getGlobalVariable('yarn-global'))); // TODO: which one on windows?
366385
if (process.platform.startsWith('win')) {
367-
paths.unshift(env.NPM_CONFIG_PREFIX!);
386+
paths.unshift(getGlobalVariable('npm-global'));
368387
} else {
369-
paths.unshift(join(env.NPM_CONFIG_PREFIX!, 'bin'));
388+
paths.unshift(join(getGlobalVariable('npm-global'), 'bin'));
370389
}
371390

372391
env.PATH = paths.join(delimiter);

0 commit comments

Comments
 (0)