mirror of
https://github.com/tw93/Mole.git
synced 2026-03-24 00:55:08 +00:00
fix(windows): initialize visual defaults safely
This commit is contained in:
@@ -47,24 +47,36 @@ $script:DefaultIcons = @{
|
|||||||
Trash = [char]0x2718 # ✘ (trash substitute)
|
Trash = [char]0x2718 # ✘ (trash substitute)
|
||||||
}
|
}
|
||||||
|
|
||||||
function Initialize-MoleVisualDefaults {
|
function Get-OrCreateScriptHashtable {
|
||||||
if (-not ($script:Colors -is [hashtable])) {
|
param(
|
||||||
$script:Colors = @{}
|
[Parameter(Mandatory)]
|
||||||
|
[string]$Name
|
||||||
|
)
|
||||||
|
|
||||||
|
$existing = Get-Variable -Name $Name -Scope Script -ErrorAction SilentlyContinue
|
||||||
|
if ($existing -and $existing.Value -is [hashtable]) {
|
||||||
|
return $existing.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$table = @{}
|
||||||
|
Set-Variable -Name $Name -Scope Script -Value $table
|
||||||
|
return $table
|
||||||
|
}
|
||||||
|
|
||||||
|
function Initialize-MoleVisualDefaults {
|
||||||
|
$colors = Get-OrCreateScriptHashtable -Name "Colors"
|
||||||
|
|
||||||
foreach ($entry in $script:DefaultColors.GetEnumerator()) {
|
foreach ($entry in $script:DefaultColors.GetEnumerator()) {
|
||||||
if (-not $script:Colors.ContainsKey($entry.Key)) {
|
if (-not $colors.ContainsKey($entry.Key)) {
|
||||||
$script:Colors[$entry.Key] = $entry.Value
|
$colors[$entry.Key] = $entry.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-not ($script:Icons -is [hashtable])) {
|
$icons = Get-OrCreateScriptHashtable -Name "Icons"
|
||||||
$script:Icons = @{}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($entry in $script:DefaultIcons.GetEnumerator()) {
|
foreach ($entry in $script:DefaultIcons.GetEnumerator()) {
|
||||||
if (-not $script:Icons.ContainsKey($entry.Key)) {
|
if (-not $icons.ContainsKey($entry.Key)) {
|
||||||
$script:Icons[$entry.Key] = $entry.Value
|
$icons[$entry.Key] = $entry.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ BeforeAll {
|
|||||||
$script:WindowsDir = Split-Path -Parent $PSScriptRoot
|
$script:WindowsDir = Split-Path -Parent $PSScriptRoot
|
||||||
$script:BinDir = Join-Path $script:WindowsDir "bin"
|
$script:BinDir = Join-Path $script:WindowsDir "bin"
|
||||||
$script:InstallScript = Join-Path $script:WindowsDir "install.ps1"
|
$script:InstallScript = Join-Path $script:WindowsDir "install.ps1"
|
||||||
|
$script:VisualDefaultsErrorPattern = 'property ''Solid'' cannot be found|找不到属性.?Solid|VariableIsUndefined|\$script:Colors'
|
||||||
}
|
}
|
||||||
|
|
||||||
Describe "Clean Command" {
|
Describe "Clean Command" {
|
||||||
@@ -49,7 +50,7 @@ Describe "Clean Command" {
|
|||||||
Stop-Job $job -ErrorAction SilentlyContinue
|
Stop-Job $job -ErrorAction SilentlyContinue
|
||||||
Remove-Job $job -Force -ErrorAction SilentlyContinue
|
Remove-Job $job -Force -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
$output | Should -Not -Match "property 'Solid' cannot be found|找不到属性.?Solid"
|
$output | Should -Not -Match $script:VisualDefaultsErrorPattern
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,7 +176,9 @@ Describe "Main Entry Point" {
|
|||||||
It "Should show version without error" {
|
It "Should show version without error" {
|
||||||
$result = & powershell -ExecutionPolicy Bypass -File $script:MolePath -Version 2>&1
|
$result = & powershell -ExecutionPolicy Bypass -File $script:MolePath -Version 2>&1
|
||||||
$result | Should -Not -BeNullOrEmpty
|
$result | Should -Not -BeNullOrEmpty
|
||||||
$result -join "`n" | Should -Match "Mole|v\d+\.\d+"
|
$output = $result -join "`n"
|
||||||
|
$output | Should -Not -Match $script:VisualDefaultsErrorPattern
|
||||||
|
$output | Should -Match "Mole|v\d+\.\d+"
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Should not throw missing Solid property errors during menu startup" {
|
It "Should not throw missing Solid property errors during menu startup" {
|
||||||
@@ -189,7 +192,7 @@ Describe "Main Entry Point" {
|
|||||||
Stop-Job $job -ErrorAction SilentlyContinue
|
Stop-Job $job -ErrorAction SilentlyContinue
|
||||||
Remove-Job $job -Force -ErrorAction SilentlyContinue
|
Remove-Job $job -Force -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
$output | Should -Not -Match "property 'Solid' cannot be found|找不到属性.?Solid"
|
$output | Should -Not -Match $script:VisualDefaultsErrorPattern
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Should list available commands in help" {
|
It "Should list available commands in help" {
|
||||||
|
|||||||
@@ -37,6 +37,41 @@ Describe "Base Module" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Context "Visual Defaults Initialization" {
|
||||||
|
BeforeEach {
|
||||||
|
$script:OriginalColors = $script:Colors.Clone()
|
||||||
|
$script:OriginalIcons = $script:Icons.Clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
AfterEach {
|
||||||
|
$script:Colors = $script:OriginalColors.Clone()
|
||||||
|
$script:Icons = $script:OriginalIcons.Clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should initialize missing visual tables under strict mode" {
|
||||||
|
Remove-Variable -Name Colors -Scope Script -ErrorAction SilentlyContinue
|
||||||
|
Remove-Variable -Name Icons -Scope Script -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
{ Initialize-MoleVisualDefaults } | Should -Not -Throw
|
||||||
|
$script:Colors.Cyan | Should -Not -BeNullOrEmpty
|
||||||
|
$script:Colors.Green | Should -Not -BeNullOrEmpty
|
||||||
|
$script:Icons.Solid | Should -Not -BeNullOrEmpty
|
||||||
|
$script:Icons.Trash | Should -Not -BeNullOrEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should preserve existing values while filling missing defaults" {
|
||||||
|
$script:Colors = @{ Cyan = "custom-cyan" }
|
||||||
|
$script:Icons = @{ Solid = "*" }
|
||||||
|
|
||||||
|
Initialize-MoleVisualDefaults
|
||||||
|
|
||||||
|
$script:Colors.Cyan | Should -Be "custom-cyan"
|
||||||
|
$script:Colors.Green | Should -Not -BeNullOrEmpty
|
||||||
|
$script:Icons.Solid | Should -Be "*"
|
||||||
|
$script:Icons.Admin | Should -Not -BeNullOrEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Context "Test-IsAdmin" {
|
Context "Test-IsAdmin" {
|
||||||
It "Should return a boolean" {
|
It "Should return a boolean" {
|
||||||
$result = Test-IsAdmin
|
$result = Test-IsAdmin
|
||||||
|
|||||||
Reference in New Issue
Block a user