forked from dotnet/msbuild
/
ToolsetConfigurationReader_Tests.cs
682 lines (599 loc) · 32.7 KB
/
ToolsetConfigurationReader_Tests.cs
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Configuration;
using Microsoft.Build.Collections;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Shared;
using ToolsetConfigurationSection = Microsoft.Build.Evaluation.ToolsetConfigurationSection;
using Xunit;
using System;
using System.Collections.Generic;
namespace Microsoft.Build.UnitTests.Definition
{
/// <summary>
/// Unit tests for ToolsetConfigurationReader class
/// </summary>
public class ToolsetConfigurationReaderTests : IDisposable
{
private static string s_msbuildToolsets = "msbuildToolsets";
public void Dispose()
{
ToolsetConfigurationReaderTestHelper.CleanUp();
}
#region "msbuildToolsets element tests"
/// <summary>
/// msbuildToolsets element is empty
/// </summary>
[Fact]
public void MSBuildToolsetsTest_EmptyElement()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets />
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
Assert.Null(msbuildToolsetSection.MSBuildOverrideTasksPath);
Assert.NotNull(msbuildToolsetSection);
Assert.Null(msbuildToolsetSection.Default);
Assert.NotNull(msbuildToolsetSection.Toolsets);
Assert.Empty(msbuildToolsetSection.Toolsets);
}
/// <summary>
/// tests if ToolsetConfigurationReaderTests is successfully initialized from the config file
/// </summary>
[Fact]
public void MSBuildToolsetsTest_Basic()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ConfigurationSection section = config.GetSection(s_msbuildToolsets);
ToolsetConfigurationSection msbuildToolsetSection = section as ToolsetConfigurationSection;
Assert.Null(msbuildToolsetSection.MSBuildOverrideTasksPath);
Assert.Equal("2.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal("2.0", msbuildToolsetSection.Toolsets.GetElement(0).toolsVersion);
Assert.Single(msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements);
Assert.Equal(
@"D:\windows\Microsoft.NET\Framework\v2.0.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("MSBuildBinPath").Value);
Assert.Empty(msbuildToolsetSection.Toolsets.GetElement(0).AllProjectImportSearchPaths);
}
/// <summary>
/// Tests if ToolsetConfigurationReaderTests is successfully initialized from the config file when msbuildOVerrideTasksPath is set.
/// Also verify the msbuildOverrideTasksPath is properly read in.
/// </summary>
[Fact]
public void MSBuildToolsetsTest_Basic2()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"" msbuildOverrideTasksPath=""c:\foo"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ConfigurationSection section = config.GetSection(s_msbuildToolsets);
ToolsetConfigurationSection msbuildToolsetSection = section as ToolsetConfigurationSection;
Assert.Equal("c:\\foo", msbuildToolsetSection.MSBuildOverrideTasksPath);
}
/// <summary>
/// Tests if ToolsetConfigurationReaderTests is successfully initialized from the config file and that msbuildOVerrideTasksPath
/// is correctly read in when the value is empty.
/// </summary>
[Fact]
public void MSBuildToolsetsTest_Basic3()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"" msbuildOverrideTasksPath="""">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ConfigurationSection section = config.GetSection(s_msbuildToolsets);
ToolsetConfigurationSection msbuildToolsetSection = section as ToolsetConfigurationSection;
Assert.Null(msbuildToolsetSection.MSBuildOverrideTasksPath);
}
/// <summary>
/// tests if ToolsetConfigurationReaderTests is successfully initialized from the config file
/// </summary>
[Fact]
public void MSBuildToolsetsTest_BasicWithOtherConfigEntries()
{
// NOTE: for some reason, <configSections> MUST be the first element under <configuration>
// for the API to read it. The docs don't make this clear.
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<startup>
<supportedRuntime imageVersion=""v2.0.60510"" version=""v2.0.x86chk""/>
<requiredRuntime imageVersion=""v2.0.60510"" version=""v2.0.x86chk"" safemode=""true""/>
</startup>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
</msbuildToolsets>
<runtime>
<assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
<dependentAssembly>
<assemblyIdentity name=""Microsoft.Build.Framework"" publicKeyToken=""b03f5f7f11d50a3a"" culture=""neutral""/>
<bindingRedirect oldVersion=""0.0.0.0-99.9.9.9"" newVersion=""2.0.0.0""/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
Assert.Equal("2.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal("2.0", msbuildToolsetSection.Toolsets.GetElement(0).toolsVersion);
Assert.Single(msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements);
Assert.Equal(
@"D:\windows\Microsoft.NET\Framework\v2.0.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("MSBuildBinPath").Value);
Assert.Empty(msbuildToolsetSection.Toolsets.GetElement(0).AllProjectImportSearchPaths);
}
#endregion
#region "toolsVersion element tests"
#region "Invalid cases (exception is expected to be thrown)"
/// <summary>
/// name attribute is missing from toolset element
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
public void ToolsVersionTest_NameNotSpecified()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset>
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
<toolset toolsVersion=""4.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// More than 1 toolset element with the same name
/// </summary>
[Fact]
public void ToolsVersionTest_MultipleElementsWithSameName()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
</toolset>
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// empty toolset element
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
public void ToolsVersionTest_EmptyElement()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset />
<toolset toolsVersion=""4.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
#endregion
#region "Valid cases (No exception expected)"
/// <summary>
/// only 1 toolset is specified
/// </summary>
[Fact]
public void ToolsVersionTest_SingleElement()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset toolsVersion=""4.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
Assert.Equal("4.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal("4.0", msbuildToolsetSection.Toolsets.GetElement(0).toolsVersion);
Assert.Single(msbuildToolsetSection.Toolsets.GetElement("4.0").PropertyElements);
Assert.Equal(
@"D:\windows\Microsoft.NET\Framework\v3.5.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("4.0").PropertyElements.GetElement("MSBuildBinPath").Value);
}
#endregion
#endregion
#region "Property"
#region "Invalid cases (exception is expected to be thrown)"
/// <summary>
/// name attribute is missing
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
public void PropertyTest_NameNotSpecified()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset toolsVersion=""4.0"">
<property value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// value attribute is missing
/// </summary>
[Fact]
public void PropertyTest_ValueNotSpecified()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset name=""4.0"">
<property name=""MSBuildBinPath"" />
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// more than 1 property element with the same name
/// </summary>
[Fact]
public void PropertyTest_MultipleElementsWithSameName()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset ToolsVersion=""msbuilddefaulttoolsversion"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// property element is an empty element
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
public void PropertyTest_EmptyElement()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset toolsVersion=""4.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<property />
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
#endregion
#region "Valid cases"
/// <summary>
/// more than 1 property element specified
/// </summary>
[Fact]
public void PropertyTest_MultipleElement()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<property name=""SomeOtherPropertyName"" value=""SomeOtherPropertyValue""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
Assert.Equal("2.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal(2, msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.Count);
Assert.Equal(
@"D:\windows\Microsoft.NET\Framework\v2.0.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("MSBuildBinPath").Value);
Assert.Equal(
@"SomeOtherPropertyValue",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("SomeOtherPropertyName").Value);
}
/// <summary>
/// tests GetElement(string name) function in propertycollection class
/// </summary>
[Fact]
public void PropertyTest_GetValueByName()
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<property name=""SomeOtherPropertyName"" value=""SomeOtherPropertyValue""/>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
// Verifications
Assert.Equal("2.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal(2, msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.Count);
Assert.Equal(@"D:\windows\Microsoft.NET\Framework\v2.0.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("MSBuildBinPath").Value);
Assert.Equal(@"SomeOtherPropertyValue",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("SomeOtherPropertyName").Value);
}
#endregion
#endregion
#region Extensions Paths
/// <summary>
/// Tests multiple extensions paths from the config file, specified for multiple OSes
/// </summary>
[Fact]
public void ExtensionPathsTest_Basic1()
{
// NOTE: for some reason, <configSections> MUST be the first element under <configuration>
// for the API to read it. The docs don't make this clear.
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""2.0"">
<toolset toolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<property name=""MSBuildToolsPath"" value=""D:\windows\Microsoft.NET\Framework\v2.0.x86ret\""/>
<projectImportSearchPaths>
<searchPaths os=""windows"">
<property name=""MSBuildExtensionsPath"" value=""c:\foo""/>
<property name=""MSBuildExtensionsPath64"" value=""c:\foo64;c:\bar64""/>
</searchPaths>
<searchPaths os=""osx"">
<property name=""MSBuildExtensionsPath"" value=""/tmp/foo""/>
<property name=""MSBuildExtensionsPath32"" value=""/tmp/foo32;/tmp/bar32""/>
</searchPaths>
<searchPaths os=""unix"">
<property name=""MSBuildExtensionsPath"" value=""/tmp/bar""/>
</searchPaths>
</projectImportSearchPaths>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
ToolsetConfigurationSection msbuildToolsetSection = config.GetSection(s_msbuildToolsets) as ToolsetConfigurationSection;
Assert.Equal("2.0", msbuildToolsetSection.Default);
Assert.Single(msbuildToolsetSection.Toolsets);
Assert.Equal("2.0", msbuildToolsetSection.Toolsets.GetElement(0).toolsVersion);
Assert.Equal(2, msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.Count);
Assert.Equal(
@"D:\windows\Microsoft.NET\Framework\v2.0.x86ret\",
msbuildToolsetSection.Toolsets.GetElement("2.0").PropertyElements.GetElement("MSBuildBinPath").Value);
Assert.Equal(3, msbuildToolsetSection.Toolsets.GetElement(0).AllProjectImportSearchPaths.Count);
var allPaths = msbuildToolsetSection.Toolsets.GetElement(0).AllProjectImportSearchPaths;
Assert.Equal("windows", allPaths.GetElement(0).OS);
Assert.Equal(2, allPaths.GetElement(0).PropertyElements.Count);
Assert.Equal(@"c:\foo", allPaths.GetElement(0).PropertyElements.GetElement("MSBuildExtensionsPath").Value);
Assert.Equal(@"c:\foo64;c:\bar64", allPaths.GetElement(0).PropertyElements.GetElement("MSBuildExtensionsPath64").Value);
Assert.Equal("osx", allPaths.GetElement(1).OS);
Assert.Equal(2, allPaths.GetElement(1).PropertyElements.Count);
Assert.Equal(@"/tmp/foo", allPaths.GetElement(1).PropertyElements.GetElement("MSBuildExtensionsPath").Value);
Assert.Equal(@"/tmp/foo32;/tmp/bar32", allPaths.GetElement(1).PropertyElements.GetElement("MSBuildExtensionsPath32").Value);
Assert.Equal("unix", allPaths.GetElement(2).OS);
Assert.Single(allPaths.GetElement(2).PropertyElements);
Assert.Equal( @"/tmp/bar", allPaths.GetElement(2).PropertyElements.GetElement("MSBuildExtensionsPath").Value);
var reader = GetStandardConfigurationReader();
Dictionary<string, Toolset> toolsets = new Dictionary<string, Toolset>(StringComparer.OrdinalIgnoreCase);
reader.ReadToolsets(toolsets, new PropertyDictionary<ProjectPropertyInstance>(),
new PropertyDictionary<ProjectPropertyInstance>(), true,
out string msbuildOverrideTasksPath, out string defaultOverrideToolsVersion);
Dictionary<string, ProjectImportPathMatch> pathsTable = toolsets["2.0"].ImportPropertySearchPathsTable;
if (NativeMethodsShared.IsWindows)
{
CheckPathsTable(pathsTable, "MSBuildExtensionsPath", new string[] {"c:\\foo"});
CheckPathsTable(pathsTable, "MSBuildExtensionsPath64", new string[] {"c:\\foo64", "c:\\bar64"});
}
else if (NativeMethodsShared.IsOSX)
{
CheckPathsTable(pathsTable, "MSBuildExtensionsPath", new string[] {"/tmp/foo"});
CheckPathsTable(pathsTable, "MSBuildExtensionsPath32", new string[] {"/tmp/foo32", "/tmp/bar32"});
}
else
{
CheckPathsTable(pathsTable, "MSBuildExtensionsPath", new string[] {"/tmp/bar"});
}
}
private void CheckPathsTable(Dictionary<string, ProjectImportPathMatch> pathsTable, string kind, string[] expectedPaths)
{
Assert.True(pathsTable.ContainsKey(kind));
var paths = pathsTable[kind];
Assert.Equal(paths.SearchPaths.Count, expectedPaths.Length);
for (int i = 0; i < paths.SearchPaths.Count; i ++)
{
Assert.Equal(paths.SearchPaths[i], expectedPaths[i]);
}
}
/// <summary>
/// more than 1 searchPaths elements with the same OS
/// </summary>
[Fact]
public void ExtensionsPathsTest_MultipleElementsWithSameOS()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset ToolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
<projectImportSearchPaths>
<searchPaths os=""windows"">
<property name=""MSBuildExtensionsPath"" value=""c:\foo""/>
</searchPaths>
<searchPaths os=""windows"">
<property name=""MSBuildExtensionsPath"" value=""c:\bar""/>
</searchPaths>
</projectImportSearchPaths>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
/// <summary>
/// more than value is element found for a the same extensions path property name+os
/// </summary>
[Fact]
public void ExtensionsPathsTest_MultipleElementsWithSamePropertyNameForSameOS()
{
Assert.Throws<ConfigurationErrorsException>(() =>
{
ToolsetConfigurationReaderTestHelper.WriteConfigFile(ObjectModelHelpers.CleanupFileContents(@"
<configuration>
<configSections>
<section name=""msbuildToolsets"" type=""Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build"" />
</configSections>
<msbuildToolsets default=""4.0"">
<toolset ToolsVersion=""2.0"">
<property name=""MSBuildBinPath"" value=""D:\windows\Microsoft.NET\Framework\v3.5.x86ret\""/>
<projectImportSearchPaths>
<searchPaths os=""windows"">
<property name=""MSBuildExtensionsPath"" value=""c:\foo""/>
<property name=""MSBuildExtensionsPath"" value=""c:\bar""/>
</searchPaths>
</projectImportSearchPaths>
</toolset>
</msbuildToolsets>
</configuration>"));
Configuration config = ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest();
config.GetSection(s_msbuildToolsets);
}
);
}
private ToolsetConfigurationReader GetStandardConfigurationReader()
{
return new ToolsetConfigurationReader(new ProjectCollection().EnvironmentProperties, new PropertyDictionary<ProjectPropertyInstance>(), ToolsetConfigurationReaderTestHelper.ReadApplicationConfigurationTest);
}
#endregion
}
}