diff --git a/install.ps1 b/install.ps1 index f55c26a..964bb13 100644 --- a/install.ps1 +++ b/install.ps1 @@ -1,8 +1,8 @@ #requires -Version 5.1 [CmdletBinding()] param( - [ValidatePattern('^[a-z0-9][a-z0-9-]*$')][string]$Machine = $env:CONFIG_CENTRAL_MACHINE, - [ValidatePattern('^[A-Za-z0-9._/-]+$')][string]$Branch = $env:CONFIG_CENTRAL_BRANCH, + [string]$Machine = $env:CONFIG_CENTRAL_MACHINE, + [string]$Branch = $env:CONFIG_CENTRAL_BRANCH, [switch]$SkipApply ) @@ -20,7 +20,15 @@ if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { if ([string]::IsNullOrWhiteSpace($env:TEMP) -or [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { throw 'The interactive Windows user profile is unavailable.' } -if ([string]::IsNullOrWhiteSpace($Branch)) { $Branch = 'main' } +if ([string]::IsNullOrWhiteSpace($Branch)) { + $Branch = 'main' +} +elseif ($Branch -notmatch '^[A-Za-z0-9._/-]+$') { + throw "Invalid branch name '$Branch'." +} +if (-not [string]::IsNullOrWhiteSpace($Machine) -and $Machine -notmatch '^[a-z0-9][a-z0-9-]*$') { + throw "Invalid machine name '$Machine'." +} if ($env:CONFIG_CENTRAL_SKIP_APPLY -eq '1') { $SkipApply = $true } function Get-SavedMachine { diff --git a/tests/Installer.Tests.ps1 b/tests/Installer.Tests.ps1 index e2a05ef..859766e 100644 --- a/tests/Installer.Tests.ps1 +++ b/tests/Installer.Tests.ps1 @@ -39,4 +39,23 @@ Describe 'One-stop installer machine selection' { Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('example-desktop') | Should -Be 'example-desktop' } + + It 'allows unset environment defaults through parameter binding' { + $previousMachine = $env:CONFIG_CENTRAL_MACHINE + $previousBranch = $env:CONFIG_CENTRAL_BRANCH + try { + $env:CONFIG_CENTRAL_MACHINE = $null + $env:CONFIG_CENTRAL_BRANCH = $null + $parameterProbe = [scriptblock]::Create( + $installerAst.ParamBlock.Extent.Text + "`n@{ Machine = `$Machine; Branch = `$Branch }" + ) + $bound = & $parameterProbe + $bound.Machine | Should -BeNullOrEmpty + $bound.Branch | Should -BeNullOrEmpty + } + finally { + $env:CONFIG_CENTRAL_MACHINE = $previousMachine + $env:CONFIG_CENTRAL_BRANCH = $previousBranch + } + } }