diff --git a/AppInspector.CLI/Properties/AssemblyInfo.cs b/AppInspector.CLI/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..b9702bd7 --- /dev/null +++ b/AppInspector.CLI/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("AppInspector.Tests")] diff --git a/AppInspector.CLI/Writers/JsonWriter.cs b/AppInspector.CLI/Writers/JsonWriter.cs index 7cf15826..a02b774e 100644 --- a/AppInspector.CLI/Writers/JsonWriter.cs +++ b/AppInspector.CLI/Writers/JsonWriter.cs @@ -39,7 +39,7 @@ public override void WriteResults(Result result, CLICommandOptions commandOption case TagDiffResult: case ExportTagsResult: case VerifyRulesResult: - JsonSerializer.Serialize(StreamWriter.BaseStream, result, options); + JsonSerializer.Serialize(StreamWriter.BaseStream, result, result.GetType(), options); break; case PackRulesResult prr: JsonSerializer.Serialize(StreamWriter.BaseStream, prr.Rules, options); diff --git a/AppInspector.Tests/Commands/TestExportTagsCmd.cs b/AppInspector.Tests/Commands/TestExportTagsCmd.cs index 6057c2c9..a8447b48 100644 --- a/AppInspector.Tests/Commands/TestExportTagsCmd.cs +++ b/AppInspector.Tests/Commands/TestExportTagsCmd.cs @@ -59,4 +59,41 @@ public void NoDefaultNoCustomRules() }; Assert.Throws(() => new ExportTagsCommand(options)); } + + [Fact] + public void ExportJsonSerialization() + { + ExportTagsOptions options = new() + { + IgnoreDefaultRules = true, + CustomRulesPath = testRulesPath + }; + ExportTagsCommand command = new(options, factory); + var result = command.GetResult(); + + // Test JSON serialization via the production JsonWriter to ensure tags are included + using var memoryStream = new MemoryStream(); + using var streamWriter = new StreamWriter(memoryStream); + + // Use the JsonWriter that production code uses + var jsonWriter = new Microsoft.ApplicationInspector.CLI.Writers.JsonWriter(streamWriter, factory); + var cliOptions = new Microsoft.ApplicationInspector.CLI.CLIExportTagsCmdOptions + { + IgnoreDefaultRules = true, + CustomRulesPath = testRulesPath + }; + + jsonWriter.WriteResults(result, cliOptions, autoClose: false); + streamWriter.Flush(); + + memoryStream.Position = 0; + using var reader = new StreamReader(memoryStream); + string json = reader.ReadToEnd(); + + // Verify tags are present in JSON output from JsonWriter + Assert.Contains("Test.Tags.Linux", json); + Assert.Contains("Test.Tags.Windows", json); + Assert.Contains("tagsList", json); + Assert.Contains("appVersion", json); + } } \ No newline at end of file