Skip to content

Commit

Permalink
Implementing GetFormatting helper + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka committed Oct 17, 2020
1 parent f2108fe commit 14370ed
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 40 deletions.
8 changes: 7 additions & 1 deletion src/Sarif.Driver/Sdk/CommonOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using CommandLine;
using Newtonsoft.Json;

namespace Microsoft.CodeAnalysis.Sarif.Driver
{
Expand Down Expand Up @@ -64,8 +65,13 @@ public class CommonOptionsBase
'v',
"sarif-output-version",
HelpText =
@"The SARIF version of the output log file. Valid values are OneZeroZero and Current",
"The SARIF version of the output log file. Valid values are OneZeroZero and Current",
Default = SarifVersion.Current)]
public SarifVersion SarifOutputVersion { get; set; }

public Formatting GetFormatting()
{
return this.PrettyPrint ? Formatting.Indented : Formatting.None;
}
}
}
1 change: 0 additions & 1 deletion src/Sarif.Driver/Sdk/MultifileOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class MultipleFilesOptionsBase : CommonOptionsBase
HelpText = "Overwrite each input file with the corresponding transformed file.")]
public bool Inline { get; set; }


[Value(0,
MetaName = "<files>",
HelpText = "Files to process (wildcards ? and * allowed).",
Expand Down
4 changes: 1 addition & 3 deletions src/Sarif.Multitool.Library/AbsoluteUriCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public int Run(AbsoluteUriOptions absoluteUriOptions)
_fileSystem.DirectoryCreate(absoluteUriOptions.OutputDirectoryPath);
}

Formatting formatting = absoluteUriOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = absoluteUriOptions.GetFormatting();

foreach (AbsoluteUriFile absoluteUriFile in absoluteUriFiles)
{
Expand Down
1 change: 0 additions & 1 deletion src/Sarif.Multitool.Library/AbsoluteUriOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ namespace Microsoft.CodeAnalysis.Sarif.Multitool
[Verb("absoluteuri", HelpText = "Turn all relative Uris into absolute URIs (to be used after rebaseUri is run)")]
public class AbsoluteUriOptions : MultipleFilesOptionsBase
{

}
}
11 changes: 5 additions & 6 deletions src/Sarif.Multitool.Library/FileWorkItemsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
namespace Microsoft.CodeAnalysis.Sarif.Multitool
{
/// <summary>
/// A class that drives SARIF work item filing. This class is responsible for
/// A class that drives SARIF work item filing. This class is responsible for
/// collecting and verifying all options relevant to driving the work item filing
/// process. These options may be retrieved from a serialized version of the
/// process. These options may be retrieved from a serialized version of the
/// aggregated configuration (currently rendered as XML, via the PropertiesDictionary
/// class). Command-line arguments will override any options specified in the
/// class). Command-line arguments will override any options specified in the
/// file-based serialized configuration (if present). After verifying that all
/// configured options are valid, the command will instantiate an instance of
/// configured options are valid, the command will instantiate an instance of
/// SarifWorkItemFiler in order to complete the work.
/// </summary>
public class FileWorkItemsCommand : CommandBase
Expand Down Expand Up @@ -95,7 +95,7 @@ public int Run(FileWorkItemsOptions options, IFileSystem fileSystem)
string outputFilePath = options.OutputFilePath;
if (!string.IsNullOrEmpty(outputFilePath))
{
Formatting formatting = options.PrettyPrint ? Formatting.Indented : Formatting.None;
Formatting formatting = options.GetFormatting();

string sarifLogText = JsonConvert.SerializeObject(sarifLog, formatting);

Expand Down Expand Up @@ -164,7 +164,6 @@ private static bool ValidateHostUri(string hostUriString, SarifWorkItemContext w
// Any command-line argument that's provided overrides values specified in the configuration.
workItemFilingConfiguration.HostUri = hostUri ?? workItemFilingConfiguration.HostUri;


if (!workItemFilingConfiguration.HostUri.IsAbsoluteUri)
{
string optionDescription = hostUri != null ? "--project-uri" : "--configuration";
Expand Down
16 changes: 6 additions & 10 deletions src/Sarif.Multitool.Library/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public int Run(MergeOptions mergeOptions)
if (mergeOptions.SplittingStrategy != SplittingStrategy.PerRule)
{
// Write output to file.
Formatting formatting = mergeOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = mergeOptions.GetFormatting();

_fileSystem.DirectoryCreate(outputDirectory);

Expand Down Expand Up @@ -106,7 +104,7 @@ public int Run(MergeOptions mergeOptions)
}
}
}

foreach (string ruleId in ruleToRunsMap.Keys)
{
HashSet<Run> runs = ruleToRunsMap[ruleId];
Expand All @@ -120,9 +118,7 @@ public int Run(MergeOptions mergeOptions)
new FixupVisitor().VisitSarifLog(perRuleLog);
}

Formatting formatting = mergeOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = mergeOptions.GetFormatting();

_fileSystem.DirectoryCreate(outputDirectory);

Expand Down Expand Up @@ -169,12 +165,12 @@ internal static string GetOutputFileName(MergeOptions mergeOptions, string prefi

private static string GetPrefix(string prefix)
{
if (prefix != null && !prefix.EndsWith("_"))
if (prefix?.EndsWith("_") == false)
{
prefix = prefix + "_";
prefix += "_";
}

return prefix == null ? "" : prefix;
return prefix ?? "";
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Sarif.Multitool.Library/QueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ public class QueryOptions
HelpText = "A file path of the SARIF subset output file, if desired.",
Required = false)]
public string OutputFilePath { get; set; }

}
}
4 changes: 1 addition & 3 deletions src/Sarif.Multitool.Library/RebaseUriCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public int Run(RebaseUriOptions rebaseOptions)
_fileSystem.DirectoryCreate(rebaseOptions.OutputDirectoryPath);
}

