Skip to content

Commit

Permalink
Honor insert and remove arguments for rebase uri command. (#1927)
Browse files Browse the repository at this point in the history
* Add data insert/removal to rebase uri command.

* Update release notes for rebase uri command.

* Remove console message.
  • Loading branch information
michaelcfanning authored Jun 24, 2020
1 parent 8982f1a commit a1c3eab
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# SARIF Package Release History (SDK, Driver, Converters, and Multitool)
## **v2.3.1** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.1) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.1) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.1) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.1)
* BUGFIX: Rebase URI command now honors `--insert` and `--remove` arguments for injecting or eliding optional data (such as region snippets).

## **v2.3.0** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.0) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.0) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.0) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.0)
* BUGFIX: `ResultLogJsonWriter` now creates an empty `results` array if there are no results, rather than leaving `results` as `null`. [#1821](https://github.com/microsoft/sarif-sdk/issues/1821)
Expand Down
15 changes: 15 additions & 0 deletions src/Sarif.Multitool/RebaseUriCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Linq;
using Microsoft.CodeAnalysis.Sarif.Driver;
using Microsoft.CodeAnalysis.Sarif.Processors;
using Microsoft.CodeAnalysis.Sarif.Visitors;

using Newtonsoft.Json;

namespace Microsoft.CodeAnalysis.Sarif.Multitool
Expand Down Expand Up @@ -49,8 +51,21 @@ public int Run(RebaseUriOptions rebaseOptions)
? Formatting.Indented
: Formatting.None;

OptionallyEmittedData dataToRemove = rebaseOptions.DataToRemove.ToFlags();
OptionallyEmittedData dataToInsert = rebaseOptions.DataToInsert.ToFlags();

foreach (RebaseUriFile rebaseUriFile in rebaseUriFiles)
{
if (dataToRemove != 0)
{
rebaseUriFile.Log = new RemoveOptionalDataVisitor(dataToRemove).VisitSarifLog(rebaseUriFile.Log);
}

if (dataToInsert != 0)
{
rebaseUriFile.Log = new InsertOptionalDataVisitor(dataToInsert).VisitSarifLog(rebaseUriFile.Log);
}

rebaseUriFile.Log = rebaseUriFile.Log.RebaseUri(rebaseOptions.BasePathToken, rebaseOptions.RebaseRelativeUris, baseUri);

WriteSarifFile(_fileSystem, rebaseUriFile.Log, rebaseUriFile.OutputFilePath, formatting);
Expand Down
90 changes: 87 additions & 3 deletions src/Test.UnitTests.Sarif.Multitool/RebaseUriCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +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 System;
using System.IO;
using System.Text;
using FluentAssertions;

using Microsoft.Extensions.Options;

using Moq;

using Newtonsoft.Json;

using Xunit;
using Xunit.Abstractions;

Expand All @@ -13,25 +20,102 @@ namespace Microsoft.CodeAnalysis.Sarif.Multitool
public class RebaseUriCommandTests : FileDiffingUnitTests
{
private static readonly ResourceExtractor Extractor = new ResourceExtractor(typeof(RebaseUriCommandTests));

private RebaseUriOptions options;

public RebaseUriCommandTests(ITestOutputHelper outputHelper) : base(outputHelper) { }

[Fact]
public void RebaseUriCommand_InjectsRegions()
{
string productDirectory = FileDiffingFunctionalTests.GetProductDirectory();
string analysisFile = Path.Combine(productDirectory, @"ReleaseHistory.md");
File.Exists(analysisFile).Should().BeTrue();

var sarifLog = new SarifLog
{
Runs = new[]
{
new Run { Results = new[] {
new Result { Locations = new [] {
new Location {
PhysicalLocation = new PhysicalLocation {
Region = new Region { StartLine = 7 },
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri(analysisFile)
}
}
} }
} } }
}
};

string inputSarifLog = JsonConvert.SerializeObject(sarifLog);

string logFilePath = @"c:\logs\mylog.sarif";
StringBuilder transformedContents = new StringBuilder();

RebaseUriOptions options = CreateDefaultOptions();

options.TargetFileSpecifiers = new string[] { logFilePath };

options.DataToInsert = new[]
{
OptionallyEmittedData.RegionSnippets |
OptionallyEmittedData.ContextRegionSnippets
};

Mock<IFileSystem> mockFileSystem = ArrangeMockFileSystem(inputSarifLog, logFilePath, transformedContents);

// Test snippet injection.
var rebaseUriCommand = new RebaseUriCommand(mockFileSystem.Object);

int returnCode = rebaseUriCommand.Run(options);
returnCode.Should().Be(0);

SarifLog actualLog = JsonConvert.DeserializeObject<SarifLog>(transformedContents.ToString());
actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Should().NotBeNull();
actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.ContextRegion.Snippet.Should().NotBeNull();

// Now test that this data is removed.
inputSarifLog = JsonConvert.SerializeObject(actualLog);
transformedContents.Length = 0;
mockFileSystem = ArrangeMockFileSystem(inputSarifLog, logFilePath, transformedContents);
rebaseUriCommand = new RebaseUriCommand(mockFileSystem.Object);

options.DataToRemove = options.DataToInsert;
options.DataToInsert = null;

returnCode = rebaseUriCommand.Run(options);
returnCode.Should().Be(0);

actualLog = JsonConvert.DeserializeObject<SarifLog>(transformedContents.ToString());
actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Should().BeNull();
actualLog.Runs[0].Results[0].Locations[0].PhysicalLocation.ContextRegion.Snippet.Should().BeNull();
}


[Fact]
public void RebaseUriCommand_RebaseRunWithArtifacts()
{
string testFilePath = "RunWithArtifacts.sarif";

this.options = new RebaseUriOptions
this.options = CreateDefaultOptions();

RunTest(testFilePath);
}

private static RebaseUriOptions CreateDefaultOptions()
{
return new RebaseUriOptions
{
BasePath = @"C:\vs\src\2\s\",
BasePathToken = "SRCROOT",
Inline = true,
SarifOutputVersion = SarifVersion.Current,
PrettyPrint = true
};

RunTest(testFilePath);
}

protected override string ConstructTestOutputFromInputResource(string testFilePath, object parameter)
Expand Down
8 changes: 5 additions & 3 deletions src/Test.Utilities.Sarif/FileDiffingUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public static string GetTestDirectory(string subdirectory = "")
{
return Path.GetFullPath(Path.Combine(@".\TestData", subdirectory));
}
public static string GetProductDirectory()
{
return Path.GetFullPath($@"..\..\..\..\..\src\");
}

// Retrieving the source path of the tests is only used in developer ad hoc
// rebaselining scenarios. i.e., this path won't be consumed by AppVeyor.
public static string GetProductTestDataDirectory(string testBinaryName, string subdirectory = "")
{
return Path.GetFullPath(Path.Combine($@"..\..\..\..\..\src\{testBinaryName}\TestData", subdirectory));
return Path.GetFullPath(Path.Combine(GetProductDirectory(), $".\\{testBinaryName}\\TestData", subdirectory));
}

private readonly ITestOutputHelper _outputHelper;
Expand Down

0 comments on commit a1c3eab

Please sign in to comment.