-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
SolutionProjectGenerator_Tests.cs
2696 lines (2350 loc) · 139 KB
/
SolutionProjectGenerator_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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.BackEnd;
using Microsoft.Build.Framework;
using Microsoft.Build.Construction;
using Microsoft.Build.Execution;
using Microsoft.Build.Shared;
using LoggingService = Microsoft.Build.BackEnd.Logging.LoggingService;
using ILoggingService = Microsoft.Build.BackEnd.Logging.ILoggingService;
using LoggerMode = Microsoft.Build.BackEnd.Logging.LoggerMode;
using Project = Microsoft.Build.Evaluation.Project;
using ProjectCollection = Microsoft.Build.Evaluation.ProjectCollection;
using Toolset = Microsoft.Build.Evaluation.Toolset;
using InternalUtilities = Microsoft.Build.Internal.Utilities;
using XMakeElements = Microsoft.Build.Shared.XMakeElements;
using ResourceUtilities = Microsoft.Build.Shared.ResourceUtilities;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using FrameworkLocationHelper = Microsoft.Build.Shared.FrameworkLocationHelper;
using Xunit;
using Xunit.Abstractions;
using Shouldly;
using Microsoft.Build.UnitTests.Shared;
namespace Microsoft.Build.UnitTests.Construction
{
public class SolutionProjectGenerator_Tests : IDisposable
{
private readonly ITestOutputHelper output;
private string _originalVisualStudioVersion = null;
private static readonly BuildEventContext _buildEventContext = new BuildEventContext(0, 0, BuildEventContext.InvalidProjectContextId, 0);
public SolutionProjectGenerator_Tests(ITestOutputHelper output)
{
this.output = output;
// Save off the value for use during cleanup
_originalVisualStudioVersion = Environment.GetEnvironmentVariable("VisualStudioVersion");
}
public void Dispose()
{
// Need to make sure the environment is cleared up for later tests
Environment.SetEnvironmentVariable("VisualStudioVersion", _originalVisualStudioVersion);
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
}
/// <summary>
/// Test that if a before.{sln}>.targets or after.{sln}.targets file has one of the default targets (Build, Clean, etc.) that it includes only the user-defined target.
/// </summary>
[Theory]
[InlineData("before.MySln.sln.targets")]
[InlineData("after.MySln.sln.targets")]
[InlineData("name.that.does.Not.Affect.The.Build.targets")]
public void SolutionProjectIgnoresDuplicateDefaultTargets(string name)
{
using (TestEnvironment testEnvironment = TestEnvironment.Create())
{
TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true);
TransientTestFile sln = testEnvironment.CreateFile(folder, "MySln.sln", @"Microsoft Visual Studio Solution File, Format Version 16.00");
TransientTestFile targetsFile = testEnvironment.CreateFile(folder, name,
@"<Project>
<Target Name=""Build"" AfterTargets=""NonsenseTarget"">
</Target>
</Project>");
ProjectInstance[] instances = SolutionProjectGenerator.Generate(SolutionFile.Parse(sln.Path), null, null, _buildEventContext, CreateMockLoggingService());
instances.ShouldHaveSingleItem();
instances[0].Targets["Build"].AfterTargets.ShouldBe(string.Empty);
MockLogger logger = new MockLogger(output);
instances[0].Build(targets: null, new List<ILogger> { logger }).ShouldBeTrue();
}
}
/// <summary>
/// Test that a solution filter file excludes projects not covered by its list of projects or their dependencies.
/// </summary>
[Fact]
public void SolutionFilterFiltersProjects()
{
using (TestEnvironment testEnvironment = TestEnvironment.Create())
{
TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true);
TransientTestFolder classLibFolder = testEnvironment.CreateFolder(Path.Combine(folder.Path, "ClassLibrary"), createFolder: true);
TransientTestFolder classLibSubFolder = testEnvironment.CreateFolder(Path.Combine(classLibFolder.Path, "ClassLibrary"), createFolder: true);
TransientTestFile classLibrary = testEnvironment.CreateFile(classLibSubFolder, "ClassLibrary.csproj",
@"<Project>
<Target Name=""ClassLibraryTarget"">
<Message Text=""ClassLibraryBuilt""/>
</Target>
</Project>
");
TransientTestFolder simpleProjectFolder = testEnvironment.CreateFolder(Path.Combine(folder.Path, "SimpleProject"), createFolder: true);
TransientTestFolder simpleProjectSubFolder = testEnvironment.CreateFolder(Path.Combine(simpleProjectFolder.Path, "SimpleProject"), createFolder: true);
TransientTestFile simpleProject = testEnvironment.CreateFile(simpleProjectSubFolder, "SimpleProject.csproj",
@"<Project DefaultTargets=""SimpleProjectTarget"">
<Target Name=""SimpleProjectTarget"">
<Message Text=""SimpleProjectBuilt""/>
</Target>
</Project>
");
// Slashes here (and in the .slnf) are hardcoded as backslashes intentionally to support the common case.
TransientTestFile solutionFile = testEnvironment.CreateFile(simpleProjectFolder, "SimpleProject.sln",
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29326.124
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""SimpleProject"", ""SimpleProject\SimpleProject.csproj"", ""{79B5EBA6-5D27-4976-BC31-14422245A59A}""
EndProject
Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""ClassLibrary"", ""..\ClassLibrary\ClassLibrary\ClassLibrary.csproj"", ""{8EFCCA22-9D51-4268-90F7-A595E11FCB2D}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{79B5EBA6-5D27-4976-BC31-14422245A59A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79B5EBA6-5D27-4976-BC31-14422245A59A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79B5EBA6-5D27-4976-BC31-14422245A59A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79B5EBA6-5D27-4976-BC31-14422245A59A}.Release|Any CPU.Build.0 = Release|Any CPU
{8EFCCA22-9D51-4268-90F7-A595E11FCB2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EFCCA22-9D51-4268-90F7-A595E11FCB2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EFCCA22-9D51-4268-90F7-A595E11FCB2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EFCCA22-9D51-4268-90F7-A595E11FCB2D}.Release|Any CPU.Build.0 = Release|Any CPU
{06A4DD1B-5027-41EF-B72F-F586A5A83EA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06A4DD1B-5027-41EF-B72F-F586A5A83EA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06A4DD1B-5027-41EF-B72F-F586A5A83EA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06A4DD1B-5027-41EF-B72F-F586A5A83EA5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DE7234EC-0C4D-4070-B66A-DCF1B4F0CFEF}
EndGlobalSection
EndGlobal
");
TransientTestFile filterFile = testEnvironment.CreateFile(folder, "solutionFilter.slnf",
@"
{
""solution"": {
// I'm a comment
""path"": "".\\SimpleProject\\SimpleProject.sln"",
""projects"": [
/* ""..\\ClassLibrary\\ClassLibrary\\ClassLibrary.csproj"", */
""SimpleProject\\SimpleProject.csproj"",
]
}
}
");
Directory.GetCurrentDirectory().ShouldNotBe(Path.GetDirectoryName(filterFile.Path));
SolutionFile solution = SolutionFile.Parse(filterFile.Path);
ILoggingService mockLogger = CreateMockLoggingService();
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, null, _buildEventContext, mockLogger);
instances.ShouldHaveSingleItem();
// Check that dependencies are built, and non-dependencies in the .sln are not.
MockLogger logger = new MockLogger(output);
instances[0].Build(targets: null, new List<ILogger> { logger }).ShouldBeTrue();
logger.AssertLogContains(new string[] { "SimpleProjectBuilt" });
logger.AssertLogDoesntContain("ClassLibraryBuilt");
}
}
[Fact]
public void BuildProjectAsTarget()
{
using (TestEnvironment testEnvironment = TestEnvironment.Create())
{
TransientTestFolder folder = testEnvironment.CreateFolder(createFolder: true);
TransientTestFolder classLibFolder = testEnvironment.CreateFolder(Path.Combine(folder.Path, "classlib"), createFolder: true);
TransientTestFile classLibrary = testEnvironment.CreateFile(classLibFolder, "classlib.csproj",
@"<Project>
<Target Name=""ClassLibraryTarget"">
<Message Text=""ClassLibraryBuilt""/>
</Target>
</Project>
");
TransientTestFolder simpleProjectFolder = testEnvironment.CreateFolder(Path.Combine(folder.Path, "simpleProject"), createFolder: true);
TransientTestFile simpleProject = testEnvironment.CreateFile(simpleProjectFolder, "simpleProject.csproj",
@"<Project>
<Target Name=""SimpleProjectTarget"">
<Message Text=""SimpleProjectBuilt""/>
</Target>
</Project>
");
TransientTestFile solutionFile = testEnvironment.CreateFile(folder, "testFolder.sln",
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.6.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""simpleProject"", ""simpleProject\simpleProject.csproj"", ""{AA52A05F-A9C0-4C89-9933-BF976A304C91}""
EndProject
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""classlib"", ""classlib\classlib.csproj"", ""{80B8E6B8-E46D-4456-91B1-848FD35C4AB9}""
EndProject
");
RunnerUtilities.ExecMSBuild(solutionFile.Path + " /t:classlib", out bool success);
success.ShouldBeTrue();
}
}
/// <summary>
/// Verify the AddNewErrorWarningMessageElement method
/// </summary>
[Fact]
public void AddNewErrorWarningMessageElement()
{
MockLogger logger = new MockLogger(output);
/**
* <Project DefaultTargets=`Build` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
* <Target Name=`Build`>
* </Target>
* </Project
*/
ProjectRootElement projectXml = ProjectRootElement.Create();
ProjectTargetElement target = projectXml.AddTarget("Build");
projectXml.DefaultTargets = "Build";
projectXml.ToolsVersion = ObjectModelHelpers.MSBuildDefaultToolsVersion;
SolutionProjectGenerator.AddErrorWarningMessageElement(target, XMakeElements.message, true, "SolutionVenusProjectNoClean");
SolutionProjectGenerator.AddErrorWarningMessageElement(target, XMakeElements.warning, true, "SolutionParseUnknownProjectType", "proj1.csproj");
SolutionProjectGenerator.AddErrorWarningMessageElement(target, XMakeElements.error, true, "SolutionInvalidSolutionConfiguration");
Project project = new Project(projectXml);
project.Build(logger);
string code;
string keyword;
string text = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out code, out keyword, "SolutionParseUnknownProjectType", "proj1.csproj");
// check the error event
Assert.Single(logger.Warnings);
BuildWarningEventArgs warning = logger.Warnings[0];
Assert.Equal(text, warning.Message);
Assert.Equal(code, warning.Code);
Assert.Equal(keyword, warning.HelpKeyword);
text = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out code, out keyword, "SolutionInvalidSolutionConfiguration");
// check the warning event
Assert.Single(logger.Errors);
BuildErrorEventArgs error = logger.Errors[0];
Assert.Equal(text, error.Message);
Assert.Equal(code, error.Code);
Assert.Equal(keyword, error.HelpKeyword);
text = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out code, out keyword, "SolutionVenusProjectNoClean");
// check the message event
Assert.Contains(text, logger.FullLog); // "Log should contain the regular message"
}
/// <summary>
/// Test to make sure we properly set the ToolsVersion attribute on the in-memory project based
/// on the Solution File Format Version.
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
[Trait("Category", "netcore-osx-failing")]
[Trait("Category", "netcore-linux-failing")]
public void EmitToolsVersionAttributeToInMemoryProject9()
{
if (FrameworkLocationHelper.PathToDotNetFrameworkV35 == null)
{
// ".NET Framework 3.5 is required to be installed for this test, but it is not installed.");
return;
}
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 9.00
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
Release|Win32 = Release|Win32
Other|Any CPU = Other|Any CPU
Other|Win32 = Other|Win32
EndGlobalSection
EndGlobal
";
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, "3.5", _buildEventContext, CreateMockLoggingService());
Assert.Equal("3.5", instances[0].ToolsVersion);
}
/// <summary>
/// Test to make sure we properly set the ToolsVersion attribute on the in-memory project based
/// on the Solution File Format Version.
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
[Trait("Category", "netcore-osx-failing")]
[Trait("Category", "netcore-linux-failing")]
public void EmitToolsVersionAttributeToInMemoryProject10()
{
if (FrameworkLocationHelper.PathToDotNetFrameworkV35 == null)
{
// ".NET Framework 3.5 is required to be installed for this test, but it is not installed.");
return;
}
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 10.00
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
Release|Win32 = Release|Win32
Other|Any CPU = Other|Any CPU
Other|Win32 = Other|Win32
EndGlobalSection
EndGlobal
";
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, "3.5", _buildEventContext, CreateMockLoggingService());
Assert.Equal("3.5", instances[0].ToolsVersion);
}
/// <summary>
/// Test to make sure that if the solution file version doesn't map to a sub-toolset version, we won't try
/// to force it to be used.
/// </summary>
[Fact(Skip = "Needs investigation")]
public void DefaultSubToolsetIfSolutionVersionSubToolsetDoesntExist()
{
Environment.SetEnvironmentVariable("VisualStudioVersion", null);
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 10.00
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
Release|Win32 = Release|Win32
Other|Any CPU = Other|Any CPU
Other|Win32 = Other|Win32
EndGlobalSection
EndGlobal
";
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, null, _buildEventContext, CreateMockLoggingService());
Assert.Equal(ObjectModelHelpers.MSBuildDefaultToolsVersion, instances[0].ToolsVersion);
Toolset t = ProjectCollection.GlobalProjectCollection.GetToolset(instances[0].ToolsVersion);
Assert.Equal(t.DefaultSubToolsetVersion, instances[0].SubToolsetVersion);
if (t.DefaultSubToolsetVersion != null)
{
Assert.Equal(t.DefaultSubToolsetVersion, instances[0].GetPropertyValue("VisualStudioVersion"));
}
else
{
Assert.Equal(String.Empty, instances[0].GetPropertyValue("VisualStudioVersion"));
}
}
/// <summary>
/// Test to make sure that if the solution version corresponds to an existing sub-toolset version,
/// barring other factors that might override, the sub-toolset will be based on the solution version.
/// </summary>
[Fact]
public void SubToolsetSetBySolutionVersion()
{
Environment.SetEnvironmentVariable("VisualStudioVersion", null);
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
Release|Win32 = Release|Win32
Other|Any CPU = Other|Any CPU
Other|Win32 = Other|Win32
EndGlobalSection
EndGlobal
";
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, null, _buildEventContext, CreateMockLoggingService());
Assert.Equal(ObjectModelHelpers.MSBuildDefaultToolsVersion, instances[0].ToolsVersion);
// being cautious -- we can't expect the sub-toolset to be picked if it doesn't exist in the first place
if (instances[0].Toolset.SubToolsets.ContainsKey("11.0"))
{
Assert.Equal("11.0", instances[0].SubToolsetVersion);
Assert.Equal("11.0", instances[0].GetPropertyValue("VisualStudioVersion"));
}
}
/// <summary>
/// Test to make sure that even if the solution version corresponds to an existing sub-toolset version,
/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
public void SolutionBasedSubToolsetVersionOverriddenByEnvironment()
{
Environment.SetEnvironmentVariable("VisualStudioVersion", "ABC");
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
Release|Win32 = Release|Win32
Other|Any CPU = Other|Any CPU
Other|Win32 = Other|Win32
EndGlobalSection
EndGlobal
";
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(solution, null, null, _buildEventContext, CreateMockLoggingService());
Assert.Equal(ObjectModelHelpers.MSBuildDefaultToolsVersion, instances[0].ToolsVersion);
Assert.Equal("ABC", instances[0].SubToolsetVersion);
Assert.Equal("ABC", instances[0].GetPropertyValue("VisualStudioVersion"));
}
/// <summary>
/// Test to make sure that even if the solution version corresponds to an existing sub-toolset version
/// </summary>
[Fact(Skip = "Needs investigation")]
public void SolutionPassesSubToolsetToChildProjects2()
{
string classLibraryContentsToolsV4 = ObjectModelHelpers.CleanupFileContents(
@"
<Project ToolsVersion=""4.0"" DefaultTargets=""Build"" xmlns='msbuildnamespace'>
<Target Name='Build'>
<Message Text='.[$(VisualStudioVersion)]. .[$(MSBuildToolsVersion)].' />
</Target>
</Project>
");
string classLibraryContentsToolsV12 = ObjectModelHelpers.CleanupFileContents(
@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" DefaultTargets=""Build"" xmlns='msbuildnamespace'>
<Target Name='Build'>
<Message Text='.[$(VisualStudioVersion)]. .[$(MSBuildToolsVersion)].' />
</Target>
</Project>
");
string solutionFilePreambleV11 =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Dev11
";
string solutionFilePreambleV12 =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Dev11
VisualStudioVersion = 12.0.20311.0 VSPRO_PLATFORM
MinimumVisualStudioVersion = 10.0.40219.1
";
string solutionBodySingleProjectContents =
@"
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""ClassLibrary1"", ""ClassLibrary1.csproj"", ""{6185CC21-BE89-448A-B3C0-D1C27112E595}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Debug|Mixed Platforms.ActiveCfg = CSConfig1|Any CPU
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Debug|Mixed Platforms.Build.0 = CSConfig1|Any CPU
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Release|Any CPU.ActiveCfg = CSConfig2|Any CPU
EndGlobalSection
EndGlobal
";
string solutionBodyMultipleProjectsContents =
@"
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""ClassLibrary1"", ""ClassLibrary1.csproj"", ""{A437DBE9-DCAA-46D8-9D80-A50EDB2244FD}""
EndProject
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""ClassLibrary2"", ""ClassLibrary2.csproj"", ""{84AA5584-4B0F-41DE-95AA-589E1447EDA0}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A437DBE9-DCAA-46D8-9D80-A50EDB2244FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A437DBE9-DCAA-46D8-9D80-A50EDB2244FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A437DBE9-DCAA-46D8-9D80-A50EDB2244FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A437DBE9-DCAA-46D8-9D80-A50EDB2244FD}.Release|Any CPU.Build.0 = Release|Any CPU
{84AA5584-4B0F-41DE-95AA-589E1447EDA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84AA5584-4B0F-41DE-95AA-589E1447EDA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84AA5584-4B0F-41DE-95AA-589E1447EDA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84AA5584-4B0F-41DE-95AA-589E1447EDA0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
string solutionFileContentsDev11 = solutionFilePreambleV11 + solutionBodySingleProjectContents;
string solutionFileContentsDev12 = solutionFilePreambleV12 + solutionBodySingleProjectContents;
string[] solutions = { solutionFileContentsDev11, solutionFileContentsDev12, solutionFileContentsDev12 };
string[] projects = { classLibraryContentsToolsV4, classLibraryContentsToolsV4, classLibraryContentsToolsV12 };
string[] logoutputs = { ".[11.0]. .[4.0].", ".[11.0]. .[4.0].", String.Format(".[{0}]. .[{0}].", ObjectModelHelpers.MSBuildDefaultToolsVersion) };
string previousLegacyEnvironmentVariable = Environment.GetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION");
try
{
Environment.SetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION", "1");
InternalUtilities.RefreshInternalEnvironmentValues();
for (int i = 0; i < solutions.Length; i++)
{
string solutionFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("Foo.sln", solutions[i]);
string projectFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("ClassLibrary1.csproj", projects[i]);
SolutionFile sp = new SolutionFile();
sp.FullPath = solutionFile;
sp.ParseSolutionFile();
ProjectInstance[] instances = SolutionProjectGenerator.Generate(sp, null, null, _buildEventContext, CreateMockLoggingService());
MockLogger logger = new MockLogger(output);
List<ILogger> loggers = new List<ILogger>(1);
loggers.Add(logger);
instances[0].Build(loggers);
logger.AssertLogContains(logoutputs[i]);
}
// Test Dev 12 sln and mixed v4.0 and v12.0 projects
string solutionFileContentsDev12MultipleProjects = solutionFilePreambleV12 + solutionBodyMultipleProjectsContents;
string solutionFileMultipleProjects = ObjectModelHelpers.CreateFileInTempProjectDirectory("Foo.sln", solutionFileContentsDev12MultipleProjects);
string projectFileV4 = ObjectModelHelpers.CreateFileInTempProjectDirectory("ClassLibrary1.csproj", classLibraryContentsToolsV4);
string projectFileV12 = ObjectModelHelpers.CreateFileInTempProjectDirectory("ClassLibrary2.csproj", classLibraryContentsToolsV12);
SolutionFile sp1 = new SolutionFile();
sp1.FullPath = solutionFileMultipleProjects;
sp1.ParseSolutionFile();
ProjectInstance[] instances1 = SolutionProjectGenerator.Generate(sp1, null, null, _buildEventContext, CreateMockLoggingService());
MockLogger logger1 = new MockLogger(output);
List<ILogger> loggers1 = new List<ILogger>(1);
loggers1.Add(logger1);
instances1[0].Build(loggers1);
logger1.AssertLogContains(".[11.0]. .[4.0].");
logger1.AssertLogContains(String.Format(".[{0}]. .[{0}].", ObjectModelHelpers.MSBuildDefaultToolsVersion));
}
finally
{
Environment.SetEnvironmentVariable("MSBUILDLEGACYDEFAULTTOOLSVERSION", previousLegacyEnvironmentVariable);
InternalUtilities.RefreshInternalEnvironmentValues();
}
}
/// <summary>
/// Test to make sure that, when we're not TV 4.0 -- which even for Dev11 solutions we are not by default -- that we
/// do not pass VisualStudioVersion down to the child projects.
/// </summary>
[Fact(Skip = "Needs investigation")]
public void SolutionDoesntPassSubToolsetToChildProjects()
{
try
{
string classLibraryContents =
@"
<Project ToolsVersion=""4.0"" DefaultTargets=""Build"" xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Target Name='Build'>
<Message Text='.[$(VisualStudioVersion)].' />
<Message Text='.[[$(MSBuildToolsVersion)]].' />
</Target>
</Project>
";
string projectFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("ClassLibrary1.csproj", classLibraryContents);
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Dev11
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""ClassLibrary1"", ""ClassLibrary1.csproj"", ""{6185CC21-BE89-448A-B3C0-D1C27112E595}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Debug|Mixed Platforms.ActiveCfg = CSConfig1|Any CPU
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Debug|Mixed Platforms.Build.0 = CSConfig1|Any CPU
{6185CC21-BE89-448A-B3C0-D1C27112E595}.Release|Any CPU.ActiveCfg = CSConfig2|Any CPU
EndGlobalSection
EndGlobal
";
string solutionFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("Foo.sln", solutionFileContents);
SolutionFile sp = new SolutionFile();
sp.FullPath = solutionFile;
sp.ParseSolutionFile();
ProjectInstance[] instances = SolutionProjectGenerator.Generate(sp, null, null, _buildEventContext, CreateMockLoggingService());
Assert.Equal(ObjectModelHelpers.MSBuildDefaultToolsVersion, instances[0].ToolsVersion);
Assert.Equal("11.0", instances[0].SubToolsetVersion);
Assert.Equal("11.0", instances[0].GetPropertyValue("VisualStudioVersion"));
MockLogger logger = new MockLogger(output);
List<ILogger> loggers = new List<ILogger>(1);
loggers.Add(logger);
instances[0].Build(loggers);
logger.AssertLogContains(String.Format(".[{0}].", ObjectModelHelpers.MSBuildDefaultToolsVersion));
}
finally
{
ObjectModelHelpers.DeleteTempProjectDirectory();
}
}
/// <summary>
/// Verify that we throw the appropriate error if the solution declares a dependency
/// on a project that doesn't exist.
/// </summary>
[Fact]
public void SolutionWithMissingDependencies()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `B`, `Project2\B.csproj`, `{881C1674-4ECA-451D-85B6-D7C59B7F16FA}`
ProjectSection(ProjectDependencies) = postProject
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167} = {4A727FF8-65F2-401E-95AD-7C8BBFBE3167}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = preSolution
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|x64.ActiveCfg = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|x64.Build.0 = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|Any CPU.Build.0 = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|x64.ActiveCfg = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
".Replace("`", "\"");
SolutionFile sp = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectInstance[] instances = SolutionProjectGenerator.Generate(sp, null, null, _buildEventContext, CreateMockLoggingService());
}
);
}
/// <summary>
/// Blob should contain dependency info
/// Here B depends on C
/// </summary>
[Fact]
public void SolutionConfigurationWithDependencies()
{
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `A`, `Project1\A.csproj`, `{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}`
EndProject
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `B`, `Project2\B.csproj`, `{881C1674-4ECA-451D-85B6-D7C59B7F16FA}`
ProjectSection(ProjectDependencies) = postProject
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167} = {4A727FF8-65F2-401E-95AD-7C8BBFBE3167}
EndProjectSection
EndProject
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `C`, `Project3\C.csproj`, `{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}`
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = preSolution
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|x64.ActiveCfg = Debug|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|x64.Build.0 = Debug|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Release|Any CPU.Build.0 = Release|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Release|x64.ActiveCfg = Release|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Release|x64.Build.0 = Release|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Debug|x64.ActiveCfg = Debug|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Debug|x64.Build.0 = Debug|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Release|Any CPU.Build.0 = Release|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Release|x64.ActiveCfg = Release|Any CPU
{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}.Release|x64.Build.0 = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|x64.ActiveCfg = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|x64.Build.0 = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|Any CPU.Build.0 = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|x64.ActiveCfg = Release|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
".Replace("`", "\"");
SolutionFile solution = SolutionFile_Tests.ParseSolutionHelper(solutionFileContents);
ProjectRootElement projectXml = ProjectRootElement.Create();
foreach (SolutionConfigurationInSolution solutionConfiguration in solution.SolutionConfigurations)
{
SolutionProjectGenerator.AddPropertyGroupForSolutionConfiguration(projectXml, solution, solutionConfiguration);
}
Project msbuildProject = new Project(projectXml);
// Both projects configurations should be present for solution configuration "Debug|Mixed Platforms"
msbuildProject.SetGlobalProperty("Configuration", "Debug");
msbuildProject.SetGlobalProperty("Platform", "Any CPU");
msbuildProject.ReevaluateIfNecessary();
string solutionConfigurationContents = msbuildProject.GetPropertyValue("CurrentSolutionConfigurationContents");
// Only the specified solution configuration is represented in THE BLOB: nothing for x64 in this case
string expected = $@"<SolutionConfiguration>
<ProjectConfiguration Project=`{{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}}` AbsolutePath=`##temp##{Path.Combine("Project1", "A.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU</ProjectConfiguration>
<ProjectConfiguration Project=`{{881C1674-4ECA-451D-85B6-D7C59B7F16FA}}` AbsolutePath=`##temp##{Path.Combine("Project2", "B.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU<ProjectDependency Project=`{{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}}` /></ProjectConfiguration>
<ProjectConfiguration Project=`{{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}}` AbsolutePath=`##temp##{Path.Combine("Project3", "C.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU</ProjectConfiguration>
</SolutionConfiguration>".Replace("`", "\"").Replace("##temp##", Path.GetTempPath());
Helpers.VerifyAssertLineByLine(expected, solutionConfigurationContents);
}
/// <summary>
/// This test forces a metaproj to be generated as part of the build. Since metaproj files are not written to disk, it will fail if its cached form does not align
/// with the version that is being built as when a property is part of the version added to the cache, but that version is not passed to the BuildManager.
/// </summary>
[Fact]
public void SolutionGeneratingMetaproj()
{
using (TestEnvironment env = TestEnvironment.Create())
{
TransientTestFile proj1 = env.CreateFile("A.csproj", @"<Project><Target Name=""Printer""><Message Importance=""high"" Text=""print string"" /></Target></Project>");
TransientTestFile proj2 = env.CreateFile("B.csproj", @"<Project><Target Name=""Printer""><Message Importance=""high"" Text=""print string"" /></Target></Project>");
TransientTestFile proj3 = env.CreateFile("C.csproj", @"<Project><Target Name=""Printer""><Message Importance=""high"" Text=""print string"" /></Target></Project>");
TransientTestFile proj = env.CreateFile("mysln.sln",
@$"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project(`{"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"}`) = `A`, `{proj1.Path}`, `{"{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}"}`
EndProject
Project(`{"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"}`) = `B`, `{proj2.Path}`, `{"{881C1674-4ECA-451D-85B6-D7C59B7F16FA}"}`
ProjectSection(ProjectDependencies) = postProject
{"{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}"} = {"{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}"}
EndProjectSection
EndProject
Project(`{"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"}`) = `C`, `{proj3.Path}`, `{"{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}"}`
EndProject
".Replace("`", "\""));
RunnerUtilities.ExecMSBuild("\"" + proj.Path + "\"", out bool successfulExit);
successfulExit.ShouldBeTrue();
}
}
/// <summary>
/// Generated project metaproj should declare its outputs for relay.
/// Here B depends on C (via solution dep only) and D (via ProjectReference only)
/// </summary>
/// <seealso href="https://github.com/Microsoft/msbuild/issues/69">
/// MSBuild should generate metaprojects that relay the outputs of the individual MSBuild invocations
/// </seealso>
[Fact]
public void SolutionConfigurationWithDependenciesRelaysItsOutputs()
{
#region Large strings representing solution & projects
const string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 11
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `B`, `B.csproj`, `{881C1674-4ECA-451D-85B6-D7C59B7F16FA}`
ProjectSection(ProjectDependencies) = postProject
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167} = {4A727FF8-65F2-401E-95AD-7C8BBFBE3167}
EndProjectSection
EndProject
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `C`, `C.csproj`, `{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}`
EndProject
Project(`{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}`) = `D`, `D.csproj`, `{B6E7E06F-FC0B-48F1-911A-55E0E1566F00}`
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = preSolution
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}.Debug|Any CPU.Build.0 = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{881C1674-4ECA-451D-85B6-D7C59B7F16FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6E7E06F-FC0B-48F1-911A-55E0E1566F00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6E7E06F-FC0B-48F1-911A-55E0E1566F00}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
const string projectBravoFileContents =
@"
<Project ToolsVersion='msbuilddefaulttoolsversion' DefaultTargets='Build' xmlns='msbuildnamespace'>
<Target Name='Build' Outputs='@(ComputedQuestion)'>
<ItemGroup>
<ComputedQuestion Include='What do you get if you multiply six by nine' />
</ItemGroup>
</Target>
<ItemGroup>
<ProjectReference Include='D.csproj'>
<Project>{B6E7E06F-FC0B-48F1-911A-55E0E1566F00}</Project>
<Name>D</Name>
</ProjectReference>
</ItemGroup>
</Project>
";
const string projectCharlieFileContents =
@"
<Project ToolsVersion='msbuilddefaulttoolsversion' DefaultTargets='Build' xmlns='msbuildnamespace'>
<Target Name='Build' Outputs='@(ComputedAnswer)'>
<ItemGroup>
<ComputedAnswer Include='42' />
</ItemGroup>
</Target>
</Project>
";
const string projectDeltaFileContents =
@"
<Project ToolsVersion='msbuilddefaulttoolsversion' DefaultTargets='Build' xmlns='msbuildnamespace'>
<PropertyGroup>
<ProjectGuid>{B6E7E06F-FC0B-48F1-911A-55E0E1566F00}</ProjectGuid>
</PropertyGroup>
<Target Name='Build' Outputs='@(ComputedPunctuation)'>
<ItemGroup>
<ComputedPunctuation Include='!!!' />
</ItemGroup>
</Target>
</Project>
";
const string automaticProjectFileContents = @"
<Project ToolsVersion='msbuilddefaulttoolsversion' DefaultTargets='compile' xmlns='msbuildnamespace'>
<Target Name='compile'>
<!-- Build projects to get a baseline for their output -->
<MSBuild Projects='B.csproj' Targets='Build'>
<Output
TaskParameter='TargetOutputs'
ItemName='BravoProjectOutputs' />
</MSBuild>
<Message Importance='high' Text='BravoProjectOutputs: @(BravoProjectOutputs)' />
<MSBuild Projects='C.csproj' Targets='Build'>
<Output
TaskParameter='TargetOutputs'
ItemName='CharlieProjectOutputs' />
</MSBuild>
<Message Importance='high' Text='CharlieProjectOutputs: @(CharlieProjectOutputs)' />
<MSBuild Projects='D.csproj' Targets='Build'>
<Output
TaskParameter='TargetOutputs'
ItemName='DeltaProjectOutputs' />
</MSBuild>
<Message Importance='high' Text='DeltaProjectOutputs: @(DeltaProjectOutputs)' />
<PropertyGroup>
<StringifiedBravoProjectOutputs>@(BravoProjectOutputs)</StringifiedBravoProjectOutputs>
<StringifiedCharlieProjectOutputs>@(CharlieProjectOutputs)</StringifiedCharlieProjectOutputs>
<StringifiedDeltaProjectOutputs>@(DeltaProjectOutputs)</StringifiedDeltaProjectOutputs>
</PropertyGroup>
<!-- Explicitly build the metaproject generated for B -->
<MSBuild Projects='B.csproj.metaproj' Targets='Build'>
<Output
TaskParameter='TargetOutputs'
ItemName='BravoMetaProjectOutputs' />
</MSBuild>
<Message Importance='high' Text='BravoMetaProjectOutputs: @(BravoMetaProjectOutputs)' />
<Error Condition=` '@(BravoProjectOutputs)' != '@(BravoMetaProjectOutputs)' ` Text='Metaproj outputs must match outputs of normal project build.' />
<!-- Build the solution as a whole (which will build the metaproj and return overall outputs) -->
<MSBuild Projects='MSBuildIssue.sln'>
<Output
TaskParameter='TargetOutputs'
ItemName='SolutionProjectOutputs' />
</MSBuild>
<Message Importance='high' Text='SolutionProjectOutputs: @(SolutionProjectOutputs)' />
<Error Condition=` '@(SolutionProjectOutputs->Count())' != '3' ` Text='Overall sln outputs must include outputs of each referenced project (there should be 3).' />
<Error Condition=` '@(SolutionProjectOutputs->AnyHaveMetadataValue('Identity', '$(StringifiedBravoProjectOutputs)'))' != 'true'` Text='Overall sln outputs must include outputs of normal project build of project B.' />
<Error Condition=` '@(SolutionProjectOutputs->AnyHaveMetadataValue('Identity', '$(StringifiedCharlieProjectOutputs)'))' != 'true' ` Text='Overall sln outputs must include outputs of normal project build of project C.' />
<Error Condition=` '@(SolutionProjectOutputs->AnyHaveMetadataValue('Identity', '$(StringifiedDeltaProjectOutputs)'))' != 'true' ` Text='Overall sln outputs must include outputs of normal project build of project D.' />
</Target>
</Project>";
#endregion
var logger = new MockLogger(output);
var loggers = new List<ILogger>(1) { logger };
var solutionFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("MSBuildIssue.sln", solutionFileContents);
ObjectModelHelpers.CreateFileInTempProjectDirectory("B.csproj", projectBravoFileContents);
ObjectModelHelpers.CreateFileInTempProjectDirectory("C.csproj", projectCharlieFileContents);
ObjectModelHelpers.CreateFileInTempProjectDirectory("D.csproj", projectDeltaFileContents);
var solution = new SolutionFile { FullPath = solutionFile };
solution.ParseSolutionFile();
var instances = SolutionProjectGenerator.Generate(solution, null, null, _buildEventContext, CreateMockLoggingService());
var projectBravoMetaProject = instances[1];
Assert.DoesNotContain(projectBravoMetaProject.Targets, kvp => kvp.Value.Outputs.Equals("@()")); // "The outputItem parameter can be null; the Target element should not have an Outputs attribute in that case."
// saves the in-memory metaproj to disk
projectBravoMetaProject.ToProjectRootElement().Save(projectBravoMetaProject.FullPath);
var automaticProjectFile = ObjectModelHelpers.CreateFileInTempProjectDirectory("automatic.msbuild", automaticProjectFileContents);
var automaticProject = new Project(automaticProjectFile);
var buildResult = automaticProject.Build(loggers);
// NOTE: most of the actual assertions for this test are embedded in automaticProjectFileContents as <Error>s
Assert.True(buildResult, String.Join(Environment.NewLine, logger.Errors.Select(beea => beea.Message)));
}
/// <summary>
/// Test the SolutionProjectGenerator.AddPropertyGroupForSolutionConfiguration method
/// </summary>
[Fact]
public void TestAddPropertyGroupForSolutionConfiguration()
{
string solutionFileContents =
@"
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project('{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}') = 'ClassLibrary1', 'ClassLibrary1\ClassLibrary1.csproj', '{6185CC21-BE89-448A-B3C0-D1C27112E595}'
EndProject
Project('{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}') = 'MainApp', 'MainApp\MainApp.vcxproj', '{A6F99D27-47B9-4EA4-BFC9-25157CBDC281}'
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms