Skip to content

Commit dc4ae4b

Browse files
Tadasandyleejordan
andauthored
Make Settings type detection more robust (#1967)
* Add PSObject unwrapping for all Settings types * Update Engine/Settings.cs --------- Co-authored-by: Andy Jordan <[email protected]>
1 parent 5648cf5 commit dc4ae4b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Engine/Settings.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,13 @@ private static bool IsBuiltinSettingPreset(object settingPreset)
497497
internal static SettingsMode FindSettingsMode(object settings, string path, out object settingsFound)
498498
{
499499
var settingsMode = SettingsMode.None;
500+
501+
// 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
502+
if (settings is PSObject settingsFoundPSObject)
503+
{
504+
settings = settingsFoundPSObject.BaseObject;
505+
}
506+
500507
settingsFound = settings;
501508
if (settingsFound == null)
502509
{
@@ -532,11 +539,6 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
532539
{
533540
settingsMode = SettingsMode.Hashtable;
534541
}
535-
// 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
536-
else if (settingsFound is PSObject settingsFoundPSObject)
537-
{
538-
TryResolveSettingForStringType(settingsFoundPSObject.BaseObject, ref settingsMode, ref settingsFound);
539-
}
540542
}
541543
}
542544

Tests/Engine/Settings.tests.ps1

+30
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,34 @@ Describe "Settings Class" {
377377
@{ Expr = ';)' }
378378
)
379379
}
380+
381+
Context "FindSettingsMode" {
382+
BeforeAll {
383+
$findSettingsMode = ($settingsTypeName -as [type]).GetMethod(
384+
'FindSettingsMode',
385+
[System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static)
386+
387+
$outputObject = [System.Object]::new()
388+
}
389+
390+
It "Should detect hashtable" {
391+
$settings = @{}
392+
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "Hashtable"
393+
}
394+
395+
It "Should detect hashtable wrapped by a PSObject" {
396+
$settings = [PSObject]@{} # Force the settings hashtable to be wrapped
397+
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "Hashtable"
398+
}
399+
400+
It "Should detect string" {
401+
$settings = ""
402+
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "File"
403+
}
404+
405+
It "Should detect string wrapped by a PSObject" {
406+
$settings = [PSObject]"" # Force the settings string to be wrapped
407+
$findSettingsMode.Invoke($null, @($settings, $null, [ref]$outputObject)) | Should -Be "File"
408+
}
409+
}
380410
}

0 commit comments

Comments
 (0)