diff --git a/scripts/setup-tools.ps1 b/scripts/setup-tools.ps1 index f9ac67e..0df4227 100644 --- a/scripts/setup-tools.ps1 +++ b/scripts/setup-tools.ps1 @@ -1,21 +1,17 @@ -function Initialize-PowerShellEnvironment { - Write-Host "Initializing PowerShell environment..." -ForegroundColor Yellow - - # Add any environment setup logic here, such as importing modules, setting variables, etc. - # Example: Import-Module SomeModule +function Import-PowerShellYamlModule { Import-Module powershell-yaml -ErrorAction SilentlyContinue - - Write-Host "PowerShell environment initialized." -ForegroundColor Green } function Invoke-Main { Write-Host "Preparing environment..." -ForegroundColor Cyan Write-Host - Initialize-PowerShellEnvironment + Write-Host "Initializing PowerShell environment..." -ForegroundColor Yellow + Import-PowerShellYamlModule + Write-Host "PowerShell environment initialized." -ForegroundColor Green Write-Host Write-Host "Environment preparation complete!" -ForegroundColor Green } -Invoke-Main \ No newline at end of file +Invoke-Main diff --git a/tests/setup-tools.Tests.ps1 b/tests/setup-tools.Tests.ps1 new file mode 100644 index 0000000..f1e96a4 --- /dev/null +++ b/tests/setup-tools.Tests.ps1 @@ -0,0 +1,48 @@ +$setupToolsScript = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..\scripts\setup-tools.ps1')).Path +$expectedOutput = @( + 'Preparing environment...' + '' + 'Initializing PowerShell environment...' + 'PowerShell environment initialized.' + '' + 'Environment preparation complete!' +) -join "`n" + +Describe 'setup-tools.ps1' { + It 'imports powershell-yaml and preserves the user-facing output' { + $originalModulePath = $env:PSModulePath + try { + $moduleRoot = Join-Path $TestDrive 'available-modules' + $moduleDirectory = Join-Path $moduleRoot 'powershell-yaml' + $null = New-Item -ItemType Directory -Path $moduleDirectory + Set-Content -LiteralPath (Join-Path $moduleDirectory 'powershell-yaml.psm1') -Value '' + $env:PSModulePath = $moduleRoot + + $escapedScriptPath = $setupToolsScript.Replace("'", "''") + $command = "& '$escapedScriptPath'; if (-not (Get-Module -Name powershell-yaml)) { exit 42 }" + + $output = @(& pwsh -NoProfile -Command $command 6>&1 | ForEach-Object { $_.ToString() }) + + $LASTEXITCODE | Should Be 0 + ($output -join "`n") | Should Be $expectedOutput + } + finally { + $env:PSModulePath = $originalModulePath + } + } + + It 'continues successfully when powershell-yaml is unavailable' { + $originalModulePath = $env:PSModulePath + try { + $env:PSModulePath = Join-Path $TestDrive 'missing-modules' + + $output = @(& pwsh -NoProfile -File $setupToolsScript 6>&1 | ForEach-Object { $_.ToString() }) + + $LASTEXITCODE | Should Be 0 + ($output -join "`n") | Should Be $expectedOutput + } + finally { + $env:PSModulePath = $originalModulePath + } + } +}