From 35855c91fd8b72653c498a7be74a1b2498c0d007 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 13 May 2026 18:30:47 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20[Code=20Health]=20Refactor=20dup?= =?UTF-8?q?licated=20user=20prompt=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduced a `Confirm-Action` function to abstract away the repeated `Read-Host` and conditional check logic. This function accepts a prompt string and a scriptblock to execute if the user confirms. Replaced the three repeated blocks for `chkdsk`, `Microsoft Store`, and `Restart-Computer` with this new function, improving code maintainability. Adjusted Pester tests to mock the fully qualified path for chkdsk. Co-authored-by: murilogiatti <38364583+murilogiatti@users.noreply.github.com> --- CleanupAndUpdateEverything.Tests.ps1 | 4 ++-- CleanupAndUpdateEverything.ps1 | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CleanupAndUpdateEverything.Tests.ps1 b/CleanupAndUpdateEverything.Tests.ps1 index 9477787..157486a 100644 --- a/CleanupAndUpdateEverything.Tests.ps1 +++ b/CleanupAndUpdateEverything.Tests.ps1 @@ -90,7 +90,7 @@ Describe "CleanupAndUpdateEverything.ps1" { # Arrange Mock Test-Path { return $true } # Make it think reboot is pending Mock Read-Host { return "N" } # Make it answer 'N' to prompts - Mock chkdsk {} + Mock "$sysDir\chkdsk.exe" {} Mock Start-Process {} Mock Restart-Computer {} @@ -99,7 +99,7 @@ Describe "CleanupAndUpdateEverything.ps1" { # Assert Should -Invoke -CommandName Read-Host -Times 3 - Should -Invoke -CommandName chkdsk -Times 0 + Should -Invoke -CommandName "$sysDir\chkdsk.exe" -Times 0 Should -Invoke -CommandName Start-Process -Times 0 Should -Invoke -CommandName Restart-Computer -Times 0 } diff --git a/CleanupAndUpdateEverything.ps1 b/CleanupAndUpdateEverything.ps1 index 6c6e3c6..a16a74d 100644 --- a/CleanupAndUpdateEverything.ps1 +++ b/CleanupAndUpdateEverything.ps1 @@ -5,6 +5,17 @@ param ( Write-Host "Iniciando processo automatizado..." -ForegroundColor Green # --- FUNÇÕES --- +function Confirm-Action { + param( + [string]$Prompt, + [scriptblock]$Action + ) + $choice = Read-Host $Prompt + if ($choice -eq "S") { + & $Action + } +} + function Safe-Remove { param([string]$Path) Remove-Item -Path $Path -Recurse -Force -ErrorAction SilentlyContinue @@ -74,17 +85,17 @@ Write-Host "`n" + "="*60 -ForegroundColor Yellow Write-Host "MANUTENÇÃO CONCLUÍDA. REVISÃO DE AÇÕES PENDENTES:" -ForegroundColor Yellow Write-Host "="*60 -ForegroundColor Yellow -$checkDisk = Read-Host "Deseja agendar CHKDSK para o próximo boot? (S/N)" -if ($checkDisk -eq "S") { Write-Output y | & "$env:windir\System32\chkdsk.exe" C: /f /r } +Confirm-Action -Prompt "Deseja agendar CHKDSK para o próximo boot? (S/N)" -Action { Write-Output y | & "$env:windir\System32\chkdsk.exe" C: /f /r } + + +Confirm-Action -Prompt "Deseja abrir a Microsoft Store? (S/N)" -Action { Start-Process "ms-windows-store://downloadsandupdates" } -$openStore = Read-Host "Deseja abrir a Microsoft Store? (S/N)" -if ($openStore -eq "S") { Start-Process "ms-windows-store://downloadsandupdates" } $RebootPending = Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" if ($RebootPending) { Write-Host "`n[!] REBOOT NECESSÁRIO!" -ForegroundColor Red - $rebootChoice = Read-Host "Reiniciar agora? (S/N)" - if ($rebootChoice -eq "S") { Restart-Computer -Force } + Confirm-Action -Prompt "Reiniciar agora? (S/N)" -Action { Restart-Computer -Force } + } Write-Host "`nProcesso finalizado."