mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-16 09:50:36 -06:00
Update configs.Tests.ps1
This commit is contained in:
parent
e6dbc811f4
commit
26a2b3df9b
@ -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) {
|
|
||||||
Describe "Config Files - $($configuration.name)" {
|
|
||||||
$name = $configuration.name
|
|
||||||
$config = $configuration.config
|
|
||||||
$undo = $configuration.undo
|
|
||||||
|
|
||||||
Context "$name config file" {
|
|
||||||
It "Imports with no errors" {
|
|
||||||
$global:importedconfigs | Should -Not -BeNullOrEmpty -Because "The imported configs should not be null or empty"
|
|
||||||
$global:importedconfigs.ContainsKey($name) | Should -BeTrue -Because "The configuration '$name' should exist in the imported configs"
|
|
||||||
$global:importedconfigs[$name] | Should -Not -BeNullOrEmpty -Because "The configuration '$name' should not be null or empty"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($config -and $global:importedconfigs -and $global:importedconfigs.ContainsKey($name)) {
|
Context "Config File Structure" {
|
||||||
It "Imports should be the correct structure" {
|
It "Should import all config files without errors" {
|
||||||
$applications = $global:importedconfigs[$name] | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
$global:importedConfigs | Should -Not -BeNullOrEmpty -Because "No config files were imported successfully"
|
||||||
$template = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
Write-Verbose "Imported configs: $($global:importedConfigs.Keys -join ', ')"
|
||||||
$result = New-Object System.Collections.Generic.List[System.Object]
|
}
|
||||||
Foreach ($application in $applications) {
|
|
||||||
$compare = $global:importedconfigs[$name].$application | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
foreach ($configName in $configTemplates.Keys) {
|
||||||
if ($(Compare-Object $compare $template)) {
|
It "Should have the correct structure for $configName" {
|
||||||
$result.Add($application)
|
$global:importedConfigs.ContainsKey($configName) | Should -BeTrue -Because "Config file '$configName' is missing"
|
||||||
|
$config = $global:importedConfigs[$configName]
|
||||||
|
$config | Should -Not -BeNullOrEmpty -Because "Config file '$configName' is empty"
|
||||||
|
|
||||||
|
$properties = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
|
||||||
|
foreach ($prop in $properties) {
|
||||||
|
$itemProperties = $config.$prop | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
|
||||||
|
$missingProperties = Compare-Object $configTemplates[$configName] $itemProperties | Where-Object { $_.SideIndicator -eq "<=" } | Select-Object -ExpandProperty InputObject
|
||||||
|
$missingProperties | Should -BeNullOrEmpty -Because "Item '$prop' in '$configName' config is missing properties: $($missingProperties -join ', ')"
|
||||||
|
if ($missingProperties) {
|
||||||
|
Write-Verbose "Missing properties in $configName.$prop: $($missingProperties -join ', ')"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result | Where-Object { $_ -like "WPF*" } | Should -BeNullOrEmpty
|
Context "Tweaks Configuration" {
|
||||||
|
It "Should have original values for all tweaks" {
|
||||||
|
$tweaks = $global:importedConfigs.tweaks
|
||||||
|
$tweaks | Should -Not -BeNullOrEmpty -Because "Tweaks configuration is missing or empty"
|
||||||
|
|
||||||
|
$originals = @{
|
||||||
|
registry = "OriginalValue"
|
||||||
|
service = "OriginalType"
|
||||||
|
ScheduledTask = "OriginalState"
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($tweak in ($tweaks | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)) {
|
||||||
|
foreach ($type in $originals.Keys) {
|
||||||
|
$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"
|
||||||
|
if ($originalCount -ne $totalCount) {
|
||||||
|
Write-Verbose "Tweak '$tweak' of type '$type' has $originalCount original values out of $totalCount total values"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($undo -and $global:importedconfigs -and $global:importedconfigs.ContainsKey($name)) {
|
Context "Applications Configuration" {
|
||||||
It "Tweaks should contain original Value" {
|
It "Should have all required fields for each application" {
|
||||||
$tweaks = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
$apps = $global:importedConfigs.applications
|
||||||
$result = New-Object System.Collections.Generic.List[System.Object]
|
$apps | Should -Not -BeNullOrEmpty -Because "Applications configuration is missing or empty"
|
||||||
|
|
||||||
foreach ($tweak in $tweaks) {
|
foreach ($app in ($apps | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)) {
|
||||||
$Originals = @(
|
foreach ($field in $configTemplates.applications) {
|
||||||
@{
|
$apps.$app.$field | Should -Not -BeNullOrEmpty -Because "Application '$app' is missing the '$field' field"
|
||||||
name = "registry"
|
if (-not $apps.$app.$field) {
|
||||||
value = "OriginalValue"
|
Write-Verbose "Application '$app' is missing the '$field' field"
|
||||||
},
|
|
||||||
@{
|
|
||||||
name = "service"
|
|
||||||
value = "OriginalType"
|
|
||||||
},
|
|
||||||
@{
|
|
||||||
name = "ScheduledTask"
|
|
||||||
value = "OriginalState"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
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)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$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!"
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user