Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pester Tests to Support branch #1035

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/helpers/gui-helpers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Get-ChildItem -Path $PSScriptRoot\gui -Filter *.ps1 -Recurse | ForEach-Object { . $_.FullName }
17 changes: 17 additions & 0 deletions Tests/helpers/gui/Get-GuiFeature.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function Get-GuiFeature {
<#
.Synopsis
Helper function to call chocolateyguicli and return the feature information as a PSCustomObject.
#>
[CmdletBinding()]
param(
[string[]]$Feature = '*'
)

$featureList = (Invoke-GuiCli feature list -r).Lines |
ConvertFrom-Csv -Delimiter '|' -Header Name, State, Description |
Select-Object Name, @{Name = 'enabled'; Expression = { $_.State -eq 'Enabled' } }, Description
foreach ($ftr in $Feature) {
$featureList | Where-Object Name -Like $ftr
}
}
23 changes: 23 additions & 0 deletions Tests/helpers/gui/Invoke-GuiCli.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function Invoke-GuiCli {
<#
.Synopsis
Helper function to call chocolateyguicli with any number of specified arguments,
and return a hashtable with the output as well as the exit code.
#>
[CmdletBinding()]
param(
# The arguments to use when calling the chocolateyguicli executable
[Parameter(Position = 1, ValueFromRemainingArguments)]
[string[]]$Arguments
)

$output = & chocolateyguicli.exe @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
}
}
49 changes: 49 additions & 0 deletions Tests/pester-tests/chocolateyguicli.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Import-Module ../helpers/gui-helpers.psm1

Describe "chocolateyguicli" -Tag ChocolateyGuiCli {
BeforeDiscovery {
# Perhaps a better way is to pull these from the LiteDB similar to how we do features from the xml in CLI, but this will do for an initial setup.
$Features = Get-GuiFeature
}

Context "Basic CLI functionality" {
BeforeAll {
$Output = Invoke-GuiCli
}

It 'Should exit Success (0)' {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It 'Should output appropriate message' {
$Output.Lines | Should -Contain "Please run chocolateyguicli with 'chocolateyguicli -?' or 'chocolateyguicli <command> -?' for specific help on each command"
}
}

Context "Lists available features" {
BeforeAll {
$Output = Invoke-GuiCli feature list
}

It "Contains reference of the option (<_.Name>)" -ForEach $Features {
$Output.String | Should -Match $Name
}
}

Context "Toggles the feature (<_.Name>) successfully" -ForEach $Features {
BeforeAll {
if ($Enabled) {
$DisableOutput = Invoke-GuiCli feature disable --name $_.Name
$EnableOutput = Invoke-GuiCli feature enable --name $_.Name
} else {
$EnableOutput = Invoke-GuiCli feature enable --name $_.Name
$DisableOutput = Invoke-GuiCli feature disable --name $_.Name
}
}

It "Should exit success (0)" {
$EnableOutput.ExitCode | Should -Be 0 -Because $EnableOutput.String
$DisableOutput.ExitCode | Should -Be 0 -Because $DisableOutput.String
}
}
}
Loading