From 74917b9090a7561d4f96bec70b159378b52d15c7 Mon Sep 17 00:00:00 2001 From: Garrett Serack Date: Thu, 16 Jun 2016 13:22:48 -0700 Subject: [PATCH] Summary comments contain improperly escaped backslashes (Issue #1152) (#1176) * removing escaping of backslashes in comments. * Adding unit test project for CSharp generator * making MemoryFileSystem IDisposable to support test parallelization better * search for tools better * added csharp unit tests to .sln * regenerated expected acceptance test files * Adjusted namespace --- AutoRest.sln | 15 ++++ AutoRest/AutoRest.Core/Template.cs | 3 - .../Utilities/MemoryFileSystem.cs | 16 +++- .../Validation/AutoRestValidationTest.cs | 2 +- .../Validation/IAutoRestValidationTest.cs | 2 +- .../CSharp/CSharp.Unit.Tests/Bug1152.cs | 26 ++++++ .../CSharp/CSharp.Unit.Tests/Bug1161.cs | 24 ++++++ .../CSharp/CSharp.Unit.Tests/BugTest.cs | 24 ++++++ .../CSharp.Unit.Tests.csproj | 86 +++++++++++++++++++ .../CSharp.Unit.Tests/Resource/AutoRest.json | 24 ++++++ .../CSharp.Unit.Tests/Resource/Bug1152.yaml | 20 +++++ .../CSharp.Unit.Tests/Resource/Bug1161.yaml | 77 +++++++++++++++++ .../CSharp.Unit.Tests/TestExtensions.cs | 43 ++++++++++ .../CSharp/CSharp.Unit.Tests/app.config | 6 ++ .../CSharp/CSharp.Unit.Tests/packages.config | 12 +++ Tools/verify-settings.ps1 | 4 +- 16 files changed, 376 insertions(+), 8 deletions(-) create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1152.cs create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1161.cs create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/BugTest.cs create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/CSharp.Unit.Tests.csproj create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/AutoRest.json create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1152.yaml create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1161.yaml create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/TestExtensions.cs create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/app.config create mode 100644 AutoRest/Generators/CSharp/CSharp.Unit.Tests/packages.config diff --git a/AutoRest.sln b/AutoRest.sln index c3c9fe2c2148a..a3b6f48ed2caa 100644 --- a/AutoRest.sln +++ b/AutoRest.sln @@ -124,6 +124,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CSharp.Tests", "AutoRest\Ge EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Azure.CSharp.Tests", "AutoRest\Generators\CSharp\Azure.CSharp.Tests\Azure.CSharp.Tests.xproj", "{43DFEF27-2FD7-4827-A669-DFD2DD1CBBCC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp.Unit.Tests", "AutoRest\Generators\CSharp\CSharp.Unit.Tests\CSharp.Unit.Tests.csproj", "{8192BAA3-C606-4D44-B0D7-46BE7AFA4096}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -518,6 +520,18 @@ Global {43DFEF27-2FD7-4827-A669-DFD2DD1CBBCC}.Portable-Release|Any CPU.Build.0 = Release|Any CPU {43DFEF27-2FD7-4827-A669-DFD2DD1CBBCC}.Release|Any CPU.ActiveCfg = Release|Any CPU {43DFEF27-2FD7-4827-A669-DFD2DD1CBBCC}.Release|Any CPU.Build.0 = Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Debug|Any CPU.ActiveCfg = Portable-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Debug|Any CPU.Build.0 = Portable-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Net45-Debug|Any CPU.ActiveCfg = Net45-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Net45-Debug|Any CPU.Build.0 = Net45-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Net45-Release|Any CPU.ActiveCfg = Net45-Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Net45-Release|Any CPU.Build.0 = Net45-Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Portable-Debug|Any CPU.ActiveCfg = Portable-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Portable-Debug|Any CPU.Build.0 = Portable-Debug|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Portable-Release|Any CPU.ActiveCfg = Portable-Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Portable-Release|Any CPU.Build.0 = Portable-Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Release|Any CPU.ActiveCfg = Portable-Release|Any CPU + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096}.Release|Any CPU.Build.0 = Portable-Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -562,5 +576,6 @@ Global {1C3B4A33-E045-4C8F-9202-1B651A686567} = {B6A1FC1E-0779-4900-84EE-262DD0E53FF4} {784D4B45-F12A-4B8F-A86C-35A080814A19} = {03E75B1D-A443-4A12-B31F-860DB2B45140} {43DFEF27-2FD7-4827-A669-DFD2DD1CBBCC} = {03E75B1D-A443-4A12-B31F-860DB2B45140} + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096} = {03E75B1D-A443-4A12-B31F-860DB2B45140} EndGlobalSection EndGlobal diff --git a/AutoRest/AutoRest.Core/Template.cs b/AutoRest/AutoRest.Core/Template.cs index f74c816d34475..31b8ad37e733a 100644 --- a/AutoRest/AutoRest.Core/Template.cs +++ b/AutoRest/AutoRest.Core/Template.cs @@ -222,9 +222,6 @@ protected virtual string WrapComment(string prefix, string comment) return null; } - // escape comment as needed - comment = comment.Replace("\\", Resources.CommentString); - int available = MaximumCommentColumn - // Maximum desired width Indentation.Length - // - Space used for indent diff --git a/AutoRest/AutoRest.Core/Utilities/MemoryFileSystem.cs b/AutoRest/AutoRest.Core/Utilities/MemoryFileSystem.cs index 97fa11bd3b098..f7a0f378d8135 100644 --- a/AutoRest/AutoRest.Core/Utilities/MemoryFileSystem.cs +++ b/AutoRest/AutoRest.Core/Utilities/MemoryFileSystem.cs @@ -12,7 +12,7 @@ namespace Microsoft.Rest.Generator.Utilities { // TODO: MemoryFileSystem is for testing. Consider moving to test project. - public class MemoryFileSystem : IFileSystem + public class MemoryFileSystem : IFileSystem, IDisposable { private const string FolderKey = "Folder"; @@ -177,5 +177,19 @@ private static string WildcardToRegex(string wildcard) } return sb.ToString().ToLowerInvariant(); } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _virtualStore?.Clear(); + } + } } } diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs index 10084ec790a3b..f8e5b3cf7e419 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/AutoRestValidationTest.cs @@ -51,7 +51,7 @@ public partial class AutoRestValidationTest : ServiceClient - /// Required string following pattern \\\\d{2}-\\\\d{2}-\\\\d{4} + /// Required string following pattern \d{2}-\d{2}-\d{4} /// public string ApiVersion { get; set; } diff --git a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/IAutoRestValidationTest.cs b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/IAutoRestValidationTest.cs index 5a0622eff83e8..b3e1259788538 100644 --- a/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/IAutoRestValidationTest.cs +++ b/AutoRest/Generators/CSharp/CSharp.Tests/Expected/AcceptanceTests/Validation/IAutoRestValidationTest.cs @@ -44,7 +44,7 @@ public partial interface IAutoRestValidationTest : IDisposable string SubscriptionId { get; set; } /// - /// Required string following pattern \\\\d{2}-\\\\d{2}-\\\\d{4} + /// Required string following pattern \d{2}-\d{2}-\d{4} /// string ApiVersion { get; set; } diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1152.cs b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1152.cs new file mode 100644 index 0000000000000..bbccff72be49a --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1152.cs @@ -0,0 +1,26 @@ +using System.Text.RegularExpressions; +using Xunit; + +namespace Microsoft.Rest.Generator.CSharp.UnitTest +{ + public class Bug1152 : BugTest + { + /// + /// https://github.com/Azure/autorest/issues/1152 + /// + /// The C# code generator seems to needlessly escape backslashes when generating /// + /// + [Fact] + public void SummaryCommentsContainImproperlyEscapedBackslashes() + { + // simplified test pattern for unit testing aspects of code generation + using (var fileSystem = "Bug1152.yaml".GenerateCodeInto(CreateMockFilesystem())) + { + Assert.True(fileSystem.FileExists(@"GeneratedCode\Models\TestObject.cs")); + var testObject = fileSystem.ReadFileAsText(@"GeneratedCode\Models\TestObject.cs"); + + Assert.DoesNotContain(@"\\\\" , Regex.Match(testObject, "Default is.*").Value); + } + } + } +} \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1161.cs b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1161.cs new file mode 100644 index 0000000000000..48fad472f91db --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Bug1161.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Xunit; + +namespace Microsoft.Rest.Generator.CSharp.UnitTest +{ + public class Bug1161 : BugTest + { + [Fact] + public void DoubleQuestionMarksWhenUsingEnumsInAnArray() + { + using (var fileSystem = "Bug1161.yaml".GenerateCodeInto(CreateMockFilesystem())) + { + Assert.True(fileSystem.FileExists(@"GeneratedCode\Models\RecurrenceSchedule.cs")); + + var recurrenceSchedule = fileSystem.ReadFileAsText(@"GeneratedCode\Models\RecurrenceSchedule.cs"); + Assert.NotNull(recurrenceSchedule); + + + } + } + } +} \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/BugTest.cs b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/BugTest.cs new file mode 100644 index 0000000000000..f072c9a81b480 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/BugTest.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.IO; +using Microsoft.Rest.Generator.Logging; +using Microsoft.Rest.Generator.Utilities; + +namespace Microsoft.Rest.Generator.CSharp.UnitTest +{ + public class BugTest + { + public BugTest() + { + Logger.Entries.Clear(); + } + + protected MemoryFileSystem CreateMockFilesystem() + { + var fs = new MemoryFileSystem(); + fs.Copy(Path.Combine("Resource", "AutoRest.json")); + return fs; + } + } +} \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/CSharp.Unit.Tests.csproj b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/CSharp.Unit.Tests.csproj new file mode 100644 index 0000000000000..f555000fe4523 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/CSharp.Unit.Tests.csproj @@ -0,0 +1,86 @@ + + + + + + + ..\..\..\ + AnyCPU + net45 + 10.0.0 + 2.0 + {8192BAA3-C606-4D44-B0D7-46BE7AFA4096} + Library + Microsoft.Rest.Generator.CSharp.UnitTest + AutoRest.Csharp.Unit.Tests + true + true + Net45-Debug + + + + false + bin\Net45-Debug + 4 + + + + + + $(SolutionDir)\packages\xunit.abstractions.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.abstractions.dll + + + $(SolutionDir)\packages\xunit.assert.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.assert.dll + True + + + $(SolutionDir)\packages\xunit.extensibility.core.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.core.dll + True + + + $(SolutionDir)\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll + True + + + + + + + + + + + {c876085f-9dc3-41f0-b7b4-17022cd84684} + AutoRest.Core + + + {cc1eec95-41ef-44b6-8761-00fa3e647248} + AutoRest.Generator.CSharp + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/AutoRest.json b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/AutoRest.json new file mode 100644 index 0000000000000..faa066a9b056f --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/AutoRest.json @@ -0,0 +1,24 @@ +{ + "codeGenerators": { + "Angular": { + "type": "JavaScriptAngularCodeGenerator, AutoRest.Generator.JavaScript.Angular" + }, + "CSharp": { + "type": "CSharpCodeGenerator, AutoRest.Generator.CSharp" + }, + "NodeJS": { + "type": "NodeJSCodeGenerator, AutoRest.Generator.NodeJS", + "settings": { + "disableTypeScriptGeneration": true + } + } + }, + "modelers": { + "Swagger": { + "type": "SwaggerModeler, AutoRest.Modeler.Swagger" + }, + "CompositeSwagger": { + "type": "CompositeSwaggerModeler, AutoRest.Modeler.CompositeSwagger" + } + } +} \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1152.yaml b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1152.yaml new file mode 100644 index 0000000000000..8e8b4a218ef5a --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1152.yaml @@ -0,0 +1,20 @@ +swagger: '2.0' +info: + version: 1.0.0 + title: Simple API +paths: + /: + get: + operationId: my_operation + responses: + 200: + description: OK + schema: + $ref: '#/definitions/TestObject' +definitions: + TestObject: + properties: + pattern: + type: string + default: '\w+' + description: 'A regular expression pattern to match token separators. Default is "\W+".' diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1161.yaml b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1161.yaml new file mode 100644 index 0000000000000..2771e3bc2837c --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/Resource/Bug1161.yaml @@ -0,0 +1,77 @@ +swagger: '2.0' +info: + version: 1.0.0 + title: Simple API +paths: + /getday: + get: + operationId: getday + responses: + 200: + description: OK + schema: + $ref: '#/definitions/SomethingWithADay' + /: + get: + operationId: myoperation + responses: + 200: + description: OK + schema: + $ref: '#/definitions/testObject' + + /getSchedule: + get: + operationId: getschedule + responses: + 200: + description: OK + schema: + $ref: '#/definitions/RecurrenceSchedule' + +definitions: + + DayOfWeek: + type: string + enum: + - Sunday + - Monday + - Tuesday + - Wednesday + - Thursday + - Friday + - Saturday + x-ms-enum: + name: DayOfWeek + modelAsString: false + + SomethingWithADay: + type: object + properties: + theDay : + type: array + $ref: '#/definitions/DayOfWeek' + anotherday: + type: array + $ref: '#/definitions/DayOfWeek' + + testObject: + properties: + pattern: + type: string + default: '\w+' + description: 'A regular expression pattern to match token separators. Default is "\W+".' + + RecurrenceSchedule: + type: object + properties: + weekDays : + type: array + items: + $ref: '#/definitions/DayOfWeek' + otherweekDays : + type: array + items: + $ref: '#/definitions/DayOfWeek' + + diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/TestExtensions.cs b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/TestExtensions.cs new file mode 100644 index 0000000000000..7c34a6f647df1 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/TestExtensions.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.IO; +using Microsoft.Rest.Generator.Extensibility; +using Microsoft.Rest.Generator.Utilities; + +namespace Microsoft.Rest.Generator.CSharp.UnitTest +{ + internal static class TestExtensions + { + internal static void Copy(this IFileSystem fileSystem , string sourceFileOnDisk) + { + Copy( fileSystem, sourceFileOnDisk, Path.GetFileName(sourceFileOnDisk)); + } + internal static void Copy(this IFileSystem fileSystem, string sourceFileOnDisk, string destination) + { + fileSystem.WriteFile(destination, File.ReadAllText(sourceFileOnDisk)); + } + + internal static MemoryFileSystem GenerateCodeInto(this string inputFile, MemoryFileSystem fileSystem ) + { + fileSystem.Copy(Path.Combine("Resource", inputFile)); + + var settings = new Settings + { + Modeler = "Swagger", + CodeGenerator = "CSharp", + FileSystem = fileSystem, + OutputDirectory = "GeneratedCode", + Input = inputFile, + }; + + var codeGenerator = new CSharpCodeGenerator(settings); + Modeler modeler = ExtensionsLoader.GetModeler(settings); + var sc = modeler.Build(); + codeGenerator.NormalizeClientModel(sc); + codeGenerator.Generate(sc).GetAwaiter().GetResult(); + + return fileSystem; + } + } +} \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/app.config b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/app.config new file mode 100644 index 0000000000000..9735dc7351385 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/app.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp.Unit.Tests/packages.config b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/packages.config new file mode 100644 index 0000000000000..ae1f28a3a41c2 --- /dev/null +++ b/AutoRest/Generators/CSharp/CSharp.Unit.Tests/packages.config @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/Tools/verify-settings.ps1 b/Tools/verify-settings.ps1 index b8f0ee93cc74a..6be742e72efa3 100644 --- a/Tools/verify-settings.ps1 +++ b/Tools/verify-settings.ps1 @@ -234,8 +234,8 @@ find-orAdd "msbuild.exe" -hint "Install Visual studio 2015" find-orAdd "javac.exe" -hint "Download and install JAVA JDK" -find-orAdd "node.exe" -hint "See: https://nodejs.org" -find-orAdd "gulp.cmd" -hint "maybe use 'npm install -g gulp'" +find-orAdd "node.exe" -hint "See: https://nodejs.org" -folders @("${env:appdata}","${env:ProgramFiles(x86)}","${env:ProgramFiles}") +find-orAdd "gulp.cmd" -hint "maybe use 'npm install -g gulp'" -folders @("${env:appdata}","${env:ProgramFiles(x86)}","${env:ProgramFiles}") find-orAdd "ruby.exe" (@() + ((dir -ea 0 c:\ruby*).fullname) + @( "${env:ProgramFiles(x86)}","${env:ProgramFiles}","c:\tools")) -hint "see http://rubyinstaller.org/downloads/"