mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-16 09:50:36 -06:00
Compare commits
No commits in common. "0d9b6f3a797ee8e49e029fdd6ad35de8be2596de" and "31ec62f00ba19586f0cf6589f0bb94db8899db53" have entirely different histories.
0d9b6f3a79
...
31ec62f00b
@ -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" }
|
Context "$name config file" {
|
||||||
Type = @{ Type = "String" }
|
It "Imports with no errors" {
|
||||||
Value = @{ Type = "String" }
|
$global:importedconfigs.$name | should -Not -BeNullOrEmpty
|
||||||
OriginalValue = @{ Type = "String" }
|
}
|
||||||
}
|
if ($config) {
|
||||||
Required = @("Path", "Name", "Type", "Value", "OriginalValue")
|
It "Imports should be the correct structure" {
|
||||||
}
|
$applications = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||||
service = @{
|
$template = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||||
Type = "Object"
|
$result = New-Object System.Collections.Generic.List[System.Object]
|
||||||
Properties = @{
|
Foreach ($application in $applications) {
|
||||||
Name = @{ Type = "String" }
|
$compare = $global:importedconfigs.$name.$application | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||||
StartupType = @{ Type = "String" }
|
if ($(Compare-Object $compare $template) -ne $null) {
|
||||||
OriginalType = @{ Type = "String" }
|
$result.Add($application)
|
||||||
}
|
|
||||||
Required = @("Name", "StartupType", "OriginalType")
|
|
||||||
}
|
|
||||||
ScheduledTask = @{
|
|
||||||
Type = "Object"
|
|
||||||
Properties = @{
|
|
||||||
Name = @{ Type = "String" }
|
|
||||||
State = @{ Type = "String" }
|
|
||||||
OriginalState = @{ Type = "String" }
|
|
||||||
}
|
|
||||||
Required = @("Name", "State", "OriginalState")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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]
|
||||||
|
|
||||||
Context "Config File Structure" {
|
foreach ($tweak in $tweaks) {
|
||||||
It "Should import all config files without errors" {
|
$Originals = @(
|
||||||
$global:importedConfigs | Should -Not -BeNullOrEmpty -Because "No config files were imported successfully"
|
@{
|
||||||
}
|
name = "registry"
|
||||||
|
value = "OriginalValue"
|
||||||
It "Should have the correct structure for all configs" {
|
},
|
||||||
$testSchemaScriptBlock = ${function:Test-Schema}.ToString()
|
@{
|
||||||
|
name = "service"
|
||||||
$results = $configSchemas.Keys | ForEach-Object -Parallel {
|
value = "OriginalType"
|
||||||
$configName = $_
|
},
|
||||||
$importedConfigs = $using:global:importedConfigs
|
@{
|
||||||
$configSchemas = $using:configSchemas
|
name = "ScheduledTask"
|
||||||
$config = $importedConfigs[$configName]
|
value = "OriginalState"
|
||||||
$schema = $configSchemas[$configName]
|
}
|
||||||
|
)
|
||||||
if (-not $config) {
|
Foreach ($original in $Originals) {
|
||||||
return "Config file '$configName' is missing or empty"
|
$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
|
||||||
$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!"
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user