Skip to content

Allow -Setting parameter to resolve setting presets as well when object is still a PSObject in BeginProcessing #928

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

Merged
Merged
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
48 changes: 25 additions & 23 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,40 +690,42 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
}
else
{
var settingsFilePath = settingsFound as String;
if (settingsFilePath != null)
{
if (IsBuiltinSettingPreset(settingsFilePath))
{
settingsMode = SettingsMode.Preset;
settingsFound = GetSettingPresetFilePath(settingsFilePath);
}
else
{
settingsMode = SettingsMode.File;
settingsFound = settingsFilePath;
}
}
else
if (!TryResolveSettingForStringType(settingsFound, ref settingsMode, ref settingsFound))
{
if (settingsFound is Hashtable)
{
settingsMode = SettingsMode.Hashtable;
}
else // 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
// 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)
{
if (settingsFound is PSObject settingsFoundPSObject)
{
if (settingsFoundPSObject.BaseObject is String)
{
settingsMode = SettingsMode.File;
}
}
TryResolveSettingForStringType(settingsFoundPSObject.BaseObject, ref settingsMode, ref settingsFound);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no check of the return, does it not matter if we get to this point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for the return value is only needed for the first method call. If we still cannot resolve the settings type at this point, then the mode will be SettingsMode.None (this is the initial default) and that gets handled later on.

}
}
}

return settingsMode;
}

// If the settings object is a string determine wheter it is one of the settings preset or a file path and resolve the setting in the former case.
private static bool TryResolveSettingForStringType(object settingsObject, ref SettingsMode settingsMode, ref object resolvedSettingValue)
{
if (settingsObject is string settingsString)
{
if (IsBuiltinSettingPreset(settingsString))
{
settingsMode = SettingsMode.Preset;
resolvedSettingValue = GetSettingPresetFilePath(settingsString);
}
else
{
settingsMode = SettingsMode.File;
resolvedSettingValue = settingsString;
}
return true;
}

return false;
}
}
}
7 changes: 7 additions & 0 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ Describe "Test CustomizedRulePath" {
Pop-Location
}
}

It "resolves rule preset when passed in via pipeline" {
$warnings = 'CodeFormattingStroustrup' | ForEach-Object {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems overly complicated, isn't this just:

$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'if ($true){}' -Settings CodeFormattingStroustrup
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'PSUseConsistentWhitespace'

all the pipes and where's aren't needed and only add complexity

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pipes are needed to reproduce the actual bug because otherwise the settings object is already resolved to a string in your case but I will get rid of the where clause and add the additional assertion as you suggested.

Invoke-ScriptAnalyzer -ScriptDefinition 'if ($true){}' -Settings $_}
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'PSUseConsistentWhitespace'
}

It "Should use the CustomRulePath parameter" {
$settings = @{
Expand Down