Skip to content

Commit

Permalink
Fixing json configuration export (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka authored Mar 3, 2021
1 parent 46b26e5 commit 02f96e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Sarif/PropertiesDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Xml;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace Microsoft.CodeAnalysis.Sarif
Expand All @@ -20,7 +21,9 @@ public class PropertiesDictionary : TypedPropertiesDictionary<object>
{
internal const string DEFAULT_POLICY_NAME = "default";

public PropertiesDictionary() : this(null) { }
public PropertiesDictionary() : this(null)
{
}

public PropertiesDictionary(PropertiesDictionary initializer) :
this(initializer, null)
Expand Down Expand Up @@ -157,8 +160,9 @@ public void SaveToJson(string filePath, bool prettyPrint = true)

var settings = new JsonSerializerSettings
{
Formatting = formatting
Formatting = formatting,
};
settings.Converters.Add(new StringEnumConverter());

File.WriteAllText(filePath, JsonConvert.SerializeObject(this, settings));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<None Remove="TestData\PageCommand\elfie-arriba.sarif" />
<None Remove="TestData\RebaseUriCommand\ExpectedOutputs\RunWithArtifacts.sarif" />
<None Remove="TestData\RebaseUriCommand\RunWithArtifacts.sarif" />
<None Remove="TestData\ValidateCommand\Configuration.json" />
<None Remove="TestData\ValidateCommand\Configuration.xml" />
<None Remove="xunit.runner.json" />
</ItemGroup>
Expand Down Expand Up @@ -56,6 +57,7 @@
<EmbeddedResource Include="TestData\PageCommand\elfie-arriba.sarif" />
<EmbeddedResource Include="TestData\RebaseUriCommand\ExpectedOutputs\RunWithArtifacts.sarif" />
<EmbeddedResource Include="TestData\RebaseUriCommand\Inputs\RunWithArtifacts.sarif" />
<EmbeddedResource Include="TestData\ValidateCommand\Configuration.json" />
<EmbeddedResource Include="TestData\ValidateCommand\Configuration.xml" />
<EmbeddedResource Include="TestData\ValidateCommand\ValidateSarif.sarif" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"SARIF2002.ProvideMessageArguments.Options": {
"RuleEnabled": "error"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ValidateCommandTests
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object""
}";

private const string SchemaFilePath = @"c:\schemas\SimpleSchemaForTest.json";
private const string LogFileDirectory = @"C:\Users\John\logs";
private const string LogFileName = "example.sarif";
Expand Down Expand Up @@ -103,7 +104,7 @@ public void WhenWeDoNotHaveResultsWithoutVerbose()
}

[Fact]
public void WhenWeDoHaveConfigurationChangingFailureLevel()
public void WhenWeDoHaveConfigurationChangingFailureLevelXml()
{
string path = "ValidateSarif.sarif";
string configuration = "Configuration.xml";
Expand All @@ -116,6 +117,20 @@ public void WhenWeDoHaveConfigurationChangingFailureLevel()
sarifLog.Runs[0].Results.Count.Should().Be(1);
}

[Fact]
public void WhenWeDoHaveConfigurationChangingFailureLevelJson()
{
string path = "ValidateSarif.sarif";
string configuration = "Configuration.json";
string outputPath = "ValidateSarifOutput.sarif";
File.WriteAllText(path, Extractor.GetResourceText($"ValidateCommand.{path}"));
File.WriteAllText(configuration, Extractor.GetResourceText($"ValidateCommand.{configuration}"));

SarifLog sarifLog = ExecuteTest(path, outputPath, configuration);
sarifLog.Runs.Count.Should().Be(1);
sarifLog.Runs[0].Results.Count.Should().Be(1);
}

private static SarifLog ExecuteTest(string path, string outputPath, string configuration = null)
{
var options = new ValidateOptions
Expand Down

0 comments on commit 02f96e0

Please sign in to comment.