in DevSkim-DotNet/Microsoft.DevSkim.CLI/Tester.cs [132:208]
private bool TestFile(string fileName)
{
bool result = true;
// See if file name is a valid rule ID and preload default values
string defaultId = Path.GetFileNameWithoutExtension(fileName);
Rule? fileRule = _rules.Select(x => x.DevSkimRule).FirstOrDefault(x => x.Id == defaultId);
var languages = fileRule?.AppliesTo?.ToArray() ?? Array.Empty<string>(); ;
// Load file header and content
string fileHeader = string.Empty;
string fileContent = File.ReadAllText(fileName);
Regex reg = new Regex("^={3,}\\s+", RegexOptions.Multiline);
Match match = reg.Match(fileContent);
if (match.Success)
{
fileHeader = fileContent.Substring(0, match.Index);
fileContent = fileContent.Substring(match.Index + match.Length);
}
languages = GetLanguges(fileHeader, languages);
Dictionary<int, List<string>> expecations = GetExpectations(fileHeader, defaultId);
RuleProcessor processor = new RuleProcessor(_rules);
processor.EnableSuppressions = false;
processor.SeverityLevel = Severity.Critical | Severity.Important | Severity.Moderate | Severity.BestPractice | Severity.ManualReview;
Issue[] issues = processor.Analyze(fileContent, languages);
List<KeyValuePair<Location, string>> unexpected = new List<KeyValuePair<Location, string>>();
foreach (Issue issue in issues)
{
AddToCoverageList(issue.Rule.Id);
// if issue on this line was expected remove it from expecations
int line = issue.StartLocation.Line;
if (expecations.ContainsKey(line) && expecations[line].Contains(issue.Rule.Id))
{
expecations[line].Remove(issue.Rule.Id);
}
// otherwise add it to unexpected
else
{
unexpected.Add(new KeyValuePair<Location, string>(issue.StartLocation, issue.Rule.Id));
}
}
if (unexpected.Count > 0 || expecations.Any(x => x.Value.Count > 0))
{
result = false;
Console.Error.WriteLine("file:{0}", fileName);
foreach (KeyValuePair<Location, string> pair in unexpected)
{
Console.Error.WriteLine("\tline:{0},{1} unexpected {2}", pair.Key.Line, pair.Key.Column, pair.Value);
}
foreach (int line in expecations.Keys)
{
if (expecations[line].Count > 0)
{
foreach (string id in expecations[line])
{
string exists = string.Empty;
if (_rules.Select(x => x.DevSkimRule).FirstOrDefault(x => x.Id == id) == null)
exists = " (no such rule) ";
Console.Error.WriteLine("\tline:{0} expecting {1}{2}", line, id, exists);
}
}
}
Console.Error.WriteLine();
}
return result;
}