Skip to content

Throw error instead of warning for profiles #392

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 2 commits into from
Dec 2, 2015
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
14 changes: 14 additions & 0 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public string Configuration
}
private string configuration;

private bool stopProcessing;

#endregion Parameters

#region Overrides
Expand All @@ -181,6 +183,12 @@ protected override void BeginProcessing()
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
this.SessionState, recurseCustomRulePath);

if (!ScriptAnalyzer.Instance.ParseProfile(this.configuration, this.SessionState.Path, this))
{
stopProcessing = true;
return;
}

ScriptAnalyzer.Instance.Initialize(
this,
rulePaths,
Expand All @@ -196,6 +204,12 @@ protected override void BeginProcessing()
/// </summary>
protected override void ProcessRecord()
{
if (stopProcessing)
{
stopProcessing = false;
return;
}

if (String.Equals(this.ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
{
// throws Item Not Found Exception
Expand Down
153 changes: 82 additions & 71 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,49 +151,25 @@ public void Initialize(
profile);
}

private void Initialize(
IOutputWriter outputWriter,
PathIntrinsics path,
CommandInvocationIntrinsics invokeCommand,
string[] customizedRulePath,
string[] includeRuleNames,
string[] excludeRuleNames,
string[] severity,
bool suppressedOnly = false,
string profile = null)
internal bool ParseProfile(string profile, PathIntrinsics path, IOutputWriter writer)
{
if (outputWriter == null)
{
throw new ArgumentNullException("outputWriter");
}

this.outputWriter = outputWriter;

#region Verifies rule extensions and loggers path

List<string> paths = this.GetValidCustomRulePaths(customizedRulePath, path);
IEnumerable<string> includeRuleList = new List<string>();
IEnumerable<string> excludeRuleList = new List<string>();
IEnumerable<string> severityList = new List<string>();

#endregion

#region Initializes Rules

this.severity = severity;
this.suppressedOnly = suppressedOnly;
this.includeRule = includeRuleNames;
this.excludeRule = excludeRuleNames;
this.includeRegexList = new List<Regex>();
this.excludeRegexList = new List<Regex>();
bool hasError = false;

#region ParseProfile
if (!String.IsNullOrWhiteSpace(profile))
{
try
{
{
profile = path.GetResolvedPSPathFromPSPath(profile).First().Path;
}
catch
{
this.outputWriter.WriteWarning(string.Format(CultureInfo.CurrentCulture, Strings.FileNotFound, profile));
writer.WriteError(new ErrorRecord(new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Strings.FileNotFound, profile)),
Strings.ConfigurationFileNotFound, ErrorCategory.ResourceUnavailable, profile));
hasError = true;
}

if (File.Exists(profile))
Expand All @@ -206,7 +182,9 @@ private void Initialize(
// no hashtable, raise warning
if (hashTableAsts.Count() == 0)
{
this.outputWriter.WriteWarning(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, profile));
writer.WriteError(new ErrorRecord(new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, profile)),
Strings.ConfigurationFileHasNoHashTable, ErrorCategory.ResourceUnavailable, profile));
hasError = true;
}
else
{
Expand All @@ -217,8 +195,9 @@ private void Initialize(
if (!(kvp.Item1 is StringConstantExpressionAst))
{
// first item (the key) should be a string
this.outputWriter.WriteWarning(
string.Format(CultureInfo.CurrentCulture, Strings.WrongKeyFormat, kvp.Item1.Extent.StartLineNumber, kvp.Item1.Extent.StartColumnNumber, profile));
writer.WriteError(new ErrorRecord(new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongKeyFormat, kvp.Item1.Extent.StartLineNumber, kvp.Item1.Extent.StartColumnNumber, profile)),
Strings.ConfigurationKeyNotAString, ErrorCategory.InvalidData, profile));
hasError = true;
continue;
}

Expand All @@ -241,10 +220,10 @@ private void Initialize(
// Statements property is never null
if (arrayExp.SubExpression != null)
{
StatementAst stateAst = arrayExp.SubExpression.Statements.First();
StatementAst stateAst = arrayExp.SubExpression.Statements.FirstOrDefault();
if (stateAst != null && stateAst is PipelineAst)
{
CommandBaseAst cmdBaseAst = (stateAst as PipelineAst).PipelineElements.First();
CommandBaseAst cmdBaseAst = (stateAst as PipelineAst).PipelineElements.FirstOrDefault();
if (cmdBaseAst != null && cmdBaseAst is CommandExpressionAst)
{
CommandExpressionAst cmdExpAst = cmdBaseAst as CommandExpressionAst;
Expand All @@ -268,8 +247,9 @@ private void Initialize(
// all the values in the array needs to be string
if (!(element is StringConstantExpressionAst))
{
this.outputWriter.WriteWarning(
string.Format(CultureInfo.CurrentCulture, Strings.WrongValueFormat, element.Extent.StartLineNumber, element.Extent.StartColumnNumber, profile));
writer.WriteError(new ErrorRecord(new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueFormat, element.Extent.StartLineNumber, element.Extent.StartColumnNumber, profile)),
Strings.ConfigurationValueNotAString, ErrorCategory.InvalidData, profile));
hasError = true;
continue;
}

Expand All @@ -281,55 +261,82 @@ private void Initialize(

if (rhsList.Count == 0)
{
this.outputWriter.WriteWarning(
string.Format(CultureInfo.CurrentCulture, Strings.WrongValueFormat, kvp.Item2.Extent.StartLineNumber, kvp.Item2.Extent.StartColumnNumber, profile));
break;
writer.WriteError(new ErrorRecord(new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueFormat, kvp.Item2.Extent.StartLineNumber, kvp.Item2.Extent.StartColumnNumber, profile)),
Strings.ConfigurationValueWrongFormat, ErrorCategory.InvalidData, profile));
hasError = true;
continue;
}

switch ((kvp.Item1 as StringConstantExpressionAst).Value.ToLower())
string key = (kvp.Item1 as StringConstantExpressionAst).Value.ToLower();

switch (key)
{
case "severity":
if (this.severity == null)
{
this.severity = rhsList.ToArray();
}
else
{
this.severity = this.severity.Union(rhsList).ToArray();
}
severityList = severityList.Union(rhsList);
break;
case "includerules":
if (this.includeRule == null)
{
this.includeRule = rhsList.ToArray();
}
else
{
this.includeRule = this.includeRule.Union(rhsList).ToArray();
}
includeRuleList = includeRuleList.Union(rhsList);
break;
case "excluderules":
if (this.excludeRule == null)
{
this.excludeRule = rhsList.ToArray();
}
else
{
this.excludeRule = this.excludeRule.Union(rhsList).ToArray();
}
excludeRuleList = excludeRuleList.Union(rhsList);
break;
default:
this.outputWriter.WriteWarning(
string.Format(CultureInfo.CurrentCulture, Strings.WrongKey, kvp.Item1.Extent.StartLineNumber, kvp.Item1.Extent.StartColumnNumber, profile));
writer.WriteError(new ErrorRecord(
new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongKey, key, kvp.Item1.Extent.StartLineNumber, kvp.Item1.Extent.StartColumnNumber, profile)),
Strings.WrongConfigurationKey, ErrorCategory.InvalidData, profile));
hasError = true;
break;
}
}
}
}
}

