Skip to content

Commit b16bad7

Browse files
Revert "Annotatively research current state of exception handling surrounding settings file load invocation"
This reverts commit 3bfd45b.
1 parent cb04937 commit b16bad7

File tree

3 files changed

+1
-38
lines changed

3 files changed

+1
-38
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ protected override void BeginProcessing()
269269
Helper.Instance = new Helper(
270270
SessionState.InvokeCommand,
271271
this);
272-
// NOTE Helper.Instance.Initialize() does *not* modify this.settings.
273272
Helper.Instance.Initialize();
274273

275274
var psVersionTable = this.SessionState.PSVariable.GetValue("PSVersionTable") as Hashtable;
@@ -278,7 +277,6 @@ protected override void BeginProcessing()
278277
Helper.Instance.SetPSVersionTable(psVersionTable);
279278
}
280279

281-
// NOTE Helper.ProcessCustomRulePaths does *not* modify this.settings.
282280
string[] rulePaths = Helper.ProcessCustomRulePaths(
283281
customRulePath,
284282
this.SessionState,
@@ -287,7 +285,6 @@ protected override void BeginProcessing()
287285
if (IsFileParameterSet() && Path != null)
288286
{
289287
// just used to obtain the directory to use to find settings below
290-
// NOTE ProcessPath() does *not* modify this.settings.
291288
ProcessPath();
292289
}
293290

@@ -296,24 +293,17 @@ protected override void BeginProcessing()
296293
var combIncludeDefaultRules = IncludeDefaultRules.IsPresent;
297294
try
298295
{
299-
// THROW PSSASettings.Create(object, string, IOutputWriter, GetResolvedProviderPathFromPSPath) throws if ...
300-
// NOTE Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.Create(...) types the input, gets the input (if necessary), and parses it (if any).
301296
var settingsObj = PSSASettings.Create(
302-
// NOTHROW A null this.settings results in returning an "empty" (but not null) settingsObj without exception.
303-
// NOTE this.settings is unmodified since start of BeginProcessing(). Thus, it is exactly the raw argument value, if any.
304297
settings,
305298
processedPaths == null || processedPaths.Count == 0 ? CurrentProviderLocation("FileSystem").ProviderPath : processedPaths[0],
306299
this,
307300
GetResolvedProviderPathFromPSPath);
308-
// NOTE settingsObj cannot be null here since PSSASettings.Create(...) returns exactly `new Settings(settingsFound)`, which can never be null (but can throw).
309301
if (settingsObj != null)
310302
{
311-
// NOTHROW UpdateSettings(object) can throw an ArgumentNullException, but that will never happen since settingsObj is tested for non-nullity immediately above.
312303
ScriptAnalyzer.Instance.UpdateSettings(settingsObj);
313304

314305
// For includeDefaultRules and RecurseCustomRulePath we override the value in the settings file by
315306
// command line argument.
316-
// NOTHROW InvokeScriptAnalyzerCommand.OverrideSwitchParam(bool, string)
317307
combRecurseCustomRulePath = OverrideSwitchParam(
318308
settingsObj.RecurseCustomRulePath,
319309
"RecurseCustomRulePath");
@@ -326,7 +316,6 @@ protected override void BeginProcessing()
326316
// simultaneously. But since, this was done before with IncludeRules, ExcludeRules and Severity,
327317
// we use the same strategy for CustomRulePath. So, we take the union of CustomRulePath provided in
328318
// the settings file and if provided on command line.
329-
// THROW Helper.ProcessCustomRulePaths(string[], SessionState, bool) throws one of six different exceptions if a settings' custom rule path is invalid somehow (e.g. drive doesn't exit; no wildcards but item doesn't exist; provider throws a lower-level exception; etc.). See the implementation of Helper.ProcessCustomRulePaths(string[], SessionState, bool) for details.
330319
var settingsCustomRulePath = Helper.ProcessCustomRulePaths(
331320
settingsObj?.CustomRulePath?.ToArray(),
332321
this.SessionState,
@@ -339,7 +328,6 @@ protected override void BeginProcessing()
339328
}
340329
catch (Exception e)
341330
{
342-
// NOTE Any exception in resolving, getting, parsing, updating, etc. the settings herein results in an contextless WriteWarning(Strings.SettingsNotParsable), regardless of provenance.
343331
string errorId;
344332
ErrorCategory errorCategory;
345333
switch (e)

Engine/Helper.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,12 +1506,6 @@ public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState s
15061506
Collection<PathInfo> pathInfo = new Collection<PathInfo>();
15071507
foreach (string rulePath in rulePaths)
15081508
{
1509-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.Management.Automation.ProviderNotFoundException] if path is a provider-qualified path and the specified provider does not exist.
1510-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.Management.Automation.DriveNotFoundException] if path is a drive-qualified path and the specified drive does not exist.
1511-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.Management.Automation.ProviderInvocationException] if the provider throws an exception when its MakePath gets called.
1512-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.NotSupportedException] if the provider does not support multiple items.
1513-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.InvalidOperationException] the home location for the provider is not set and path starts with a "~".
1514-
// THROW PathIntrinsics.GetResolvedPSPathFromPSPath(rulePath) throws [System.Management.Automation.ItemNotFoundException] if path does not contain wildcard characters and could not be found.
15151509
Collection<PathInfo> pathInfosForRulePath = sessionState.Path.GetResolvedPSPathFromPSPath(rulePath);
15161510
if (null != pathInfosForRulePath)
15171511
{

Engine/Settings.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ public Settings(object settings, Func<string, string> presetResolver)
7373
if (File.Exists(settingsFilePath))
7474
{
7575
filePath = settingsFilePath;
76-
// QUESTION When does parseSettingsFile(string) throw?
7776
parseSettingsFile(settingsFilePath);
7877
}
7978
else
8079
{
81-
// THROW ArgumentException(Strings.InvalidPath) if the resolution of the settings argument via GetResolvedProviderPathFromPSPathDelegate is non-null but not an existing file. (e.g. it may be a directory)
8280
throw new ArgumentException(
8381
String.Format(
8482
CultureInfo.CurrentCulture,
@@ -91,12 +89,10 @@ public Settings(object settings, Func<string, string> presetResolver)
9189
var settingsHashtable = settings as Hashtable;
9290
if (settingsHashtable != null)
9391
{
94-
// QUESTION When does parseSettingsHashtable(Hashtable) throw?
9592
parseSettingsHashtable(settingsHashtable);
9693
}
9794
else
9895
{
99-
// THROW ArgumentException(Strings.SettingsInvalidType) if settings is not convertible (`as` keyword) to Hashtable.
10096
throw new ArgumentException(Strings.SettingsInvalidType);
10197
}
10298
}
@@ -183,12 +179,11 @@ public static string GetSettingPresetFilePath(string settingPreset)
183179
/// <param name="outputWriter">An output writer.</param>
184180
/// <param name="getResolvedProviderPathFromPSPathDelegate">The GetResolvedProviderPathFromPSPath method from PSCmdlet to resolve relative path including wildcard support.</param>
185181
/// <returns>An object of Settings type.</returns>
186-
// THROW Settings.Create(object, string, IOutputWriter, GetResolvedProviderPathFromPSPath) throws only because of the contained invocation to the Settings constructor, which does throw.
187182
internal static Settings Create(object settingsObj, string cwd, IOutputWriter outputWriter,
188183
PathResolver.GetResolvedProviderPathFromPSPath<string, ProviderInfo, Collection<string>> getResolvedProviderPathFromPSPathDelegate)
189184
{
190185
object settingsFound;
191-
var settingsMode = FindSettingsMode(settingsObj, cwd, out settingsFound); // NOTHROW
186+
var settingsMode = FindSettingsMode(settingsObj, cwd, out settingsFound);
192187

193188
switch (settingsMode)
194189
{
@@ -210,7 +205,6 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou
210205
var userProvidedSettingsString = settingsFound.ToString();
211206
try
212207
{
213-
// THROW ..Single() throws [System.InvalidOperationException] if the settings path does not resolve to exactly one item. (more or less)
214208
var resolvedPath = getResolvedProviderPathFromPSPathDelegate(userProvidedSettingsString, out ProviderInfo providerInfo).Single();
215209
settingsFound = resolvedPath;
216210
outputWriter?.WriteVerbose(
@@ -219,11 +213,8 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou
219213
Strings.SettingsUsingFile,
220214
resolvedPath));
221215
}
222-
// NOTE This catch block effectively makes *any* exception resulting from resolving the settings file path reduce to the case of no settings (by way of null settingsFound). Only a WriteVerbose(Strings.SettingsCannotFindFile) identifies what happened.
223216
catch
224217
{
225-
// TODO Upgrade WriteVerbose(Strings.SettingsCannotFindFile) to WriteWarning(Strings.SettingsCannotFindFile).
226-
// TODO Perform a ShouldContinue (?) confirmation check in the event that an exception occurred while resolving the settings file path.
227218
outputWriter?.WriteVerbose(
228219
String.Format(
229220
CultureInfo.CurrentCulture,
@@ -247,7 +238,6 @@ internal static Settings Create(object settingsObj, string cwd, IOutputWriter ou
247238
return null;
248239
}
249240

250-
// THROW new Settings(object)
251241
return new Settings(settingsFound);
252242
}
253243

@@ -466,19 +456,16 @@ private void parseSettingsHashtable(Hashtable settingsHashtable)
466456
}
467457
}
468458

469-
// NOTE parseSettingsFile concludes by calling parseSettingsHashtable if it (parseSettingsFile) is successful.
470459
private void parseSettingsFile(string settingsFilePath)
471460
{
472461
Token[] parserTokens = null;
473462
ParseError[] parserErrors = null;
474-
// QUESTION Can Parser.ParseFile throw?
475463
Ast profileAst = Parser.ParseFile(settingsFilePath, out parserTokens, out parserErrors);
476464
IEnumerable<Ast> hashTableAsts = profileAst.FindAll(item => item is HashtableAst, false);
477465

478466
// no hashtable, raise warning
479467
if (hashTableAsts.Count() == 0)
480468
{
481-
// THROW parseSettingsFile throws ArgumentException(Strings.InvalidProfile) if no hashTableAst is detected in settings file.
482469
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, settingsFilePath));
483470
}
484471

@@ -488,26 +475,22 @@ private void parseSettingsFile(string settingsFilePath)
488475
{
489476
// ideally we should use HashtableAst.SafeGetValue() but since
490477
// it is not available on PSv3, we resort to our own narrow implementation.
491-
// QUESTION When does GetSafeValueFromHashtableAst(hashTableAst) throw?
492478
hashtable = GetSafeValueFromHashtableAst(hashTableAst);
493479
}
494480
catch (InvalidOperationException e)
495481
{
496-
// THROW parseSettingsFile throws ArgumentException(Strings.InvalidProfile) if GetSafeValueFromHashtableAst(hashTableAst) throws an InvalidOperationException.
497482
throw new ArgumentException(Strings.InvalidProfile, e);
498483
}
499484

500485
if (hashtable == null)
501486
{
502-
// THROW parseSettingsFile throws ArgumentException if GetSafeValueFromHashtableAst(hashTableAst) returns null.
503487
throw new ArgumentException(
504488
String.Format(
505489
CultureInfo.CurrentCulture,
506490
Strings.InvalidProfile,
507491
settingsFilePath));
508492
}
509493

510-
// QUESTION When does parseSettingsHashtable(Hashtable) throw?
511494
parseSettingsHashtable(hashtable);
512495
}
513496

@@ -703,7 +686,6 @@ private static bool IsBuiltinSettingPreset(object settingPreset)
703686
return false;
704687
}
705688

706-
// NOTHROW FindSettingsMode(object, string, out object)
707689
internal static SettingsMode FindSettingsMode(object settings, string path, out object settingsFound)
708690
{
709691
var settingsMode = SettingsMode.None;
@@ -724,7 +706,6 @@ internal static SettingsMode FindSettingsMode(object settings, string path, out
724706
{
725707
// if settings are not provided explicitly, look for it in the given path
726708
// check if pssasettings.psd1 exists
727-
// COMBAK Refactor the magic string literal "PSScriptAnalyzerSettings.psd1" to a public const field on the class. (bare minimum... although will also help open possibility for user-configurable default name)
728709
var settingsFilename = "PSScriptAnalyzerSettings.psd1";
729710
var settingsFilePath = Path.Combine(directory, settingsFilename);
730711
settingsFound = settingsFilePath;

0 commit comments

Comments
 (0)