Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Settings type detection more robust #1967

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}