Skip to content

Commit c455196

Browse files
authored
Upgrade testfx and build script invocation (#28474)
1 parent 0dae713 commit c455196

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

tools/BuildScripts/BuildModules.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ else {
175175
Write-Output "Modules are added to test sln file"
176176
}
177177

178-
$buildCmdResult = "dotnet $BuildAction $Buildsln -c $Configuration -fl '/flp1:logFile=$LogFile;verbosity=quiet'"
178+
$buildCmdArgs = @("$BuildAction", "$Buildsln", "-c", "$Configuration", "-fl", "/flp1:logFile=$LogFile;verbosity=quiet")
179179
If ($GenerateDocumentationFile -eq "false") {
180-
$buildCmdResult += " -p:GenerateDocumentationFile=false"
180+
$buildCmdArgs += "-p:GenerateDocumentationFile=false"
181181
}
182182
if ($EnableTestCoverage -eq "true") {
183-
$buildCmdResult += " -p:TestCoverage=TESTCOVERAGE"
183+
$buildCmdArgs += "-p:TestCoverage=TESTCOVERAGE"
184184
}
185-
Invoke-Expression -Command $buildCmdResult
185+
# Use argument splatting to prevent injection
186+
& dotnet @buildCmdArgs
186187

187188
$versionControllerCsprojPath = Join-Path $toolDirectory 'VersionController' 'VersionController.Netcore.csproj'
188189
dotnet build $versionControllerCsprojPath -c $Configuration

tools/Modules/TestFx-Tasks.psm1

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ function New-TestFxServicePrincipal {
181181
}
182182
}
183183

184-
$sp = Invoke-TestFxCommand -Command "New-AzADServicePrincipal -DisplayName $ServicePrincipalDisplayName"
184+
$sp = Invoke-TestFxCommand -ScriptBlock {
185+
param($DisplayName)
186+
New-AzADServicePrincipal -DisplayName $DisplayName
187+
} -Parameters @{ DisplayName = $ServicePrincipalDisplayName }
185188
Start-Sleep -Seconds 10
186189
Set-TestFxServicePrincipalPermission -SubscriptionId $SubscriptionId -ServicePrincipalObjectId $sp.Id
187190

