public void SerializeMetadata_ReturnsExpectedString_WithLegacyItem_WhenEscaping( string value, string escapedValue) { // Arrange var identity = "../files/azureMonitor.json"; var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal) { { "Value", value } }; var input = new Mock <ITaskItem>(MockBehavior.Strict); input.SetupGet(i => i.ItemSpec).Returns(identity).Verifiable(); input.Setup(i => i.CloneCustomMetadata()).Returns(metadata).Verifiable(); var expectedResult = $"Identity={identity}|Value={escapedValue}"; // Act var result = MetadataSerializer.SerializeMetadata(input.Object); // Assert Assert.Equal(expectedResult, result); input.VerifyGet(i => i.ItemSpec, Times.Once); input.Verify(i => i.CloneCustomMetadata(), Times.Once); }
public void SerializeMetadata_ReturnsExpectedString_WithLegacyItem() { // Arrange var identity = "../files/azureMonitor.json"; var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal) { { "ClassName", "azureMonitorClient" }, { "CodeGenerator", "NSwagCSharp" }, { "FirstForGenerator", "true" }, { "Namespace", "ConsoleClient" }, { "Options", "" }, { "OriginalItemSpec", identity }, { "OutputPath", "C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs" }, }; var input = new Mock <ITaskItem>(MockBehavior.Strict); input.SetupGet(i => i.ItemSpec).Returns(identity).Verifiable(); input.Setup(i => i.CloneCustomMetadata()).Returns(metadata).Verifiable(); var expectedResult = $"Identity={identity}|ClassName=azureMonitorClient|" + "CodeGenerator=NSwagCSharp|FirstForGenerator=true|Namespace=ConsoleClient|" + "Options=|OriginalItemSpec=../files/azureMonitor.json|" + "OutputPath=C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs"; // Act var result = MetadataSerializer.SerializeMetadata(input.Object); // Assert Assert.Equal(expectedResult, result); input.VerifyGet(i => i.ItemSpec, Times.Once); input.Verify(i => i.CloneCustomMetadata(), Times.Once); }
public void SerializeMetadata_ReturnsExpectedString() { // Arrange var identity = "../files/azureMonitor.json"; var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal) { { "ClassName", "azureMonitorClient" }, { "CodeGenerator", "NSwagCSharp" }, { "FirstForGenerator", "true" }, { "Namespace", "ConsoleClient" }, { "Options", "" }, { "OriginalItemSpec", identity }, { "OutputPath", "C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs" }, }; var input = new TaskItem(identity, metadata); var expectedResult = $"Identity={identity}|ClassName=azureMonitorClient|" + "CodeGenerator=NSwagCSharp|FirstForGenerator=true|Namespace=ConsoleClient|" + "Options=|OriginalItemSpec=../files/azureMonitor.json|" + "OutputPath=C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs"; // Act var result = MetadataSerializer.SerializeMetadata(input); // Assert Assert.Equal(expectedResult, result); }
public void SerializeMetadata_ReturnsExpectedString_WhenEscapingIdentity(string escapedValue) { // Arrange var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal) { { "Value", "a value" } }; var expectedResult = $"Identity={escapedValue}|Value=a value"; var input = new TaskItem(escapedValue, metadata); // Act var result = MetadataSerializer.SerializeMetadata(input); // Assert Assert.Equal(expectedResult, result); }
public void SerializeMetadata_ReturnsExpectedString_WhenEscaping(string escapedValue) { // Arrange var identity = "../files/azureMonitor.json"; var expectedResult = $"Identity={identity}|Value={escapedValue}"; var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal) { { "Value", escapedValue } }; var input = new TaskItem(identity, metadata); // Act var result = MetadataSerializer.SerializeMetadata(input); // Assert Assert.Equal(expectedResult, result); }
/// <inheritdoc /> public override bool Execute() { var outputs = new List <ITaskItem>(Inputs.Length); var codeGenerators = new HashSet <string>(); var destinations = new HashSet <string>(); foreach (var item in Inputs) { var codeGenerator = item.GetMetadata("CodeGenerator"); if (string.IsNullOrEmpty(codeGenerator)) { // This case occurs when user overrides the required metadata with an empty string. var type = string.IsNullOrEmpty(item.GetMetadata("SourceProject")) ? "OpenApiReference" : "OpenApiProjectReference"; Log.LogError( Resources.FormatInvalidEmptyMetadataValue("CodeGenerator", "OpenApiReference", item.ItemSpec)); continue; } var newItem = new TaskItem(item); outputs.Add(newItem); if (codeGenerators.Add(codeGenerator)) { newItem.SetMetadata("FirstForGenerator", "true"); } else { newItem.SetMetadata("FirstForGenerator", "false"); } var outputPath = item.GetMetadata("OutputPath"); if (string.IsNullOrEmpty(outputPath)) { // No need to further sanitize this path because the file must exist. var filename = item.GetMetadata("Filename"); var isTypeScript = codeGenerator.EndsWith( TypeScriptLanguageName, StringComparison.OrdinalIgnoreCase); outputPath = $"{filename}Client{(isTypeScript ? ".ts" : Extension)}"; } // Place output file in correct directory (relative to project directory). if (!Path.IsPathRooted(outputPath) && !string.IsNullOrEmpty(OutputDirectory)) { outputPath = Path.Combine(OutputDirectory, outputPath); } if (!destinations.Add(outputPath)) { // This case may occur when user is experimenting e.g. with multiple code generators or options. // May also occur when user accidentally duplicates OutputPath metadata. Log.LogError(Resources.FormatDuplicateFileOutputPaths(outputPath)); continue; } MetadataSerializer.SetMetadata(newItem, "OutputPath", outputPath); var className = item.GetMetadata("ClassName"); if (string.IsNullOrEmpty(className)) { var outputFilename = Path.GetFileNameWithoutExtension(outputPath); className = CSharpIdentifier.SanitizeIdentifier(outputFilename); MetadataSerializer.SetMetadata(newItem, "ClassName", className); } var @namespace = item.GetMetadata("Namespace"); if (string.IsNullOrEmpty(@namespace)) { MetadataSerializer.SetMetadata(newItem, "Namespace", Namespace); } // Add metadata which may be used as a property and passed to an inner build. newItem.RemoveMetadata("SerializedMetadata"); newItem.SetMetadata("SerializedMetadata", MetadataSerializer.SerializeMetadata(newItem)); } Outputs = outputs.ToArray(); return(!Log.HasLoggedErrors); }