From 14f9b37d2614a90879216440e9e56c1c30d439f5 Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Tue, 2 Mar 2021 11:54:55 -0300 Subject: [PATCH] Fixing json configuration export --- src/Sarif/PropertiesDictionary.cs | 8 ++++++-- ...est.UnitTests.Sarif.Multitool.Library.csproj | 2 ++ .../TestData/ValidateCommand/Configuration.json | 5 +++++ .../ValidateCommandTests.cs | 17 ++++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/Test.UnitTests.Sarif.Multitool.Library/TestData/ValidateCommand/Configuration.json diff --git a/src/Sarif/PropertiesDictionary.cs b/src/Sarif/PropertiesDictionary.cs index b5c60888b..1a6f8d3e0 100644 --- a/src/Sarif/PropertiesDictionary.cs +++ b/src/Sarif/PropertiesDictionary.cs @@ -10,6 +10,7 @@ using System.Xml; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; namespace Microsoft.CodeAnalysis.Sarif @@ -20,7 +21,9 @@ public class PropertiesDictionary : TypedPropertiesDictionary { internal const string DEFAULT_POLICY_NAME = "default"; - public PropertiesDictionary() : this(null) { } + public PropertiesDictionary() : this(null) + { + } public PropertiesDictionary(PropertiesDictionary initializer) : this(initializer, null) @@ -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)); } diff --git a/src/Test.UnitTests.Sarif.Multitool.Library/Test.UnitTests.Sarif.Multitool.Library.csproj b/src/Test.UnitTests.Sarif.Multitool.Library/Test.UnitTests.Sarif.Multitool.Library.csproj index 32b4706cc..7c78e2b4f 100644 --- a/src/Test.UnitTests.Sarif.Multitool.Library/Test.UnitTests.Sarif.Multitool.Library.csproj +++ b/src/Test.UnitTests.Sarif.Multitool.Library/Test.UnitTests.Sarif.Multitool.Library.csproj @@ -29,6 +29,7 @@ + @@ -56,6 +57,7 @@ + diff --git a/src/Test.UnitTests.Sarif.Multitool.Library/TestData/ValidateCommand/Configuration.json b/src/Test.UnitTests.Sarif.Multitool.Library/TestData/ValidateCommand/Configuration.json new file mode 100644 index 000000000..2bfa03a45 --- /dev/null +++ b/src/Test.UnitTests.Sarif.Multitool.Library/TestData/ValidateCommand/Configuration.json @@ -0,0 +1,5 @@ +{ + "SARIF2002.ProvideMessageArguments.Options": { + "RuleEnabled": "error" + } +} \ No newline at end of file diff --git a/src/Test.UnitTests.Sarif.Multitool.Library/ValidateCommandTests.cs b/src/Test.UnitTests.Sarif.Multitool.Library/ValidateCommandTests.cs index 5d46184d8..621e0afdb 100644 --- a/src/Test.UnitTests.Sarif.Multitool.Library/ValidateCommandTests.cs +++ b/src/Test.UnitTests.Sarif.Multitool.Library/ValidateCommandTests.cs @@ -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"; @@ -103,7 +104,7 @@ public void WhenWeDoNotHaveResultsWithoutVerbose() } [Fact] - public void WhenWeDoHaveConfigurationChangingFailureLevel() + public void WhenWeDoHaveConfigurationChangingFailureLevelXml() { string path = "ValidateSarif.sarif"; string configuration = "Configuration.xml"; @@ -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