Formatting formatting = rebaseOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = rebaseOptions.GetFormatting();

OptionallyEmittedData dataToRemove = rebaseOptions.DataToRemove.ToFlags();
OptionallyEmittedData dataToInsert = rebaseOptions.DataToInsert.ToFlags();
Expand Down
4 changes: 2 additions & 2 deletions src/Sarif.Multitool.Library/ResultMatchSetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public int Run(ResultMatchSetOptions options)
{
int returnCode = SUCCESS;

options.OutputFolderPath = options.OutputFolderPath ?? Path.Combine(options.FolderPath, "Out");
options.OutputFolderPath ??= Path.Combine(options.FolderPath, "Out");

ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner();
Formatting formatting = options.PrettyPrint ? Formatting.Indented : Formatting.None;
Formatting formatting = options.GetFormatting();

// Remove previous results.
if (_fileSystem.DirectoryExists(options.OutputFolderPath) && options.Force)
Expand Down
4 changes: 1 addition & 3 deletions src/Sarif.Multitool.Library/RewriteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public int Run(RewriteOptions rewriteOptions)

string fileName = CommandUtilities.GetTransformedOutputFileName(rewriteOptions);

Formatting formatting = rewriteOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = rewriteOptions.GetFormatting();

WriteSarifFile(_fileSystem, reformattedLog, fileName, formatting);

Expand Down
4 changes: 1 addition & 3 deletions src/Sarif.Multitool.Library/TransformCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public int Run(TransformOptions transformOptions)
// NOTE: we don't actually utilize the dataToInsert command-line data yet...
OptionallyEmittedData dataToInsert = transformOptions.DataToInsert.ToFlags();

Formatting formatting = transformOptions.PrettyPrint
? Formatting.Indented
: Formatting.None;
Formatting formatting = transformOptions.GetFormatting();

string inputFilePath = transformOptions.InputFilePath;
string inputVersion = SniffVersion(inputFilePath);
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/Writers/SarifOneZeroZeroLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Microsoft.CodeAnalysis.Sarif.Writers
{
/// <summary>
/// A SARIF logger that works by generating a SARIF v2 log file which is transformed into
/// A SARIF logger that works by generating a SARIF v2 log file which is transformed into
/// SARIF v1 when the instance is disposed. The file location used to produced the preliminary
/// v2 content is overwritten in place to produce the transformed v1 file.
/// </summary>
Expand Down
9 changes: 4 additions & 5 deletions src/Test.UnitTests.Sarif/Visitors/RebaseUriVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public void RebaseUriVisitor_VisitRun_DoesNotPatchFileDictionaryKeysWhenNotABase
[Fact]
public void RebaseUriVisitor_VisitFileData_PatchesParentUri()
{
Uri rootfileUri = new Uri(@"file://C:/src/root/blah.zip");
Uri childFileUri = new Uri(@"/stuff.doc", UriKind.RelativeOrAbsolute);
Uri rootfileUri = new Uri("file://C:/src/root/blah.zip");
Uri childFileUri = new Uri("/stuff.doc", UriKind.RelativeOrAbsolute);

Artifact rootFileData = new Artifact() { Location = new ArtifactLocation { Uri = rootfileUri }, ParentIndex = -1 };
Artifact childFileData = new Artifact() { Location = new ArtifactLocation { Uri = childFileUri }, ParentIndex = 0 };
Expand Down Expand Up @@ -253,11 +253,10 @@ public override ArtifactLocation VisitArtifactLocation(ArtifactLocation node)
{
if (_currentRun.OriginalUriBaseIds == null || !_currentRun.OriginalUriBaseIds.Values.Contains(node))
{

FileLocationUris = FileLocationUris ?? new List<string>();
FileLocationUris ??= new List<string>();
FileLocationUris.Add(node.Uri.OriginalString);

FileLocationUriBaseIds = FileLocationUriBaseIds ?? new List<string>();
FileLocationUriBaseIds ??= new List<string>();
FileLocationUriBaseIds.Add(node.UriBaseId);
}
return base.VisitArtifactLocation(node);
Expand Down

0 comments on commit 14370ed

Please sign in to comment.