blob: f15da618473f675f3c80515e6adc75210d1d0364 [file] [log] [blame]
Jan Tattermusch7cb55972020-06-02 09:26:25 +02001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
Jan Tattermusch7cb55972020-06-02 09:26:25 +02004//
Joshua Haberman8c05a642023-09-08 17:13:18 -07005// Use of this source code is governed by a BSD-style
6// license that can be found in the LICENSE file or at
7// https://developers.google.com/open-source/licenses/bsd
Jan Tattermusch7cb55972020-06-02 09:26:25 +02008#endregion
9
10using NUnit.Framework;
11using System.Diagnostics;
12using System;
13using System.Reflection;
14using System.IO;
15
16namespace Google.Protobuf
17{
18 public class RefStructCompatibilityTest
19 {
20 /// <summary>
21 /// Checks that the generated code can be compiler with an old C# compiler.
22 /// The reason why this test is needed is that even though dotnet SDK projects allow to set LangVersion,
23 /// this setting doesn't accurately simulate compilation with an actual old pre-roslyn C# compiler.
24 /// For instance, the "ref struct" types are only supported by C# 7.2 and higher, but even if
25 /// LangVersion is set low, the roslyn compiler still understands the concept of ref struct
26 /// and silently accepts them. Therefore we try to build the generated code with an actual old C# compiler
27 /// to be able to catch these sort of compatibility problems.
28 /// </summary>
29 [Test]
30 public void GeneratedCodeCompilesWithOldCsharpCompiler()
31 {
32 if (Environment.OSVersion.Platform != PlatformID.Win32NT)
33 {
34 // This tests needs old C# compiler which is only available on Windows. Skipping it on all other platforms.
35 return;
36 }
37
38 var currentAssemblyDir = Path.GetDirectoryName(typeof(RefStructCompatibilityTest).GetTypeInfo().Assembly.Location);
39 var testProtosProjectDir = Path.GetFullPath(Path.Combine(currentAssemblyDir, "..", "..", "..", "..", "Google.Protobuf.Test.TestProtos"));
James Newton-Kinge5ae3bb2022-03-22 16:56:39 +080040 var testProtosOutputDir = (currentAssemblyDir.Contains("bin/Debug/") || currentAssemblyDir.Contains("bin\\Debug\\")) ? "bin\\Debug\\net462" : "bin\\Release\\net462";
Jan Tattermusch7cb55972020-06-02 09:26:25 +020041
42 // If "ref struct" types are used in the generated code, compilation with an old compiler will fail with the following error:
43 // "XYZ is obsolete: 'Types with embedded references are not supported in this version of your compiler.'"
44 // We build the code with GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE to avoid the use of ref struct in the generated code.
45 var compatibilityFlag = "-define:GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE";
46 var sources = "*.cs"; // the generated sources from the TestProtos project
Jon Skeetae9e22b2022-10-26 17:00:43 +010047 // We suppress CS1691, which flags a warning for the generated line of
48 // #pragma warning disable 1591, 0612, 3021, 8981
49 // because CS8981 is unknown to this version of the compiler.
50 var args = $"-langversion:3 -nologo -nowarn:1691 -target:library {compatibilityFlag} -reference:{testProtosOutputDir}\\Google.Protobuf.dll -out:{testProtosOutputDir}\\TestProtos.RefStructCompatibilityTest.OldCompiler.dll {sources}";
Jan Tattermusch7cb55972020-06-02 09:26:25 +020051 RunOldCsharpCompilerAndCheckSuccess(args, testProtosProjectDir);
52 }
53
54 /// <summary>
55 /// Invoke an old C# compiler in a subprocess and check it finished successful.
56 /// </summary>
57 /// <param name="args"></param>
58 /// <param name="workingDirectory"></param>
59 private void RunOldCsharpCompilerAndCheckSuccess(string args, string workingDirectory)
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070060 {
61 using var process = new Process();
62
63 // Get the path to the old C# 5 compiler from .NET framework. This approach is not 100% reliable, but works on most machines.
64 // Alternative way of getting the framework path is System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
65 // but it only works with the net45 target.
66 var oldCsharpCompilerPath = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "Microsoft.NET", "Framework", "v4.0.30319", "csc.exe");
67 process.StartInfo.FileName = oldCsharpCompilerPath;
68 process.StartInfo.RedirectStandardOutput = true;
69 process.StartInfo.RedirectStandardError = true;
70 process.StartInfo.UseShellExecute = false;
71 process.StartInfo.Arguments = args;
72 process.StartInfo.WorkingDirectory = workingDirectory;
73
74 process.OutputDataReceived += (sender, e) =>
Jan Tattermusch7cb55972020-06-02 09:26:25 +020075 {
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070076 if (e.Data != null)
Jan Tattermusch7cb55972020-06-02 09:26:25 +020077 {
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070078 Console.WriteLine(e.Data);
79 }
80 };
81 process.ErrorDataReceived += (sender, e) =>
82 {
83 if (e.Data != null)
Jan Tattermusch7cb55972020-06-02 09:26:25 +020084 {
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070085 Console.WriteLine(e.Data);
86 }
87 };
Jan Tattermusch7cb55972020-06-02 09:26:25 +020088
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070089 process.Start();
Jan Tattermusch7cb55972020-06-02 09:26:25 +020090
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070091 process.BeginErrorReadLine();
92 process.BeginOutputReadLine();
Jan Tattermusch7cb55972020-06-02 09:26:25 +020093
Erik Mavrinac9f58ee32022-06-23 02:05:16 -070094 process.WaitForExit();
95 Assert.AreEqual(0, process.ExitCode);
Jan Tattermusch7cb55972020-06-02 09:26:25 +020096 }
97 }
98}