Skip to content

Commit bc5b2dd

Browse files
bergmeisterJamesWTruher
authored andcommitted
Fix parsing the -Settings object as a path when the path object originates from an expression (#915)
* Fix parsing the -Settings object as a path when the path is an expression that is not fully evaluated yet. * address PR comments, improve verbose logging and tidy up * Improve check for file type by trying to cast to PSObject and checking its BaseObject property.
1 parent 6421cc9 commit bc5b2dd

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

Engine/Settings.cs

+33-16
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,25 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou
202202

203203
case SettingsMode.Preset:
204204
case SettingsMode.File:
205-
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(settingsFound.ToString(), out ProviderInfo providerInfo).Single();
206-
settingsFound = resolvedPath;
207-
outputWriter?.WriteVerbose(
208-
String.Format(
209-
CultureInfo.CurrentCulture,
210-
Strings.SettingsUsingFile,
211-
resolvedPath));
205+
var userProvidedSettingsString = settingsFound.ToString();
206+
try
207+
{
208+
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(userProvidedSettingsString, out ProviderInfo providerInfo).Single();
209+
settingsFound = resolvedPath;
210+
outputWriter?.WriteVerbose(
211+
String.Format(
212+
CultureInfo.CurrentCulture,
213+
Strings.SettingsUsingFile,
214+
resolvedPath));
215+
}
216+
catch
217+
{
218+
outputWriter?.WriteVerbose(
219+
String.Format(
220+
CultureInfo.CurrentCulture,
221+
Strings.SettingsCannotFindFile,
222+
userProvidedSettingsString));
223+
}
212224
break;
213225

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

221-
default: // case SettingsMode.None
233+
default:
222234
outputWriter?.WriteVerbose(
223235
String.Format(
224236
CultureInfo.CurrentCulture,
225-
Strings.SettingsCannotFindFile));
226-
break;
227-
}
228-
229-
if (settingsMode != SettingsMode.None)
230-
{
231-
return new Settings(settingsFound);
237+
Strings.SettingsObjectCouldNotBResolved));
238+
return null;
232239
}
233240

234-
return null;
241+
return new Settings(settingsFound);
235242
}
236243

237244
/// <summary>
@@ -703,6 +710,16 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
703710
{
704711
settingsMode = SettingsMode.Hashtable;
705712
}
713+
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
714+
{
715+
if (settingsFound is PSObject settingsFoundPSObject)
716+
{
717+
if (settingsFoundPSObject.BaseObject is String)
718+
{
719+
settingsMode = SettingsMode.File;
720+
}
721+
}
722+
}
706723
}
707724
}
708725

Engine/Strings.Designer.cs

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Engine/Strings.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
<value>Using settings hashtable.</value>
269269
</data>
270270
<data name="SettingsCannotFindFile" xml:space="preserve">
271-
<value>Cannot find a settings file.</value>
271+
<value>Cannot resolve settings file path '{0}'.</value>
272272
</data>
273273
<data name="SettingsNotParsable" xml:space="preserve">
274274
<value>Cannot parse settings. Will abort the invocation.</value>
@@ -321,4 +321,7 @@
321321
<data name="PositionRefPosLessThanInputPos" xml:space="preserve">
322322
<value>Input position should be less than that of the invoking object.</value>
323323
</data>
324+
<data name="SettingsObjectCouldNotBResolved" xml:space="preserve">
325+
<value>Settings object could not be resolved.</value>
326+
</data>
324327
</root>

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

+13-3
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,23 @@ Describe "Test CustomizedRulePath" {
387387
Context "When used from settings file" {
388388
It "Should process relative settings path" {
389389
try {
390-
$initialLocation = Get-Location
391-
Set-Location $PSScriptRoot
390+
Push-Location $PSScriptRoot
392391
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings .\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1
393392
$warnings.Count | Should -Be 1
394393
}
395394
finally {
396-
Set-Location $initialLocation
395+
Pop-Location
396+
}
397+
}
398+
399+
It "Should process relative settings path even when settings path object is not resolved to a string yet" {
400+
try {
401+
Push-Location $PSScriptRoot
402+
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings (Join-Path (Get-Location).Path '.\SettingsTest\..\SettingsTest\Project1\PSScriptAnalyzerSettings.psd1')
403+
$warnings.Count | Should -Be 1
404+
}
405+
finally {
406+
Pop-Location
397407
}
398408
}
399409

0 commit comments

Comments
 (0)