public static string FormatForVisualStudio(this Result result, IRule rule) { if (result == null) { throw new ArgumentNullException(nameof(result)); } if (rule == null) { throw new ArgumentNullException(nameof(rule)); } var messageLines = new List <string>(); foreach (var location in result.Locations) { Uri uri = location.PhysicalLocation.FileLocation.Uri; string path = uri.IsAbsoluteUri && uri.IsFile ? uri.LocalPath : uri.ToString(); messageLines.Add( string.Format( CultureInfo.InvariantCulture, "{0}{1}: {2} {3}: {4}", path, location.PhysicalLocation.Region.FormatForVisualStudio(), result.Level.FormatForVisualStudio(), result.RuleId, result.GetMessageText(rule) )); } return(string.Join(Environment.NewLine, messageLines)); }
public void SarifExtensions_Result_GetMessageText_Concise() { var result = new Result() { Message = new Message() { Arguments = new List <string> { "fox", "dog" }, MessageId = "ruleStr1" } }; var rule = new ReportingDescriptor() { MessageStrings = new Dictionary <string, MultiformatMessageString>() { ["ruleStr1"] = new MultiformatMessageString { Text = "The quick brown {0} jumps over the lazy {1}. That {1} sure is lazy!" } } }; string expected = "The quick brown fox jumps over the lazy dog."; string actual = result.GetMessageText(rule, concise: true); Assert.Equal(expected, actual); }
public void SarifExtensions_Result_GetMessageText_Concise_Truncated() { var result = new Result { Message = new Message { Id = "ruleStr1" } }; var rule = new ReportingDescriptor { MessageStrings = new Dictionary <string, MultiformatMessageString> { ["ruleStr1"] = new MultiformatMessageString { Text = "First sentence is very long. Second sentence." } } }; const string Expected = "First sentence is ve\u2026"; // \u2026 is Unicode "horizontal ellipsis". int maxLength = Expected.Length - 1; // The -1 is for the ellipsis character. string actual = result.GetMessageText(rule, concise: true, maxLength); Assert.Equal(Expected, actual); }
public void SarifExtensions_Result_GetMessageText_WithArguments() { var result = new Result() { Message = new Message() { Arguments = new List <string> { "fox", "dog" }, MessageId = "ruleStr1" }, }; var rule = new Rule() { MessageStrings = new Dictionary <string, string>() { { "ruleStr1", "The quick brown {0} jumps over the lazy {1}. That {1} sure is lazy!" } } }; string expected = "The quick brown fox jumps over the lazy dog. That dog sure is lazy!"; string actual = result.GetMessageText(rule); Assert.Equal(expected, actual); }
public void SarifExtensions_Result_GetMessageText_MessageText() { var result = new Result() { Message = new Message() { Text = "The quick brown fox jumps over the lazy dog." } }; string expected = "The quick brown fox jumps over the lazy dog."; string actual = result.GetMessageText(null); Assert.Equal(expected, actual); }
public void Log(IRule rule, Result result) { if (result == null) { throw new ArgumentNullException(nameof(result)); } string message = result.GetMessageText(rule, concise: false); // TODO we need better retrieval for locations than these defaults // Note that we can potentially emit many messages from a single result PhysicalLocation physicalLocation = result.Locations?.First().ResultFile ?? result.Locations?.First().AnalysisTarget; WriteToConsole( result.Level, physicalLocation?.Uri, physicalLocation?.Region, result.RuleId, message); }
public void Log(ReportingDescriptor rule, Result result) { if (result == null) { throw new ArgumentNullException(nameof(result)); } string message = result.GetMessageText(rule); // TODO we need better retrieval for locations than these defaults // Note that we can potentially emit many messages from a single result PhysicalLocation physicalLocation = result.Locations?.First().PhysicalLocation; WriteToConsole( result.Kind, result.Level, physicalLocation?.ArtifactLocation?.Uri, physicalLocation?.Region, result.RuleId, message); }