Skip to content

Commit b9effb5

Browse files
committed
test: remove use of global npm cache and config
1 parent 22af652 commit b9effb5

File tree

6 files changed

+65
-24
lines changed

6 files changed

+65
-24
lines changed

tests/legacy-cli/e2e/setup/200-create-tmp-dir.ts renamed to tests/legacy-cli/e2e/setup/001-create-tmp-dir.ts

-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ export default function () {
1717
}
1818
console.log(` Using "${tempRoot}" as temporary directory for a new project.`);
1919
setGlobalVariable('tmp-root', tempRoot);
20-
process.chdir(tempRoot);
2120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { mkdir, writeFile } from 'fs/promises';
2+
import { delimiter, join } from 'path';
3+
import { getGlobalVariable } from '../utils/env';
4+
import { npm } from '../utils/process';
5+
6+
/**
7+
* Configure npm to use a unique sandboxed environment.
8+
*/
9+
export default async function () {
10+
const tempRoot: string = getGlobalVariable('tmp-root');
11+
const npmGlobalModules = join(tempRoot, 'npm-global');
12+
const npmCache = join(tempRoot, 'npm-cache');
13+
const npmrc = join(tempRoot, '.npmrc');
14+
15+
// Re-enforce some of those configs in env vars
16+
process.env.NPM_CONFIG_USERCONFIG = npmrc;
17+
process.env.NPM_CONFIG_CACHE = npmCache;
18+
process.env.NPM_CONFIG_PREFIX = npmGlobalModules;
19+
20+
// Ensure the custom npm global .bin is first on the PATH
21+
process.env.PATH = join(npmGlobalModules, 'bin') + delimiter + process.env.PATH;
22+
23+
console.log(` Using "${npmGlobalModules}" as an isolated global npm cache.`);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { mkdir } from 'fs/promises';
2+
import { join } from 'path';
3+
import { getGlobalVariable } from '../utils/env';
4+
5+
export default async function () {
6+
const tempRoot: string = getGlobalVariable('tmp-root');
7+
const projectsRoot = join(tempRoot, 'e2e-test');
8+
9+
await mkdir(projectsRoot);
10+
11+
console.log(` Using "${projectsRoot}" as temporary directory for a new project.`);
12+
process.chdir(projectsRoot);
13+
}

tests/legacy-cli/e2e/setup/500-create-project.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { join } from 'path';
22
import { getGlobalVariable } from '../utils/env';
3-
import { expectFileToExist, writeFile } from '../utils/fs';
3+
import { expectFileToExist } from '../utils/fs';
44
import { gitClean } from '../utils/git';
5-
import { setRegistry as setNPMConfigRegistry } from '../utils/packages';
6-
import { ng, npm } from '../utils/process';
5+
import { setRegistry as setNPMConfigRegistry, setRegistry } from '../utils/packages';
6+
import { ng } from '../utils/process';
77
import { prepareProjectForE2e, updateJsonFile } from '../utils/project';
88

99
export default async function () {
@@ -18,8 +18,6 @@ export default async function () {
1818
await gitClean();
1919
} else {
2020
const extraArgs = [];
21-
const testRegistry = getGlobalVariable('package-registry');
22-
const isCI = getGlobalVariable('ci');
2321

2422
// Ensure local test registry is used when outside a project
2523
await setNPMConfigRegistry(true);
@@ -28,11 +26,7 @@ export default async function () {
2826
await expectFileToExist(join(process.cwd(), 'test-project'));
2927
process.chdir('./test-project');
3028

31-
// If on CI, the user configuration set above will handle project usage
32-
if (!isCI) {
33-
// Ensure local test registry is used inside a project
34-
await writeFile('.npmrc', `registry=${testRegistry}`);
35-
}
29+
setRegistry(true);
3630

3731
// Setup esbuild builder if requested on the commandline
3832
const useEsbuildBuilder = !!getGlobalVariable('argv')['esbuild'];

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,11 @@ export async function setRegistry(useTestRegistry: boolean): Promise<void> {
4949
? getGlobalVariable('package-registry')
5050
: 'https://registry.npmjs.org';
5151

52-
const isCI = getGlobalVariable('ci');
5352
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
5453

5554
// Ensure local test registry is used when outside a project
56-
if (isCI) {
57-
// Safe to set a user configuration on CI
58-
await npm('config', 'set', 'registry', url);
59-
} else {
60-
// Yarn supports both `NPM_CONFIG_REGISTRY` and `YARN_REGISTRY`.
61-
process.env['NPM_CONFIG_REGISTRY'] = url;
62-
}
55+
// Yarn supports both `NPM_CONFIG_REGISTRY` and `YARN_REGISTRY`.
56+
process.env['NPM_CONFIG_REGISTRY'] = url;
6357

6458
// Snapshot builds may contain versions that are not yet released (e.g., RC phase main branch).
6559
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.

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

+22-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,27 @@ const treeKill = require('tree-kill');
1010
interface ExecOptions {
1111
silent?: boolean;
1212
waitForMatch?: RegExp;
13-
env?: { [varname: string]: string };
13+
env?: { [varname: string]: string } | 'npm';
1414
stdin?: string;
1515
cwd?: string;
1616
}
1717

18+
const NPM_CONFIG_RE = /^npm_config_/i;
19+
20+
function extractNpmEnv() {
21+
return Object.keys(process.env)
22+
.filter((v) => NPM_CONFIG_RE.test(v))
23+
.reduce(
24+
(vars, n) => {
25+
vars[n] = process.env[n];
26+
return vars;
27+
},
28+
{
29+
PATH: process.env.PATH,
30+
},
31+
);
32+
}
33+
1834
let _processes: child_process.ChildProcess[] = [];
1935

2036
export type ProcessOutput = {
@@ -30,7 +46,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
3046
let stdout = '';
3147
let stderr = '';
3248
const cwd = options.cwd ?? process.cwd();
33-
const env = options.env;
49+
const env = options.env === 'npm' ? extractNpmEnv() : options.env;
3450
console.log(
3551
`==========================================================================================`,
3652
);
@@ -269,21 +285,22 @@ export function silentNpm(
269285
{
270286
silent: true,
271287
cwd: (options as { cwd?: string } | undefined)?.cwd,
288+
env: 'npm',
272289
},
273290
'npm',
274291
params,
275292
);
276293
} else {
277-
return _exec({ silent: true }, 'npm', args as string[]);
294+
return _exec({ silent: true, env: 'npm' }, 'npm', args as string[]);
278295
}
279296
}
280297

281298
export function silentYarn(...args: string[]) {
282-
return _exec({ silent: true }, 'yarn', args);
299+
return _exec({ silent: true, env: 'npm' }, 'yarn', args);
283300
}
284301

285302
export function npm(...args: string[]) {
286-
return _exec({}, 'npm', args);
303+
return _exec({ env: 'npm' }, 'npm', args);
287304
}
288305

289306
export function node(...args: string[]) {

0 commit comments

Comments
 (0)