Debian Codegraph @

src/csharp/Grpc.Tools.Tests/ProtoToolsPlatformTaskTest.cs

raw
1 #region Copyright notice and license
2
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 #endregion
18
19 using System.Runtime.InteropServices; // For NETCORE tests.
20 using Microsoft.Build.Framework;
21 using Moq;
22 using NUnit.Framework;
23
24 namespace Grpc.Tools.Tests
25 {
26 public class ProtoToolsPlatformTaskTest
27 {
28 ProtoToolsPlatform _task;
29 int _cpuMatched, _osMatched;
30
31 [OneTimeSetUp]
32 public void SetUp()
33 {
34 var mockEng = new Mock<IBuildEngine>();
35 _task = new ProtoToolsPlatform() { BuildEngine = mockEng.Object };
36 _task.Execute();
37 }
38
39 [OneTimeTearDown]
40 public void TearDown()
41 {
42 Assert.AreEqual(1, _cpuMatched, "CPU type detection failed");
43 Assert.AreEqual(1, _osMatched, "OS detection failed");
44 }
45
46 #if NETCORE
47 // PlatformAttribute not yet available in NUnit, coming soon:
48 // https://github.com/nunit/nunit/pull/3003.
49 // Use same test case names as under the full framework.
50 [Test]
51 public void CpuIsX86()
52 {
53 if (RuntimeInformation.OSArchitecture == Architecture.X86)
54 {
55 _cpuMatched++;
56 Assert.AreEqual("x86", _task.Cpu);
57 }
58 }
59
60 [Test]
61 public void CpuIsX64()
62 {
63 if (RuntimeInformation.OSArchitecture == Architecture.X64)
64 {
65 _cpuMatched++;
66 Assert.AreEqual("x64", _task.Cpu);
67 }
68 }
69
70 [Test]
71 public void CpuIsArm64()
72 {
73 if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
74 {
75 _cpuMatched++;
76
77 // On macosx arm64, x64 is used until a native protoc is shipped
78 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
79 {
80 Assert.AreEqual("x64", _task.Cpu);
81 }
82 else
83 {
84 Assert.AreEqual("arm64", _task.Cpu);
85 }
86 }
87 }
88
89 [Test]
90 public void OsIsWindows()
91 {
92 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
93 {
94 _osMatched++;
95 Assert.AreEqual("windows", _task.Os);
96 }
97 }
98
99 [Test]
100 public void OsIsLinux()
101 {
102 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
103 {
104 _osMatched++;
105 Assert.AreEqual("linux", _task.Os);
106 }
107 }
108
109 [Test]
110 public void OsIsMacOsX()
111 {
112 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
113 {
114 _osMatched++;
115 Assert.AreEqual("macosx", _task.Os);
116 }
117 }
118
119 #else // !NETCORE, i.e. full framework.
120
121 [Test, Platform("32-Bit")]
122 public void CpuIsX86()
123 {
124 _cpuMatched++;
125 Assert.AreEqual("x86", _task.Cpu);
126 }
127
128 [Test, Platform("64-Bit")]
129 public void CpuIsX64()
130 {
131 _cpuMatched++;
132 Assert.AreEqual("x64", _task.Cpu);
133 }
134
135 [Test, Platform("Win")]
136 public void OsIsWindows()
137 {
138 _osMatched++;
139 Assert.AreEqual("windows", _task.Os);
140 }
141
142 [Test, Platform("Linux")]
143 public void OsIsLinux()
144 {
145 _osMatched++;
146 Assert.AreEqual("linux", _task.Os);
147 }
148
149 [Test, Platform("MacOSX")]
150 public void OsIsMacOsX()
151 {
152 _osMatched++;
153 Assert.AreEqual("macosx", _task.Os);
154 }
155
156 #endif // NETCORE
157 };
158 }