豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 0fb1e5b

Browse files
committed
feat(bazel): always fake home-system directories in integration tests
This allows us to remove all the Yarn tricks for locally-installed NPM package archives (those were cached accidentally -- when run locally without a sandbox for example). Also this makes all integration tests more hermetic.
1 parent 6b5886c commit 0fb1e5b

File tree

6 files changed

+44
-16
lines changed

6 files changed

+44
-16
lines changed

bazel/integration/test_runner/runner.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as fs from 'fs';
1010
import * as path from 'path';
1111
import * as tmp from 'tmp';
12+
import * as os from 'os';
1213

1314
import {
1415
BazelExpandedValue,
@@ -35,6 +36,9 @@ import {debug} from './debug';
3536
/** Error class that is used when an integration command fails. */
3637
class IntegrationTestCommandError extends Error {}
3738

39+
/** Type describing an environment configuration that can be passed to the runner. */
40+
type EnvironmentConfig = Record<string, BazelExpandedValue>;
41+
3842
/**
3943
* Test runner that takes a set of files within a Bazel package and copies the files
4044
* to a temporary directory where it then executes a list of specified commands.
@@ -44,15 +48,19 @@ class IntegrationTestCommandError extends Error {}
4448
* test runner will patch the top-level `package.json` of the test Bazel package for that.
4549
*/
4650
export class TestRunner {
51+
private readonly environment: EnvironmentConfig;
52+
4753
constructor(
4854
private readonly testFiles: BazelFileInfo[],
4955
private readonly testPackage: string,
5056
private readonly testPackageRelativeWorkingDir: string,
5157
private readonly toolMappings: Record<string, BazelFileInfo>,
5258
private readonly npmPackageMappings: Record<string, BazelFileInfo>,
5359
private readonly commands: [[binary: BazelExpandedValue, ...args: string[]]],
54-
private readonly environment: Record<string, BazelExpandedValue>,
55-
) {}
60+
environment: EnvironmentConfig,
61+
) {
62+
this.environment = this._assignDefaultEnvironmentVariables(environment);
63+
}
5664

5765
async run() {
5866
const testTmpDir = await this._getTestTmpDirectoryPath();
@@ -262,4 +270,27 @@ export class TestRunner {
262270
}
263271
return mappings;
264272
}
273+
274+
/**
275+
* Assigns the default environment environments.
276+
*
277+
* We intend to always fake the system home-related directory environment variables
278+
* to temporary directories. This helps as integration tests (even within the Bazel sandbox)
279+
* have read access to the system home directory and attempt to write to it.
280+
*/
281+
private _assignDefaultEnvironmentVariables(baseEnv: EnvironmentConfig): EnvironmentConfig {
282+
const defaults: EnvironmentConfig = {
283+
'HOME': {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false},
284+
};
285+
286+
// Support windows-specific system variables. We don't want to always assign these as
287+
// it would result in unnecessary directories being created all the time.
288+
if (os.platform() === 'win32') {
289+
defaults.USERPROFILE = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
290+
defaults.APPDATA = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
291+
defaults.LOCALAPPDATA = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
292+
}
293+
294+
return {...defaults, ...baseEnv};
295+
}
265296
}

bazel/integration/tests/angular-cli/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ integration_test(
1414
"//bazel/browsers/chromium",
1515
],
1616
environment = {
17-
# Ensure Yarn does not try to access the shared system cache (even within sandboxing)
18-
"HOME": "<TMP>",
1917
"CHROMEDRIVER_SKIP_DOWNLOAD": "true",
2018
"CHROMEDRIVER_PATH": "$(CHROMEDRIVER)",
2119
"CHROME_BIN": "$(CHROMIUM)",

bazel/integration/tests/custom_env_variables/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ if (process.env.MANUAL_ROOT_PATH !== '../npm/node_modules/semver/package.json')
2020
process.exit(1);
2121
}
2222

23+
if (!process.env.HOME.includes('.tmp-env-')) {
24+
console.error('Expected `HOME` to point to a temporary environment directory.');
25+
process.exit(1);
26+
}
27+
2328
const bazeliskHome = process.env.BAZELISK_HOME;
2429
const bazeliskHome_2 = process.env.BAZELISK_HOME_2;
2530

bazel/integration/tests/nested_bazel_workspaces/BUILD.bazel

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ integration_test(
1010
"yarn",
1111
"yarn test",
1212
],
13-
environment = {
14-
# Setup a HOME directory so that Bazelisk can work, both Linux/macOS and Windows variants
15-
# are configured to provide a fake home directory so that Bazelisk can download Bazel.
16-
"HOME": "<TMP>",
17-
"LOCALAPPDATA": "<TMP>",
18-
},
1913
tags = [
2014
# This test relies on `yarn` so there needs to be internet access.
2115
"requires-network",

bazel/integration/tests/package_mappings/BUILD.bazel

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ integration_test(
77
"test.mjs",
88
],
99
commands = [
10-
# Note: We use a cache folder within the integration test as otherwise
11-
# the NPM package mapped archive would be cached in the system.
12-
# See: https://github.com/yarnpkg/yarn/issues/2165.
13-
"yarn install --cache-folder .yarn_cache_folder/",
10+
"yarn",
1411
"node ./test.mjs",
1512
],
1613
npm_packages = {
1714
"//bazel/integration/tests/package_mappings/fake_pkg_srcs:archive": "fake_pkg",
1815
},
16+
tags = [
17+
# We want to run a basic integration test on Windows to ensure the integration
18+
# test rule works properly for Windows contributors.
19+
"windows",
20+
],
1921
tool_mappings = {
2022
"//:yarn_classic_vendored": "yarn",
2123
"@nodejs_toolchains//:resolved_toolchain": "node",

bazel/integration/tests/playwright_chromium/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ integration_test(
1212
"//bazel/browsers/chromium",
1313
],
1414
environment = {
15-
# Ensure Yarn does not try to access the shared system cache (even within sandboxing)
16-
"HOME": "<TMP>",
1715
"CHROME_BIN": "$(CHROMIUM)",
1816
},
1917
tags = [

0 commit comments

Comments
 (0)