-
Notifications
You must be signed in to change notification settings - Fork 93
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
Enable export rules in multitool #2052
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
70d2cad
Enable export rules in multitool
eddynaka 853671d
updating folder level and namespaces
eddynaka 4e4b888
updating to environment.newline to minimize lines of code
eddynaka fed3ca1
code review - 1
eddynaka 30ee353
code review - 2
eddynaka 0022c68
code review - 3
eddynaka 215b97b
Adding unit test
eddynaka ef6debd
code review - 4
eddynaka eb5b3e3
code review - 6
eddynaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
using Microsoft.CodeAnalysis.Sarif.Multitool.Rules; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ExportRuleDocumentationCommand : CommandBase | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public ExportRuleDocumentationCommand(IFileSystem fileSystem = null) | ||
{ | ||
_fileSystem = fileSystem ?? new FileSystem(); | ||
} | ||
|
||
public int Run(ExportRuleDocumentationOptions options) | ||
{ | ||
try | ||
{ | ||
var rules = CompositionUtilities.GetExports<SarifValidationSkimmerBase>( | ||
new Assembly[] { Assembly.GetExecutingAssembly() }).ToList(); | ||
|
||
var sb = new StringBuilder(); | ||
sb.AppendLine($"# Rules{Environment.NewLine}"); | ||
|
||
foreach (SarifValidationSkimmerBase rule in rules) | ||
{ | ||
BuildRule(rule, sb); | ||
} | ||
|
||
_fileSystem.WriteAllText(options.OutputFilePath, sb.ToString()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return FAILURE; | ||
} | ||
|
||
return SUCCESS; | ||
} | ||
|
||
internal void BuildRule(SarifValidationSkimmerBase rule, StringBuilder sb) | ||
{ | ||
sb.AppendLine($"## Rule `{rule.Moniker}`{Environment.NewLine}"); | ||
sb.AppendLine($"### Description{Environment.NewLine}"); | ||
sb.AppendLine($"{rule.FullDescription.Text}{Environment.NewLine}"); | ||
sb.AppendLine($"### Messages{Environment.NewLine}"); | ||
|
||
foreach (KeyValuePair<string, MultiformatMessageString> message in rule.MessageStrings) | ||
{ | ||
sb.AppendLine($"#### `{message.Key.Split('_').Last()}`: {rule.DefaultLevel}{Environment.NewLine}"); | ||
sb.AppendLine($"{message.Value.Text}{Environment.NewLine}"); | ||
} | ||
|
||
sb.AppendLine($"---{Environment.NewLine}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using CommandLine; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
[Verb("export-rule-documentation", HelpText = "Export the documentation for the validation rules to a Markdown file.")] | ||
public class ExportRuleDocumentationOptions | ||
{ | ||
[Option( | ||
'o', | ||
"output-file-path", | ||
HelpText = "Path to the generated Markdown file. Default: ValidationRules.md in the current directory.")] | ||
public string OutputFilePath { get; internal set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Test.UnitTests.Sarif.Multitool/ExportRuleDocumentationCommandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Text; | ||
using FluentAssertions; | ||
using Microsoft.CodeAnalysis.Sarif.Multitool.Rules; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ExportRuleDocumentationCommandTests | ||
{ | ||
[Fact] | ||
public void BuildRule_GeneratesExpectedMarkdown() | ||
{ | ||
var resourceExtractor = new ResourceExtractor(this.GetType()); | ||
var sb = new StringBuilder(); | ||
var testRule = new TestRule(); | ||
var command = new ExportRuleDocumentationCommand(); | ||
command.BuildRule(testRule, sb); | ||
|
||
string expectedMarkdown = resourceExtractor.GetResourceText("Test.md"); | ||
|
||
sb.ToString().Should().Be(expectedMarkdown); | ||
} | ||
|
||
private class TestRule : SarifValidationSkimmerBase | ||
{ | ||
public override string Id => "TEST0001"; | ||
public override string Name => "TEST"; | ||
public override FailureLevel DefaultLevel => FailureLevel.Note; | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = "full description text" }; | ||
public override IDictionary<string, MultiformatMessageString> MessageStrings => new Dictionary<string, MultiformatMessageString> | ||
{ | ||
{ "TEST0001_TEST_Note_Default", new MultiformatMessageString{ Text="default text"} } | ||
}; | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...Sarif.Multitool/TestData/ExportRuleDocumentationCommand/ExpectedOutputs/Test.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## Rule `TEST0001.TEST` | ||
|
||
### Description | ||
|
||
full description text | ||
|
||
### Messages | ||
|
||
#### `Default`: Note | ||
|
||
default text | ||
|
||
--- | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful! #ByDesign