Skip to content

Commit

Permalink
Add PSObject unwrapping for all Settings types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadas authored and andyleejordan committed Feb 19, 2025
1 parent 20135fb commit 16f6601
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,13 @@ private static bool IsBuiltinSettingPreset(object settingPreset)
internal static SettingsMode FindSettingsMode(object settings, string path, out object settingsFound)
{
var settingsMode = SettingsMode.None;

// if the provided settings argument is wrapped in an expressions then PowerShell resolves it but it will be of type PSObject and we have to operate then on the BaseObject
if (settings is PSObject)
{
settings = ((PSObject)settings).BaseObject;
}

settingsFound = settings;
if (settingsFound == null)
{
Expand Down Expand Up @@ -532,11 +539,6 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
{
settingsMode = SettingsMode.Hashtable;
}
// if the provided argument is wrapped in an expressions then PowerShell resolves it but it will be of type PSObject and we have to operate then on the BaseObject
else if (settingsFound is PSObject settingsFoundPSObject)
{
TryResolveSettingForStringType(settingsFoundPSObject.BaseObject, ref settingsMode, ref settingsFound);
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/Engine/Settings.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,34 @@ Describe "Settings Class" {
@{ Expr = ';)' }
)
}

Context "FindSettingsMode" {
BeforeAll {
$findSettingsMode = ($settingsTypeName -as [type]).GetMethod(
'FindSettingsMode',
[System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static)

$outputObject = [System.Object]::new()
}

It "Should detect hashtable" {
$settings = @{}
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "Hashtable"
}

It "Should detect hashtable wrapped by a PSObject" {
$settings = [PSObject]@{} # Force the settings hashtable to be wrapped
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "Hashtable"
}

It "Should detect string" {
$settings = ""
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "File"
}

It "Should detect string wrapped by a PSObject" {
$settings = [PSObject]"" # Force the settings string to be wrapped
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "File"
}
}
}

0 comments on commit 16f6601

Please sign in to comment.