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

Fix parsing the -Settings object as a path when the path object originates from an expression #915

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
49 changes: 33 additions & 16 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,25 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou

case SettingsMode.Preset:
case SettingsMode.File:
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single();
settingsFound = resolvedPath;
outputWriter?.WriteVerbose(
String.Format(
CultureInfo.CurrentCulture,
Strings.SettingsUsingFile,
resolvedPath));
var userProvidedSettingsString = settingsFound.ToString();
try
{
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(userProvidedSettingsString, out ProviderInfo providerInfo).Single();
settingsFound = resolvedPath;
outputWriter?.WriteVerbose(
String.Format(
CultureInfo.CurrentCulture,
Strings.SettingsUsingFile,
resolvedPath));
}
catch
{
outputWriter?.WriteVerbose(
String.Format(
CultureInfo.CurrentCulture,
Strings.SettingsCannotFindFile,
userProvidedSettingsString));
}
break;

case SettingsMode.Hashtable:
Expand All @@ -218,20 +230,15 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou
Strings.SettingsUsingHashtable));
break;

default: // case SettingsMode.None
default:
outputWriter?.WriteVerbose(
String.Format(
CultureInfo.CurrentCulture,
Strings.SettingsCannotFindFile));
break;
}

if (settingsMode != SettingsMode.None)
{
return new Settings(settingsFound);
Strings.SettingsObjectCouldNotBResolved));
return null;
}

return null;
return new Settings(settingsFound);
}

/// <summary>
Expand Down Expand Up @@ -703,6 +710,16 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
{
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 (settingsFound is PSObject settingsFoundPSObject)
{
if (settingsFoundPSObject.BaseObject is String)
{
settingsMode = SettingsMode.File;
}
}
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion Engine/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Engine/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
<value>Using settings hashtable.</value>
</data>
<data name="SettingsCannotFindFile" xml:space="preserve">
<value>Cannot find a settings file.</value>
<value>Cannot resolve settings file path '{0}'.</value>
</data>
<data name="SettingsNotParsable" xml:space="preserve">
<value>Cannot parse settings. Will abort the invocation.</value>
Expand Down Expand Up @@ -321,4 +321,7 @@
<data name="PositionRefPosLessThanInputPos" xml:space="preserve">
<value>Input position should be less than that of the invoking object.</value>
</data>
<data name="SettingsObjectCouldNotBResolved" xml:space="preserve">
<value>Settings object could not be resolved.</value>
</data>
</root>
16 changes: 13 additions & 3 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,23 @@ Describe "Test CustomizedRulePath" {
Context "When used from settings file" {
It "Should process relative settings path" {
try {
$initialLocation = Get-Location
Set-Location $PSScriptRoot
Push-Location $PSScriptRoot
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings .\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1
$warnings.Count | Should -Be 1
}
finally {
Set-Location $initialLocation
Pop-Location
}
}

It "Should process relative settings path even when settings path object is not resolved to a string yet" {
try {
Push-Location $PSScriptRoot
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings (Join-Path (Get-Location).Path '.\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1')
$warnings.Count | Should -Be 1
}
finally {
Pop-Location
}
}

Expand Down