mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-16 01:40:35 -06:00
Compare commits
11 Commits
31ec62f00b
...
0d9b6f3a79
Author | SHA1 | Date | |
---|---|---|---|
|
0d9b6f3a79 | ||
|
5d53b96553 | ||
|
66164379c8 | ||
|
44521776ba | ||
|
bf56639572 | ||
|
c79fa58500 | ||
|
26a2b3df9b | ||
|
e6dbc811f4 | ||
|
97bf9520a1 | ||
|
01f41982f6 | ||
|
be9fae199c |
@ -1,81 +1,146 @@
|
||||
# Import Config Files
|
||||
$global:importedconfigs = @{}
|
||||
Get-ChildItem .\config | Where-Object {$_.Extension -eq ".json"} | ForEach-Object {
|
||||
$global:importedconfigs[$psitem.BaseName] = Get-Content $psitem.FullName | ConvertFrom-Json
|
||||
}
|
||||
# Enable verbose output
|
||||
$VerbosePreference = "Continue"
|
||||
|
||||
|
||||
#===========================================================================
|
||||
# Tests - Application Installs
|
||||
#===========================================================================
|
||||
|
||||
Describe "Config Files" -ForEach @(
|
||||
@{
|
||||
name = "applications"
|
||||
config = $('{
|
||||
"winget": "value",
|
||||
"choco": "value",
|
||||
"category": "value",
|
||||
"content": "value",
|
||||
"description": "value",
|
||||
"link": "value"
|
||||
}' | ConvertFrom-Json)
|
||||
},
|
||||
@{
|
||||
name = "tweaks"
|
||||
undo = $true
|
||||
}
|
||||
) {
|
||||
Context "$name config file" {
|
||||
It "Imports with no errors" {
|
||||
$global:importedconfigs.$name | should -Not -BeNullOrEmpty
|
||||
}
|
||||
if ($config) {
|
||||
It "Imports should be the correct structure" {
|
||||
$applications = $global:importedconfigs.$name | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||
$template = $config | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||
$result = New-Object System.Collections.Generic.List[System.Object]
|
||||
Foreach ($application in $applications) {
|
||||
$compare = $global:importedconfigs.$name.$application | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name
|
||||
if ($(Compare-Object $compare $template) -ne $null) {
|
||||
$result.Add($application)
|
||||
}
|
||||
}
|
||||
|
||||
$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]
|
||||
|
||||
foreach ($tweak in $tweaks) {
|
||||
$Originals = @(
|
||||
@{
|
||||
name = "registry"
|
||||
value = "OriginalValue"
|
||||
},
|
||||
@{
|
||||
name = "service"
|
||||
value = "OriginalType"
|
||||
},
|
||||
@{
|
||||
name = "ScheduledTask"
|
||||
value = "OriginalState"
|
||||
}
|
||||
# Define Test-Schema function
|
||||
function Test-Schema {
|
||||
param (
|
||||
$Object,
|
||||
$Schema
|
||||
)
|
||||
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)")
|
||||
|
||||
$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
|
||||
}
|
||||
}
|
||||
$result | Select-String "WPF*" | should -BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($requiredProp in $Schema.Required) {
|
||||
if (-not $Object.PSObject.Properties.Name.Contains($requiredProp)) {
|
||||
$errors += "Required property '$requiredProp' is missing"
|
||||
}
|
||||
}
|
||||
|
||||
return $errors
|
||||
}
|
||||
|
||||
# Import Config Files
|
||||
$global:importedConfigs = @{}
|
||||
Get-ChildItem .\config -Filter *.json | ForEach-Object {
|
||||
try {
|
||||
$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 = @{
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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" {
|
||||
$testSchemaScriptBlock = ${function:Test-Schema}.ToString()
|
||||
|
||||
$results = $configSchemas.Keys | ForEach-Object -Parallel {
|
||||
$configName = $_
|
||||
$importedConfigs = $using:global:importedConfigs
|
||||
$configSchemas = $using:configSchemas
|
||||
$config = $importedConfigs[$configName]
|
||||
$schema = $configSchemas[$configName]
|
||||
|
||||
if (-not $config) {
|
||||
return "Config file '$configName' is missing or empty"
|
||||
}
|
||||
|
||||
$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