@@ -204,7 +207,14 @@ function Set-TestFxServicePrincipalPermission {
204207
try {
205208
$spRoleAssg = Get-AzRoleAssignment -ObjectId $ServicePrincipalObjectId -Scope $scope -RoleDefinitionName $roleName -ErrorAction Stop
206209
if ($null -eq $spRoleAssg) {
207-
Invoke-TestFxCommand -Command "New-AzRoleAssignment -ObjectId $ServicePrincipalObjectId -RoleDefinitionName $roleName -Scope $scope | Out-Null"
210+
Invoke-TestFxCommand -ScriptBlock {
211+
param($ObjectId, $RoleName, $Scope)
212+
New-AzRoleAssignment -ObjectId $ObjectId -RoleDefinitionName $RoleName -Scope $Scope | Out-Null
213+
} -Parameters @{
214+
ObjectId = $ServicePrincipalObjectId
215+
RoleName = $roleName
216+
Scope = $scope
217+
}
208218
}
209219
}
210220
catch {
@@ -264,34 +274,46 @@ function Get-TestFxEnvironment {
264274
function Invoke-TestFxCommand {
265275
[CmdletBinding()]
266276
param (
267-
[Parameter(Mandatory, ValueFromPipeline)]
277+
[Parameter(Mandatory)]
268278
[ValidateNotNullOrEmpty()]
269-
[string] $Command
279+
[scriptblock] $ScriptBlock,
280+
281+
[Parameter()]
282+
[hashtable] $Parameters = @{},
283+
284+
[Parameter()]
285+
[int] $MaxRetries = 3,
286+
287+
[Parameter()]
288+
[int] $RetryDelaySeconds = 5
270289
)
271290

272291
$cmdRetryCount = 0
273292

274293
do {
275294
try {
276-
Write-Verbose "Start to execute the command `"$Command`"."
277-
$cmdResult = Invoke-Expression -Command $Command -ErrorAction Stop
278-
Write-Verbose "Successfully executed the command `"$Command`"."
295+
Write-Verbose "Start to execute the command."
296+
297+
$cmdResult = & $ScriptBlock @Parameters -ErrorAction Stop
298+
299+
Write-Verbose "Successfully executed the command."
300+
279301
$cmdResult
280302
break
281303
}
282304
catch {
283305
$cmdErrorMessage = $_.Exception.Message
284-
if ($cmdRetryCount -le 3) {
285-
Write-Warning "Error occurred when executing the command `"$Command`" with error message `"$cmdErrorMessage`"."
286-
Write-Warning "Will retry automatically in 5 seconds."
306+
if ($cmdRetryCount -le $MaxRetries) {
307+
Write-Warning "Error occurred when executing the command with error message `"$cmdErrorMessage`"."
308+
Write-Warning "Will retry automatically in $RetryDelaySeconds seconds."
287309
Write-Host
288310

289-
Start-Sleep -Seconds 5
311+
Start-Sleep -Seconds $RetryDelaySeconds
290312
$cmdRetryCount++
291-
Write-Warning "Retrying #$cmdRetryCount to execute the command `"$Command`"."
313+
Write-Warning "Retrying #$cmdRetryCount to execute the command."
292314
}
293315
else {
294-
throw "Failed to execute the command `"$Command`" after retrying for 3 times with error message `"$cmdErrorMessage`"."
316+
throw "Failed to execute the command after retrying for $MaxRetries times with error message `"$cmdErrorMessage`"."
295317
}
296318
}
297319
}

tools/TestFx/Live/InvokeLiveTestCITask.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ param (
1313
)
1414

1515
if ($UseWindowsPowerShell) {
16-
$process = "powershell"
1716
Write-Host "##[section]Using Windows PowerShell"
17+
$executable = "powershell"
18+
$baseArguments = @("-NoLogo", "-NoProfile", "-NonInteractive")
1819
}
1920
else {
20-
$process = "dotnet tool run pwsh"
2121
Write-Host "##[section]Using PowerShell"
2222
dotnet tool run pwsh -NoLogo -NoProfile -NonInteractive -Version
23+
$executable = "dotnet"
24+
$baseArguments = @("tool", "run", "pwsh", "-NoLogo", "-NoProfile", "-NonInteractive")
2325
}
2426

2527
switch ($PSCmdlet.ParameterSetName) {
2628
"ByScriptFile" {
27-
Invoke-Expression "$process -NoLogo -NoProfile -NonInteractive -File $ScriptFile"
29+
$arguments = $baseArguments + @("-Command", "& $ScriptFile")
30+
& $executable @arguments
2831
}
2932
"ByScriptBlock" {
30-
Invoke-Expression "$process -NoLogo -NoProfile -NonInteractive -Command $ScriptBlock"
33+
$arguments = $baseArguments + @("-Command", $ScriptBlock)
34+
& $executable @arguments
3135
}
3236
}

tools/TestFx/Live/LiveTestUtility.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ function Invoke-LiveTestCommand {
222222

223223
do {
224224
try {
225-
$expandedCommand = $ExecutionContext.InvokeCommand.ExpandString($Command)
226-
Write-Host "##[section]Start executing the command `"$expandedCommand`"."
227-
Write-Host "##[command]The command `"$expandedCommand`" is running."
225+
$displayCommand = $Command.ToString()
226+
Write-Host "##[section]Start executing the command `"$displayCommand`"."
227+
Write-Host "##[command]The command `"$displayCommand`" is running."
228228

229229
$cmdResult = $Command.InvokeWithContext($null, [psvariable]::new("ErrorActionPreference", "Stop"))
230230

231-
Write-Host "##[section]Finish executing the command `"$expandedCommand`"."
231+
Write-Host "##[section]Finish executing the command `"$displayCommand`"."
232232

233233
$cmdResult
234234
break

0 commit comments

Comments
 (0)