if (hasError)
{
return false;
}

this.severity = (severityList.Count() == 0) ? null : severityList.ToArray();
this.includeRule = (includeRuleList.Count() == 0) ? null : includeRuleList.ToArray();
this.excludeRule = (excludeRuleList.Count() == 0) ? null : excludeRuleList.ToArray();

return true;
}

private void Initialize(
IOutputWriter outputWriter,
PathIntrinsics path,
CommandInvocationIntrinsics invokeCommand,
string[] customizedRulePath,
string[] includeRuleNames,
string[] excludeRuleNames,
string[] severity,
bool suppressedOnly = false,
string profile = null)
{
if (outputWriter == null)
{
throw new ArgumentNullException("outputWriter");
}

this.outputWriter = outputWriter;

#region Verifies rule extensions and loggers path

List<string> paths = this.GetValidCustomRulePaths(customizedRulePath, path);

#endregion

#region Initializes Rules

this.suppressedOnly = suppressedOnly;
this.severity = this.severity == null ? severity : this.severity.Union(severity ?? new String[0]).ToArray();
this.includeRule = this.includeRule == null ? includeRuleNames : this.includeRule.Union(includeRuleNames ?? new String[0]).ToArray();
this.excludeRule = this.excludeRule == null ? excludeRuleNames : this.excludeRule.Union(excludeRuleNames ?? new String[0]).ToArray();
this.includeRegexList = new List<Regex>();
this.excludeRegexList = new List<Regex>();

//Check wild card input for the Include/ExcludeRules and create regex match patterns
if (this.includeRule != null)
{
Expand All @@ -339,6 +346,7 @@ private void Initialize(
this.includeRegexList.Add(includeRegex);
}
}

if (this.excludeRule != null)
{
foreach (string rule in excludeRule)
Expand Down Expand Up @@ -1446,7 +1454,10 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
if (severity != null)
{
var diagSeverity = severity.Select(item => Enum.Parse(typeof(DiagnosticSeverity), item, true));
diagnosticsList = diagnostics.Where(item => diagSeverity.Contains(item.Severity));
if (diagSeverity.Count() != 0)
{
diagnosticsList = diagnostics.Where(item => diagSeverity.Contains(item.Severity));
}
}

return this.suppressedOnly ?
Expand Down
56 changes: 55 additions & 1 deletion Engine/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion Engine/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
<value>Cannot find any DiagnosticRecord with the Rule Suppression ID {0}.</value>
</data>
<data name="WrongKey" xml:space="preserve">
<value>{0} is not a valid key in the profile hashtable: line {0} column {1} in file {2}</value>
<value>{0} is not a valid key in the profile hashtable: line {1} column {2} in file {3}. Valid keys are ExcludeRules, IncludeRules and Severity.</value>
</data>
<data name="WrongKeyFormat" xml:space="preserve">
<value>Key in the profile hashtable should be a string: line {0} column {1} in file {2}</value>
Expand All @@ -216,4 +216,22 @@
<data name="VerboseScriptDefinitionMessage" xml:space="preserve">
<value>Analyzing Script Definition.</value>
</data>
<data name="ConfigurationFileHasNoHashTable" xml:space="preserve">
<value>ConfigurationFileHasNoHashTable</value>
</data>
<data name="ConfigurationFileNotFound" xml:space="preserve">
<value>ConfigurationFileNotFound</value>
</data>
<data name="ConfigurationKeyNotAString" xml:space="preserve">
<value>ConfigurationKeyNotAString</value>
</data>
<data name="ConfigurationValueNotAString" xml:space="preserve">
<value>ConfigurationValueNotAString</value>
</data>
<data name="ConfigurationValueWrongFormat" xml:space="preserve">
<value>ConfigurationValueWrongFormat</value>
</data>
<data name="WrongConfigurationKey" xml:space="preserve">
<value>WrongConfigurationKey</value>
</data>
</root>
Loading