Skip to content

Commit

Permalink
Added feature helpers (#1010)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KonstantinTyukalov authored Jan 30, 2024
1 parent 9f5319f commit 718a306
Show file tree
Hide file tree
Showing 11 changed files with 140 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 @@ -2,6 +2,14 @@

## 4.x

### 4.9.0

- Added internal feature helpers [#1010](https://github.com/microsoft/azure-pipelines-task-lib/pull/1010)

### 4.8.0

- Added `source` property for error/warning [#1009](https://github.com/microsoft/azure-pipelines-task-lib/pull/1009)

### 4.7.0

Replaced mockery - [#989](https://github.com/microsoft/azure-pipelines-task-lib/pull/989)
Expand Down
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.getInput = task.getInput;
module.exports.getInputRequired = task.getInputRequired;
module.exports.getBoolInput = task.getBoolInput;
module.exports.getBoolFeatureFlag = task.getBoolFeatureFlag;
module.exports.getPipelineFeature = task.getPipelineFeature;
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.8.2",
"version": "4.9.0",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down
27 changes: 25 additions & 2 deletions node/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,11 @@ export function getBoolInput(name: string, required?: boolean): boolean {

/**
* Gets the value of an feature flag and converts to a bool.
*
* @IMPORTANT This method is only for internal Microsoft development. Do not use it for external tasks.
* @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
* @deprecated Don't use this for new development. Use getPipelineFeature instead.
*/
export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false): boolean {
const ffValue = process.env[ffName];
Expand All @@ -297,6 +298,28 @@ export function getBoolFeatureFlag(ffName: string, defaultValue: boolean = false
return ffValue.toLowerCase() === "true";
}

/**
* Gets the value of an task feature and converts to a bool.
* @IMPORTANT This method is only for internal Microsoft development. Do not use it for external tasks.
* @param name name of the feature to get.
* @returns boolean
*/
export function getPipelineFeature(featureName: string): boolean {
const variableName = im._getVariableKey(`DistributedTask.Tasks.${featureName}`);
const featureValue = process.env[variableName];

if (!featureValue) {
debug(`Feature '${featureName}' not found. Returning false as default.`);
return false;
}

const boolValue = featureValue.toLowerCase() === "true";

debug(`Feature '${featureName}' = '${featureValue}'. Processed as '${boolValue}'.`);

return boolValue;
}

/**
* 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 @@ -801,7 +824,7 @@ export function mkdirP(p: string): void {
let testDir: string = p;
while (true) {
// validate the loop is not out of control
if (stack.length >= (process.env['TASKLIB_TEST_MKDIRP_FAILSAFE'] || 1000)) {
if (stack.length >= Number(process.env['TASKLIB_TEST_MKDIRP_FAILSAFE'] || 1000)) {
// let the framework throw
debug('loop is out of control');
fs.mkdirSync(p);
Expand Down
34 changes: 34 additions & 0 deletions node/test/inputtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,4 +1200,38 @@ describe('Input Tests', function () {
assert.equal(ffValue, true);
})
});

describe('Pipeline features tests', () => {
it(`Should return if no feature variable present.`, () => {
const featureName = "TestFeature"
delete process.env[im._getVariableKey(`DistributedTask.Tasks.${featureName}`)];

const ffValue = tl.getPipelineFeature(featureName);

assert.deepStrictEqual(ffValue, false);
})

const testInputs = ([
["true", true],
["TRUE", true],
["TruE", true],
["false", false],
["treu", false],
["fasle", false],
["On", false],
["", false],
[undefined, false]
] as [string, boolean][])
for (const [input, expected] of testInputs) {
it(`Should return '${expected}' if feature is '${input}'`, () => {
const featureVariable = "DISTRIBUTEDTASK_TASKS_TESTFEATURE";
const featureName = "TestFeature";
process.env[featureVariable] = input;

const result = tl.getPipelineFeature(featureName);

assert.deepStrictEqual(result, expected);
})
}
})
});
36 changes: 36 additions & 0 deletions powershell/Tests/L0/Get-PipelineFeature.InputsTest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[CmdletBinding()]
param()

. $PSScriptRoot\..\lib\Initialize-Test.ps1

$featureName = "TestFeature"
$featureVariable = "DISTRIBUTEDTASK_TASKS_TESTFEATURE"

Invoke-VstsTaskScript -ScriptBlock {
$testInputs = @(
@("true", $true),
@("TRUE", $true),
@("TruE", $true),
@("false", $false),
@("treu", $false),
@("fasle", $false),
@("On", $false),
@("", $false),
@($null, $false)
)
foreach ($testInput in $testInputs) {
$inputValue = $testInput[0]
$expectedValue = $testInput[1]

Set-Item -Path env:$featureVariable -Value $inputValue

$result = Get-VstsPipelineFeature -FeatureName $featureName

try {
Assert-AreEqual -Expected $expectedValue -Actual $result -Message "Suite failed. Input value: '$inputValue'"
}
finally {
${env:$featureVariable} = ""
}
}
}
31 changes: 31 additions & 0 deletions powershell/VstsTaskSdk/InputFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,37 @@ function Set-TaskVariable {
Write-SetVariable -Name $Name -Value $Value -Secret:$Secret
}

<#
.SYNOPSIS
Gets the value of an task feature and converts to a bool.
.PARAMETER $FeatureName
Name of the feature to get.
.NOTES
This method is only for internal Microsoft development. Do not use it for external tasks.
#>
function Get-PipelineFeature {
[CmdletBinding(DefaultParameterSetName = 'Require')]
param(
[Parameter(Mandatory = $true)]
[string]$FeatureName
)

$featureValue = Get-TaskVariable -Name "DistributedTask.Tasks.$FeatureName"

if (!$featureValue) {
Write-Debug "Feature '$FeatureName' is not set. Defaulting to 'false'"
return $false
}

$boolValue = $featureValue.ToLowerInvariant() -eq 'true'

Write-Debug "Feature '$FeatureName' = '$featureValue'. Processed as '$boolValue'"

return $boolValue
}

########################################
# Private functions.
########################################
Expand Down
1 change: 1 addition & 0 deletions powershell/VstsTaskSdk/VstsTaskSdk.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Export-ModuleMember -Function @(
'Get-TaskVariable'
'Get-TaskVariableInfo'
'Set-TaskVariable'
'Get-PipelineFeature'
# Legacy find functions.
'Find-Files'
# Localization functions.
Expand Down
2 changes: 1 addition & 1 deletion powershell/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 powershell/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.18.2",
"version": "0.19.0",
"private": true,
"scripts": {
"build": "node make.js build",
Expand Down

0 comments on commit 718a306

Please sign in to comment.