Skip to content
Merged
37 changes: 17 additions & 20 deletions apps/rush-lib/src/api/EnvironmentConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InternalError } from '@rushstack/node-core-library';
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

Expand Down Expand Up @@ -155,7 +154,7 @@ export const enum EnvironmentVariableNames {
* Initialize will throw if any unknown parameters are present.
*/
export class EnvironmentConfiguration {
private static _hasBeenInitialized: boolean = false;
private static _hasBeenValidated: boolean = false;

private static _rushTempFolderOverride: string | undefined;

Expand All @@ -181,7 +180,7 @@ export class EnvironmentConfiguration {
* An override for the common/temp folder path.
*/
public static get rushTempFolderOverride(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._rushTempFolderOverride;
}

Expand All @@ -190,7 +189,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_ABSOLUTE_SYMLINKS}
*/
public static get absoluteSymlinks(): boolean {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._absoluteSymlinks;
}

Expand All @@ -202,7 +201,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_ALLOW_UNSUPPORTED_NODEJS}.
*/
public static get allowUnsupportedNodeVersion(): boolean {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._allowUnsupportedNodeVersion;
}

Expand All @@ -212,7 +211,7 @@ export class EnvironmentConfiguration {
* or `0` to disallow them. (See the comments in the command-line.json file for more information).
*/
public static get allowWarningsInSuccessfulBuild(): boolean {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._allowWarningsInSuccessfulBuild;
}

Expand All @@ -221,7 +220,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_PNPM_STORE_PATH}
*/
public static get pnpmStorePathOverride(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._pnpmStorePathOverride;
}

Expand All @@ -230,7 +229,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_GLOBAL_FOLDER}
*/
public static get rushGlobalFolderOverride(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._rushGlobalFolderOverride;
}

Expand All @@ -239,7 +238,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_CREDENTIAL}
*/
public static get buildCacheCredential(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._buildCacheCredential;
}

Expand All @@ -248,7 +247,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_ENABLED}
*/
public static get buildCacheEnabled(): boolean | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._buildCacheEnabled;
}

Expand All @@ -257,7 +256,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_WRITE_ALLOWED}
*/
public static get buildCacheWriteAllowed(): boolean | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._buildCacheWriteAllowed;
}

