mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-16 01:40:35 -06:00
Update configs.Tests.ps1
This commit is contained in:
parent
bf56639572
commit
44521776ba
@ -3,22 +3,18 @@ $VerbosePreference = "Continue"
|
||||
|
||||
# Import Config Files
|
||||
$global:importedConfigs = @{}
|
||||
$configFiles = Get-ChildItem .\config -Filter *.json -ErrorAction SilentlyContinue
|
||||
if ($configFiles) {
|
||||
foreach ($file in $configFiles) {
|
||||
Get-ChildItem .\config -Filter *.json | ForEach-Object {
|
||||
try {
|
||||
$global:importedConfigs[$file.BaseName] = Get-Content $file.FullName | ConvertFrom-Json
|
||||
Write-Verbose "Successfully imported config file: $($file.FullName)"
|
||||
$global:importedConfigs[$_.BaseName] = Get-Content $_.FullName | ConvertFrom-Json
|
||||
Write-Verbose "Successfully imported config file: $($_.FullName)"
|
||||
} catch {
|
||||
Write-Error "Failed to import config file: $($file.FullName). Error: $_"
|
||||
Write-Error "Failed to import config file: $($_.FullName). Error: $_"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Write-Warning "No config files found in the .\config directory"
|
||||
}
|
||||
|
||||
Describe "Config Files Validation" {
|
||||
$configSchemas = @{
|
||||
BeforeAll {
|
||||
$script:configSchemas = @{
|
||||
applications = @{
|
||||
Type = "Object"
|
||||
Properties = @{
|
||||
@ -67,51 +63,70 @@ Describe "Config Files Validation" {
|
||||
}
|
||||
}
|
||||
|
||||
Context "Config File Structure" {
|
||||
It "Should import all config files without errors" {
|
||||
$global:importedConfigs | Should -Not -BeNullOrEmpty -Because "No config files were imported successfully"
|
||||
Write-Verbose "Imported configs: $($global:importedConfigs.Keys -join ', ')"
|
||||
}
|
||||
|
||||
foreach ($configName in $configSchemas.Keys) {
|
||||
It "Should have the correct structure for $configName" {
|
||||
$global:importedConfigs | Should -Not -BeNullOrEmpty
|
||||
$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"
|
||||
|
||||
$schema = $configSchemas[$configName]
|
||||
|
||||
function Test-Schema {
|
||||
param (
|
||||
$Object,
|
||||
$Schema
|
||||
)
|
||||
|
||||
$errors = @()
|
||||
|
||||
$Object.PSObject.Properties | ForEach-Object {
|
||||
$propName = $_.Name
|
||||
$propValue = $_.Value
|
||||
|
||||
$propSchema = $Schema.Properties[$propName]
|
||||
$propSchema | Should -Not -BeNullOrEmpty -Because "Property '$propName' is not defined in the schema"
|
||||
if (-not $propSchema) {
|
||||
$errors += "Property '$propName' is not defined in the schema"
|
||||
return
|
||||
}
|
||||
|
||||
switch ($propSchema.Type) {
|
||||
"String" { $propValue | Should -BeOfType [string] }
|
||||
"String" {
|
||||
if ($propValue -isnot [string]) {
|
||||
$errors += "Property '$propName' should be a string but is $($propValue.GetType())"
|
||||
}
|
||||
}
|
||||
"Object" {
|
||||
$propValue | Should -BeOfType [PSCustomObject]
|
||||
Test-Schema -Object $propValue -Schema $propSchema
|
||||
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) {
|
||||
$Object.PSObject.Properties.Name | Should -Contain $requiredProp -Because "Required property '$requiredProp' is missing"
|
||||
if (-not $Object.PSObject.Properties.Name.Contains($requiredProp)) {
|
||||
$errors += "Required property '$requiredProp' is missing"
|
||||
}
|
||||
}
|
||||
|
||||
Test-Schema -Object $config -Schema $schema
|
||||
return $errors
|
||||
}
|
||||
}
|
||||
|
||||
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" {
|
||||
$results = $configSchemas.Keys | ForEach-Object -Parallel {
|
||||
$configName = $_
|
||||
$config = $using:global:importedConfigs[$configName]
|
||||
$schema = $using:configSchemas[$configName]
|
||||
|
||||
if (-not $config) {
|
||||
return "Config file '$configName' is missing or empty"
|
||||
}
|
||||
|
||||
& $using:Test-Schema -Object $config -Schema $schema
|
||||
} -ThrottleLimit 4
|
||||
|
||||
$results | Should -BeNullOrEmpty -Because "The following schema violations were found: $($results -join '; ')"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user