Update configs.Tests.ps1

This commit is contained in:
Chris Titus 2024-08-28 16:24:28 -05:00
parent e6dbc811f4
commit 26a2b3df9b

View File

@ -1,96 +1,94 @@
# Enable verbose output
$VerbosePreference = "Continue"
# Import Config Files # Import Config Files
$global:importedconfigs = @{} $global:importedConfigs = @{}
Get-ChildItem .\config | Where-Object {$_.Extension -eq ".json"} | ForEach-Object { Get-ChildItem .\config -Filter *.json | ForEach-Object {
try { try {
$global:importedconfigs[$psitem.BaseName] = Get-Content $psitem.FullName | ConvertFrom-Json $global:importedConfigs[$_.BaseName] = Get-Content $_.FullName | ConvertFrom-Json
Write-Verbose "Successfully imported config file: $($_.FullName)"
} catch { } catch {
Write-Warning "Failed to import config file: $($psitem.FullName). Error: $_" Write-Error "Failed to import config file: $($_.FullName). Error: $_"
} }
} }
Describe "Config Files Validation" {
#=========================================================================== $configTemplates = @{
# Tests - Application Installs applications = @("winget", "choco", "category", "content", "description", "link")
#=========================================================================== tweaks = @("registry", "service", "ScheduledTask")
$configurations = @(
@{
name = "applications"
config = $('{
"winget": "value",
"choco": "value",
"category": "value",
"content": "value",
"description": "value",
"link": "value"
}' | ConvertFrom-Json)
},
@{
name = "tweaks"
undo = $true
} }
)
foreach ($configuration in $configurations) { Context "Config File Structure" {
Describe "Config Files - $($configuration.name)" { It "Should import all config files without errors" {
$name = $configuration.name $global:importedConfigs | Should -Not -BeNullOrEmpty -Because "No config files were imported successfully"
$config = $configuration.config Write-Verbose "Imported configs: $($global:importedConfigs.Keys -join ', ')"
$undo = $configuration.undo }
Context "$name config file" { foreach ($configName in $configTemplates.Keys) {
It "Imports with no errors" { It "Should have the correct structure for $configName" {
$global:importedconfigs | Should -Not -BeNullOrEmpty -Because "The imported configs should not be null or empty" $global:importedConfigs.ContainsKey($configName) | Should -BeTrue -Because "Config file '$configName' is missing"
$global:importedconfigs.ContainsKey($name) | Should -BeTrue -Because "The configuration '$name' should exist in the imported configs" $config = $global:importedConfigs[$configName]
$global:importedconfigs[$name] | Should -Not -BeNullOrEmpty -Because "The configuration '$name' should not be null or empty" $config | Should -Not -BeNullOrEmpty -Because "Config file '$configName' is empty"
}
if ($config -and $global:importedconfigs -and $global:importedconfigs.ContainsKey($name)) { $properties = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
It "Imports should be the correct structure" { foreach ($prop in $properties) {
$applications = $global:importedconfigs[$name] | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name $itemProperties = $config.$prop | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
$template = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name $missingProperties = Compare-Object $configTemplates[$configName] $itemProperties | Where-Object { $_.SideIndicator -eq "<=" } | Select-Object -ExpandProperty InputObject
$result = New-Object System.Collections.Generic.List[System.Object] $missingProperties | Should -BeNullOrEmpty -Because "Item '$prop' in '$configName' config is missing properties: $($missingProperties -join ', ')"
Foreach ($application in $applications) { if ($missingProperties) {
$compare = $global:importedconfigs[$name].$application | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name Write-Verbose "Missing properties in $configName.$prop: $($missingProperties -join ', ')"
if ($(Compare-Object $compare $template)) {
$result.Add($application)
}
} }
$result | Where-Object { $_ -like "WPF*" } | Should -BeNullOrEmpty
} }
} }
}
}
if ($undo -and $global:importedconfigs -and $global:importedconfigs.ContainsKey($name)) { Context "Tweaks Configuration" {
It "Tweaks should contain original Value" { It "Should have original values for all tweaks" {
$tweaks = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name $tweaks = $global:importedConfigs.tweaks
$result = New-Object System.Collections.Generic.List[System.Object] $tweaks | Should -Not -BeNullOrEmpty -Because "Tweaks configuration is missing or empty"
foreach ($tweak in $tweaks) { $originals = @{
$Originals = @( registry = "OriginalValue"
@{ service = "OriginalType"
name = "registry" ScheduledTask = "OriginalState"
value = "OriginalValue" }
},
@{ foreach ($tweak in ($tweaks | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)) {
name = "service" foreach ($type in $originals.Keys) {
value = "OriginalType" $totalCount = ($tweaks.$tweak.$type).Count
}, $originalCount = ($tweaks.$tweak.$type.$($originals[$type]) | Where-Object { $_ }).Count
@{ $originalCount | Should -Be $totalCount -Because "Tweak '$tweak' of type '$type' is missing some original values"
name = "ScheduledTask" if ($originalCount -ne $totalCount) {
value = "OriginalState" Write-Verbose "Tweak '$tweak' of type '$type' has $originalCount original values out of $totalCount total values"
} }
) }
Foreach ($original in $Originals) { }
$TotalCount = ($global:importedconfigs.$name.$tweak.$($original.name)).count }
$OriginalCount = ($global:importedconfigs.$name.$tweak.$($original.name).$($original.value) | Where-Object {$_}).count }
if($TotalCount -ne $OriginalCount) {
$result.Add("$Tweak,$($original.name)") Context "Applications Configuration" {
} It "Should have all required fields for each application" {
} $apps = $global:importedConfigs.applications
$apps | Should -Not -BeNullOrEmpty -Because "Applications configuration is missing or empty"
foreach ($app in ($apps | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)) {
foreach ($field in $configTemplates.applications) {
$apps.$app.$field | Should -Not -BeNullOrEmpty -Because "Application '$app' is missing the '$field' field"
if (-not $apps.$app.$field) {
Write-Verbose "Application '$app' is missing the '$field' field"
} }
$result | Where-Object { $_ -like "WPF*" } | Should -BeNullOrEmpty
} }
} }
} }
} }
} }
# Summarize test results
$testResults = Invoke-Pester -PassThru
if ($testResults.FailedCount -gt 0) {
Write-Error "Tests failed. $($testResults.FailedCount) out of $($testResults.TotalCount) tests failed."
exit 1
} else {
Write-Output "All tests passed successfully!"
}