diff --git a/.aider.conf.yml b/.aider.conf.yml index 8f11632205..7fee50b033 100644 --- a/.aider.conf.yml +++ b/.aider.conf.yml @@ -101,7 +101,7 @@ git: false #code-theme: "monokai" ## edit the whole file -edit-format: whole +#edit-format: diff ## yesss yes-always: true diff --git a/.aider/aider.psm1 b/.aider/aider.psm1 index 8334e0de5b..834bacdb13 100644 --- a/.aider/aider.psm1 +++ b/.aider/aider.psm1 @@ -184,10 +184,12 @@ function Update-PesterTest { if ($PSCmdlet.ShouldProcess($filename, "Running tests on $filename")) { if ($script:xplat -contains $cmdName) { Write-Warning "Running integration and unit tests for $filename" - aider --test --test-cmd "/workspace/tests/Configs/aider.test.ps1 -TestIntegration -ScriptAnalyzer $filename" + #aider --test --test-cmd "/workspace/tests/Configs/aider.test.ps1 -TestIntegration -ScriptAnalyzer $filename" + Test-Command -Path $filename -TestIntegration -ScriptAnalyzer } else { Write-Warning "Running unit tests for $filename" - aider --test --test-cmd "/workspace/tests/Configs/aider.test.ps1 $filename" + #aider --test --test-cmd "/workspace/tests/Configs/aider.test.ps1 $filename" + Test-Command -Path $filename -ScriptAnalyzer } } } @@ -195,7 +197,49 @@ function Update-PesterTest { } } +function Test-Command { + [CmdletBinding()] + param ( + [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] + [Alias('FullName')] + [string[]]$Path, + [ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')] + [string]$Show = "Normal", + [switch]$PassThru, + [switch]$TestIntegration, + [switch]$Coverage, + [switch]$DependencyCoverage, + [switch]$ScriptAnalyzer, + [switch]$NoReimport + ) + process { + $cmdArgs = @() + # Build command line arguments + if ($PSBoundParameters.ContainsKey('Path')) { + $cmdArgs += $Path + } + if ($PSBoundParameters.ContainsKey('Show')) { + $cmdArgs += "-Show" + $cmdArgs += $Show + } + if ($PassThru) { $cmdArgs += "-PassThru" } + if ($TestIntegration) { $cmdArgs += "-TestIntegration" } + if ($Coverage) { $cmdArgs += "-Coverage" } + if ($DependencyCoverage) { $cmdArgs += "-DependencyCoverage" } + if ($ScriptAnalyzer) { $cmdArgs += "-ScriptAnalyzer" } + if ($NoReimport) { $cmdArgs += "-NoReimport" } + + # Convert array to space-separated string + $cmdString = $cmdArgs -join ' ' + + Write-Warning "Running tests with command: $cmdString" + # Call aider with the constructed command string + # add a file!! + # it stops apparently, do while or 10 times + aider --test --test-cmd "/workspace/tests/Configs/aider.test.ps1 $cmdString" --edit-format diff --file $Path + } +} function Repair-Error { <# @@ -679,7 +723,7 @@ function Invoke-Aider { if ($VerbosePreference -eq 'Continue') { Write-Verbose "Executing: aider $($arguments -join ' ')" } - $arguments | write-warning + aider @arguments } } diff --git a/private/testing/Invoke-ManualPester.ps1 b/private/testing/Invoke-ManualPester.ps1 index 8ab3eae661..3dbdc0a8f5 100644 --- a/private/testing/Invoke-ManualPester.ps1 +++ b/private/testing/Invoke-ManualPester.ps1 @@ -133,7 +133,7 @@ function Invoke-ManualPester { WarningAction = "SilentlyContinue" Global = $true } - Import-Module @splat + Import-Module @splat *> $null } function Get-CoverageIndications($Path, $ModuleBase) { @@ -166,7 +166,7 @@ function Invoke-ManualPester { # exclude always used functions ?! if ($f -in ('Connect-DbaInstance', 'Select-DefaultView', 'Stop-Function', 'Write-Message')) { continue } # can I find a correspondence to a physical file (again, on the convenience of having Get-DbaFoo.ps1 actually defining Get-DbaFoo)? - $res = $allfiles | Where-Object { $_.Name.Replace('.ps1', '') -eq $f } + $res = $allfiles | Where-Object { $PSItem.Name.Replace('.ps1', '') -eq $f } if ($res.count -gt 0) { $testpaths += $res.FullName } @@ -189,53 +189,75 @@ function Invoke-ManualPester { } } - $invokeFormatterVersion = (Get-Command Invoke-Formatter -ErrorAction SilentlyContinue).Version - $HasScriptAnalyzer = $null -ne $invokeFormatterVersion - $MinimumPesterVersion = [Version] '4.0.0.0' # Because this is when -Show was introduced - $MaximumPesterVersion = [Version] '6.0.0.0' # Because we have either pester4 or pester5 tests - $PesterVersion = (Get-Command Invoke-Pester -ErrorAction SilentlyContinue).Version - $HasPester = $null -ne $PesterVersion - $ScriptAnalyzerCorrectVersion = '1.18.2' - - if (!($HasScriptAnalyzer)) { - Write-Warning "Please install PSScriptAnalyzer" - Write-Warning " Install-Module -Name PSScriptAnalyzer -RequiredVersion '$ScriptAnalyzerCorrectVersion'" - Write-Warning " or go to https://github.com/PowerShell/PSScriptAnalyzer" - } else { - if ($invokeFormatterVersion -ne $ScriptAnalyzerCorrectVersion) { - Remove-Module PSScriptAnalyzer - try { - Import-Module PSScriptAnalyzer -RequiredVersion $ScriptAnalyzerCorrectVersion -ErrorAction Stop - } catch { - Write-Warning "Please install PSScriptAnalyzer $ScriptAnalyzerCorrectVersion" - Write-Warning " Install-Module -Name PSScriptAnalyzer -RequiredVersion '$ScriptAnalyzerCorrectVersion'" - } + # Version requirements + $ScriptAnalyzerRequiredVersion = '1.18.2' + $MinimumPesterVersion = [Version]'4.0.0.0' + $MaximumPesterVersion = [Version]'6.0.0.0' + $TargetPesterVersion = [Version]'5.3.3' + + # Get all required modules at once (single expensive operation) + $availableModules = @{} + $foundModules = Get-Module -Name Pester, PSScriptAnalyzer -ListAvailable + + foreach ($module in $foundModules) { + if (!$availableModules[$module.Name]) { + $availableModules[$module.Name] = @() } + $availableModules[$module.Name] += $module } - if (!($HasPester)) { - Write-Warning "Please install Pester" - Write-Warning " Install-Module -Name Pester -Force -SkipPublisherCheck" - Write-Warning " or go to https://github.com/pester/Pester" + # Sort versions once + $moduleNames = @($availableModules.Keys) + foreach ($name in $moduleNames) { + $availableModules[$name] = $availableModules[$name] | Sort-Object Version -Descending } - if ($PesterVersion -lt $MinimumPesterVersion) { - Write-Warning "Please update Pester to at least 3.4.5" - Write-Warning " Install-Module -Name Pester -MaximumVersion '4.10' -Force -SkipPublisherCheck" - Write-Warning " or go to https://github.com/pester/Pester" + + # If target Pester not found, use highest 5.x version + if (-not ($availableModules.Pester | Where-Object Version -eq $TargetPesterVersion)) { + $TargetPesterVersion = ($availableModules.Pester | Where-Object { $_.Version.Major -eq 5 } | Select-Object -First 1).Version } - if ($PesterVersion -gt $MaximumPesterVersion) { - Write-Warning "Please get Pester to the 5.* release" - Write-Warning " Install-Module -Name Pester -MaximumVersion '5.6.1' -Force -SkipPublisherCheck" - Write-Warning " or go to https://github.com/pester/Pester" + + # PSScriptAnalyzer checks + if (-not $availableModules.PSScriptAnalyzer) { + Write-Warning "PSScriptAnalyzer not found. Please install with:" + Write-Warning "Install-Module -Name PSScriptAnalyzer -RequiredVersion '$ScriptAnalyzerRequiredVersion'" + Write-Warning "or go to https://github.com/PowerShell/PSScriptAnalyzer" + return } - if (($HasPester -and $HasScriptAnalyzer -and ($PesterVersion -ge $MinimumPesterVersion) -and ($PesterVersion -lt $MaximumPesterVersion) -and ($invokeFormatterVersion -eq $ScriptAnalyzerCorrectVersion)) -eq $false) { - Write-Warning "Exiting..." - $stopProcess = $true + $importedAnalyzerModule = Get-Module -Name PSScriptAnalyzer + if (-not $importedAnalyzerModule -or $importedAnalyzerModule.Version.ToString() -ne $ScriptAnalyzerRequiredVersion) { + if ($importedAnalyzerModule) { Remove-Module PSScriptAnalyzer -Force } + try { + Import-Module PSScriptAnalyzer -RequiredVersion $ScriptAnalyzerRequiredVersion -ErrorAction Stop + } catch { + Write-Warning "Failed to import PSScriptAnalyzer $ScriptAnalyzerRequiredVersion" + Write-Warning "Please install correct version: Install-Module -Name PSScriptAnalyzer -RequiredVersion '$ScriptAnalyzerRequiredVersion'" + return + } } + # Pester checks + if (-not $availableModules.Pester) { + Write-Warning "Pester not found. Please install with:" + Write-Warning "Install-Module -Name Pester -Force -SkipPublisherCheck" + Write-Warning "or go to https://github.com/pester/Pester" + return + } + + $importedPesterModule = Get-Module -Name Pester + $highestVersion = $availableModules.Pester[0].Version + if ($highestVersion -lt $MinimumPesterVersion -or $highestVersion -gt $MaximumPesterVersion) { + Write-Warning "Pester version must be between $MinimumPesterVersion and $MaximumPesterVersion" + Write-Warning "Install-Module -Name Pester -RequiredVersion $TargetPesterVersion -Force -SkipPublisherCheck" + return + } + if ($importedPesterModule -and $importedPesterModule.Version -ne $TargetPesterVersion) { + Remove-Module Pester -Force + Import-Module Pester -RequiredVersion $TargetPesterVersion -Force + } } process { if ($stopProcess) { diff --git a/tests/Configs/aider.test.ps1 b/tests/Configs/aider.test.ps1 index d69a5753a5..2336461c08 100644 --- a/tests/Configs/aider.test.ps1 +++ b/tests/Configs/aider.test.ps1 @@ -1,2 +1,9 @@ Import-Module /workspace/dbatools.psm1 -Invoke-ManualPester -NoReimport $args \ No newline at end of file +$PSDefaultParameterValues['*:Passthru'] = $true +Invoke-ManualPester -NoReimport -ErrorAction Stop $args -OutVariable testResults #-edit-format diff # -editor-model + +if ($testResults.FailedCount -gt 0) { + exit 1 +} else { + exit 0 +} \ No newline at end of file