-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding command policy to multitool (#2118)
* Adding command policy to multitool * updating changelog * Larry's code review - 1 * fixing ordering
- Loading branch information
Showing
8 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
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,65 @@ | ||
// 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.Diagnostics; | ||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ApplyPolicyCommand : CommandBase | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public ApplyPolicyCommand(IFileSystem fileSystem = null) | ||
{ | ||
_fileSystem = fileSystem ?? FileSystem.Instance; | ||
} | ||
|
||
public int Run(ApplyPolicyOptions applyPolicyOptions) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine($"Applying policy '{applyPolicyOptions.InputFilePath}' => '{applyPolicyOptions.OutputFilePath}'..."); | ||
Stopwatch w = Stopwatch.StartNew(); | ||
|
||
bool valid = ValidateOptions(applyPolicyOptions); | ||
if (!valid) { return FAILURE; } | ||
|
||
SarifLog actualLog = ReadSarifFile<SarifLog>(_fileSystem, applyPolicyOptions.InputFilePath); | ||
|
||
actualLog.ApplyPolicies(); | ||
|
||
string fileName = CommandUtilities.GetTransformedOutputFileName(applyPolicyOptions); | ||
|
||
Formatting formatting = applyPolicyOptions.PrettyPrint | ||
? Formatting.Indented | ||
: Formatting.None; | ||
|
||
WriteSarifFile(_fileSystem, actualLog, fileName, formatting); | ||
|
||
w.Stop(); | ||
Console.WriteLine($"Rewrite completed in {w.Elapsed}."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return FAILURE; | ||
} | ||
|
||
return SUCCESS; | ||
} | ||
|
||
private bool ValidateOptions(ApplyPolicyOptions applyPolicyOptions) | ||
{ | ||
bool valid = true; | ||
|
||
valid &= applyPolicyOptions.Validate(); | ||
|
||
valid &= DriverUtilities.ReportWhetherOutputFileCanBeCreated(applyPolicyOptions.OutputFilePath, applyPolicyOptions.Force, _fileSystem); | ||
|
||
return valid; | ||
} | ||
} | ||
} |
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,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using CommandLine; | ||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
[Verb("apply-policy", HelpText = "Apply policies from SARIF log.")] | ||
public class ApplyPolicyOptions : SingleFileOptionsBase | ||
{ | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/Test.UnitTests.Sarif.Multitool.Library/ApplyPolicyCommandTests.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,57 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.IO; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class ApplyPolicyCommandTests | ||
{ | ||
private static readonly ResourceExtractor Extractor = new ResourceExtractor(typeof(ApplyPolicyCommandTests)); | ||
|
||
[Fact] | ||
public void WhenInputContainsOnePolicy_ShouldSucceed() | ||
{ | ||
string path = "WithPolicy.sarif"; | ||
File.WriteAllText(path, Extractor.GetResourceText($"ApplyPolicyCommand.{path}")); | ||
|
||
// Verify log loads, has correct Result count, and spot check a Result | ||
SarifLog log = ExecuteTest(path); | ||
log.Runs[0].Results.Count.Should().Be(1); | ||
log.Runs[0].Results[0].Level.Should().Be(FailureLevel.Error); | ||
} | ||
|
||
[Fact] | ||
public void WhenInputContainsMultiplePolicies_ShouldApplyPoliciesInOrder() | ||
{ | ||
string path = "WithPolicy2.sarif"; | ||
File.WriteAllText(path, Extractor.GetResourceText($"ApplyPolicyCommand.{path}")); | ||
|
||
// Verify log loads, has correct Result count, and spot check a Result | ||
SarifLog log = ExecuteTest(path); | ||
log.Runs[0].Results.Count.Should().Be(1); | ||
log.Runs[0].Results[0].Level.Should().Be(FailureLevel.Note); | ||
} | ||
|
||
private SarifLog ExecuteTest(string path) | ||
{ | ||
var options = new ApplyPolicyOptions | ||
{ | ||
InputFilePath = path, | ||
OutputFilePath = path, | ||
Force = true | ||
}; | ||
|
||
// Verify command returned success | ||
int returnCode = new ApplyPolicyCommand().Run(options); | ||
returnCode.Should().Be(0); | ||
|
||
// Verify SARIF output log exists | ||
File.Exists(path).Should().BeTrue(); | ||
|
||
return SarifLog.Load(path); | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
src/Test.UnitTests.Sarif.Multitool.Library/TestData/ApplyPolicyCommand/WithPolicy.sarif
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,52 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.1", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Test", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"name": "Test", | ||
"shortDescription": { | ||
"text": "Test description." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST0001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"text": "Test text." | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits", | ||
"policies": [ | ||
{ | ||
"name": "TEST Policy", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "error" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Test.UnitTests.Sarif.Multitool.Library/TestData/ApplyPolicyCommand/WithPolicy2.sarif
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,64 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.1", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Test", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"name": "Test", | ||
"shortDescription": { | ||
"text": "Test description." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description." | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST0001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"text": "Test text." | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits", | ||
"policies": [ | ||
{ | ||
"name": "TEST Policy", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "error" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "TEST Policy 2", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"defaultConfiguration": { | ||
"level": "note" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |