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
bf56639572
commit
44521776ba
@ -3,22 +3,18 @@ $VerbosePreference = "Continue"
|
|||||||
|
|
||||||
# Import Config Files
|
# Import Config Files
|
||||||
$global:importedConfigs = @{}
|
$global:importedConfigs = @{}
|
||||||
$configFiles = Get-ChildItem .\config -Filter *.json -ErrorAction SilentlyContinue
|
Get-ChildItem .\config -Filter *.json | ForEach-Object {
|
||||||
if ($configFiles) {
|
|
||||||
foreach ($file in $configFiles) {
|
|
||||||
try {
|
try {
|
||||||
$global:importedConfigs[$file.BaseName] = Get-Content $file.FullName | ConvertFrom-Json
|
$global:importedConfigs[$_.BaseName] = Get-Content $_.FullName | ConvertFrom-Json
|
||||||
Write-Verbose "Successfully imported config file: $($file.FullName)"
|
Write-Verbose "Successfully imported config file: $($_.FullName)"
|
||||||
} catch {
|
} 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" {
|
Describe "Config Files Validation" {
|
||||||
$configSchemas = @{
|
BeforeAll {
|
||||||
|
$script:configSchemas = @{
|
||||||
applications = @{
|
applications = @{
|
||||||
Type = "Object"
|
Type = "Object"
|
||||||
Properties = @{
|
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 {
|
function Test-Schema {
|
||||||
param (
|
param (
|
||||||
$Object,
|
$Object,
|
||||||
$Schema
|
$Schema
|
||||||
)
|
)
|
||||||
|
|
||||||
|
$errors = @()
|
||||||
|
|
||||||
$Object.PSObject.Properties | ForEach-Object {
|
$Object.PSObject.Properties | ForEach-Object {
|
||||||
$propName = $_.Name
|
$propName = $_.Name
|
||||||
$propValue = $_.Value
|
$propValue = $_.Value
|
||||||
|
|
||||||
$propSchema = $Schema.Properties[$propName]
|
$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) {
|
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" {
|
"Object" {
|
||||||
$propValue | Should -BeOfType [PSCustomObject]
|
if ($propValue -isnot [PSCustomObject]) {
|
||||||
Test-Schema -Object $propValue -Schema $propSchema
|
$errors += "Property '$propName' should be an object but is $($propValue.GetType())"
|
||||||
|
} else {
|
||||||
|
$errors += Test-Schema -Object $propValue -Schema $propSchema
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($requiredProp in $Schema.Required) {
|
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