RefStructCompatibilityTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using NUnit.Framework;
  33. using System.Diagnostics;
  34. using System;
  35. using System.Reflection;
  36. using System.IO;
  37. namespace Google.Protobuf
  38. {
  39. public class RefStructCompatibilityTest
  40. {
  41. /// <summary>
  42. /// Checks that the generated code can be compiler with an old C# compiler.
  43. /// The reason why this test is needed is that even though dotnet SDK projects allow to set LangVersion,
  44. /// this setting doesn't accurately simulate compilation with an actual old pre-roslyn C# compiler.
  45. /// For instance, the "ref struct" types are only supported by C# 7.2 and higher, but even if
  46. /// LangVersion is set low, the roslyn compiler still understands the concept of ref struct
  47. /// and silently accepts them. Therefore we try to build the generated code with an actual old C# compiler
  48. /// to be able to catch these sort of compatibility problems.
  49. /// </summary>
  50. [Test]
  51. public void GeneratedCodeCompilesWithOldCsharpCompiler()
  52. {
  53. if (Environment.OSVersion.Platform != PlatformID.Win32NT)
  54. {
  55. // This tests needs old C# compiler which is only available on Windows. Skipping it on all other platforms.
  56. return;
  57. }
  58. var currentAssemblyDir = Path.GetDirectoryName(typeof(RefStructCompatibilityTest).GetTypeInfo().Assembly.Location);
  59. var testProtosProjectDir = Path.GetFullPath(Path.Combine(currentAssemblyDir, "..", "..", "..", "..", "Google.Protobuf.Test.TestProtos"));
  60. var testProtosOutputDir = (currentAssemblyDir.Contains("bin/Debug/") || currentAssemblyDir.Contains("bin\\Debug\\")) ? "bin\\Debug\\net45" : "bin\\Release\\net45";
  61. // If "ref struct" types are used in the generated code, compilation with an old compiler will fail with the following error:
  62. // "XYZ is obsolete: 'Types with embedded references are not supported in this version of your compiler.'"
  63. // We build the code with GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE to avoid the use of ref struct in the generated code.
  64. var compatibilityFlag = "-define:GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE";
  65. var sources = "*.cs"; // the generated sources from the TestProtos project
  66. var args = $"-langversion:3 -target:library {compatibilityFlag} -reference:{testProtosOutputDir}\\Google.Protobuf.dll -out:{testProtosOutputDir}\\TestProtos.RefStructCompatibilityTest.OldCompiler.dll {sources}";
  67. RunOldCsharpCompilerAndCheckSuccess(args, testProtosProjectDir);
  68. }
  69. /// <summary>
  70. /// Invoke an old C# compiler in a subprocess and check it finished successful.
  71. /// </summary>
  72. /// <param name="args"></param>
  73. /// <param name="workingDirectory"></param>
  74. private void RunOldCsharpCompilerAndCheckSuccess(string args, string workingDirectory)
  75. {
  76. using (var process = new Process())
  77. {
  78. // Get the path to the old C# 5 compiler from .NET framework. This approach is not 100% reliable, but works on most machines.
  79. // Alternative way of getting the framework path is System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
  80. // but it only works with the net45 target.
  81. var oldCsharpCompilerPath = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "Microsoft.NET", "Framework", "v4.0.30319", "csc.exe");
  82. process.StartInfo.FileName = oldCsharpCompilerPath;
  83. process.StartInfo.RedirectStandardOutput = true;
  84. process.StartInfo.RedirectStandardError = true;
  85. process.StartInfo.UseShellExecute = false;
  86. process.StartInfo.Arguments = args;
  87. process.StartInfo.WorkingDirectory = workingDirectory;
  88. process.OutputDataReceived += (sender, e) =>
  89. {
  90. if (e.Data != null)
  91. {
  92. Console.WriteLine(e.Data);
  93. }
  94. };
  95. process.ErrorDataReceived += (sender, e) =>
  96. {
  97. if (e.Data != null)
  98. {
  99. Console.WriteLine(e.Data);
  100. }
  101. };
  102. process.Start();
  103. process.BeginErrorReadLine();
  104. process.BeginOutputReadLine();
  105. process.WaitForExit();
  106. Assert.AreEqual(0, process.ExitCode);
  107. }
  108. }
  109. }
  110. }