Compare commits

..

11 Commits

Author SHA1 Message Date
Chris Titus
0d9b6f3a79 Update configs.Tests.ps1 2024-08-28 16:46:18 -05:00
Chris Titus
5d53b96553 Update configs.Tests.ps1 2024-08-28 16:43:36 -05:00
Chris Titus
66164379c8 Update configs.Tests.ps1 2024-08-28 16:42:03 -05:00
Chris Titus
44521776ba Update configs.Tests.ps1 2024-08-28 16:40:38 -05:00
Chris Titus
bf56639572 Update configs.Tests.ps1 2024-08-28 16:38:02 -05:00
Chris Titus
c79fa58500 Update configs.Tests.ps1 2024-08-28 16:34:21 -05:00
Chris Titus
26a2b3df9b Update configs.Tests.ps1 2024-08-28 16:24:28 -05:00
Chris Titus
e6dbc811f4 Update configs.Tests.ps1 2024-08-28 16:20:11 -05:00
Chris Titus
97bf9520a1 Update configs.Tests.ps1 2024-08-28 16:16:30 -05:00
Chris Titus
01f41982f6 Update configs.Tests.ps1 2024-08-28 16:08:11 -05:00
Chris Titus
be9fae199c Update configs.Tests.ps1 2024-08-28 16:03:49 -05:00

View File

@ -1,81 +1,146 @@
# Import Config Files # Enable verbose output
$global:importedconfigs = @{} $VerbosePreference = "Continue"
Get-ChildItem .\config | Where-Object {$_.Extension -eq ".json"} | ForEach-Object {
$global:importedconfigs[$psitem.BaseName] = Get-Content $psitem.FullName | ConvertFrom-Json
}
# Define Test-Schema function
#=========================================================================== function Test-Schema {
# Tests - Application Installs param (
#=========================================================================== $Object,
$Schema
Describe "Config Files" -ForEach @(
@{
name = "applications"
config = $('{
"winget": "value",
"choco": "value",
"category": "value",
"content": "value",
"description": "value",
"link": "value"
}' | ConvertFrom-Json)
},
@{
name = "tweaks"
undo = $true
}
) {
Context "$name config file" {
It "Imports with no errors" {
$global:importedconfigs.$name | should -Not -BeNullOrEmpty
}
if ($config) {
It "Imports should be the correct structure" {
$applications = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
$template = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
$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
if ($(Compare-Object $compare $template) -ne $null) {
$result.Add($application)
}
}
$result | Select-String "WPF*" | should -BeNullOrEmpty
}
}
if($undo) {
It "Tweaks should contain original Value" {
$tweaks = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
$result = New-Object System.Collections.Generic.List[System.Object]
foreach ($tweak in $tweaks) {
$Originals = @(
@{
name = "registry"
value = "OriginalValue"
},
@{
name = "service"
value = "OriginalType"
},
@{
name = "ScheduledTask"
value = "OriginalState"
}
) )
Foreach ($original in $Originals) {
$TotalCount = ($global:importedconfigs.$name.$tweak.$($original.name)).count $errors = @()
$OriginalCount = ($global:importedconfigs.$name.$tweak.$($original.name).$($original.value) | Where-Object {$_}).count
if($TotalCount -ne $OriginalCount) { $Object.PSObject.Properties | ForEach-Object {
$result.Add("$Tweak,$($original.name)") $propName = $_.Name
$propValue = $_.Value
$propSchema = $Schema.Properties[$propName]
if (-not $propSchema) {
$errors += "Property '$propName' is not defined in the schema"
return
}
switch ($propSchema.Type) {
"String" {
if ($propValue -isnot [string]) {
$errors += "Property '$propName' should be a string but is $($propValue.GetType())"
} }
} }
"Object" {
if ($propValue -isnot [PSCustomObject]) {
$errors += "Property '$propName' should be an object but is $($propValue.GetType())"
} else {
$errors += Test-Schema -Object $propValue -Schema $propSchema
}
} }
$result | Select-String "WPF*" | should -BeNullOrEmpty
} }
} }
foreach ($requiredProp in $Schema.Required) {
if (-not $Object.PSObject.Properties.Name.Contains($requiredProp)) {
$errors += "Required property '$requiredProp' is missing"
}
}
return $errors
}
# Import Config Files
$global:importedConfigs = @{}
Get-ChildItem .\config -Filter *.json | ForEach-Object {
try {
$global:importedConfigs[$_.BaseName] = Get-Content $_.FullName | ConvertFrom-Json
Write-Verbose "Successfully imported config file: $($_.FullName)"
} catch {
Write-Error "Failed to import config file: $($_.FullName). Error: $_"
} }
} }
Describe "Config Files Validation" {
BeforeAll {
$script:configSchemas = @{
applications = @{
Type = "Object"
Properties = @{
winget = @{ Type = "String" }
choco = @{ Type = "String" }
category = @{ Type = "String" }
content = @{ Type = "String" }
description = @{ Type = "String" }
link = @{ Type = "String" }
}
Required = @("winget", "choco", "category", "content", "description", "link")
}
tweaks = @{
Type = "Object"
Properties = @{
registry = @{
Type = "Object"
Properties = @{
Path = @{ Type = "String" }
Name = @{ Type = "String" }
Type = @{ Type = "String" }
Value = @{ Type = "String" }
OriginalValue = @{ Type = "String" }
}
Required = @("Path", "Name", "Type", "Value", "OriginalValue")
}
service = @{
Type = "Object"
Properties = @{
Name = @{ Type = "String" }
StartupType = @{ Type = "String" }
OriginalType = @{ Type = "String" }
}
Required = @("Name", "StartupType", "OriginalType")
}
ScheduledTask = @{
Type = "Object"
Properties = @{
Name = @{ Type = "String" }
State = @{ Type = "String" }
OriginalState = @{ Type = "String" }
}
Required = @("Name", "State", "OriginalState")
}
}
}
}
}
Context "Config File Structure" {
It "Should import all config files without errors" {
$global:importedConfigs | Should -Not -BeNullOrEmpty -Because "No config files were imported successfully"
}
It "Should have the correct structure for all configs" {
$testSchemaScriptBlock = ${function:Test-Schema}.ToString()
$results = $configSchemas.Keys | ForEach-Object -Parallel {
$configName = $_
$importedConfigs = $using:global:importedConfigs
$configSchemas = $using:configSchemas
$config = $importedConfigs[$configName]
$schema = $configSchemas[$configName]
if (-not $config) {
return "Config file '$configName' is missing or empty"
}
$testSchemaFunction = [ScriptBlock]::Create($using:testSchemaScriptBlock)
& $testSchemaFunction -Object $config -Schema $schema
} -ThrottleLimit 4
$results | Should -BeNullOrEmpty -Because "The following schema violations were found: $($results -join '; ')"
}
}
}
# 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!"
}