From edcf0be6d9bf15be1bf7b1c86965e39dbe222496 Mon Sep 17 00:00:00 2001 From: Rain Sallow <32407840+vexx32@users.noreply.github.com> Date: Mon, 5 Dec 2022 11:34:37 -0500 Subject: [PATCH] (#2872) Add end to end tests for [A] prompt action --- .../commands/choco-install.Tests.ps1 | 30 ++++++++++++++ .../1.0.0/packagewithscript.nuspec | 17 ++++++++ .../1.0.0/tools/chocolateyBeforeModify.ps1 | 1 + .../1.0.0/tools/chocolateyinstall.ps1 | 1 + .../1.0.0/tools/chocolateyuninstall.ps1 | 1 + .../common/Chocolatey/Invoke-Choco.ps1 | 39 +++++++++++++------ 6 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/packagewithscript.nuspec create mode 100644 tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyBeforeModify.ps1 create mode 100644 tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyinstall.ps1 create mode 100644 tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyuninstall.ps1 diff --git a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 index 7f47f481d3..f95ceeb1fd 100644 --- a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 @@ -1594,4 +1594,34 @@ Describe "choco install" -Tag Chocolatey, InstallCommand { $Output.Lines | Should -Contain "$($_ -replace '',$env:ChocolateyInstall)" -Because $Output.String } } + + + Context "Installing multiple packages at once without allowGlobalConfirmation" { + + BeforeAll { + Restore-ChocolateyInstallSnapshot + Disable-ChocolateyFeature -Name allowGlobalConfirmation + + $PackageUnderTest = "installpackage", "packagewithscript" + + $Output = "a`n"*2 | Invoke-Choco install @PackageUnderTest + } + + It "Installs successfully and exits with success (0)" { + $Output.ExitCode | Should -Be 0 + } + + It "Installed the packages to the lib directory" { + $PackageUnderTest | ForEach-Object { + "$env:ChocolateyInstall\lib\$_" | Should -Exist + } + } + + It "Ran both installation scripts after selecting [A] Yes to all at the first prompt" { + $promptLine = "Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint):" + $prompts = $Output.Lines | Where-Object { $_.Trim() -eq $promptLine } + + $prompts.Count | Should -Be 1 + } + } } diff --git a/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/packagewithscript.nuspec b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/packagewithscript.nuspec new file mode 100644 index 0000000000..9ff3b5fd10 --- /dev/null +++ b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/packagewithscript.nuspec @@ -0,0 +1,17 @@ + + + + packagewithscript + 1.0.0 + packagewithscript + __REPLACE_AUTHORS_OF_SOFTWARE__ + __REPLACE_YOUR_NAME__ + false + __REPLACE__ + __REPLACE__ + packagewithscript + + + + + diff --git a/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyBeforeModify.ps1 b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyBeforeModify.ps1 new file mode 100644 index 0000000000..af5bb142e8 --- /dev/null +++ b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyBeforeModify.ps1 @@ -0,0 +1 @@ +Write-Output "$env:PackageName $env:PackageVersion Before Modification" \ No newline at end of file diff --git a/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyinstall.ps1 b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyinstall.ps1 new file mode 100644 index 0000000000..b82e87da73 --- /dev/null +++ b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyinstall.ps1 @@ -0,0 +1 @@ +Write-Output "Installing $env:PackageName $env:PackageVersion" diff --git a/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyuninstall.ps1 b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyuninstall.ps1 new file mode 100644 index 0000000000..9ead91ffa3 --- /dev/null +++ b/tests/chocolatey-tests/commands/testpackages/packagewithscript/1.0.0/tools/chocolateyuninstall.ps1 @@ -0,0 +1 @@ +Write-Output "$env:PackageName $env:PackageVersion Uninstalled" \ No newline at end of file diff --git a/tests/helpers/common/Chocolatey/Invoke-Choco.ps1 b/tests/helpers/common/Chocolatey/Invoke-Choco.ps1 index 8390fd08af..79e136aef1 100644 --- a/tests/helpers/common/Chocolatey/Invoke-Choco.ps1 +++ b/tests/helpers/common/Chocolatey/Invoke-Choco.ps1 @@ -8,19 +8,34 @@ param( # The arguments to use when calling the Choco executable [Parameter(Position = 1, ValueFromRemainingArguments)] - [string[]]$Arguments - ) + [string[]]$Arguments, - $chocoPath = Get-ChocoPath + # Pipeline input to direct into choco.exe. Used mainly to "respond" to prompts + # in a noninteractive context. Provide input as a single string with line-feed + # characters ("`n") between input characters in that context. + [Parameter(ValueFromPipeline)] + [string]$PipelineInput + ) + begin { + $chocoPath = Get-ChocoPath $firstArgument, [string[]]$remainingArguments = $Arguments - $arguments = @($firstArgument; '--allow-unofficial'; $remainingArguments) - $output = & $chocoPath @arguments - [PSCustomObject]@{ - # We trim all the lines, so we do not take into account - # trimming the lines when asserting, and that extra whitespace - # is not considered in our assertions. - Lines = if ($output) { $output.Trim() } else { @() } - String = $output -join "`r`n" - ExitCode = $LastExitCode + $arguments = @($firstArgument; '--allow-unofficial'; $remainingArguments) + } + end { + $output = if ($PipelineInput) { + $PipelineInput | & $chocoPath @arguments + } + else { + & $chocoPath @arguments + } + + [PSCustomObject]@{ + # We trim all the lines, so we do not take into account + # trimming the lines when asserting, and that extra whitespace + # is not considered in our assertions. + Lines = if ($output) { $output.Trim() } else { @() } + String = $output -join "`r`n" + ExitCode = $LastExitCode + } } }