Skip to content

Commit c1268a5

Browse files
authored
Merge pull request #2813 from EmmanuelOluyomi/eoluyomi/EnvironConfig
Environment Configuration property validation
2 parents 048607f + 9b37c6c commit c1268a5

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

apps/rush-lib/src/api/EnvironmentConfiguration.ts

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { InternalError } from '@rushstack/node-core-library';
21
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
32
// See LICENSE in the project root for license information.
43

@@ -155,7 +154,7 @@ export const enum EnvironmentVariableNames {
155154
* Initialize will throw if any unknown parameters are present.
156155
*/
157156
export class EnvironmentConfiguration {
158-
private static _hasBeenInitialized: boolean = false;
157+
private static _hasBeenValidated: boolean = false;
159158

160159
private static _rushTempFolderOverride: string | undefined;
161160

@@ -181,7 +180,7 @@ export class EnvironmentConfiguration {
181180
* An override for the common/temp folder path.
182181
*/
183182
public static get rushTempFolderOverride(): string | undefined {
184-
EnvironmentConfiguration._ensureInitialized();
183+
EnvironmentConfiguration._ensureValidated();
185184
return EnvironmentConfiguration._rushTempFolderOverride;
186185
}
187186

@@ -190,7 +189,7 @@ export class EnvironmentConfiguration {
190189
* See {@link EnvironmentVariableNames.RUSH_ABSOLUTE_SYMLINKS}
191190
*/
192191
public static get absoluteSymlinks(): boolean {
193-
EnvironmentConfiguration._ensureInitialized();
192+
EnvironmentConfiguration._ensureValidated();
194193
return EnvironmentConfiguration._absoluteSymlinks;
195194
}
196195

@@ -202,7 +201,7 @@ export class EnvironmentConfiguration {
202201
* See {@link EnvironmentVariableNames.RUSH_ALLOW_UNSUPPORTED_NODEJS}.
203202
*/
204203
public static get allowUnsupportedNodeVersion(): boolean {
205-
EnvironmentConfiguration._ensureInitialized();
204+
EnvironmentConfiguration._ensureValidated();
206205
return EnvironmentConfiguration._allowUnsupportedNodeVersion;
207206
}
208207

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

@@ -221,7 +220,7 @@ export class EnvironmentConfiguration {
221220
* See {@link EnvironmentVariableNames.RUSH_PNPM_STORE_PATH}
222221
*/
223222
public static get pnpmStorePathOverride(): string | undefined {
224-
EnvironmentConfiguration._ensureInitialized();
223+
EnvironmentConfiguration._ensureValidated();
225224
return EnvironmentConfiguration._pnpmStorePathOverride;
226225
}
227226

@@ -230,7 +229,7 @@ export class EnvironmentConfiguration {
230229
* See {@link EnvironmentVariableNames.RUSH_GLOBAL_FOLDER}
231230
*/
232231
public static get rushGlobalFolderOverride(): string | undefined {
233-
EnvironmentConfiguration._ensureInitialized();
232+
EnvironmentConfiguration._ensureValidated();
234233
return EnvironmentConfiguration._rushGlobalFolderOverride;
235234
}
236235

@@ -239,7 +238,7 @@ export class EnvironmentConfiguration {
239238
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_CREDENTIAL}
240239
*/
241240
public static get buildCacheCredential(): string | undefined {
242-
EnvironmentConfiguration._ensureInitialized();
241+
EnvironmentConfiguration._ensureValidated();
243242
return EnvironmentConfiguration._buildCacheCredential;
244243
}
245244

@@ -248,7 +247,7 @@ export class EnvironmentConfiguration {
248247
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_ENABLED}
249248
*/
250249
public static get buildCacheEnabled(): boolean | undefined {
251-
EnvironmentConfiguration._ensureInitialized();
250+
EnvironmentConfiguration._ensureValidated();
252251
return EnvironmentConfiguration._buildCacheEnabled;
253252
}
254253

@@ -257,7 +256,7 @@ export class EnvironmentConfiguration {
257256
* See {@link EnvironmentVariableNames.RUSH_BUILD_CACHE_WRITE_ALLOWED}
258257
*/
259258
public static get buildCacheWriteAllowed(): boolean | undefined {
260-
EnvironmentConfiguration._ensureInitialized();
259+
EnvironmentConfiguration._ensureValidated();
261260
return EnvironmentConfiguration._buildCacheWriteAllowed;
262261
}
263262

@@ -266,7 +265,7 @@ export class EnvironmentConfiguration {
266265
* See {@link EnvironmentVariableNames.RUSH_GIT_BINARY_PATH}
267266
*/
268267
public static get gitBinaryPath(): string | undefined {
269-
EnvironmentConfiguration._ensureInitialized();
268+
EnvironmentConfiguration._ensureValidated();
270269
return EnvironmentConfiguration._gitBinaryPath;
271270
}
272271

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

294293
const unknownEnvVariables: string[] = [];
@@ -411,7 +410,7 @@ export class EnvironmentConfiguration {
411410
EnvironmentConfiguration._rushGlobalFolderOverride =
412411
EnvironmentConfiguration._getRushGlobalFolderOverride(process.env);
413412

414-
EnvironmentConfiguration._hasBeenInitialized = true;
413+
EnvironmentConfiguration._hasBeenValidated = true;
415414
}
416415

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

423-
EnvironmentConfiguration._hasBeenInitialized = false;
422+
EnvironmentConfiguration._hasBeenValidated = false;
424423
}
425424

426-
private static _ensureInitialized(): void {
427-
if (!EnvironmentConfiguration._hasBeenInitialized) {
428-
throw new InternalError(
429-
'The EnvironmentConfiguration must be initialized before values can be accessed.'
430-
);
425+
private static _ensureValidated(): void {
426+
if (!EnvironmentConfiguration._hasBeenValidated) {
427+
EnvironmentConfiguration.validate();
431428
}
432429
}
433430

apps/rush-lib/src/api/RushConfiguration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ export class RushConfiguration {
490490
*/
491491
private constructor(rushConfigurationJson: IRushConfigurationJson, rushJsonFilename: string) {
492492
this._rushConfigurationJson = rushConfigurationJson;
493-
EnvironmentConfiguration.initialize();
493+
EnvironmentConfiguration.validate();
494494

495495
if (rushConfigurationJson.nodeSupportedVersionRange) {
496496
if (!semver.validRange(rushConfigurationJson.nodeSupportedVersionRange)) {

apps/rush-lib/src/api/test/EnvironmentConfiguration.test.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,63 @@ describe('EnvironmentConfiguration', () => {
1717
process.env = _oldEnv;
1818
});
1919

20-
describe('initialize', () => {
20+
describe('validate', () => {
2121
it('correctly allows no environment variables', () => {
22-
expect(EnvironmentConfiguration.initialize).not.toThrow();
22+
expect(EnvironmentConfiguration.validate).not.toThrow();
2323
});
2424

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

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

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

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

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

4444
expect(EnvironmentConfiguration.rushTempFolderOverride).toEqual('/var/tempB');
4545
});
4646
});
4747

4848
describe('rushTempDirOverride', () => {
49-
it('throws if EnvironmentConfiguration is not initialized', () => {
50-
expect(() => EnvironmentConfiguration.rushTempFolderOverride).toThrow();
51-
});
52-
5349
it('returns undefined for unset environment variables', () => {
54-
EnvironmentConfiguration.initialize();
50+
EnvironmentConfiguration.validate();
5551

5652
expect(EnvironmentConfiguration.rushTempFolderOverride).not.toBeDefined();
5753
});
5854

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

6460
expect(EnvironmentConfiguration.rushTempFolderOverride).toEqual(expectedValue);
6561
});
6662
});
6763

6864
describe('pnpmStorePathOverride', () => {
6965
const ENV_VAR: string = 'RUSH_PNPM_STORE_PATH';
70-
it('throws if EnvironmentConfiguration is not initialized', () => {
71-
expect(() => EnvironmentConfiguration.pnpmStorePathOverride).toThrow();
72-
});
7366

7467
it('returns undefined for unset environment variable', () => {
75-
EnvironmentConfiguration.initialize();
68+
EnvironmentConfiguration.validate();
7669

7770
expect(EnvironmentConfiguration.pnpmStorePathOverride).not.toBeDefined();
7871
});
7972

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

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

93-
EnvironmentConfiguration.initialize();
86+
EnvironmentConfiguration.validate();
9487

9588
expect(EnvironmentConfiguration.pnpmStorePathOverride).toEqual(expectedValue);
9689
});

apps/rush-lib/src/cli/RushCommandLineParser.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ export class RushCommandLineParser extends CommandLineParser {
250250

251251
this._validateCommandLineConfigCommand(command);
252252

253-
const overrideAllowWarnings: boolean =
254-
this.rushConfiguration && EnvironmentConfiguration.allowWarningsInSuccessfulBuild;
253+
const overrideAllowWarnings: boolean = EnvironmentConfiguration.allowWarningsInSuccessfulBuild;
255254

256255
switch (command.commandKind) {
257256
case RushConstants.bulkCommandKind:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "[email protected]"
11+
}

0 commit comments

Comments
 (0)