Compare commits

..

No commits in common. "0d9b6f3a797ee8e49e029fdd6ad35de8be2596de" and "31ec62f00ba19586f0cf6589f0bb94db8899db53" have entirely different histories.

View File

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