Skip to content

Commit 81cd54f

Browse files
Pester migration - First Batch (#9529)
1 parent 270c3af commit 81cd54f

9 files changed

+561
-242
lines changed

.aider/aider.psm1

Lines changed: 210 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function Update-PesterTest {
88
and converts them to use the newer Pester v5 parameter validation syntax. It skips files that have
99
already been converted or exceed the specified size limit.
1010
11+
.PARAMETER InputObject
12+
Array of objects that can be either file paths, FileInfo objects, or command objects (from Get-Command).
13+
If not specified, will process commands from the dbatools module.
14+
1115
.PARAMETER First
1216
Specifies the maximum number of commands to process. Defaults to 1000.
1317
@@ -20,7 +24,6 @@ function Update-PesterTest {
2024
2125
.PARAMETER CacheFilePath
2226
The path to the file containing cached conventions.
23-
Defaults to "/workspace/.aider/prompts/conventions.md".
2427
2528
.PARAMETER MaxFileSize
2629
The maximum size of test files to process, in bytes. Files larger than this will be skipped.
@@ -37,64 +40,134 @@ function Update-PesterTest {
3740
.EXAMPLE
3841
PS C:\> Update-PesterTest -First 10 -Skip 5
3942
Updates 10 test files starting from the 6th command, skipping the first 5.
43+
44+
.EXAMPLE
45+
PS C:\> "C:\tests\Get-DbaDatabase.Tests.ps1", "C:\tests\Get-DbaBackup.Tests.ps1" | Update-PesterTest
46+
Updates the specified test files to v5 format.
47+
48+
.EXAMPLE
49+
PS C:\> Get-Command -Module dbatools -Name "*Database*" | Update-PesterTest
50+
Updates test files for all commands in dbatools module that match "*Database*".
51+
52+
.EXAMPLE
53+
PS C:\> Get-ChildItem ./tests/Add-DbaRegServer.Tests.ps1 | Update-PesterTest -Verbose
54+
Updates the specific test file from a Get-ChildItem result.
4055
#>
4156
[CmdletBinding(SupportsShouldProcess)]
4257
param (
58+
[Parameter(ValueFromPipeline)]
59+
[PSObject[]]$InputObject,
4360
[int]$First = 1000,
4461
[int]$Skip = 0,
4562
[string[]]$PromptFilePath = "/workspace/.aider/prompts/template.md",
46-
[string[]]$CacheFilePath = "/workspace/.aider/prompts/conventions.md",
63+
[string[]]$CacheFilePath = @("/workspace/.aider/prompts/conventions.md","/workspace/private/testing/Get-TestConfig.ps1"),
4764
[int]$MaxFileSize = 8kb
4865
)
49-
# Full prompt path
50-
if (-not (Get-Module dbatools.library -ListAvailable)) {
51-
Write-Warning "dbatools.library not found, installing"
52-
Install-Module dbatools.library -Scope CurrentUser -Force
53-
}
54-
Import-Module /workspace/dbatools.psm1 -Force
55-
56-
$promptTemplate = Get-Content $PromptFilePath
57-
$commands = Get-Command -Module dbatools -Type Function, Cmdlet | Select-Object -First $First -Skip $Skip
58-
59-
$commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters
60-
61-
foreach ($command in $commands) {
62-
$cmdName = $command.Name
63-
$filename = "/workspace/tests/$cmdName.Tests.ps1"
64-
65-
if (-not (Test-Path $filename)) {
66-
Write-Warning "No tests found for $cmdName"
67-
Write-Warning "$filename not found"
68-
continue
66+
begin {
67+
# Full prompt path
68+
if (-not (Get-Module dbatools.library -ListAvailable)) {
69+
Write-Warning "dbatools.library not found, installing"
70+
Install-Module dbatools.library -Scope CurrentUser -Force
6971
}
72+
Import-Module /workspace/dbatools.psm1 -Force
7073

71-
# if it matches Should -HaveParameter then skip becuase it's been done
72-
if (Select-String -Path $filename -Pattern "Should -HaveParameter") {
73-
Write-Warning "Skipping $cmdName because it's already been converted to Pester v5"
74-
continue
75-
}
74+
$promptTemplate = Get-Content $PromptFilePath
75+
$commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters
76+
$commandsToProcess = @()
77+
}
7678

77-
# if file is larger than 8kb, skip
78-
if ((Get-Item $filename).Length -gt $MaxFileSize) {
79-
Write-Warning "Skipping $cmdName because it's too large"
80-
continue
79+
process {
80+
if ($InputObject) {
81+
foreach ($item in $InputObject) {
82+
Write-Verbose "Processing input object of type: $($item.GetType().FullName)"
83+
84+
if ($item -is [System.Management.Automation.CommandInfo]) {
85+
$commandsToProcess += $item
86+
} elseif ($item -is [System.IO.FileInfo]) {
87+
$path = $item.FullName
88+
Write-Verbose "Processing FileInfo path: $path"
89+
if (Test-Path $path) {
90+
$cmdName = [System.IO.Path]::GetFileNameWithoutExtension($path) -replace '\.Tests$', ''
91+
Write-Verbose "Extracted command name: $cmdName"
92+
$cmd = Get-Command -Name $cmdName -ErrorAction SilentlyContinue
93+
if ($cmd) {
94+
$commandsToProcess += $cmd
95+
} else {
96+
Write-Warning "Could not find command for test file: $path"
97+
}
98+
}
99+
} elseif ($item -is [string]) {
100+
Write-Verbose "Processing string path: $item"
101+
if (Test-Path $item) {
102+
$cmdName = [System.IO.Path]::GetFileNameWithoutExtension($item) -replace '\.Tests$', ''
103+
Write-Verbose "Extracted command name: $cmdName"
104+
$cmd = Get-Command -Name $cmdName -ErrorAction SilentlyContinue
105+
if ($cmd) {
106+
$commandsToProcess += $cmd
107+
} else {
108+
Write-Warning "Could not find command for test file: $item"
109+
}
110+
} else {
111+
Write-Warning "File not found: $item"
112+
}
113+
} else {
114+
Write-Warning "Unsupported input type: $($item.GetType().FullName)"
115+
}
116+
}
81117
}
118+
}
82119

83-
$parameters = $command.Parameters.Values | Where-Object Name -notin $commonParameters
84-
$cmdPrompt = $promptTemplate -replace "--CMDNAME--", $cmdName
85-
$cmdPrompt = $cmdPrompt -replace "--PARMZ--", ($parameters.Name -join "`n")
86-
$cmdprompt = $cmdPrompt -join "`n"
87-
88-
$aiderParams = @{
89-
Message = $cmdPrompt
90-
File = $filename
91-
YesAlways = $true
92-
Stream = $false
93-
CachePrompts = $true
94-
ReadFile = $CacheFilePath
120+
end {
121+
if (-not $commandsToProcess) {
122+
Write-Verbose "No input objects provided, getting commands from dbatools module"
123+
$commandsToProcess = Get-Command -Module dbatools -Type Function, Cmdlet | Select-Object -First $First -Skip $Skip
95124
}
96125

97-
Invoke-Aider @aiderParams
126+
foreach ($command in $commandsToProcess) {
127+
$cmdName = $command.Name
128+
$filename = "/workspace/tests/$cmdName.Tests.ps1"
129+
130+
Write-Verbose "Processing command: $cmdName"
131+
Write-Verbose "Test file path: $filename"
132+
133+
if (-not (Test-Path $filename)) {
134+
Write-Warning "No tests found for $cmdName"
135+
Write-Warning "$filename not found"
136+
continue
137+
}
138+
139+
<# Check if it's already been converted
140+
if (Select-String -Path $filename -Pattern "Should -HaveParameter") {
141+
Write-Warning "Skipping $cmdName because it's already been converted to Pester v5"
142+
continue
143+
}
144+
#>
145+
146+
# if file is larger than MaxFileSize, skip
147+
if ((Get-Item $filename).Length -gt $MaxFileSize) {
148+
Write-Warning "Skipping $cmdName because it's too large"
149+
continue
150+
}
151+
152+
$parameters = $command.Parameters.Values | Where-Object Name -notin $commonParameters
153+
$cmdPrompt = $promptTemplate -replace "--CMDNAME--", $cmdName
154+
$cmdPrompt = $cmdPrompt -replace "--PARMZ--", ($parameters.Name -join "`n")
155+
$cmdprompt = $cmdPrompt -join "`n"
156+
157+
if ($PSCmdlet.ShouldProcess($filename, "Update Pester test to v5 format and/or style")) {
158+
$aiderParams = @{
159+
Message = $cmdPrompt
160+
File = $filename
161+
YesAlways = $true
162+
Stream = $false
163+
CachePrompts = $true
164+
ReadFile = $CacheFilePath
165+
}
166+
167+
Write-Verbose "Invoking Aider to update test file"
168+
Invoke-Aider @aiderParams
169+
}
170+
}
98171
}
99172
}
100173

@@ -286,8 +359,11 @@ function Invoke-Aider {
286359
.PARAMETER NoPretty
287360
Disable pretty, colorized output.
288361
362+
.PARAMETER Stream
363+
Enable streaming responses. Cannot be used with -NoStream.
364+
289365
.PARAMETER NoStream
290-
Disable streaming responses.
366+
Disable streaming responses. Cannot be used with -Stream.
291367
292368
.PARAMETER YesAlways
293369
Automatically confirm all prompts.
@@ -352,6 +428,9 @@ function Invoke-Aider {
352428
[string]$Model,
353429
[string]$EditorModel,
354430
[switch]$NoPretty,
431+
[Parameter(ParameterSetName = 'Stream')]
432+
[switch]$Stream,
433+
[Parameter(ParameterSetName = 'NoStream')]
355434
[switch]$NoStream,
356435
[switch]$YesAlways,
357436
[switch]$CachePrompts,
@@ -392,7 +471,9 @@ function Invoke-Aider {
392471
$params += "--no-pretty"
393472
}
394473

395-
if ($NoStream) {
474+
if ($Stream) {
475+
# Stream is enabled, so don't add --no-stream
476+
} elseif ($NoStream) {
396477
$params += "--no-stream"
397478
}
398479

@@ -455,3 +536,86 @@ function Invoke-Aider {
455536

456537
aider @params
457538
}
539+
540+
function Repair-Error {
541+
<#
542+
.SYNOPSIS
543+
Repairs errors in dbatools Pester test files.
544+
545+
.DESCRIPTION
546+
Processes and repairs errors found in dbatools Pester test files. This function reads error
547+
information from a JSON file and attempts to fix the identified issues in the test files.
548+
549+
.PARAMETER First
550+
Specifies the maximum number of commands to process. Defaults to 1000.
551+
552+
.PARAMETER Skip
553+
Specifies the number of commands to skip before processing. Defaults to 0.
554+
555+
.PARAMETER PromptFilePath
556+
The path to the template file containing the prompt structure.
557+
Defaults to "/workspace/.aider/prompts/fix-errors.md".
558+
559+
.PARAMETER CacheFilePath
560+
The path to the file containing cached conventions.
561+
Defaults to "/workspace/.aider/prompts/conventions.md".
562+
563+
.PARAMETER ErrorFilePath
564+
The path to the JSON file containing error information.
565+
Defaults to "/workspace/.aider/prompts/errors.json".
566+
567+
.NOTES
568+
Tags: Testing, Pester, ErrorHandling
569+
Author: dbatools team
570+
571+
.EXAMPLE
572+
PS C:\> Repair-Error
573+
Processes and attempts to fix all errors found in the error file using default parameters.
574+
575+
.EXAMPLE
576+
PS C:\> Repair-Error -ErrorFilePath "custom-errors.json"
577+
Processes and repairs errors using a custom error file.
578+
#>
579+
[CmdletBinding()]
580+
param (
581+
[int]$First = 1000,
582+
[int]$Skip = 0,
583+
[string[]]$PromptFilePath = "/workspace/.aider/prompts/fix-errors.md",
584+
[string[]]$CacheFilePath = "/workspace/.aider/prompts/conventions.md",
585+
[string]$ErrorFilePath = "/workspace/.aider/prompts/errors.json"
586+
)
587+
588+
$promptTemplate = Get-Content $PromptFilePath
589+
$testerrors = Get-Content $ErrorFilePath | ConvertFrom-Json
590+
$commands = $testerrors | Select-Object -ExpandProperty Command -Unique | Sort-Object
591+
592+
foreach ($command in $commands) {
593+
$filename = "/workspace/tests/$command.Tests.ps1"
594+
Write-Output "Processing $command"
595+
596+
if (-not (Test-Path $filename)) {
597+
Write-Warning "No tests found for $command"
598+
Write-Warning "$filename not found"
599+
continue
600+
}
601+
602+
$cmdPrompt = $promptTemplate -replace "--CMDNAME--", $command
603+
604+
$testerr = $testerrors | Where-Object Command -eq $command
605+
foreach ($err in $testerr) {
606+
$cmdPrompt += "`n`n"
607+
$cmdPrompt += "Error: $($err.ErrorMessage)`n"
608+
$cmdPrompt += "Line: $($err.LineNumber)`n"
609+
}
610+
611+
$aiderParams = @{
612+
Message = $cmdPrompt
613+
File = $filename
614+
Stream = $false
615+
CachePrompts = $true
616+
ReadFile = $CacheFilePath
617+
}
618+
619+
Invoke-Aider @aiderParams
620+
}
621+
}

0 commit comments

Comments
 (0)