Skip to content

Commit

Permalink
feat: get user confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
cuinixam authored and xxthunder committed Jul 2, 2024
1 parent 0dc0f7c commit ba278ac
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/utils.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,63 @@ Describe "Test-RunningInCIorTestEnvironment" {
Test-RunningInCIorTestEnvironment | Should -Be $false
}
}

Describe 'Get-UserConfirmation' {
Context 'When running in CI environment' {
BeforeEach {
$env:JENKINS_URL = "http://example.com"
}
AfterEach {
Remove-Item Env:JENKINS_URL
}
It 'Returns the default value' {
$result = Get-UserConfirmation -message "Test" -valueForCi $true
$result | Should -Be $true

$result = Get-UserConfirmation -message "Test" -valueForCi $false
$result | Should -Be $false

$result = Get-UserConfirmation -message "Test"
$result | Should -Be $false
}
}

Context 'When running interactively' {
BeforeEach {
Mock -CommandName Read-Host -MockWith {
return $script:MockReadHostResponse
}
}
It 'Returns true when user input is yes' {
$script:MockReadHostResponse = 'y'
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $false
$result | Should -Be $true
$script:MockReadHostResponse = 'Y'
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $false
$result | Should -Be $true
}
It 'Returns false when user input is no' {
$script:MockReadHostResponse = 'n'
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $true
$result | Should -Be $false
$script:MockReadHostResponse = 'N'
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $true
$result | Should -Be $false
}
It 'Returns default true when user input is empty' {
$script:MockReadHostResponse = ''
$result = Get-UserConfirmation -message "Test"
$result | Should -Be $true
}
It 'Returns true when user input is empty and default is true' {
$script:MockReadHostResponse = ''
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $true
$result | Should -Be $true
}
It 'Returns false when user input is empty and default is false' {
$script:MockReadHostResponse = ''
$result = Get-UserConfirmation -message "Test" -defaultValueForUser $false
$result | Should -Be $false
}
}
}
27 changes: 27 additions & 0 deletions utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,30 @@ function CloneOrPullGitRepo {
function Test-RunningInCIorTestEnvironment {
return [Boolean]($Env:JENKINS_URL -or $Env:PYTEST_CURRENT_TEST -or $Env:GITHUB_ACTIONS)
}

function Get-UserConfirmation {
param (
[Parameter(Mandatory = $true)]
[string]$message,
# Default value of the confirmation prompt
[Parameter(Mandatory = $false)]
[bool]$defaultValueForUser = $true,
# Value when running in CI or test environment
[Parameter(Mandatory = $false)]
[bool]$valueForCi = $false
)

if (Test-RunningInCIorTestEnvironment) {
return $valueForCi
} else {
$defaultText = if ($defaultValueForUser) { "[Y/n]" } else { "[y/N]" }
$userResponse = Read-Host "$message $defaultText"
if ($userResponse -eq '') {
return $defaultValueForUser
} elseif ($userResponse -match '^[Yy]') {
return $true
} else {
return $false
}
}
}

0 comments on commit ba278ac

Please sign in to comment.