Skip to content

Commit 98a3eab

Browse files
committed
test: use local yarn instead of depending on global npm
1 parent 789d78f commit 98a3eab

5 files changed

Lines changed: 42 additions & 13 deletions

File tree

tests/legacy-cli/e2e/setup/002-npm-sandbox.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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';
44

55
/**
66
* Configure npm to use a unique sandboxed environment.
77
*/
88
export default async function () {
99
const tempRoot: string = getGlobalVariable('tmp-root');
1010
const npmModulesPrefix = join(tempRoot, 'npm-global');
11+
const yarnModulesPrefix = join(tempRoot, 'yarn-global');
1112
const npmRegistry: string = getGlobalVariable('package-registry');
1213
const npmrc = join(tempRoot, '.npmrc');
1314

@@ -25,9 +26,13 @@ export default async function () {
2526
process.env['NPM_CONFIG_legacy_peer_deps'] = 'true';
2627
}
2728

28-
// Configure the registry and prefix used within the test sandbox
29+
// Configure the registry and create the prefix directories
2930
await writeFile(npmrc, `registry=${npmRegistry}\nprefix=${npmModulesPrefix}`);
3031
await mkdir(npmModulesPrefix);
32+
await mkdir(yarnModulesPrefix);
33+
34+
setGlobalVariable('npm-global', npmModulesPrefix);
35+
setGlobalVariable('yarn-global', yarnModulesPrefix);
3136

3237
console.log(` Using "${npmModulesPrefix}" as e2e test global npm cache.`);
3338
}

tests/legacy-cli/e2e/setup/010-local-publish.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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',

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/utils/process.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ 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(args: string[], env?: NodeJS.ProcessEnv) {
328+
if (!process.env.LEGACY_CLI_RUNNER) {
329+
throw new Error('The global yarn should only be executed from the primary e2e runner process');
330+
}
331+
332+
// Use the yarn global cache when executing yarn
333+
const npmPrefix = process.env.NPM_CONFIG_PREFIX;
334+
try {
335+
process.env.NPM_CONFIG_PREFIX = getGlobalVariable('yarn-global');
336+
337+
return _exec({ silent: true, env }, 'node', [YARN, ...args]);
338+
} finally {
339+
process.env.NPM_CONFIG_PREFIX = npmPrefix;
340+
}
341+
}
342+
325343
export function npm(...args: string[]) {
326344
return _exec({}, 'npm', args);
327345
}
@@ -361,12 +379,14 @@ export async function launchTestProcess(entry: string, ...args: any[]) {
361379
// non angular-cli paths such as /usr/bin for generic commands.
362380
paths = paths.filter((p) => p.startsWith(tempRoot) || !p.includes('angular-cli'));
363381

364-
// Ensure the custom npm global bin is on the PATH
382+
// Ensure the custom npm and yarn global bin is on the PATH
365383
// https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
366384
if (process.platform.startsWith('win')) {
367-
paths.unshift(env.NPM_CONFIG_PREFIX!);
385+
paths.unshift(getGlobalVariable('npm-global'));
386+
paths.unshift(getGlobalVariable('yarn-global'));
368387
} else {
369-
paths.unshift(join(env.NPM_CONFIG_PREFIX!, 'bin'));
388+
paths.unshift(join(getGlobalVariable('npm-global'), 'bin'));
389+
paths.unshift(join(getGlobalVariable('yarn-global'), 'bin'));
370390
}
371391

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

tests/legacy-cli/e2e_runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ const argv = yargsParser(process.argv.slice(2), {
5858
*/
5959
process.exitCode = 255;
6060

61+
/**
62+
* Mark this process as the main e2e_runner
63+
*/
64+
process.env.LEGACY_CLI_RUNNER = '1';
65+
6166
const logger = createConsoleLogger(argv.verbose, process.stdout, process.stderr, {
6267
info: (s) => s,
6368
debug: (s) => s,

0 commit comments

Comments
 (0)