Skip to content

Ensure correct disposal with using statement #1164

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 1 commit into from
Mar 5, 2019
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
51 changes: 26 additions & 25 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,36 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
errorRecord = null;
PSModuleInfo psModuleInfo = null;
Collection<PSObject> psObj = null;
var ps = System.Management.Automation.PowerShell.Create();
try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", filePath);
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
using (var ps = System.Management.Automation.PowerShell.Create())
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
try
{
throw;
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", filePath);
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
{
throw;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
ps.Dispose();
return psModuleInfo;
}

Expand Down
106 changes: 53 additions & 53 deletions Rules/AvoidUsingDeprecatedManifestFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,83 +40,83 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
}
if (Helper.IsModuleManifest(fileName))
{
var ps = System.Management.Automation.PowerShell.Create();
IEnumerable<PSObject> result = null;

// hash table in psd1
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();

// no hash table means not a module manifest
if (hashTableAst == null)
using (var ps = System.Management.Automation.PowerShell.Create())
{
yield break;
}
IEnumerable<PSObject> result = null;

var table = hashTableAst as HashtableAst;
// hash table in psd1
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();

// needs to find the PowerShellVersion key
foreach (var kvp in table.KeyValuePairs)
{
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
// no hash table means not a module manifest
if (hashTableAst == null)
{
var key = (kvp.Item1 as StringConstantExpressionAst).Value;
yield break;
}

// find the powershellversion key in the hashtable
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
var table = hashTableAst as HashtableAst;

// needs to find the PowerShellVersion key
foreach (var kvp in table.KeyValuePairs)
{
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
{
// get the string value of the version
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);
var key = (kvp.Item1 as StringConstantExpressionAst).Value;

if (value != null)
// find the powershellversion key in the hashtable
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
{
Version psVersion = null;
// get the string value of the version
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
if (value != null)
{
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
Version psVersion = null;

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
{
yield break;
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
{
yield break;
}
}
}
}
}
}
}

try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", fileName);

// Suppress warnings emitted during the execution of Test-ModuleManifest
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();
}
catch
{}
try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", fileName);

// Suppress warnings emitted during the execution of Test-ModuleManifest
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();
}
catch
{}

if (result != null)
{
foreach (var warning in result)
if (result != null)
{
if (warning.BaseObject != null)
foreach (var warning in result)
{
yield return
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
GetName(), DiagnosticSeverity.Warning, fileName);
if (warning.BaseObject != null)
{
yield return
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}

ps.Dispose();
}
}

}

/// <summary>
Expand Down