Skip to content

Commit dff7c8d

Browse files
author
Quoc Truong
committed
Merge BugFixes into development
2 parents 9a972fe + 62ba3c0 commit dff7c8d

File tree

133 files changed

+2304
-661
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+2304
-661
lines changed

Engine/Commands/GetScriptAnalyzerLoggerCommand.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
214
using System;
315
using System.Collections.Generic;
416
using System.ComponentModel.Composition;

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using Microsoft.PowerShell.Commands;
14+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
215
using System;
316
using System.Collections.Generic;
4-
using System.ComponentModel.Composition;
517
using System.Diagnostics.CodeAnalysis;
618
using System.Globalization;
719
using System.Linq;
820
using System.Management.Automation;
9-
using System.Resources;
10-
using System.Threading;
11-
using System.Reflection;
1221

1322
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
1423
{
@@ -44,6 +53,21 @@ public string[] Name
4453
set { name = value; }
4554
}
4655
private string[] name;
56+
57+
/// <summary>
58+
/// Severity: Array of the severity types to be enabled.
59+
/// </summary>
60+
/// </summary>
61+
[ValidateSet("Warning", "Error", "Information", IgnoreCase = true)]
62+
[Parameter(Mandatory = false)]
63+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
64+
public string[] Severity
65+
{
66+
get { return severity; }
67+
set { severity = value; }
68+
}
69+
private string[] severity;
70+
4771
#endregion Parameters
4872

