Skip to content

Commit ab676de

Browse files
authored
Add getBoolFeatureFlag (#936)
* Add getBoolFeatureFlag * Add mock export * Bump package to 4.4.0 * Update changelog * Add tests * Add default value param * Fix tests * Make false as default + upd description
1 parent 4086d6b commit ab676de

File tree

6 files changed

+85
-6
lines changed

6 files changed

+85
-6
lines changed

node/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ Backported from ver.`3.4.0`:
4040
## 4.3.0
4141

4242
- Described types for `argIf` - [#920](https://github.com/microsoft/azure-pipelines-task-lib/pull/920)
43+
44+
## 4.3.1
45+
46+
- Resolve CVE-2022-24999 in qs 6.9.4 [#924](https://github.com/microsoft/azure-pipelines-task-lib/pull/924)
47+
48+
## 4.4.0
49+
50+
- Add `getBoolFeatureFlag` [#936](https://github.com/microsoft/azure-pipelines-task-lib/pull/936)

node/mock-task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module.exports.setTaskVariable = task.setTaskVariable;
6262
module.exports.getInput = task.getInput;
6363
module.exports.getInputRequired = task.getInputRequired;
6464
module.exports.getBoolInput = task.getBoolInput;
65+
module.exports.getBoolFeatureFlag = task.getBoolFeatureFlag;
6566
module.exports.getDelimitedInput = task.getDelimitedInput;
6667
module.exports.filePathSupplied = task.filePathSupplied;
6768

node/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-task-lib",
3-
"version": "4.3.1",
3+
"version": "4.4.0",
44
"description": "Azure Pipelines Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",

node/task.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,26 @@ export function getBoolInput(name: string, required?: boolean): boolean {
273273
return (getInput(name, required) || '').toUpperCase() == "TRUE";
274274
}
275275

276+
/**
277+
* Gets the value of an feature flag and converts to a bool.
278+
*
279+
* @param name name of the feature flag to get.
280+
* @param defaultValue default value of the feature flag in case it's not found in env. (optional. Default value = false)
281+
* @returns boolean
282+
*/
283+
export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false): boolean {
284+
const ffValue = process.env[ffName];
285+
286+
if (!ffValue) {
287+
debug(`Feature flag ${ffName} not found. Returning ${defaultValue} as default.`);
288+
return defaultValue;
289+
}
290+
291+
debug(`Feature flag ${ffName} = ${ffValue}`);
292+
293+
return ffValue.toLowerCase() === "true";
294+
}
295+
276296
/**
277297
* Gets the value of an input and splits the value using a delimiter (space, comma, etc).
278298
* Empty values are removed. This function is useful for splitting an input containing a simple
@@ -647,8 +667,8 @@ export function stats(path: string): FsStats {
647667
export const exist = im._exist;
648668

649669
export function writeFile(file: string, data: string | Buffer, options?: BufferEncoding | fs.WriteFileOptions) {
650-
if (typeof(options) === 'string'){
651-
fs.writeFileSync(file, data, {encoding: options as BufferEncoding});
670+
if (typeof (options) === 'string') {
671+
fs.writeFileSync(file, data, { encoding: options as BufferEncoding });
652672
}
653673
else {
654674
fs.writeFileSync(file, data, options);
@@ -689,7 +709,7 @@ export function getAgentMode(): AgentHostedMode {
689709

690710
if (agentCloudId === undefined)
691711
return AgentHostedMode.Unknown;
692-
712+
693713
if (agentCloudId)
694714
return AgentHostedMode.MsHosted;
695715

@@ -980,7 +1000,7 @@ export function retry(func: Function, args: any[], retryOptions: RetryOptions =
9801000
* @param allowBrokenSymbolicLinks when true, broken symbolic link will not cause an error.
9811001
* @returns fs.Stats
9821002
*/
983-
function _getStats (path: string, followSymbolicLink: boolean, allowBrokenSymbolicLinks: boolean): fs.Stats {
1003+
function _getStats(path: string, followSymbolicLink: boolean, allowBrokenSymbolicLinks: boolean): fs.Stats {
9841004
// stat returns info about the target of a symlink (or symlink chain),
9851005
// lstat returns info about a symlink itself
9861006
let stats: fs.Stats;

node/test/inputtests.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,4 +1150,54 @@ describe('Input Tests', function () {
11501150
assert.equal(tl.getInput('SomeInput'), 'some input value');
11511151
done();
11521152
})
1153+
1154+
describe('Feature flags tests', () => {
1155+
1156+
([
1157+
["true", true],
1158+
["TRUE", true],
1159+
["TruE", true],
1160+
["false", false],
1161+
["treu", false],
1162+
["fasle", false],
1163+
["On", false],
1164+
["", false],
1165+
[undefined, false]
1166+
] as [string, boolean][])
1167+
.forEach(
1168+
(
1169+
[
1170+
input,
1171+
expectedResult
1172+
]
1173+
) => {
1174+
it(`Should return ${expectedResult} if feature flag env is ${input}`, () => {
1175+
const ffName = "SOME_TEST_FF"
1176+
process.env[ffName] = input
1177+
1178+
const ffValue = tl.getBoolFeatureFlag(ffName, false);
1179+
1180+
assert.equal(ffValue, expectedResult);
1181+
})
1182+
}
1183+
);
1184+
1185+
it(`Should return default value if feature flag env is empty`, () => {
1186+
const ffName = "SOME_TEST_FF"
1187+
process.env[ffName] = ""
1188+
1189+
const ffValue = tl.getBoolFeatureFlag(ffName, true);
1190+
1191+
assert.equal(ffValue, true);
1192+
})
1193+
1194+
it(`Should return default value if feature flag env is not specified`, () => {
1195+
const ffName = "SOME_TEST_FF"
1196+
delete process.env[ffName];
1197+
1198+
const ffValue = tl.getBoolFeatureFlag(ffName, true);
1199+
1200+
assert.equal(ffValue, true);
1201+
})
1202+
});
11531203
});

0 commit comments

Comments
 (0)