Skip to content

Commit

Permalink
Add getBoolFeatureFlag (#936)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KonstantinTyukalov authored Jun 16, 2023
1 parent a201ab9 commit 232610f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
8 changes: 8 additions & 0 deletions node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ Backported from ver.`3.4.0`:
## 4.3.0

- Described types for `argIf` - [#920](https://github.com/microsoft/azure-pipelines-task-lib/pull/920)

## 4.3.1

- Resolve CVE-2022-24999 in qs 6.9.4 [#924](https://github.com/microsoft/azure-pipelines-task-lib/pull/924)

## 4.4.0

- Add `getBoolFeatureFlag` [#936](https://github.com/microsoft/azure-pipelines-task-lib/pull/936)
1 change: 1 addition & 0 deletions node/mock-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports.setTaskVariable = task.setTaskVariable;
module.exports.getInput = task.getInput;
module.exports.getInputRequired = task.getInputRequired;
module.exports.getBoolInput = task.getBoolInput;
module.exports.getBoolFeatureFlag = task.getBoolFeatureFlag;
module.exports.getDelimitedInput = task.getDelimitedInput;
module.exports.filePathSupplied = task.filePathSupplied;

Expand Down
2 changes: 1 addition & 1 deletion node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "4.3.1",
"version": "4.4.0",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down
28 changes: 24 additions & 4 deletions node/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ export function getBoolInput(name: string, required?: boolean): boolean {
return (getInput(name, required) || '').toUpperCase() == "TRUE";
}

/**
* Gets the value of an feature flag and converts to a bool.
*
* @param name name of the feature flag to get.
* @param defaultValue default value of the feature flag in case it's not found in env. (optional. Default value = false)
* @returns boolean
*/
export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false): boolean {
const ffValue = process.env[ffName];

if (!ffValue) {
debug(`Feature flag ${ffName} not found. Returning ${defaultValue} as default.`);
return defaultValue;
}

debug(`Feature flag ${ffName} = ${ffValue}`);

return ffValue.toLowerCase() === "true";
}

/**
* Gets the value of an input and splits the value using a delimiter (space, comma, etc).
* Empty values are removed. This function is useful for splitting an input containing a simple
Expand Down Expand Up @@ -647,8 +667,8 @@ export function stats(path: string): FsStats {
export const exist = im._exist;

export function writeFile(file: string, data: string | Buffer, options?: BufferEncoding | fs.WriteFileOptions) {
if (typeof(options) === 'string'){
fs.writeFileSync(file, data, {encoding: options as BufferEncoding});
if (typeof (options) === 'string') {
fs.writeFileSync(file, data, { encoding: options as BufferEncoding });
}
else {
fs.writeFileSync(file, data, options);
Expand Down Expand Up @@ -689,7 +709,7 @@ export function getAgentMode(): AgentHostedMode {

if (agentCloudId === undefined)
return AgentHostedMode.Unknown;

if (agentCloudId)
return AgentHostedMode.MsHosted;

Expand Down Expand Up @@ -980,7 +1000,7 @@ export function retry(func: Function, args: any[], retryOptions: RetryOptions =
* @param allowBrokenSymbolicLinks when true, broken symbolic link will not cause an error.
* @returns fs.Stats
*/
function _getStats (path: string, followSymbolicLink: boolean, allowBrokenSymbolicLinks: boolean): fs.Stats {
function _getStats(path: string, followSymbolicLink: boolean, allowBrokenSymbolicLinks: boolean): fs.Stats {
// stat returns info about the target of a symlink (or symlink chain),
// lstat returns info about a symlink itself
let stats: fs.Stats;
Expand Down
50 changes: 50 additions & 0 deletions node/test/inputtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,54 @@ describe('Input Tests', function () {
assert.equal(tl.getInput('SomeInput'), 'some input value');
done();
})

describe('Feature flags tests', () => {

([
["true", true],
["TRUE", true],
["TruE", true],
["false", false],
["treu", false],
["fasle", false],
["On", false],
["", false],
[undefined, false]
] as [string, boolean][])
.forEach(
(
[
input,
expectedResult
]
) => {
it(`Should return ${expectedResult} if feature flag env is ${input}`, () => {
const ffName = "SOME_TEST_FF"
process.env[ffName] = input

const ffValue = tl.getBoolFeatureFlag(ffName, false);

assert.equal(ffValue, expectedResult);
})
}
);

it(`Should return default value if feature flag env is empty`, () => {
const ffName = "SOME_TEST_FF"
process.env[ffName] = ""

const ffValue = tl.getBoolFeatureFlag(ffName, true);

assert.equal(ffValue, true);
})

it(`Should return default value if feature flag env is not specified`, () => {
const ffName = "SOME_TEST_FF"
delete process.env[ffName];

const ffValue = tl.getBoolFeatureFlag(ffName, true);

assert.equal(ffValue, true);
})
});
});

0 comments on commit 232610f

Please sign in to comment.