Skip to content

Commit 994d0af

Browse files
author
devizer
committed
Module: Brush up tests, 4th
1 parent 7cbcf56 commit 994d0af

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

Install-SqlServer-Version-Management.ps1

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Param(
44
)
55

66
$ModuleName = 'SqlServer-Version-Management';
7-
$ModuleVersion = '2.1.165';
7+
$ModuleVersion = '2.1.166';
88
$ModuleFiles = @(
99
@{
1010
FileName = 'SqlServer-Version-Management\SqlServer-Version-Management.psd1';
@@ -15,7 +15,7 @@ $ModuleFiles = @(
1515
" ModuleToProcess = @('SqlServer-Version-Management.psm1')",
1616
"",
1717
" # Version below is automatically incremented on build",
18-
" ModuleVersion = `"2.1.165`"",
18+
" ModuleVersion = `"2.1.166`"",
1919
"",
2020
" GUID = 'dd03b53d-575a-4056-ae08-e6dfea3384ea'",
2121
"",
@@ -3874,11 +3874,13 @@ $ModuleFiles = @(
38743874
" `$totalErrors=0;",
38753875
" `$ret = @(); # errors",
38763876
" `$successNames = @()",
3877+
" `$successDetails = @()",
38773878
" `$failDetails = @()",
38783879
" `$quot1=[char]171; `$quot2=[char]187; ",
38793880
" `$bullet=[char]8226; `$arrow=[char]8594;",
38803881
" foreach(`$inputItem in `$inputCopy) {",
38813882
" `$theItemTitle = If (`$null -eq `$itemTitle) { `"`$inputItem`" } Else { ForEach-Object -InputObject `$inputItem -Process `$itemTitle | Select -First 1 };",
3883+
" `$itemStartAt = [System.Diagnostics.Stopwatch]::StartNew()",
38823884
" `$index++;",
38833885
" `$textCounter = `"`$index of `$(`$inputCopy.Length)`"",
38843886
" Write-Host `"`$([Environment]::NewLine)STARTING `$(`$textCounter): `$(`$actionTitle) for `$(`$quot1)`$(`$theItemTitle)`$(`$quot2)`" -ForeGroundColor Yellow",
@@ -3893,6 +3895,7 @@ $ModuleFiles = @(
38933895
" }",
38943896
" `$isOk = `$true;",
38953897
" `$successNames += `$theItemTitle",
3898+
" `$successDetails += [PSCustomObject] @{ Title = `$theItemTitle; Milliseconds = (`$itemStartAt.ElapsedMilliseconds)};",
38963899
" }",
38973900
" catch {",
38983901
" `$itemError = `$_.Exception.Message;",
@@ -3905,13 +3908,13 @@ $ModuleFiles = @(
39053908
" Write-Host `"Error On: `$inputItem`"",
39063909
" `$isOk = `$false;",
39073910
" `$totalErrors++;",
3908-
" `$failDetails += [PSCustomObject] @{ Title = `$theItemTitle; Error = `$itemError};",
3911+
" `$failDetails += [PSCustomObject] @{ Title = `$theItemTitle; Error = `$itemError; Milliseconds = (`$itemStartAt.ElapsedMilliseconds)};",
39093912
" }",
39103913
"",
39113914
" `$eta = (`$startAt.Elapsed.TotalSeconds / `$index * `$inputCopy.Length) - `$startAt.Elapsed.TotalSeconds;",
39123915
" `$color = If (`$isOk) { `"Green`" } else { `"Red`" }",
39133916
" `$status = If (`$isOk) { `"Success`" } else { `"Fail`" }",
3914-
" Write-Host `"`$(`$status): `$textCounter `$(`$actionTitle) for `$(`$quot1)`$(`$theItemTitle)`$(`$quot2) (ETA: `$(`"{0:n1}`" -f `$eta)s)`" -ForeGroundColor `$color",
3917+
" Write-Host `"`$(`$status): `$textCounter `$(`$actionTitle) for `$(`$quot1)`$(`$theItemTitle)`$(`$quot2) (ETA: `$(`"{0:n1}`" -f `$eta)s, took `$(Format-Milliseconds `$itemStartAt.ElapsedMilliseconds))`" -ForeGroundColor `$color",
39153918
" if (`$itemError) { Write-Host `"Error Message: `$itemError`" -ForeGroundColor Red }",
39163919
" # Write-Host `"ETA: `$(`"{0:n1}`" -f `$eta)s`"",
39173920
" `$host.ui.RawUI.WindowTitle = `"ETA: `$(`"{0:n1}`" -f `$eta)s. `$textCounter. `$(`$quot1)`$(`$theItemTitle)`$(`$quot2)`"",
@@ -3923,24 +3926,34 @@ $ModuleFiles = @(
39233926
" Write-Host `"Total Finished: `$(`$inputCopy.Length); Failed: `$totalErrors; Succeeded: `$(`$inputCopy.Length - `$totalErrors); Duration: `$([Math]::Round(`$startAt.Elapsed.TotalSeconds, 1))s`"",
39243927
" if (`$successNames.Length -gt 0) {",
39253928
" Write-Host `"`$actionTitle Success: `$(`$successNames.Length)`"",
3926-
" foreach(`$n in `$successNames) { Write-Host `" `$bullet `$(`$quot1)`$n`$(`$quot2)`" }",
3929+
" foreach(`$n in `$successDetails) { Write-Host `" `$bullet `$(`$quot1)`$(`$n.Title)`$(`$quot2) (`$(Format-Milliseconds `$n.Milliseconds))`" }",
39273930
" } Else {",
39283931
" Write-Host `"`$actionTitle Success: NONE`"",
39293932
" }",
39303933
" if (`$failDetails.Length -gt 0) {",
3931-
" Write-Host `"`$actionTitle Fail: `$(`$failDetails.Length)`"",
3932-
" foreach(`$n in `$failDetails) { Write-Host `" `$bullet `$(`$quot1)`$(`$n.Title)`$(`$quot2) `$arrow `$(`$n.Error)`" }",
3934+
" Write-Host `"`$actionTitle Fail: `$(`$failDetails.Length)`" -ForegroundColor Red",
3935+
" foreach(`$n in `$failDetails) { Write-Host `" `$bullet `$(`$quot1)`$(`$n.Title)`$(`$quot2) (`$(Format-Milliseconds `$n.Milliseconds)) `$arrow `$(`$n.Error)`" -ForegroundColor Red }",
39333936
" } Else {",
39343937
" Write-Host `"`$actionTitle Fail: NONE`"",
39353938
" }",
39363939
"",
39373940
" `$ret",
39383941
"}",
39393942
"",
3943+
"function Format-Milliseconds([long] `$milliseconds) {",
3944+
" `$date = ((new-object System.DateTime) + [System.Timespan]::FromMilliseconds(`$milliseconds));",
3945+
" if (`$milliseconds -le 9000) { return `"{0:0.0}s`" -f ([float] `$milliseconds / [float]1000) }",
3946+
" elseif (`$milliseconds -le (600000-1)) { return `"{0:HH:mm:ss}`" -f `$date }",
3947+
" else { return `"{0:HH:mm:ss}`" -f `$date }",
3948+
"}",
3949+
"",
3950+
"",
3951+
"",
39403952
"function Test-Try-Action-ForEach() {",
3953+
" `$random = new-object System.Random(1); ",
39413954
" `$items = 1..7 | % { [PSCustomObject] @{ Key = `"Category `$_`" } }",
39423955
" `$itemTitle = { `"`$(`$_.Key)`" }",
3943-
" `$action = { Sleep 0.2; If (`$_.Key -match `"3`") { throw `"`$(`$_.Key) is not supported`" } }",
3956+
" `$action = { Sleep -Milliseconds (200+150*`$random.Next(2,5)); If (`$_.Key -match `"3`") { throw `"`$(`$_.Key) is not supported`" } }",
39443957
" `$errors = @(`$items | Try-Action-ForEach -ActionTitle `"Sleep`" -Action `$action -ItemTitle `$itemTitle)",
39453958
" Write-Host `"```$errors.Length = `$(`$errors.Count)`"",
39463959
" Write-Host `$errors",

SqlServer-Version-Management.ps1

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,11 +3775,13 @@ function Try-Action-ForEach([string] $actionTitle, [ScriptBlock] $action, [Scrip
37753775
$totalErrors=0;
37763776
$ret = @(); # errors
37773777
$successNames = @()
3778+
$successDetails = @()
37783779
$failDetails = @()
37793780
$quot1=[char]171; $quot2=[char]187;
37803781
$bullet=[char]8226; $arrow=[char]8594;
37813782
foreach($inputItem in $inputCopy) {
37823783
$theItemTitle = If ($null -eq $itemTitle) { "$inputItem" } Else { ForEach-Object -InputObject $inputItem -Process $itemTitle | Select -First 1 };
3784+
$itemStartAt = [System.Diagnostics.Stopwatch]::StartNew()
37833785
$index++;
37843786
$textCounter = "$index of $($inputCopy.Length)"
37853787
Write-Host "$([Environment]::NewLine)STARTING $($textCounter): $($actionTitle) for $($quot1)$($theItemTitle)$($quot2)" -ForeGroundColor Yellow
@@ -3794,6 +3796,7 @@ function Try-Action-ForEach([string] $actionTitle, [ScriptBlock] $action, [Scrip
37943796
}
37953797
$isOk = $true;
37963798
$successNames += $theItemTitle
3799+
$successDetails += [PSCustomObject] @{ Title = $theItemTitle; Milliseconds = ($itemStartAt.ElapsedMilliseconds)};
37973800
}
37983801
catch {
37993802
$itemError = $_.Exception.Message;
@@ -3806,13 +3809,13 @@ function Try-Action-ForEach([string] $actionTitle, [ScriptBlock] $action, [Scrip
38063809
Write-Host "Error On: $inputItem"
38073810
$isOk = $false;
38083811
$totalErrors++;
3809-
$failDetails += [PSCustomObject] @{ Title = $theItemTitle; Error = $itemError};
3812+
$failDetails += [PSCustomObject] @{ Title = $theItemTitle; Error = $itemError; Milliseconds = ($itemStartAt.ElapsedMilliseconds)};
38103813
}
38113814

38123815
$eta = ($startAt.Elapsed.TotalSeconds / $index * $inputCopy.Length) - $startAt.Elapsed.TotalSeconds;
38133816
$color = If ($isOk) { "Green" } else { "Red" }
38143817
$status = If ($isOk) { "Success" } else { "Fail" }
3815-
Write-Host "$($status): $textCounter $($actionTitle) for $($quot1)$($theItemTitle)$($quot2) (ETA: $("{0:n1}" -f $eta)s)" -ForeGroundColor $color
3818+
Write-Host "$($status): $textCounter $($actionTitle) for $($quot1)$($theItemTitle)$($quot2) (ETA: $("{0:n1}" -f $eta)s, took $(Format-Milliseconds $itemStartAt.ElapsedMilliseconds))" -ForeGroundColor $color
38163819
if ($itemError) { Write-Host "Error Message: $itemError" -ForeGroundColor Red }
38173820
# Write-Host "ETA: $("{0:n1}" -f $eta)s"
38183821
$host.ui.RawUI.WindowTitle = "ETA: $("{0:n1}" -f $eta)s. $textCounter. $($quot1)$($theItemTitle)$($quot2)"
@@ -3824,24 +3827,34 @@ function Try-Action-ForEach([string] $actionTitle, [ScriptBlock] $action, [Scrip
38243827
Write-Host "Total Finished: $($inputCopy.Length); Failed: $totalErrors; Succeeded: $($inputCopy.Length - $totalErrors); Duration: $([Math]::Round($startAt.Elapsed.TotalSeconds, 1))s"
38253828
if ($successNames.Length -gt 0) {
38263829
Write-Host "$actionTitle Success: $($successNames.Length)"
3827-
foreach($n in $successNames) { Write-Host " $bullet $($quot1)$n$($quot2)" }
3830+
foreach($n in $successDetails) { Write-Host " $bullet $($quot1)$($n.Title)$($quot2) ($(Format-Milliseconds $n.Milliseconds))" }
38283831
} Else {
38293832
Write-Host "$actionTitle Success: NONE"
38303833
}
38313834
if ($failDetails.Length -gt 0) {
3832-
Write-Host "$actionTitle Fail: $($failDetails.Length)"
3833-
foreach($n in $failDetails) { Write-Host " $bullet $($quot1)$($n.Title)$($quot2) $arrow $($n.Error)" }
3835+
Write-Host "$actionTitle Fail: $($failDetails.Length)" -ForegroundColor Red
3836+
foreach($n in $failDetails) { Write-Host " $bullet $($quot1)$($n.Title)$($quot2) ($(Format-Milliseconds $n.Milliseconds)) $arrow $($n.Error)" -ForegroundColor Red }
38343837
} Else {
38353838
Write-Host "$actionTitle Fail: NONE"
38363839
}
38373840

38383841
$ret
38393842
}
38403843

3844+
function Format-Milliseconds([long] $milliseconds) {
3845+
$date = ((new-object System.DateTime) + [System.Timespan]::FromMilliseconds($milliseconds));
3846+
if ($milliseconds -le 9000) { return "{0:0.0}s" -f ([float] $milliseconds / [float]1000) }
3847+
elseif ($milliseconds -le (600000-1)) { return "{0:HH:mm:ss}" -f $date }
3848+
else { return "{0:HH:mm:ss}" -f $date }
3849+
}
3850+
3851+
3852+
38413853
function Test-Try-Action-ForEach() {
3854+
$random = new-object System.Random(1);
38423855
$items = 1..7 | % { [PSCustomObject] @{ Key = "Category $_" } }
38433856
$itemTitle = { "$($_.Key)" }
3844-
$action = { Sleep 0.2; If ($_.Key -match "3") { throw "$($_.Key) is not supported" } }
3857+
$action = { Sleep -Milliseconds (200+150*$random.Next(2,5)); If ($_.Key -match "3") { throw "$($_.Key) is not supported" } }
38453858
$errors = @($items | Try-Action-ForEach -ActionTitle "Sleep" -Action $action -ItemTitle $itemTitle)
38463859
Write-Host "`$errors.Length = $($errors.Count)"
38473860
Write-Host $errors

0 commit comments

Comments
 (0)