Expand All @@ -266,7 +265,7 @@ export class EnvironmentConfiguration {
* See {@link EnvironmentVariableNames.RUSH_GIT_BINARY_PATH}
*/
public static get gitBinaryPath(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
EnvironmentConfiguration._ensureValidated();
return EnvironmentConfiguration._gitBinaryPath;
}

Expand All @@ -288,7 +287,7 @@ export class EnvironmentConfiguration {
/**
* Reads and validates environment variables. If any are invalid, this function will throw.
*/
public static initialize(options: IEnvironmentConfigurationInitializeOptions = {}): void {
public static validate(options: IEnvironmentConfigurationInitializeOptions = {}): void {
EnvironmentConfiguration.reset();

const unknownEnvVariables: string[] = [];
Expand Down Expand Up @@ -411,7 +410,7 @@ export class EnvironmentConfiguration {
EnvironmentConfiguration._rushGlobalFolderOverride =
EnvironmentConfiguration._getRushGlobalFolderOverride(process.env);

EnvironmentConfiguration._hasBeenInitialized = true;
EnvironmentConfiguration._hasBeenValidated = true;
}

/**
Expand All @@ -420,14 +419,12 @@ export class EnvironmentConfiguration {
public static reset(): void {
EnvironmentConfiguration._rushTempFolderOverride = undefined;

EnvironmentConfiguration._hasBeenInitialized = false;
EnvironmentConfiguration._hasBeenValidated = false;
}

private static _ensureInitialized(): void {
if (!EnvironmentConfiguration._hasBeenInitialized) {
throw new InternalError(
'The EnvironmentConfiguration must be initialized before values can be accessed.'
);
private static _ensureValidated(): void {
if (!EnvironmentConfiguration._hasBeenValidated) {
EnvironmentConfiguration.validate();
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/rush-lib/src/api/RushConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class RushConfiguration {
*/
private constructor(rushConfigurationJson: IRushConfigurationJson, rushJsonFilename: string) {
this._rushConfigurationJson = rushConfigurationJson;
EnvironmentConfiguration.initialize();
EnvironmentConfiguration.validate();

if (rushConfigurationJson.nodeSupportedVersionRange) {
if (!semver.validRange(rushConfigurationJson.nodeSupportedVersionRange)) {
Expand Down
31 changes: 12 additions & 19 deletions apps/rush-lib/src/api/test/EnvironmentConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,63 @@ describe('EnvironmentConfiguration', () => {
process.env = _oldEnv;
});

describe('initialize', () => {
describe('validate', () => {
it('correctly allows no environment variables', () => {
expect(EnvironmentConfiguration.initialize).not.toThrow();
expect(EnvironmentConfiguration.validate).not.toThrow();
});

it('allows known environment variables', () => {
process.env['RUSH_TEMP_FOLDER'] = '/var/temp'; // eslint-disable-line dot-notation
expect(EnvironmentConfiguration.initialize).not.toThrow();
expect(EnvironmentConfiguration.validate).not.toThrow();
});

it('does not allow unknown environment variables', () => {
process.env['rush_foobar'] = 'asdf'; // eslint-disable-line dot-notation
expect(EnvironmentConfiguration.initialize).toThrow();
expect(EnvironmentConfiguration.validate).toThrow();
});

it('can be re-initialized', () => {
it('can revalidate after a reset', () => {
process.env['RUSH_TEMP_FOLDER'] = '/var/tempA'; // eslint-disable-line dot-notation
EnvironmentConfiguration.initialize({ doNotNormalizePaths: true });
EnvironmentConfiguration.validate({ doNotNormalizePaths: true });

expect(EnvironmentConfiguration.rushTempFolderOverride).toEqual('/var/tempA');

process.env['RUSH_TEMP_FOLDER'] = '/var/tempB'; // eslint-disable-line dot-notation
EnvironmentConfiguration.initialize({ doNotNormalizePaths: true });
EnvironmentConfiguration.validate({ doNotNormalizePaths: true });

expect(EnvironmentConfiguration.rushTempFolderOverride).toEqual('/var/tempB');
});
});

describe('rushTempDirOverride', () => {
it('throws if EnvironmentConfiguration is not initialized', () => {
expect(() => EnvironmentConfiguration.rushTempFolderOverride).toThrow();
});

it('returns undefined for unset environment variables', () => {
EnvironmentConfiguration.initialize();
EnvironmentConfiguration.validate();

expect(EnvironmentConfiguration.rushTempFolderOverride).not.toBeDefined();
});

it('returns the value for a set environment variable', () => {
const expectedValue: string = '/var/temp';
process.env['RUSH_TEMP_FOLDER'] = expectedValue; // eslint-disable-line dot-notation
EnvironmentConfiguration.initialize({ doNotNormalizePaths: true });
EnvironmentConfiguration.validate({ doNotNormalizePaths: true });

expect(EnvironmentConfiguration.rushTempFolderOverride).toEqual(expectedValue);
});
});

describe('pnpmStorePathOverride', () => {
const ENV_VAR: string = 'RUSH_PNPM_STORE_PATH';
it('throws if EnvironmentConfiguration is not initialized', () => {
expect(() => EnvironmentConfiguration.pnpmStorePathOverride).toThrow();
});

it('returns undefined for unset environment variable', () => {
EnvironmentConfiguration.initialize();
EnvironmentConfiguration.validate();

expect(EnvironmentConfiguration.pnpmStorePathOverride).not.toBeDefined();
});

it('returns the expected path from environment variable without normalization', () => {
const expectedValue: string = '/var/temp';
process.env[ENV_VAR] = expectedValue;
EnvironmentConfiguration.initialize({ doNotNormalizePaths: true });
EnvironmentConfiguration.validate({ doNotNormalizePaths: true });

expect(EnvironmentConfiguration.pnpmStorePathOverride).toEqual(expectedValue);
});
Expand All @@ -90,7 +83,7 @@ describe('EnvironmentConfiguration', () => {
const envVar: string = './temp';
process.env[ENV_VAR] = envVar;

EnvironmentConfiguration.initialize();
EnvironmentConfiguration.validate();

expect(EnvironmentConfiguration.pnpmStorePathOverride).toEqual(expectedValue);
});
Expand Down
3 changes: 1 addition & 2 deletions apps/rush-lib/src/cli/RushCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ export class RushCommandLineParser extends CommandLineParser {

this._validateCommandLineConfigCommand(command);

const overrideAllowWarnings: boolean =
this.rushConfiguration && EnvironmentConfiguration.allowWarningsInSuccessfulBuild;
const overrideAllowWarnings: boolean = EnvironmentConfiguration.allowWarningsInSuccessfulBuild;

switch (command.commandKind) {
case RushConstants.bulkCommandKind:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "",
"type": "none"
}
],
"packageName": "@microsoft/rush",
"email": "4673363+octogonz@users.noreply.github.com"
}