4973
#region Private Members
@@ -116,9 +140,16 @@ protected override void ProcessRecord()
116140
}
117141
else
118142
{
143+
if (severity != null)
144+
{
145+
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item));
146+
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
147+
}
148+
119149
foreach (IRule rule in rules)
120150
{
121-
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName()));
151+
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
152+
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity()));
122153
}
123154
}
124155
}

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 118 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System.Text.RegularExpressions;
14+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
215
using System;
316
using System.Collections.Generic;
417
using System.ComponentModel.Composition;
@@ -102,6 +115,7 @@ public SwitchParameter Recurse
102115
/// <summary>
103116
/// ShowSuppressed: Show the suppressed message
104117
/// </summary>
118+
[Parameter(Mandatory = false)]
105119
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
106120
public SwitchParameter SuppressedOnly
107121
{
@@ -270,7 +284,28 @@ private void AnalyzeFile(string filePath)
270284
List<SuppressedRecord> suppressed = new List<SuppressedRecord>();
271285

272286
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
273-
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();
287+
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();
288+
289+
//Check wild card input for the Include/ExcludeRules and create regex match patterns
290+
List<Regex> includeRegexList = new List<Regex>();
291+
List<Regex> excludeRegexList = new List<Regex>();
292+
if (includeRule != null)
293+
{
294+
foreach (string rule in includeRule)
295+
{
296+
Regex includeRegex = new Regex(String.Format("^{0}$", Regex.Escape(rule).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
297+
includeRegexList.Add(includeRegex);
298+
}
299+
}
300+
if (excludeRule != null)
301+
{
302+
foreach (string rule in excludeRule)
303+
{
304+
Regex excludeRegex = new Regex(String.Format("^{0}$", Regex.Escape(rule).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
305+
excludeRegexList.Add(excludeRegex);
306+
}
307+
}
308+
274309

275310
//Parse the file
276311
if (File.Exists(filePath))
@@ -327,12 +362,32 @@ private void AnalyzeFile(string filePath)
327362
#region Run ScriptRules
328363
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
329364
string fileName = System.IO.Path.GetFileName(filePath);
365+
330366
if (ScriptAnalyzer.Instance.ScriptRules != null)
331367
{
332368
foreach (IScriptRule scriptRule in ScriptAnalyzer.Instance.ScriptRules)
333369
{
334-
if ((includeRule == null || includeRule.Contains(scriptRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
335-
(excludeRule == null || !excludeRule.Contains(scriptRule.GetName(), StringComparer.OrdinalIgnoreCase)))
370+
bool includeRegexMatch = false;
371+
bool excludeRegexMatch = false;
372+
foreach (Regex include in includeRegexList)
373+
{
374+
if (include.IsMatch(scriptRule.GetName()))
375+
{
376+
includeRegexMatch = true;
377+
break;
378+
}
379+
}
380+
381+
foreach (Regex exclude in excludeRegexList)
382+
{
383+
if (exclude.IsMatch(scriptRule.GetName()))
384+
{
385+
excludeRegexMatch = true;
386+
break;
387+
}
388+
}
389+
390+
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
336391
{
337392
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, scriptRule.GetName()));
338393

@@ -347,7 +402,6 @@ private void AnalyzeFile(string filePath)
347402
catch (Exception scriptRuleException)
348403
{
349404
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
350-
continue;
351405
}
352406
}
353407
}
@@ -361,8 +415,25 @@ private void AnalyzeFile(string filePath)
361415
{
362416
foreach (ITokenRule tokenRule in ScriptAnalyzer.Instance.TokenRules)
363417
{
364-
if ((includeRule == null || includeRule.Contains(tokenRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
365-
(excludeRule == null || !excludeRule.Contains(tokenRule.GetName(), StringComparer.OrdinalIgnoreCase)))
418+
bool includeRegexMatch = false;
419+
bool excludeRegexMatch = false;
420+
foreach (Regex include in includeRegexList)
421+
{
422+
if (include.IsMatch(tokenRule.GetName()))
423+
{
424+
includeRegexMatch = true;
425+
break;
426+
}
427+
}
428+
foreach (Regex exclude in excludeRegexList)
429+
{
430+
if (exclude.IsMatch(tokenRule.GetName()))
431+
{
432+
excludeRegexMatch = true;
433+
break;
434+
}
435+
}
436+
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
366437
{
367438
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, tokenRule.GetName()));
368439

@@ -377,7 +448,6 @@ private void AnalyzeFile(string filePath)
377448
catch (Exception tokenRuleException)
378449
{
379450
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
380-
continue;
381451
}
382452
}
383453
}
@@ -391,8 +461,28 @@ private void AnalyzeFile(string filePath)
391461
// Run DSC Class rule
392462
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
393463
{
394-
if ((includeRule == null || includeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
395-
(excludeRule == null || !excludeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)))
464+
bool includeRegexMatch = false;
465+
bool excludeRegexMatch = false;
466+
467+
foreach (Regex include in includeRegexList)
468+
{
469+
if (include.IsMatch(dscResourceRule.GetName()))
470+
{
471+
includeRegexMatch = true;
472+
break;
473+
}
474+
}
475+
476+
foreach (Regex exclude in excludeRegexList)
477+
{
478+
if (exclude.IsMatch(dscResourceRule.GetName()))
479+
{
480+
excludeRegexMatch = true;
481+
break;
482+
}
483+
}
484+
485+
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || excludeRegexMatch))
396486
{
397487
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));
398488

@@ -407,7 +497,6 @@ private void AnalyzeFile(string filePath)
407497
catch (Exception dscResourceRuleException)
408498
{
409499
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
410-
continue;
411500
}
412501
}
413502
}
@@ -435,8 +524,24 @@ private void AnalyzeFile(string filePath)
435524
// Run all DSC Rules
436525
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
437526
{
438-
if ((includeRule == null || includeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
439-
(excludeRule == null || !excludeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)))
527+
bool includeRegexMatch = false;
528+
bool excludeRegexMatch = false;
529+
foreach (Regex include in includeRegexList)
530+
{
531+
if (include.IsMatch(dscResourceRule.GetName()))
532+
{
533+
includeRegexMatch = true;
534+
break;
535+
}
536+
}
537+
foreach (Regex exclude in excludeRegexList)
538+
{
539+
if (exclude.IsMatch(dscResourceRule.GetName()))
540+
{
541+
excludeRegexMatch = true;
542+
}
543+
}
544+
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
440545
{
441546
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));
442547

@@ -451,7 +556,6 @@ private void AnalyzeFile(string filePath)
451556
catch (Exception dscResourceRuleException)
452557
{
453558
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
454-
continue;
455559
}
456560
}
457561
}

Engine/Generic/AvoidCmdletGeneric.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
using System;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System;
214
using System.Collections.Generic;
315
using System.Linq;
416
using System.Text;
@@ -81,5 +93,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8193
/// </summary>
8294
/// <returns>The source type of the rule.</returns>
8395
public abstract SourceType GetSourceType();
96+
97+
/// <summary>
98+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
99+
/// </summary>
100+
/// <returns></returns>
101+
public abstract RuleSeverity GetSeverity();
84102
}
85103
}

Engine/Generic/AvoidParameterGeneric.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
using System;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System;
214
using System.Collections.Generic;
315
using System.Linq;
416
using System.Text;
@@ -92,5 +104,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
92104
/// </summary>
93105
/// <returns>The source type of the rule.</returns>
94106
public abstract SourceType GetSourceType();
107+
108+
public abstract RuleSeverity GetSeverity();
95109
}
96110
}

Engine/Generic/DiagnosticRecord.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
using System;
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System;
214
using System.Collections.Generic;
315
using System.Linq;
416
using System.Text;

0 commit comments

Comments
 (0)