Skip to content

Commit

Permalink
feat(bazel): always fake home-system directories in integration tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devversion committed Jul 11, 2022
1 parent 6b5886c commit 0fb1e5b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
35 changes: 33 additions & 2 deletions bazel/integration/test_runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
import * as os from 'os';

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

/** Type describing an environment configuration that can be passed to the runner. */
type EnvironmentConfig = Record<string, BazelExpandedValue>;

/**
* Test runner that takes a set of files within a Bazel package and copies the files
* to a temporary directory where it then executes a list of specified commands.
Expand All @@ -44,15 +48,19 @@ class IntegrationTestCommandError extends Error {}
* test runner will patch the top-level `package.json` of the test Bazel package for that.
*/
export class TestRunner {
private readonly environment: EnvironmentConfig;

constructor(
private readonly testFiles: BazelFileInfo[],
private readonly testPackage: string,
private readonly testPackageRelativeWorkingDir: string,
private readonly toolMappings: Record<string, BazelFileInfo>,
private readonly npmPackageMappings: Record<string, BazelFileInfo>,
private readonly commands: [[binary: BazelExpandedValue, ...args: string[]]],
private readonly environment: Record<string, BazelExpandedValue>,
) {}
environment: EnvironmentConfig,
) {
this.environment = this._assignDefaultEnvironmentVariables(environment);
}

async run() {
const testTmpDir = await this._getTestTmpDirectoryPath();
Expand Down Expand Up @@ -262,4 +270,27 @@ export class TestRunner {
}
return mappings;
}

/**
* Assigns the default environment environments.
*
* We intend to always fake the system home-related directory environment variables
* to temporary directories. This helps as integration tests (even within the Bazel sandbox)
* have read access to the system home directory and attempt to write to it.
*/
private _assignDefaultEnvironmentVariables(baseEnv: EnvironmentConfig): EnvironmentConfig {
const defaults: EnvironmentConfig = {
'HOME': {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false},
};

// Support windows-specific system variables. We don't want to always assign these as
// it would result in unnecessary directories being created all the time.
if (os.platform() === 'win32') {
defaults.USERPROFILE = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
defaults.APPDATA = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
defaults.LOCALAPPDATA = {value: ENVIRONMENT_TMP_PLACEHOLDER, containsExpansion: false};
}

return {...defaults, ...baseEnv};
}
}
2 changes: 0 additions & 2 deletions bazel/integration/tests/angular-cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ integration_test(
"//bazel/browsers/chromium",
],
environment = {
# Ensure Yarn does not try to access the shared system cache (even within sandboxing)
"HOME": "<TMP>",
"CHROMEDRIVER_SKIP_DOWNLOAD": "true",
"CHROMEDRIVER_PATH": "$(CHROMEDRIVER)",
"CHROME_BIN": "$(CHROMIUM)",
Expand Down
5 changes: 5 additions & 0 deletions bazel/integration/tests/custom_env_variables/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ if (process.env.MANUAL_ROOT_PATH !== '../npm/node_modules/semver/package.json')
process.exit(1);
}

if (!process.env.HOME.includes('.tmp-env-')) {
console.error('Expected `HOME` to point to a temporary environment directory.');
process.exit(1);
}

const bazeliskHome = process.env.BAZELISK_HOME;
const bazeliskHome_2 = process.env.BAZELISK_HOME_2;

Expand Down
6 changes: 0 additions & 6 deletions bazel/integration/tests/nested_bazel_workspaces/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ integration_test(
"yarn",
"yarn test",
],
environment = {
# Setup a HOME directory so that Bazelisk can work, both Linux/macOS and Windows variants
# are configured to provide a fake home directory so that Bazelisk can download Bazel.
"HOME": "<TMP>",
"LOCALAPPDATA": "<TMP>",
},
tags = [
# This test relies on `yarn` so there needs to be internet access.
"requires-network",
Expand Down
10 changes: 6 additions & 4 deletions bazel/integration/tests/package_mappings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ integration_test(
"test.mjs",
],
commands = [
# Note: We use a cache folder within the integration test as otherwise
# the NPM package mapped archive would be cached in the system.
# See: https://github.com/yarnpkg/yarn/issues/2165.
"yarn install --cache-folder .yarn_cache_folder/",
"yarn",
"node ./test.mjs",
],
npm_packages = {
"//bazel/integration/tests/package_mappings/fake_pkg_srcs:archive": "fake_pkg",
},
tags = [
# We want to run a basic integration test on Windows to ensure the integration
# test rule works properly for Windows contributors.
"windows",
],
tool_mappings = {
"//:yarn_classic_vendored": "yarn",
"@nodejs_toolchains//:resolved_toolchain": "node",
Expand Down
2 changes: 0 additions & 2 deletions bazel/integration/tests/playwright_chromium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ integration_test(
"//bazel/browsers/chromium",
],
environment = {
# Ensure Yarn does not try to access the shared system cache (even within sandboxing)
"HOME": "<TMP>",
"CHROME_BIN": "$(CHROMIUM)",
},
tags = [
Expand Down

0 comments on commit 0fb1e5b

Please sign in to comment.