Skip to content

test: use common mockHome method #23687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
import * as path from 'path';
import { env } from 'process';
import { getGlobalVariable } from '../../../utils/env';
import { mktempd } from '../../../utils/utils';
import { mockHome } from '../../../utils/utils';

import {
execAndCaptureError,
Expand Down Expand Up @@ -447,13 +447,3 @@ async function windowsTests(): Promise<void> {
}
});
}

async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await mktempd('angular-cli-e2e-home-');

try {
await cb(tempHome);
} finally {
await fs.rm(tempHome, { recursive: true, force: true });
}
}
26 changes: 1 addition & 25 deletions tests/legacy-cli/e2e/tests/commands/completion/completion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs } from 'fs';
import * as path from 'path';
import { getGlobalVariable } from '../../../utils/env';
import { mktempd } from '../../../utils/utils';
import { mockHome } from '../../../utils/utils';
import {
execAndCaptureError,
execAndWaitForOutputToMatch,
Expand Down Expand Up @@ -29,7 +29,6 @@ export default async function () {
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);

Expand All @@ -52,7 +51,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);

Expand All @@ -77,7 +75,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);

Expand All @@ -103,7 +100,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);

Expand All @@ -129,7 +125,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);

Expand Down Expand Up @@ -160,7 +155,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);

Expand Down Expand Up @@ -196,7 +190,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);

Expand All @@ -222,7 +215,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);

Expand All @@ -248,7 +240,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);

Expand Down Expand Up @@ -279,7 +270,6 @@ source <(ng completion script)
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);

Expand Down Expand Up @@ -322,7 +312,6 @@ source <(ng completion script)
const err = await execAndCaptureError('ng', ['completion'], {
...process.env,
SHELL: undefined,
HOME: home,
});
if (!err.message.includes('`$SHELL` environment variable not set.')) {
throw new Error(`Expected unset \`$SHELL\` error message, but got:\n\n${err.message}`);
Expand All @@ -334,7 +323,6 @@ source <(ng completion script)
const err = await execAndCaptureError('ng', ['completion'], {
...process.env,
SHELL: '/usr/bin/unknown',
HOME: home,
});
if (!err.message.includes('Unknown `$SHELL` environment variable')) {
throw new Error(`Expected unknown \`$SHELL\` error message, but got:\n\n${err.message}`);
Expand All @@ -346,7 +334,6 @@ source <(ng completion script)
const { stdout } = await execWithEnv('ng', ['completion'], {
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
});

if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
Expand All @@ -373,7 +360,6 @@ source <(ng completion script)
const { stdout } = await execWithEnv(localCliBinary, ['completion'], {
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
});

if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
Expand All @@ -395,13 +381,3 @@ async function windowsTests(): Promise<void> {
);
}
}

async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await mktempd('angular-cli-e2e-home-');

try {
await cb(tempHome);
} finally {
await fs.rm(tempHome, { recursive: true, force: true });
}
}
21 changes: 4 additions & 17 deletions tests/legacy-cli/e2e/tests/misc/ask-analytics-command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { promises as fs } from 'fs';
import { execWithEnv } from '../../utils/process';
import { mockHome } from '../../utils/utils';

const ANALYTICS_PROMPT = /Would you like to share anonymous usage data/;

export default async function () {
// CLI should prompt for analytics permissions.
await mockHome(async (home) => {
await mockHome(async () => {
const { stdout } = await execWithEnv(
'ng',
['version'],
{
...process.env,
HOME: home,
NG_FORCE_TTY: '1',
NG_FORCE_AUTOCOMPLETE: 'false',
},
Expand All @@ -24,10 +23,9 @@ export default async function () {
});

// CLI should skip analytics prompt with `NG_CLI_ANALYTICS=false`.
await mockHome(async (home) => {
await mockHome(async () => {
const { stdout } = await execWithEnv('ng', ['version'], {
...process.env,
HOME: home,
NG_FORCE_TTY: '1',
NG_CLI_ANALYTICS: 'false',
NG_FORCE_AUTOCOMPLETE: 'false',
Expand All @@ -39,10 +37,9 @@ export default async function () {
});

// CLI should skip analytics prompt during `ng update`.
await mockHome(async (home) => {
await mockHome(async () => {
const { stdout } = await execWithEnv('ng', ['update', '--help'], {
...process.env,
HOME: home,
NG_FORCE_TTY: '1',
NG_FORCE_AUTOCOMPLETE: 'false',
});
Expand All @@ -54,13 +51,3 @@ export default async function () {
}
});
}

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

try {
await cb(tempHome);
} finally {
await fs.rm(tempHome, { recursive: true, force: true });
}
}
17 changes: 16 additions & 1 deletion tests/legacy-cli/e2e/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtemp, realpath } from 'fs/promises';
import { mkdtemp, realpath, rm } from 'fs/promises';
import { tmpdir } from 'os';
import path from 'path';

Expand Down Expand Up @@ -26,3 +26,18 @@ export function wait(msecs: number): Promise<void> {
export async function mktempd(prefix: string): Promise<string> {
return realpath(await mkdtemp(path.join(tmpdir(), prefix)));
}

export async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await mktempd('angular-cli-e2e-home-');

const oldHome = process.env.HOME;
process.env.HOME = tempHome;

try {
await cb(tempHome);
} finally {
process.env.HOME = oldHome;

await rm(tempHome, { recursive: true, force: true });
}
}