Skip to content

Commit cc54289

Browse files
Added feature helpers (#1010)
* Add node tl feature helper * Fix typescript err * Update changelog * Bump package version * node: Format debug message * powershell: Add Get-PipelineFeature * powershell: Bump version to 0.19.0 * Reset InputFunctions encoding
1 parent f7e477b commit cc54289

11 files changed

+140
-6
lines changed

node/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## 4.x
44

5+
### 4.9.0
6+
7+
- Added internal feature helpers [#1010](https://github.com/microsoft/azure-pipelines-task-lib/pull/1010)
8+
9+
### 4.8.0
10+
11+
- Added `source` property for error/warning [#1009](https://github.com/microsoft/azure-pipelines-task-lib/pull/1009)
12+
513
### 4.7.0
614

715
Replaced mockery - [#989](https://github.com/microsoft/azure-pipelines-task-lib/pull/989)

node/mock-task.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module.exports.getInput = task.getInput;
6262
module.exports.getInputRequired = task.getInputRequired;
6363
module.exports.getBoolInput = task.getBoolInput;
6464
module.exports.getBoolFeatureFlag = task.getBoolFeatureFlag;
65+
module.exports.getPipelineFeature = task.getPipelineFeature;
6566
module.exports.getDelimitedInput = task.getDelimitedInput;
6667
module.exports.filePathSupplied = task.filePathSupplied;
6768

node/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

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

node/task.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,11 @@ export function getBoolInput(name: string, required?: boolean): boolean {
279279

280280
/**
281281
* Gets the value of an feature flag and converts to a bool.
282-
*
282+
* @IMPORTANT This method is only for internal Microsoft development. Do not use it for external tasks.
283283
* @param name name of the feature flag to get.
284284
* @param defaultValue default value of the feature flag in case it's not found in env. (optional. Default value = false)
285285
* @returns boolean
286+
* @deprecated Don't use this for new development. Use getPipelineFeature instead.
286287
*/
287288
export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false): boolean {
288289
const ffValue = process.env[ffName];
@@ -297,6 +298,28 @@ export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false
297298
return ffValue.toLowerCase() === "true";
298299
}
299300

301+
/**
302+
* Gets the value of an task feature and converts to a bool.
303+
* @IMPORTANT This method is only for internal Microsoft development. Do not use it for external tasks.
304+
* @param name name of the feature to get.
305+
* @returns boolean
306+
*/
307+
export function getPipelineFeature(featureName: string): boolean {
308+
const variableName = im._getVariableKey(`DistributedTask.Tasks.${featureName}`);
309+
const featureValue = process.env[variableName];
310+
311+
if (!featureValue) {
312+
debug(`Feature '${featureName}' not found. Returning false as default.`);
313+
return false;
314+
}
315+
316+
const boolValue = featureValue.toLowerCase() === "true";
317+
318+
debug(`Feature '${featureName}' = '${featureValue}'. Processed as '${boolValue}'.`);
319+
320+
return boolValue;
321+
}
322+
300323
/**
301324
* Gets the value of an input and splits the value using a delimiter (space, comma, etc).
302325
* Empty values are removed. This function is useful for splitting an input containing a simple
@@ -801,7 +824,7 @@ export function mkdirP(p: string): void {
801824
let testDir: string = p;
802825
while (true) {
803826
// validate the loop is not out of control
804-
if (stack.length >= (process.env['TASKLIB_TEST_MKDIRP_FAILSAFE'] || 1000)) {
827+
if (stack.length >= Number(process.env['TASKLIB_TEST_MKDIRP_FAILSAFE'] || 1000)) {
805828
// let the framework throw
806829
debug('loop is out of control');
807830
fs.mkdirSync(p);

node/test/inputtests.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1200,4 +1200,38 @@ describe('Input Tests', function () {
12001200
assert.equal(ffValue, true);
12011201
})
12021202
});
1203+
1204+
describe('Pipeline features tests', () => {
1205+
it(`Should return if no feature variable present.`, () => {
1206+
const featureName = "TestFeature"
1207+
delete process.env[im._getVariableKey(`DistributedTask.Tasks.${featureName}`)];
1208+
1209+
const ffValue = tl.getPipelineFeature(featureName);
1210+
1211+
assert.deepStrictEqual(ffValue, false);
1212+
})
1213+
1214+
const testInputs = ([
1215+
["true", true],
1216+
["TRUE", true],
1217+
["TruE", true],
1218+
["false", false],
1219+
["treu", false],
1220+
["fasle", false],
1221+
["On", false],
1222+
["", false],
1223+
[undefined, false]
1224+
] as [string, boolean][])
1225+
for (const [input, expected] of testInputs) {
1226+
it(`Should return '${expected}' if feature is '${input}'`, () => {
1227+
const featureVariable = "DISTRIBUTEDTASK_TASKS_TESTFEATURE";
1228+
const featureName = "TestFeature";
1229+
process.env[featureVariable] = input;
1230+
1231+
const result = tl.getPipelineFeature(featureName);
1232+
1233+
assert.deepStrictEqual(result, expected);
1234+
})
1235+
}
1236+
})
12031237
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
. $PSScriptRoot\..\lib\Initialize-Test.ps1
5+
6+
$featureName = "TestFeature"
7+
$featureVariable = "DISTRIBUTEDTASK_TASKS_TESTFEATURE"
8+
9+
Invoke-VstsTaskScript -ScriptBlock {
10+
$testInputs = @(
11+
@("true", $true),
12+
@("TRUE", $true),
13+
@("TruE", $true),
14+
@("false", $false),
15+
@("treu", $false),
16+
@("fasle", $false),
17+
@("On", $false),
18+
@("", $false),
19+
@($null, $false)
20+
)
21+
foreach ($testInput in $testInputs) {
22+
$inputValue = $testInput[0]
23+
$expectedValue = $testInput[1]
24+
25+
Set-Item -Path env:$featureVariable -Value $inputValue
26+
27+
$result = Get-VstsPipelineFeature -FeatureName $featureName
28+
29+
try {
30+
Assert-AreEqual -Expected $expectedValue -Actual $result -Message "Suite failed. Input value: '$inputValue'"
31+
}
32+
finally {
33+
${env:$featureVariable} = ""
34+
}
35+
}
36+
}

powershell/VstsTaskSdk/InputFunctions.ps1

+31
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,37 @@ function Set-TaskVariable {
320320
Write-SetVariable -Name $Name -Value $Value -Secret:$Secret
321321
}
322322

323+
<#
324+
.SYNOPSIS
325+
Gets the value of an task feature and converts to a bool.
326+
327+
.PARAMETER $FeatureName
328+
Name of the feature to get.
329+
330+
.NOTES
331+
This method is only for internal Microsoft development. Do not use it for external tasks.
332+
#>
333+
function Get-PipelineFeature {
334+
[CmdletBinding(DefaultParameterSetName = 'Require')]
335+
param(
336+
[Parameter(Mandatory = $true)]
337+
[string]$FeatureName
338+
)
339+
340+
$featureValue = Get-TaskVariable -Name "DistributedTask.Tasks.$FeatureName"
341+
342+
if (!$featureValue) {
343+
Write-Debug "Feature '$FeatureName' is not set. Defaulting to 'false'"
344+
return $false
345+
}
346+
347+
$boolValue = $featureValue.ToLowerInvariant() -eq 'true'
348+
349+
Write-Debug "Feature '$FeatureName' = '$featureValue'. Processed as '$boolValue'"
350+
351+
return $boolValue
352+
}
353+
323354
########################################
324355
# Private functions.
325356
########################################

powershell/VstsTaskSdk/VstsTaskSdk.psm1

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Export-ModuleMember -Function @(
4444
'Get-TaskVariable'
4545
'Get-TaskVariableInfo'
4646
'Set-TaskVariable'
47+
'Get-PipelineFeature'
4748
# Legacy find functions.
4849
'Find-Files'
4950
# Localization functions.

powershell/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

powershell/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.18.2",
2+
"version": "0.19.0",
33
"private": true,
44
"scripts": {
55
"build": "node make.js build",

0 commit comments

Comments
 (0)