-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSGen_Tests.cs
More file actions
366 lines (315 loc) · 14.6 KB
/
SGen_Tests.cs
File metadata and controls
366 lines (315 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;
#nullable disable
namespace Microsoft.Build.UnitTests
{
public class SGen_Tests
{
#if RUNTIME_TYPE_NETCORE
[Fact]
public void TaskFailsOnCore()
{
using (TestEnvironment testenv = TestEnvironment.Create())
{
MockLogger logger = ObjectModelHelpers.BuildProjectExpectFailure(@$"
<Project>
<Target Name=""MyTarget"">
<SGen
BuildAssemblyName=""Foo""
BuildAssemblyPath=""Foo""
ShouldGenerateSerializer=""true""
UseProxyTypes=""true""
UseKeep=""true""
References=""Foo""
KeyContainer=""Foo""
KeyFile=""Foo""
DelaySign=""true""
SerializationAssembly=""Foo""
SdkToolsPath=""Foo""
Platform=""Foo""
Types=""Foo""
/>
</Target>
</Project>");
logger.ErrorCount.ShouldBe(1);
logger.Errors.First().Code.ShouldBe("MSB3474");
}
}
#else
internal sealed class SGenExtension : SGen
{
internal string CommandLine()
{
return base.GenerateCommandLineCommands();
}
}
[Fact]
public void KeyFileQuotedOnCommandLineIfNecessary()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
// This should result in a nested, quoted parameter on
// the command line, which ultimately looks like this:
//
// /compiler:"/keyfile:\"c:\Some Folder\MyKeyFile.snk\""
//
sgen.KeyFile = "c:\\Some Folder\\MyKeyFile.snk";
string commandLine = sgen.CommandLine();
Assert.True(commandLine.IndexOf("/compiler:\"/keyfile:\\\"" + sgen.KeyFile + "\\\"\"", StringComparison.OrdinalIgnoreCase) >= 0);
}
[Fact]
public void TestKeepFlagTrue()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
sgen.UseKeep = true;
string commandLine = sgen.CommandLine();
Assert.True(commandLine.IndexOf("/keep", StringComparison.OrdinalIgnoreCase) >= 0);
}
[Fact]
public void TestKeepFlagFalse()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
sgen.UseKeep = false;
string commandLine = sgen.CommandLine();
Assert.True(commandLine.IndexOf("/keep", StringComparison.OrdinalIgnoreCase) < 0);
}
[Fact]
public void TestInputChecks1()
{
MockEngine engine = new MockEngine();
SGenExtension sgen = new SGenExtension();
sgen.BuildEngine = engine;
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + Path.GetInvalidPathChars()[0];
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
// This should result in a quoted parameter...
sgen.KeyFile = "c:\\Some Folder\\MyKeyFile.snk";
string commandLine = sgen.CommandLine();
Assert.Equal(1, engine.Errors);
}
[Fact]
public void TestInputChecks2()
{
MockEngine engine = new MockEngine();
SGenExtension sgen = new SGenExtension();
sgen.BuildEngine = engine;
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll" + Path.GetInvalidPathChars()[0];
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
// This should result in a quoted parameter...
sgen.KeyFile = "c:\\Some Folder\\MyKeyFile.snk";
string commandLine = sgen.CommandLine();
Assert.Equal(1, engine.Errors);
}
[Fact]
public void TestInputChecks3()
{
Assert.Throws<ArgumentNullException>(() =>
{
MockEngine engine = new MockEngine();
SGenExtension sgen = new SGenExtension();
sgen.BuildEngine = engine;
sgen.BuildAssemblyName = null;
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
// This should result in a quoted parameter...
sgen.KeyFile = "c:\\Some Folder\\MyKeyFile.snk";
string commandLine = sgen.CommandLine();
});
}
[Fact]
public void TestInputChecks4()
{
Assert.Throws<ArgumentNullException>(() =>
{
MockEngine engine = new MockEngine();
SGenExtension sgen = new SGenExtension();
sgen.BuildEngine = engine;
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = null;
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
// This should result in a quoted parameter...
sgen.KeyFile = "c:\\Some Folder\\MyKeyFile.snk";
string commandLine = sgen.CommandLine();
});
}
[Fact]
public void TestInputPlatform()
{
SGenExtension sgen = new SGenExtension();
sgen.Platform = "x86";
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = NativeMethodsShared.IsUnixLike
? "/SomeFolder/MyAsm.dll"
: "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
string commandLine = sgen.CommandLine();
string targetCommandLine = "/assembly:\"" + sgen.BuildAssemblyPath + Path.DirectorySeparatorChar
+ "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" /compiler:/platform:x86";
Assert.Equal(targetCommandLine, commandLine);
}
[Fact]
public void TestInputTypes()
{
SGenExtension sgen = new SGenExtension();
sgen.Types = new string[] { "System.String", "System.Boolean" };
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = NativeMethodsShared.IsUnixLike
? "/SomeFolder/MyAsm.dll"
: "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
string commandLine = sgen.CommandLine();
string targetCommandLine = "/assembly:\"" + sgen.BuildAssemblyPath + Path.DirectorySeparatorChar
+ "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" /type:System.String /type:System.Boolean";
Assert.Equal(targetCommandLine, commandLine);
}
[Fact]
public void TestInputEmptyTypesAndPlatform()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = NativeMethodsShared.IsUnixLike ? "/SomeFolder/MyAsm.dll" : "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
string commandLine = sgen.CommandLine();
string targetCommandLine = "/assembly:\"" + sgen.BuildAssemblyPath + Path.DirectorySeparatorChar
+ "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\"";
Assert.Equal(targetCommandLine, commandLine);
}
[Fact]
public void TestNullReferences()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
sgen.UseKeep = false;
sgen.References = null;
string commandLine = sgen.CommandLine();
Assert.True(commandLine.IndexOf("/reference:", StringComparison.OrdinalIgnoreCase) < 0);
}
[Fact]
public void TestEmptyReferences()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
sgen.UseKeep = false;
sgen.References = Array.Empty<string>();
string commandLine = sgen.CommandLine();
Assert.True(commandLine.IndexOf("/reference:", StringComparison.OrdinalIgnoreCase) < 0);
}
[Fact]
public void TestReferencesCommandLine()
{
SGenExtension sgen = new SGenExtension();
sgen.BuildAssemblyName = "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
sgen.BuildAssemblyPath = "C:\\SomeFolder\\MyAsm.dll";
sgen.ShouldGenerateSerializer = true;
sgen.UseProxyTypes = false;
sgen.UseKeep = false;
sgen.References = new string[] { "C:\\SomeFolder\\reference1.dll", "C:\\SomeFolder\\reference2.dll" };
string commandLine = sgen.CommandLine();
string targetCommandLine = "/assembly:\"" + sgen.BuildAssemblyPath + Path.DirectorySeparatorChar
+ "MyAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" /reference:\"C:\\SomeFolder\\reference1.dll,C:\\SomeFolder\\reference2.dll\"";
Assert.Equal(targetCommandLine, commandLine);
}
/// <summary>
/// Verifies that GenerateFullPathToTool returns an absolute path (or null)
/// when called with a multithreaded TaskEnvironment, validating the
/// TaskEnvironment.GetAbsolutePath() integration.
/// </summary>
[Fact]
public void GenerateFullPathToTool_ReturnsAbsolutePathOrNull()
{
string projectDir = Path.GetTempPath();
using var driver = new MultiThreadedTaskEnvironmentDriver(projectDir);
var taskEnv = new TaskEnvironment(driver);
TestableSGen t = new TestableSGen();
t.TaskEnvironment = taskEnv;
t.BuildEngine = new MockEngine();
string result = t.CallGenerateFullPathToTool();
if (result is not null)
{
Path.IsPathRooted(result).ShouldBeTrue(
$"GenerateFullPathToTool should return an absolute path, got: {result}");
}
}
/// <summary>
/// Verifies that the GetProcessStartInfo routes through the multithreadable path
/// when TaskEnvironment uses MultiThreadedTaskEnvironmentDriver,
/// and that the working directory comes from the TaskEnvironment.
/// </summary>
[Fact]
public void GetProcessStartInfo_UsesTaskEnvironmentWorkingDirectory()
{
string expectedWorkingDir = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
using var driver = new MultiThreadedTaskEnvironmentDriver(expectedWorkingDir);
var taskEnv = new TaskEnvironment(driver);
TestableSGen t = new TestableSGen();
t.TaskEnvironment = taskEnv;
t.BuildEngine = new MockEngine();
ProcessStartInfo startInfo = t.CallGetProcessStartInfo(@"C:\test\sgen.exe", "/nologo", null);
startInfo.WorkingDirectory.ShouldBe(expectedWorkingDir);
}
/// <summary>
/// Verifies that BuildAssemblyPath uses TaskEnvironment.GetAbsolutePath
/// instead of Path.GetFullPath, by providing a relative path that resolves
/// correctly against the TaskEnvironment's project directory.
/// </summary>
[Fact]
public void BuildAssemblyPath_UsesTaskEnvironmentGetAbsolutePath()
{
using var env = TestEnvironment.Create();
string projectDir = env.CreateFolder().Path;
string subDir = "output";
Directory.CreateDirectory(Path.Combine(projectDir, subDir));
using var driver = new MultiThreadedTaskEnvironmentDriver(projectDir);
var taskEnv = new TaskEnvironment(driver);
TestableSGen t = new TestableSGen();
t.TaskEnvironment = taskEnv;
t.BuildEngine = new MockEngine();
t.BuildAssemblyPath = subDir; // relative path
string result = t.BuildAssemblyPath;
Path.IsPathRooted(result).ShouldBeTrue(
"BuildAssemblyPath should return an absolute path resolved via TaskEnvironment");
result.ShouldBe(Path.Combine(projectDir, subDir));
}
/// <summary>
/// Subclass that exposes protected methods for testing without reflection.
/// </summary>
private sealed class TestableSGen : SGen
{
public string CallGenerateFullPathToTool() => GenerateFullPathToTool();
public ProcessStartInfo CallGetProcessStartInfo(string pathToTool, string commandLineCommands, string responseFileSwitch)
=> GetProcessStartInfo(pathToTool, commandLineCommands, responseFileSwitch);
}
#endif
}
}