-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathProjectCollection_Tests.cs
More file actions
1501 lines (1245 loc) · 56.4 KB
/
ProjectCollection_Tests.cs
File metadata and controls
1501 lines (1245 loc) · 56.4 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
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
// 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.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
#nullable disable
namespace Microsoft.Build.UnitTests.OM.Definition
{
/// <summary>
/// Tests for ProjectCollection
/// </summary>
public class ProjectCollection_Tests : IDisposable
{
/// <summary>
/// Gets or sets the test context.
/// </summary>
public ITestOutputHelper TestOutput { get; set; }
public ProjectCollection_Tests(ITestOutputHelper outputHelper)
{
TestOutput = outputHelper;
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
}
/// <summary>
/// Clear out the global project collection
/// </summary>
public void Dispose()
{
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
Assert.Equal(0, ProjectCollection.GlobalProjectCollection.Count);
IDictionary<string, string> globalProperties = ProjectCollection.GlobalProjectCollection.GlobalProperties;
foreach (string propertyName in globalProperties.Keys)
{
ProjectCollection.GlobalProjectCollection.RemoveGlobalProperty(propertyName);
}
Assert.Empty(ProjectCollection.GlobalProjectCollection.GlobalProperties);
}
/// <summary>
/// Add a single project from disk and verify it's put in the global project collection
/// </summary>
[Fact]
public void AddProjectFromDisk()
{
string path = null;
try
{
path = FileUtilities.GetTemporaryFileName();
ProjectRootElement xml = ProjectRootElement.Create(path);
xml.Save();
Project project = new Project(path);
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject(path);
Assert.True(ReferenceEquals(project, project2));
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// When an unnamed project is saved, it gets a name, and should be entered into
/// the appropriate project collection.
/// </summary>
[Fact]
public void AddProjectOnSave()
{
string path = null;
try
{
Project project = new Project();
Assert.Equal(0, ProjectCollection.GlobalProjectCollection.Count);
path = FileUtilities.GetTemporaryFileName();
project.Save(path);
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject(path);
Assert.True(ReferenceEquals(project, project2));
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// When an unnamed project is saved, it gets a name, and should be entered into
/// the appropriate project collection.
/// </summary>
[Fact]
public void AddProjectOnSave_SpecifiedProjectCollection()
{
string path = null;
try
{
using ProjectCollection collection = new ProjectCollection();
Project project = new Project(collection);
path = FileUtilities.GetTemporaryFileName();
project.Save(path);
Project project2 = collection.LoadProject(path);
Assert.True(ReferenceEquals(project, project2));
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// When an unnamed project is given a name, it should be entered into its
/// project collection.
/// </summary>
[Fact]
public void AddProjectOnSetName()
{
var project = new Project { FullPath = "c:\\x" };
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject("c:\\x");
Assert.True(ReferenceEquals(project, project2));
}
/// <summary>
/// Loading a project from a file inherits the project collection's global properties
/// </summary>
[Fact]
public void GlobalPropertyInheritLoadFromFile()
{
string path = null;
try
{
path = CreateProjectFile();
using var collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
Project project = collection.LoadProject(path);
Assert.Equal("v", project.GlobalProperties["p"]);
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// Loading a project from a file inherits the project collection's global properties
/// </summary>
#if FEATURE_INSTALLED_MSBUILD
[Fact]
#else
[Fact(Skip = "https://github.com/dotnet/msbuild/issues/276")]
#endif
public void GlobalPropertyInheritLoadFromFile2()
{
string path = null;
try
{
path = CreateProjectFile();
using ProjectCollection collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
Project project = collection.LoadProject(path, "4.0");
Assert.Equal("v", project.GlobalProperties["p"]);
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// Loading a project from a file inherits the project collection's global properties
/// </summary>
#if FEATURE_INSTALLED_MSBUILD
[Fact]
#else
[Fact(Skip = "https://github.com/dotnet/msbuild/issues/276")]
#endif
public void GlobalPropertyInheritLoadFromFile3()
{
string path = null;
try
{
path = CreateProjectFile();
using ProjectCollection collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
Project project = collection.LoadProject(path, null, "4.0");
Assert.Equal("v", project.GlobalProperties["p"]);
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// Loading a project from a reader inherits the project collection's global properties
/// </summary>
[Fact]
public void GlobalPropertyInheritLoadFromXml1()
{
using XmlReader reader = CreateProjectXmlReader();
using var collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
Project project = collection.LoadProject(reader);
Assert.Equal("v", project.GlobalProperties["p"]);
}
/// <summary>
/// Loading a project from a reader inherits the project collection's global properties
/// </summary>
[Fact]
public void GlobalPropertyInheritLoadFromXml2()
{
using XmlReader reader = CreateProjectXmlReader();
using var collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
Project project = collection.LoadProject(reader, ObjectModelHelpers.MSBuildDefaultToolsVersion);
Assert.Equal("v", project.GlobalProperties["p"]);
}
/// <summary>
/// Creating a project inherits the project collection's global properties
/// </summary>
[Fact]
public void GlobalPropertyInheritProjectConstructor()
{
using var collection = new ProjectCollection();
collection.SetGlobalProperty("p", "v");
var project = new Project(collection);
Assert.Equal("v", project.GlobalProperties["p"]);
}
/// <summary>
/// Load project should load a project, if it wasn't already loaded.
/// </summary>
[Fact]
public void GetLoadedProjectNonExistent()
{
string path = null;
try
{
path = FileUtilities.GetTemporaryFileName();
ProjectRootElement xml = ProjectRootElement.Create();
xml.Save(path);
Assert.Equal(0, ProjectCollection.GlobalProjectCollection.Count);
ProjectCollection.GlobalProjectCollection.LoadProject(path);
Assert.Equal(1, ProjectCollection.GlobalProjectCollection.Count);
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// Verify that one project collection doesn't contain the projects of another
/// </summary>
[Fact]
public void GetLoadedProjectWrongCollection()
{
var project1 = new Project { FullPath = "c:\\1" };
using var collection = new ProjectCollection();
var project2 = new Project(collection) { FullPath = "c:\\1" };
Assert.True(ReferenceEquals(project2, collection.LoadProject("c:\\1")));
Assert.False(ReferenceEquals(project1, collection.LoadProject("c:\\1")));
}
/// <summary>
/// Verify that one project collection doesn't contain the ProjectRootElements of another
/// -- because they don't share a ProjectRootElementCache
/// </summary>
[Fact]
public void GetLoadedProjectRootElementWrongCollection()
{
string path = null;
try
{
path = FileUtilities.GetTemporaryFileName();
ProjectRootElement.Create(path).Save();
using ProjectCollection collection1 = new ProjectCollection();
Project project1 = collection1.LoadProject(path);
Project project1b = collection1.LoadProject(path);
Assert.True(ReferenceEquals(project1.Xml, project1b.Xml));
using ProjectCollection collection2 = new ProjectCollection();
Project project2 = collection2.LoadProject(path);
Assert.False(ReferenceEquals(project1.Xml, project2.Xml));
}
finally
{
if (path != null)
{
File.Delete(path);
}
}
}
/// <summary>
/// Attempt to have two equivalent projects in a project collection fails.
/// </summary>
[Fact]
public void ErrorTwoProjectsEquivalentOneCollection()
{
_ = new Project { FullPath = "c:\\x" };
Assert.Throws<InvalidOperationException>(() =>
{
_ = new Project { FullPath = "c:\\x" };
});
}
/// <summary>
/// Validates that when loading two projects with nominally different global properties, but that match when we take
/// into account the ProjectCollection's global properties, we get the pre-existing project if one exists.
/// </summary>
[Fact]
public void TwoProjectsEquivalentWhenOneInheritsFromProjectCollection()
{
var project = new Project { FullPath = "c:\\1" };
// Set a global property on the project collection -- this should be passed on to all
// loaded projects.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Configuration", "Debug");
Assert.Equal("Debug", project.GlobalProperties["Configuration"]);
// now create a global properties dictionary to pass to a new project
var globals = new Dictionary<string, string> { { "Configuration", "Debug" } };
ProjectCollection.GlobalProjectCollection.LoadProject("c:\\1", globals, null);
Assert.Equal(1, ProjectCollection.GlobalProjectCollection.Count);
}
/// <summary>
/// Two projects may have the same path but different global properties.
/// </summary>
[Fact]
public void TwoProjectsDistinguishedByGlobalPropertiesOnly()
{
ProjectRootElement xml = ProjectRootElement.Create();
string projectDirectory = NativeMethodsShared.IsWindows ? "c:\\1" : "/l";
var globalProperties1 = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "p", "v1" } };
var project1 =
new Project(xml, globalProperties1, ObjectModelHelpers.MSBuildDefaultToolsVersion)
{
FullPath = projectDirectory
};
var globalProperties2 = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "p", "v2" } };
var project2 =
new Project(xml, globalProperties2, ObjectModelHelpers.MSBuildDefaultToolsVersion)
{
FullPath = projectDirectory
};
Assert.True(ReferenceEquals(project1, ProjectCollection.GlobalProjectCollection.LoadProject(projectDirectory, globalProperties1, ObjectModelHelpers.MSBuildDefaultToolsVersion)));
Assert.True(ReferenceEquals(project2, ProjectCollection.GlobalProjectCollection.LoadProject(projectDirectory, globalProperties2, ObjectModelHelpers.MSBuildDefaultToolsVersion)));
List<Project> projects = Helpers.MakeList(ProjectCollection.GlobalProjectCollection.LoadedProjects);
Assert.Equal(2, projects.Count);
Assert.Equal(2, ProjectCollection.GlobalProjectCollection.Count);
Assert.Contains(project1, projects);
Assert.Contains(project2, projects);
}
/// <summary>
/// Validates that we can correctly load two of the same project file with different global properties, even when
/// those global properties are applied to the project by the project collection (and then overridden in one case).
/// </summary>
[Fact]
public void TwoProjectsDistinguishedByGlobalPropertiesOnly_ProjectOverridesProjectCollection()
{
var project = new Project { FullPath = "c:\\1" };
// Set a global property on the project collection -- this should be passed on to all
// loaded projects.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Configuration", "Debug");
Assert.Equal("Debug", project.GlobalProperties["Configuration"]);
// Differentiate this project from the one below
project.SetGlobalProperty("MyProperty", "MyValue");
// now create a global properties dictionary to pass to a new project
var project2Globals =
new Dictionary<string, string> { { "Configuration", "Release" }, { "Platform", "Win32" } };
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject("c:\\1", project2Globals, null);
Assert.Equal("Release", project2.GlobalProperties["Configuration"]);
// Setting a global property on the project collection overrides all contained projects,
// whether they were initially loaded with the global project collection's value or not.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Platform", "X64");
Assert.Equal("X64", project.GlobalProperties["Platform"]);
Assert.Equal("X64", project2.GlobalProperties["Platform"]);
// But setting a global property on the project directly should override that.
project2.SetGlobalProperty("Platform", "Itanium");
Assert.Equal("Itanium", project2.GlobalProperties["Platform"]);
// Now set global properties such that the two projects have an identical set.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Configuration", "Debug2");
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Platform", "X86");
bool exceptionCaught = false;
try
{
// This will make it identical, so we should get a throw here.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("MyProperty", "MyValue2");
}
catch (InvalidOperationException)
{
exceptionCaught = true;
}
Assert.True(exceptionCaught); // "Should have caused the two projects to be identical, causing an exception to be thrown"
}
/// <summary>
/// Two projects may have the same path but different tools version.
/// </summary>
[Fact]
public void TwoProjectsDistinguishedByToolsVersionOnly()
{
if (ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version35) == null)
{
// "Requires 3.5 to be installed"
return;
}
if (ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.Version20) == null)
{
// ".NET Framework 2.0 is required to be installed for this test, but it is not installed."
return;
}
ProjectRootElement xml = ProjectRootElement.Create();
var project1 = new Project(xml, null, "2.0") { FullPath = "c:\\1" };
var project2 = new Project(xml, null, ObjectModelHelpers.MSBuildDefaultToolsVersion) { FullPath = "c:\\1" };
Assert.True(ReferenceEquals(project1, ProjectCollection.GlobalProjectCollection.LoadProject("c:\\1", null, "2.0")));
Assert.True(ReferenceEquals(project2, ProjectCollection.GlobalProjectCollection.LoadProject("c:\\1", null, ObjectModelHelpers.MSBuildDefaultToolsVersion)));
}
/// <summary>
/// If the ToolsVersion in the project file is bogus, we'll default to the current ToolsVersion and successfully
/// load it. Make sure we can RE-load it, too, and successfully pick up the correct copy of the loaded project.
/// </summary>
[Fact]
public void ReloadProjectWithInvalidToolsVersionInFile()
{
const string content = @"
<Project ToolsVersion='bogus'>
<Target Name='t'/>
</Project>
";
using ProjectFromString projectFromString = new(content);
Project project = projectFromString.Project;
project.FullPath = "c:\\123.proj";
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject("c:\\123.proj", null, null);
Assert.True(ReferenceEquals(project, project2));
}
/// <summary>
/// Make sure we can reload a project that has a ToolsVersion that doesn't match what it ends up getting
/// forced to by default (current).
/// </summary>
[Fact]
public void ReloadProjectWithProjectToolsVersionDifferentFromEffectiveToolsVersion()
{
const string content = @"
<Project ToolsVersion='4.0'>
<Target Name='t'/>
</Project>
";
using ProjectFromString projectFromString = new(content);
Project project = projectFromString.Project;
project.FullPath = "c:\\123.proj";
Project project2 = ProjectCollection.GlobalProjectCollection.LoadProject("c:\\123.proj", null, null);
Assert.True(ReferenceEquals(project, project2));
}
/// <summary>
/// Collection stores projects distinguished by path, global properties, and tools version.
/// Changing global properties should update the collection.
/// </summary>
[Fact]
public void ChangingGlobalPropertiesUpdatesCollection()
{
using ProjectCollection collection = new ProjectCollection();
var project = new Project(collection) { FullPath = "c:\\x" };
project.SetGlobalProperty("p", "v1"); // should update collection
var globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "p", "v1" } };
Project newProject = collection.LoadProject("c:\\x", globalProperties, null);
Assert.True(ReferenceEquals(project, newProject));
}
/// <summary>
/// Changing global properties on collection should update the collection's defaults,
/// and any projects even if they have defined the same global properties
/// </summary>
[Fact]
public void SettingGlobalPropertiesOnCollectionUpdatesProjects()
{
using var collection = new ProjectCollection();
var project1 = new Project(collection) { FullPath = "c:\\y" };
Assert.Empty(project1.GlobalProperties);
collection.SetGlobalProperty("g1", "v1");
collection.SetGlobalProperty("g2", "v2");
collection.SetGlobalProperty("g2", "v2"); // try dupe
Assert.Equal(2, project1.GlobalProperties.Count);
collection.RemoveGlobalProperty("g2");
var project2 = new Project(collection) { FullPath = "c:\\x" };
Assert.Single(project1.GlobalProperties);
Assert.Equal("v1", project2.GlobalProperties["g1"]);
Assert.Single(project2.GlobalProperties);
Assert.Equal("v1", project2.GlobalProperties["g1"]);
}
/// <summary>
/// Changing global properties on collection should update the collection's defaults,
/// and any projects even if they have defined the same global properties
/// </summary>
[Fact]
public void SettingGlobalPropertiesOnCollectionUpdatesProjects2()
{
using var collection = new ProjectCollection();
var project1 = new Project(collection) { FullPath = "c:\\y" };
// load into collection
project1.SetGlobalProperty("g1", "v0");
Helpers.ClearDirtyFlag(project1.Xml);
collection.SetGlobalProperty("g1", "v1");
collection.SetGlobalProperty("g2", "v2");
Assert.Equal(2, project1.GlobalProperties.Count);
Assert.Equal("v1", project1.GlobalProperties["g1"]);
Assert.Equal("v2", project1.GlobalProperties["g2"]); // Got overwritten
Assert.True(project1.IsDirty);
}
/// <summary>
/// Changing global properties on collection should update the collection's defaults,
/// and all projects as well
/// </summary>
[Fact]
public void RemovingGlobalPropertiesOnCollectionUpdatesProjects()
{
using var collection = new ProjectCollection();
var project1 = new Project(collection) { FullPath = "c:\\y" };
Assert.Empty(project1.GlobalProperties);
Helpers.ClearDirtyFlag(project1.Xml);
collection.SetGlobalProperty("g1", "v1"); // should make both dirty
collection.SetGlobalProperty("g2", "v2"); // should make both dirty
Assert.True(project1.IsDirty);
var project2 = new Project(collection) { FullPath = "c:\\x" };
Assert.True(project2.IsDirty);
Assert.Equal(2, project1.GlobalProperties.Count);
Assert.Equal("v1", project2.GlobalProperties["g1"]);
Assert.Equal(2, project2.GlobalProperties.Count);
Assert.Equal("v1", project2.GlobalProperties["g1"]);
Helpers.ClearDirtyFlag(project1.Xml);
Helpers.ClearDirtyFlag(project2.Xml);
collection.RemoveGlobalProperty("g2"); // should make both dirty
Assert.True(project1.IsDirty);
Assert.True(project2.IsDirty);
Assert.Single(project1.GlobalProperties);
Assert.Single(project2.GlobalProperties);
collection.RemoveGlobalProperty("g1");
Assert.Empty(project1.GlobalProperties);
Assert.Empty(project2.GlobalProperties);
}
/// <summary>
/// Changing global properties on collection should update the collection's defaults,
/// and all projects as well
/// </summary>
[Fact]
public void RemovingGlobalPropertiesOnCollectionUpdatesProjects2()
{
using var collection = new ProjectCollection();
collection.SetGlobalProperty("g1", "v1");
var project1 = new Project(collection) { FullPath = "c:\\y" };
project1.SetGlobalProperty("g1", "v0"); // mask collection property
Helpers.ClearDirtyFlag(project1.Xml);
collection.RemoveGlobalProperty("g1"); // should modify the project
Assert.Empty(project1.GlobalProperties);
Assert.True(project1.IsDirty);
}
/// <summary>
/// Unloading a project should remove it from the project collection
/// </summary>
[Fact]
public void UnloadProject()
{
var project = new Project { FullPath = "c:\\x" };
Assert.Equal(1, ProjectCollection.GlobalProjectCollection.Count);
ProjectCollection.GlobalProjectCollection.UnloadProject(project); // should not throw
Assert.Equal(0, ProjectCollection.GlobalProjectCollection.Count);
Assert.Empty(Helpers.MakeList(ProjectCollection.GlobalProjectCollection.LoadedProjects));
}
/// <summary>
/// Unloading project XML should remove it from the weak cache.
/// </summary>
[Fact]
public void UnloadProjectXml()
{
var project = new Project { FullPath = "c:\\x" };
ProjectRootElement xml = project.Xml;
// Unload the evaluation project, and then the XML.
ProjectCollection.GlobalProjectCollection.UnloadProject(project);
ProjectCollection.GlobalProjectCollection.UnloadProject(xml);
try
{
// If the ProjectRootElement was unloaded from the cache, then
// an attempt to load it by the pretend filename should fail,
// so it makes a good test to see that the UnloadProject method worked.
ProjectCollection.GlobalProjectCollection.LoadProject(xml.FullPath);
Assert.Fail("An InvalidProjectFileException was expected.");
}
catch (InvalidProjectFileException)
{
}
}
/// <summary>
/// Unloading project XML while it is in use should result in an exception.
/// </summary>
[Fact]
public void UnloadProjectXmlWhileInDirectUse()
{
Assert.Throws<InvalidOperationException>(() =>
{
var project = new Project { FullPath = "c:\\x" };
// Attempt to unload the xml before unloading the project evaluation.
ProjectCollection.GlobalProjectCollection.UnloadProject(project.Xml);
});
}
/// <summary>
/// Unloading project XML while it is in use should result in an exception.
/// </summary>
[Fact]
public void UnloadProjectXmlWhileInImportUse()
{
Assert.Throws<InvalidOperationException>(() =>
{
var mainProject = new Project { FullPath = "c:\\main" };
var importProject = new Project { FullPath = "c:\\import" };
ProjectRootElement importedXml = importProject.Xml;
// Import into main project
mainProject.Xml.PrependChild(mainProject.Xml.CreateImportElement(importProject.FullPath));
mainProject.ReevaluateIfNecessary();
// Unload the import evaluation, but not the main project that still has a reference to it.
ProjectCollection.GlobalProjectCollection.UnloadProject(importProject);
// Attempt to unload the import xml before unloading the project that still references it.
try
{
ProjectCollection.GlobalProjectCollection.UnloadProject(importedXml);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
throw;
}
});
}
/// <summary>
/// Renaming a project should correctly update the project collection's set of loaded projects.
/// </summary>
[Fact]
public void RenameProject()
{
var project = new Project { FullPath = "c:\\x" };
project.FullPath = "c:\\y";
Assert.Equal(1, ProjectCollection.GlobalProjectCollection.Count);
Assert.True(ReferenceEquals(project, Helpers.MakeList(ProjectCollection.GlobalProjectCollection.LoadedProjects)[0]));
ProjectCollection.GlobalProjectCollection.UnloadProject(project); // should not throw
Assert.Equal(0, ProjectCollection.GlobalProjectCollection.Count);
}
/// <summary>
/// Validates that we don't somehow lose the ProjectCollection global properties when renaming the project.
/// </summary>
[Fact]
public void RenameProjectAndVerifyStillContainsProjectCollectionGlobalProperties()
{
var project = new Project { FullPath = "c:\\1" };
// Set a global property on the project collection -- this should be passed on to all
// loaded projects.
ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Configuration", "Debug");
Assert.Equal("Debug", project.GlobalProperties["Configuration"]);
project.FullPath = "c:\\2";
Assert.Equal("Debug", project.GlobalProperties["Configuration"]);
}
/// <summary>
/// Saving a project to a new name should correctly update the project collection's set of loaded projects.
/// Reported by F#.
/// </summary>
[Fact]
public void SaveToNewNameAndUnload()
{
string file1 = null;
string file2 = null;
try
{
file1 = FileUtilities.GetTemporaryFileName();
file2 = FileUtilities.GetTemporaryFileName();
Project project = new Project();
project.Save(file1);
using ProjectCollection collection = new ProjectCollection();
Project project2 = collection.LoadProject(file1);
project2.Save(file2);
collection.UnloadProject(project2);
}
finally
{
if (file1 != null)
{
File.Delete(file1);
}
if (file2 != null)
{
File.Delete(file2);
}
}
}
/// <summary>
/// Saving a project to a new name after loading, unloading, and reloading, should work.
/// Reported by F#.
/// </summary>
[Fact]
public void LoadUnloadReloadSaveToNewName()
{
string file1 = null;
string file2 = null;
try
{
file1 = FileUtilities.GetTemporaryFileName();
file2 = FileUtilities.GetTemporaryFileName();
var project = new Project();
project.Save(file1);
project.ProjectCollection.UnloadProject(project);
using var collection = new ProjectCollection();
Project project2 = collection.LoadProject(file1);
collection.UnloadProject(project2);
Project project3 = collection.LoadProject(file1);
project3.Save(file2); // should not crash
collection.UnloadProject(project3);
}
finally
{
if (file1 != null)
{
File.Delete(file1);
}
if (file2 != null)
{
File.Delete(file2);
}
}
}
/// <summary>
/// Saving a project to a new name after loading, unloading, and reloading, should work.
/// Reported by F#.
/// </summary>
[Fact]
public void LoadUnloadAllReloadSaveToNewName()
{
string file1 = null;
string file2 = null;
try
{
file1 = FileUtilities.GetTemporaryFileName();
file2 = FileUtilities.GetTemporaryFileName();
var project = new Project();
project.Save(file1);
project.ProjectCollection.UnloadProject(project);
using var collection = new ProjectCollection();
collection.LoadProject(file1);
collection.UnloadAllProjects();
Project project3 = collection.LoadProject(file1);
project3.Save(file2); // should not crash
collection.UnloadProject(project3);
}
finally
{
if (file1 != null)
{
File.Delete(file1);
}
if (file2 != null)
{
File.Delete(file2);
}
}
}
/// <summary>
/// Add a toolset
/// </summary>
[Fact]
public void AddToolset()
{
using var collection = new ProjectCollection();
collection.RemoveAllToolsets();
var toolset = new Toolset("x", "c:\\y", collection, null);
collection.AddToolset(toolset);
Assert.Equal(toolset, collection.GetToolset("x"));
Assert.True(collection.ContainsToolset("x"));
List<Toolset> toolsets = Helpers.MakeList(collection.Toolsets);
Assert.Single(toolsets);
Assert.Equal(toolset, toolsets[0]);
}
/// <summary>
/// Add two toolsets
/// </summary>
[Fact]
public void AddTwoToolsets()
{
using var collection = new ProjectCollection();
collection.RemoveAllToolsets();
var toolset1 = new Toolset("x", "c:\\y", collection, null);
var toolset2 = new Toolset("y", "c:\\z", collection, null);
collection.AddToolset(toolset1);
collection.AddToolset(toolset2);
Assert.Equal(toolset1, collection.GetToolset("x"));
Assert.Equal(toolset2, collection.GetToolset("y"));
List<Toolset> toolsets = Helpers.MakeList(collection.Toolsets);
Assert.Equal(2, toolsets.Count);
Assert.Contains(toolset1, toolsets);
Assert.Contains(toolset2, toolsets);
}
/// <summary>
/// Add a toolset that overrides another
/// </summary>
[Fact]
public void ReplaceToolset()
{
using var collection = new ProjectCollection();
collection.RemoveAllToolsets();
var toolset1 = new Toolset("x", "c:\\y", collection, null);
var toolset2 = new Toolset("x", "c:\\z", collection, null);