Skip to content

Commit

Permalink
Make Settings type detection more robust (#1967)
Browse files Browse the repository at this point in the history
* Add PSObject unwrapping for all Settings types

* Update Engine/Settings.cs

---------

Co-authored-by: Andy Jordan <[email protected]>
  • Loading branch information
Tadas and andyleejordan authored Feb 20, 2025
1 parent 5648cf5 commit dc4ae4b
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 settingsFoundPSObject)
{
settings = settingsFoundPSObject.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 dc4ae4b

Please sign in to comment.