2024-08-28 16:24:28 -05:00
|
|
|
# Enable verbose output
|
|
|
|
$VerbosePreference = "Continue"
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Import Config Files
|
2024-08-28 16:24:28 -05:00
|
|
|
$global:importedConfigs = @{}
|
2024-08-28 16:38:02 -05:00
|
|
|
$configFiles = Get-ChildItem .\config -Filter *.json -ErrorAction SilentlyContinue
|
|
|
|
if ($configFiles) {
|
|
|
|
foreach ($file in $configFiles) {
|
|
|
|
try {
|
|
|
|
$global:importedConfigs[$file.BaseName] = Get-Content $file.FullName | ConvertFrom-Json
|
|
|
|
Write-Verbose "Successfully imported config file: $($file.FullName)"
|
|
|
|
} catch {
|
|
|
|
Write-Error "Failed to import config file: $($file.FullName). Error: $_"
|
|
|
|
}
|
2024-08-28 16:16:30 -05:00
|
|
|
}
|
2024-08-28 16:38:02 -05:00
|
|
|
} else {
|
|
|
|
Write-Warning "No config files found in the .\config directory"
|
2023-10-19 17:12:55 -05:00
|
|
|
}
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-08-28 16:24:28 -05:00
|
|
|
Describe "Config Files Validation" {
|
2024-08-28 16:38:02 -05:00
|
|
|
$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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-28 16:24:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ', ')"
|
|
|
|
}
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
foreach ($configName in $configSchemas.Keys) {
|
2024-08-28 16:24:28 -05:00
|
|
|
It "Should have the correct structure for $configName" {
|
2024-08-28 16:38:02 -05:00
|
|
|
$global:importedConfigs | Should -Not -BeNullOrEmpty
|
2024-08-28 16:24:28 -05:00
|
|
|
$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"
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
$schema = $configSchemas[$configName]
|
|
|
|
|
|
|
|
function Test-Schema {
|
|
|
|
param (
|
|
|
|
$Object,
|
|
|
|
$Schema
|
|
|
|
)
|
2024-08-28 16:03:49 -05:00
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
$Object.PSObject.Properties | ForEach-Object {
|
|
|
|
$propName = $_.Name
|
|
|
|
$propValue = $_.Value
|
2024-08-28 16:03:49 -05:00
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
$propSchema = $Schema.Properties[$propName]
|
|
|
|
$propSchema | Should -Not -BeNullOrEmpty -Because "Property '$propName' is not defined in the schema"
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
switch ($propSchema.Type) {
|
|
|
|
"String" { $propValue | Should -BeOfType [string] }
|
|
|
|
"Object" {
|
|
|
|
$propValue | Should -BeOfType [PSCustomObject]
|
|
|
|
Test-Schema -Object $propValue -Schema $propSchema
|
|
|
|
}
|
|
|
|
}
|
2024-08-28 16:24:28 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 16:38:02 -05:00
|
|
|
foreach ($requiredProp in $Schema.Required) {
|
|
|
|
$Object.PSObject.Properties.Name | Should -Contain $requiredProp -Because "Required property '$requiredProp' is missing"
|
2023-03-07 14:28:00 -06:00
|
|
|
}
|
|
|
|
}
|
2024-08-28 16:38:02 -05:00
|
|
|
|
|
|
|
Test-Schema -Object $config -Schema $schema
|
2023-03-07 14:28:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 15:35:17 -05:00
|
|
|
}
|
2024-08-28 16:24:28 -05:00
|
|
|
|
|
|
|
# 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!"
|
2024-08-28 16:34:21 -05:00
|
|
|
}
|