Skip to content

Commit

Permalink
test: move tempdir creation to util method
Browse files Browse the repository at this point in the history
(cherry picked from commit acd5ad5)
  • Loading branch information
jbedard authored and alan-agius4 committed Jul 28, 2022
1 parent eed54b3 commit df3871a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
9 changes: 4 additions & 5 deletions tests/legacy-cli/e2e/setup/001-create-tmp-dir.ts
@@ -1,9 +1,8 @@
import { mkdtempSync, realpathSync } from 'fs';
import { tmpdir } from 'os';
import { dirname, join } from 'path';
import { dirname } from 'path';
import { getGlobalVariable, setGlobalVariable } from '../utils/env';
import { mktempd } from '../utils/utils';

export default function () {
export default async function () {
const argv = getGlobalVariable('argv');

// Get to a temporary directory.
Expand All @@ -13,7 +12,7 @@ export default function () {
} else if (argv.tmpdir) {
tempRoot = argv.tmpdir;
} else {
tempRoot = mkdtempSync(join(realpathSync(tmpdir()), 'angular-cli-e2e-'));
tempRoot = await mktempd('angular-cli-e2e-');
}
console.log(` Using "${tempRoot}" as temporary directory for a new project.`);
setGlobalVariable('tmp-root', tempRoot);
Expand Down
@@ -1,8 +1,9 @@
import { promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';
import { env } from 'process';
import { getGlobalVariable } from '../../../utils/env';
import { mktempd } from '../../../utils/utils';

import {
execAndCaptureError,
execAndWaitForOutputToMatch,
Expand Down Expand Up @@ -448,7 +449,7 @@ async function windowsTests(): Promise<void> {
}

async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), 'angular-cli-e2e-home-'));
const tempHome = await mktempd('angular-cli-e2e-home-');

try {
await cb(tempHome);
Expand Down
4 changes: 2 additions & 2 deletions tests/legacy-cli/e2e/tests/commands/completion/completion.ts
@@ -1,7 +1,7 @@
import { promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';
import { getGlobalVariable } from '../../../utils/env';
import { mktempd } from '../../../utils/utils';
import {
execAndCaptureError,
execAndWaitForOutputToMatch,
Expand Down Expand Up @@ -397,7 +397,7 @@ async function windowsTests(): Promise<void> {
}

async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), 'angular-cli-e2e-home-'));
const tempHome = await mktempd('angular-cli-e2e-home-');

try {
await cb(tempHome);
Expand Down
5 changes: 2 additions & 3 deletions tests/legacy-cli/e2e/utils/registry.ts
@@ -1,17 +1,16 @@
import { spawn } from 'child_process';
import { mkdtempSync, realpathSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { getGlobalVariable } from './env';
import { writeFile, readFile } from './fs';
import { mktempd } from './utils';

export async function createNpmRegistry(
port: number,
httpsPort: number,
withAuthentication = false,
) {
// Setup local package registry
const registryPath = mkdtempSync(join(realpathSync(tmpdir()), 'angular-cli-e2e-registry-'));
const registryPath = await mktempd('angular-cli-e2e-registry-');

let configContent = await readFile(
join(__dirname, '../../', withAuthentication ? 'verdaccio_auth.yaml' : 'verdaccio.yaml'),
Expand Down
8 changes: 8 additions & 0 deletions tests/legacy-cli/e2e/utils/utils.ts
@@ -1,3 +1,7 @@
import { mkdtemp, realpath } from 'fs/promises';
import { tmpdir } from 'os';
import path from 'path';

export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<any> {
return fn().then(
() => {
Expand All @@ -18,3 +22,7 @@ export function wait(msecs: number): Promise<void> {
setTimeout(resolve, msecs);
});
}

export async function mktempd(prefix: string): Promise<string> {
return realpath(await mkdtemp(path.join(tmpdir(), prefix)));
}

0 comments on commit df3871a

Please sign in to comment.