Skip to content

Commit cc68a2e

Browse files
author
Kapil Borle
committed
Refactor MissingModuleManifest Rule
Moves the module manifest testing logic to a separate method in the Helper class. This will enable reuse of the functionality by other rules.
1 parent 567b1ae commit cc68a2e

2 files changed

Lines changed: 58 additions & 22 deletions

File tree

Engine/Helper.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,58 @@ public bool IsDscResourceModule(string filePath)
228228

229229
return false;
230230
}
231+
232+
/// <summary>
233+
/// Gets the module manifest
234+
/// </summary>
235+
/// <param name="filePath"></param>
236+
/// <param name="errorRecord"></param>
237+
/// <returns>Returns a object of type PSModuleInfo</returns>
238+
public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorRecord> errorRecord)
239+
{
240+
errorRecord = null;
241+
PSModuleInfo psModuleInfo = null;
242+
Collection<PSObject> psObj = null;
243+
var ps = System.Management.Automation.PowerShell.Create();
244+
if (ps == null)
245+
{
246+
return null;
247+
}
248+
try
249+
{
250+
ps.AddCommand("Test-ModuleManifest");
251+
ps.AddParameter("Path", filePath);
252+
253+
// Suppress warnings emitted during the execution of Test-ModuleManifest
254+
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
255+
psObj = ps.Invoke();
256+
}
257+
catch { }
258+
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
259+
{
260+
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
261+
ps.Streams.Error.CopyTo(errorRecordArr, 0);
262+
errorRecord = errorRecordArr;
263+
}
264+
if (psObj != null && psObj.Any() && psObj[0] != null)
265+
{
266+
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
267+
}
268+
ps.Dispose();
269+
return psModuleInfo;
270+
}
271+
272+
/// <summary>
273+
/// Checks if the error record is MissingMemberException
274+
/// </summary>
275+
/// <param name="errorRecord"></param>
276+
/// <returns>Returns a boolean value indicating the presence of MissingMemberException</returns>
277+
public bool IsMissingMemberException(ErrorRecord errorRecord)
278+
{
279+
return errorRecord.CategoryInfo != null
280+
&& errorRecord.CategoryInfo.Category == ErrorCategory.ResourceUnavailable
281+
&& string.Equals("MissingMemberException", errorRecord.CategoryInfo.Reason, StringComparison.OrdinalIgnoreCase);
282+
}
231283

232284
/// <summary>
233285
/// Get the list of exported function by analyzing the ast

Rules/MissingModuleManifestField.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,25 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3838

3939
if (String.Equals(System.IO.Path.GetExtension(fileName), ".psd1", StringComparison.OrdinalIgnoreCase))
4040
{
41-
var ps = System.Management.Automation.PowerShell.Create();
42-
43-
try
44-
{
45-
ps.AddCommand("Test-ModuleManifest");
46-
ps.AddParameter("Path", fileName);
47-
48-
// Suppress warnings emitted during the execution of Test-ModuleManifest
49-
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
50-
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
51-
ps.Invoke();
52-
53-
} catch { }
54-
55-
if (ps != null && ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
41+
IEnumerable<ErrorRecord> errorRecords;
42+
var psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecords);
43+
if (errorRecords != null)
5644
{
57-
foreach (var errorRecord in ps.Streams.Error)
45+
foreach (var errorRecord in errorRecords)
5846
{
59-
if (errorRecord.CategoryInfo != null && errorRecord.CategoryInfo.Category == System.Management.Automation.ErrorCategory.ResourceUnavailable
60-
&& String.Equals("MissingMemberException", errorRecord.CategoryInfo.Reason, StringComparison.OrdinalIgnoreCase))
47+
if (Helper.Instance.IsMissingMemberException(errorRecord))
6148
{
6249
System.Diagnostics.Debug.Assert(errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message), Strings.NullErrorMessage);
63-
6450
yield return
6551
new DiagnosticRecord(errorRecord.Exception.Message, ast.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
6652
}
6753

6854
}
6955
}
70-
71-
ps.Dispose();
7256
}
7357

7458
}
75-
59+
7660
/// <summary>
7761
/// GetName: Retrieves the name of this rule.
7862
/// </summary>

0 commit comments

Comments
 (0)