Skip to content

Commit

Permalink
test: remove use of global npm cache and config
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed May 20, 2022
1 parent 22af652 commit b9effb5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export default function () {
}
console.log(` Using "${tempRoot}" as temporary directory for a new project.`);
setGlobalVariable('tmp-root', tempRoot);
process.chdir(tempRoot);
}
24 changes: 24 additions & 0 deletions tests/legacy-cli/e2e/setup/002-npm-sandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { mkdir, writeFile } from 'fs/promises';
import { delimiter, join } from 'path';
import { getGlobalVariable } from '../utils/env';
import { npm } from '../utils/process';

/**
* Configure npm to use a unique sandboxed environment.
*/
export default async function () {
const tempRoot: string = getGlobalVariable('tmp-root');
const npmGlobalModules = join(tempRoot, 'npm-global');
const npmCache = join(tempRoot, 'npm-cache');
const npmrc = join(tempRoot, '.npmrc');

// Re-enforce some of those configs in env vars
process.env.NPM_CONFIG_USERCONFIG = npmrc;
process.env.NPM_CONFIG_CACHE = npmCache;
process.env.NPM_CONFIG_PREFIX = npmGlobalModules;

// Ensure the custom npm global .bin is first on the PATH
process.env.PATH = join(npmGlobalModules, 'bin') + delimiter + process.env.PATH;

console.log(` Using "${npmGlobalModules}" as an isolated global npm cache.`);
}
13 changes: 13 additions & 0 deletions tests/legacy-cli/e2e/setup/200-create-project-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mkdir } from 'fs/promises';
import { join } from 'path';
import { getGlobalVariable } from '../utils/env';

export default async function () {
const tempRoot: string = getGlobalVariable('tmp-root');
const projectsRoot = join(tempRoot, 'e2e-test');

await mkdir(projectsRoot);

console.log(` Using "${projectsRoot}" as temporary directory for a new project.`);
process.chdir(projectsRoot);
}
14 changes: 4 additions & 10 deletions tests/legacy-cli/e2e/setup/500-create-project.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from 'path';
import { getGlobalVariable } from '../utils/env';
import { expectFileToExist, writeFile } from '../utils/fs';
import { expectFileToExist } from '../utils/fs';
import { gitClean } from '../utils/git';
import { setRegistry as setNPMConfigRegistry } from '../utils/packages';
import { ng, npm } from '../utils/process';
import { setRegistry as setNPMConfigRegistry, setRegistry } from '../utils/packages';
import { ng } from '../utils/process';
import { prepareProjectForE2e, updateJsonFile } from '../utils/project';

export default async function () {
Expand All @@ -18,8 +18,6 @@ export default async function () {
await gitClean();
} else {
const extraArgs = [];
const testRegistry = getGlobalVariable('package-registry');
const isCI = getGlobalVariable('ci');

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

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

// Setup esbuild builder if requested on the commandline
const useEsbuildBuilder = !!getGlobalVariable('argv')['esbuild'];
Expand Down
10 changes: 2 additions & 8 deletions tests/legacy-cli/e2e/utils/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,11 @@ export async function setRegistry(useTestRegistry: boolean): Promise<void> {
? getGlobalVariable('package-registry')
: 'https://registry.npmjs.org';

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

// Ensure local test registry is used when outside a project
if (isCI) {
// Safe to set a user configuration on CI
await npm('config', 'set', 'registry', url);
} else {
// Yarn supports both `NPM_CONFIG_REGISTRY` and `YARN_REGISTRY`.
process.env['NPM_CONFIG_REGISTRY'] = url;
}
// Yarn supports both `NPM_CONFIG_REGISTRY` and `YARN_REGISTRY`.
process.env['NPM_CONFIG_REGISTRY'] = url;

// Snapshot builds may contain versions that are not yet released (e.g., RC phase main branch).
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
Expand Down
27 changes: 22 additions & 5 deletions tests/legacy-cli/e2e/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ const treeKill = require('tree-kill');
interface ExecOptions {
silent?: boolean;
waitForMatch?: RegExp;
env?: { [varname: string]: string };
env?: { [varname: string]: string } | 'npm';
stdin?: string;
cwd?: string;
}

const NPM_CONFIG_RE = /^npm_config_/i;

function extractNpmEnv() {
return Object.keys(process.env)
.filter((v) => NPM_CONFIG_RE.test(v))
.reduce(
(vars, n) => {
vars[n] = process.env[n];
return vars;
},
{
PATH: process.env.PATH,
},
);
}

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

export type ProcessOutput = {
Expand All @@ -30,7 +46,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
let stdout = '';
let stderr = '';
const cwd = options.cwd ?? process.cwd();
const env = options.env;
const env = options.env === 'npm' ? extractNpmEnv() : options.env;
console.log(
`==========================================================================================`,
);
Expand Down Expand Up @@ -269,21 +285,22 @@ export function silentNpm(
{
silent: true,
cwd: (options as { cwd?: string } | undefined)?.cwd,
env: 'npm',
},
'npm',
params,
);
} else {
return _exec({ silent: true }, 'npm', args as string[]);
return _exec({ silent: true, env: 'npm' }, 'npm', args as string[]);
}
}

export function silentYarn(...args: string[]) {
return _exec({ silent: true }, 'yarn', args);
return _exec({ silent: true, env: 'npm' }, 'yarn', args);
}

export function npm(...args: string[]) {
return _exec({}, 'npm', args);
return _exec({ env: 'npm' }, 'npm', args);
}

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

0 comments on commit b9effb5

Please sign in to comment.