/
ProjectItem_Tests.cs
3532 lines (3033 loc) · 138 KB
/
ProjectItem_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 System.Xml;
using Microsoft.Build.Construction;
using Microsoft.Build.Engine.UnitTests.Globbing;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Shared;
using Shouldly;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using Xunit;
namespace Microsoft.Build.UnitTests.OM.Definition
{
/// <summary>
/// Tests for ProjectItem
/// </summary>
public class ProjectItem_Tests
{
internal const string ItemWithIncludeAndExclude = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='{0}' Exclude='{1}'/>
</ItemGroup>
</Project>
";
internal const string ItemWithIncludeUpdateAndRemove= @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='{0}'>
<m>contents</m>
</i>
<i Update='{1}'>
<m>updated</m>
</i>
<i Remove='{2}'/>
</ItemGroup>
</Project>
";
/// <summary>
/// Project getter
/// </summary>
[Fact]
public void ProjectGetter()
{
Project project = new Project();
ProjectItem item = project.AddItem("i", "i1")[0];
Assert.True(Object.ReferenceEquals(project, item.Project));
}
/// <summary>
/// No metadata, simple case
/// </summary>
[Fact]
public void SingleItemWithNoMetadata()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='i1'/>
</ItemGroup>
</Project>
";
ProjectItem item = GetOneItem(content);
Assert.NotNull(item.Xml);
Assert.Equal("i", item.ItemType);
Assert.Equal("i1", item.EvaluatedInclude);
Assert.Equal("i1", item.UnevaluatedInclude);
Assert.False(item.Metadata.GetEnumerator().MoveNext());
}
/// <summary>
/// Read off metadata
/// </summary>
[Fact]
public void ReadMetadata()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='i1'>
<m1>v1</m1>
<m2>v2</m2>
</i>
</ItemGroup>
</Project>
";
ProjectItem item = GetOneItem(content);
var itemMetadata = Helpers.MakeList(item.Metadata);
Assert.Equal(2, itemMetadata.Count);
Assert.Equal("m1", itemMetadata[0].Name);
Assert.Equal("m2", itemMetadata[1].Name);
Assert.Equal("v1", itemMetadata[0].EvaluatedValue);
Assert.Equal("v2", itemMetadata[1].EvaluatedValue);
Assert.Equal(itemMetadata[0], item.GetMetadata("m1"));
Assert.Equal(itemMetadata[1], item.GetMetadata("m2"));
}
/// <summary>
/// Get metadata inherited from item definitions
/// </summary>
[Fact]
public void GetMetadataObjectsFromDefinition()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemDefinitionGroup>
<i>
<m0>v0</m0>
<m1>v1</m1>
</i>
</ItemDefinitionGroup>
<ItemGroup>
<i Include='i1'>
<m1>v1b</m1>
<m2>v2</m2>
</i>
</ItemGroup>
</Project>
";
Project project = new Project(XmlReader.Create(new StringReader(content)));
ProjectItem item = Helpers.GetFirst(project.GetItems("i"));
ProjectMetadata m0 = item.GetMetadata("m0");
ProjectMetadata m1 = item.GetMetadata("m1");
ProjectItemDefinition definition = project.ItemDefinitions["i"];
ProjectMetadata idm0 = definition.GetMetadata("m0");
ProjectMetadata idm1 = definition.GetMetadata("m1");
Assert.True(Object.ReferenceEquals(m0, idm0));
Assert.False(Object.ReferenceEquals(m1, idm1));
}
/// <summary>
/// Get metadata values inherited from item definitions
/// </summary>
[Fact]
public void GetMetadataValuesFromDefinition()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemDefinitionGroup>
<i>
<m0>v0</m0>
<m1>v1</m1>
</i>
</ItemDefinitionGroup>
<ItemGroup>
<i Include='i1'>
<m1>v1b</m1>
<m2>v2</m2>
</i>
</ItemGroup>
</Project>
";
ProjectItem item = GetOneItem(content);
Assert.Equal("v0", item.GetMetadataValue("m0"));
Assert.Equal("v1b", item.GetMetadataValue("m1"));
Assert.Equal("v2", item.GetMetadataValue("m2"));
}
/// <summary>
/// Getting nonexistent metadata should return null
/// </summary>
[Fact]
public void GetNonexistentMetadata()
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='i0'/>");
Assert.Null(item.GetMetadata("m0"));
}
/// <summary>
/// Getting value of nonexistent metadata should return String.Empty
/// </summary>
[Fact]
public void GetNonexistentMetadataValue()
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='i0'/>");
Assert.Equal(String.Empty, item.GetMetadataValue("m0"));
}
/// <summary>
/// Attempting to set metadata with an invalid XML name should fail
/// </summary>
[Fact]
public void SetInvalidXmlNameMetadata()
{
Assert.Throws<ArgumentException>(() =>
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='c:\foo\bar.baz'/>");
item.SetMetadataValue("##invalid##", "x");
}
);
}
/// <summary>
/// Attempting to set built-in metadata should fail
/// </summary>
[Fact]
public void SetInvalidBuiltInMetadata()
{
Assert.Throws<ArgumentException>(() =>
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='c:\foo\bar.baz'/>");
item.SetMetadataValue("FullPath", "x");
}
);
}
/// <summary>
/// Attempting to set reserved metadata should fail
/// </summary>
[Fact]
public void SetInvalidReservedMetadata()
{
Assert.Throws<InvalidOperationException>(() =>
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='c:\foo\bar.baz'/>");
item.SetMetadataValue("Choose", "x");
}
);
}
/// <summary>
/// Metadata enumerator should only return custom metadata
/// </summary>
[Fact]
public void MetadataEnumeratorExcludesBuiltInMetadata()
{
ProjectItem item = GetOneItemFromFragment(@"<i Include='c:\foo\bar.baz'/>");
Assert.False(item.Metadata.GetEnumerator().MoveNext());
}
/// <summary>
/// Read off built-in metadata
/// </summary>
[Fact]
public void BuiltInMetadata()
{
ProjectItem item =
GetOneItemFromFragment(
NativeMethodsShared.IsWindows ? @"<i Include='c:\foo\bar.baz'/>" : @"<i Include='/foo/bar.baz'/>");
// c:\foo\bar.baz - /foo/bar.baz %(FullPath) = full path of item
// c:\ - / %(RootDir) = root directory of item
// bar %(Filename) = item filename without extension
// .baz %(Extension) = item filename extension
// c:\foo\ - /foo/ %(RelativeDir) = item directory as given in item-spec
// foo\ - /foo/ %(Directory) = full path of item directory relative to root
// [] %(RecursiveDir) = portion of item path that matched a recursive wildcard
// c:\foo\bar.baz - /foo/bar.baz %(Identity) = item-spec as given
// [] %(ModifiedTime) = last write time of item
// [] %(CreatedTime) = creation time of item
// [] %(AccessedTime) = last access time of item
Assert.Equal(
NativeMethodsShared.IsWindows ? @"c:\foo\bar.baz" : "/foo/bar.baz",
item.GetMetadataValue("FullPath"));
Assert.Equal(NativeMethodsShared.IsWindows ? @"c:\" : "/", item.GetMetadataValue("RootDir"));
Assert.Equal(@"bar", item.GetMetadataValue("Filename"));
Assert.Equal(@".baz", item.GetMetadataValue("Extension"));
Assert.Equal(NativeMethodsShared.IsWindows ? @"c:\foo\" : "/foo/", item.GetMetadataValue("RelativeDir"));
Assert.Equal(NativeMethodsShared.IsWindows ? @"foo\" : "foo/", item.GetMetadataValue("Directory"));
Assert.Equal(String.Empty, item.GetMetadataValue("RecursiveDir"));
Assert.Equal(
NativeMethodsShared.IsWindows ? @"c:\foo\bar.baz" : "/foo/bar.baz",
item.GetMetadataValue("Identity"));
}
/// <summary>
/// Check file-timestamp related metadata
/// </summary>
[Fact]
public void BuiltInMetadataTimes()
{
string path = null;
string fileTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff";
try
{
path = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
File.WriteAllText(path, String.Empty);
FileInfo info = new FileInfo(path);
ProjectItem item = GetOneItemFromFragment(@"<i Include='" + path + "'/>");
Assert.Equal(info.LastWriteTime.ToString(fileTimeFormat), item.GetMetadataValue("ModifiedTime"));
Assert.Equal(info.CreationTime.ToString(fileTimeFormat), item.GetMetadataValue("CreatedTime"));
Assert.Equal(info.LastAccessTime.ToString(fileTimeFormat), item.GetMetadataValue("AccessedTime"));
}
finally
{
File.Delete(path);
}
}
/// <summary>
/// Test RecursiveDir metadata
/// </summary>
[Fact]
public void RecursiveDirMetadata()
{
string directory = null;
string subdirectory = null;
string file = null;
try
{
directory = Path.Combine(Path.GetTempPath(), "a");
if (File.Exists(directory))
{
File.Delete(directory);
}
subdirectory = Path.Combine(directory, "b");
if (File.Exists(subdirectory))
{
File.Delete(subdirectory);
}
file = Path.Combine(subdirectory, "c");
Directory.CreateDirectory(subdirectory);
File.WriteAllText(file, String.Empty);
ProjectItem item =
GetOneItemFromFragment(
"<i Include='" + directory + (NativeMethodsShared.IsWindows ? @"\**\*'/>" : "/**/*'/>"));
Assert.Equal(NativeMethodsShared.IsWindows ? @"b\" : "b/", item.GetMetadataValue("RecursiveDir"));
Assert.Equal("c", item.GetMetadataValue("Filename"));
}
finally
{
File.Delete(file);
FileUtilities.DeleteWithoutTrailingBackslash(subdirectory);
FileUtilities.DeleteWithoutTrailingBackslash(directory);
}
}
/// <summary>
/// Correctly establish the "RecursiveDir" value when the include
/// is semicolon separated.
/// (This is what requires that the original include fragment [before wildcard
/// expansion] is stored in the item.)
/// </summary>
[Fact]
public void RecursiveDirWithSemicolonSeparatedInclude()
{
string directory = null;
string subdirectory = null;
string file = null;
try
{
directory = Path.Combine(Path.GetTempPath(), "a");
if (File.Exists(directory))
{
File.Delete(directory);
}
subdirectory = Path.Combine(directory, "b");
if (File.Exists(subdirectory))
{
File.Delete(subdirectory);
}
file = Path.Combine(subdirectory, "c");
Directory.CreateDirectory(subdirectory);
File.WriteAllText(file, String.Empty);
IList<ProjectItem> items = ObjectModelHelpers.GetItemsFromFragment("<i Include='i0;" + directory + (NativeMethodsShared.IsWindows ? @"\**\*;i2'/>" : "/**/*;i2'/>"));
Assert.Equal(3, items.Count);
Assert.Equal("i0", items[0].EvaluatedInclude);
Assert.Equal(NativeMethodsShared.IsWindows ? @"b\" : "b/", items[1].GetMetadataValue("RecursiveDir"));
Assert.Equal("i2", items[2].EvaluatedInclude);
}
finally
{
File.Delete(file);
FileUtilities.DeleteWithoutTrailingBackslash(subdirectory);
FileUtilities.DeleteWithoutTrailingBackslash(directory);
}
}
[Theory]
[InlineData(@"<i Condition='false' Include='\**\*.cs'/>")]
[InlineData(@"<i Condition='false' Include='/**/*.cs'/>")]
[InlineData(@"<i Condition='false' Include='/**\*.cs'/>")]
[InlineData(@"<i Condition='false' Include='\**/*.cs'/>")]
public void FullFileSystemScanGlobWithFalseCondition(string itemDefinition)
{
IList<ProjectItem> items = ObjectModelHelpers.GetItemsFromFragment(itemDefinition, allItems: false, ignoreCondition: true);
items.ShouldBeEmpty();
}
[Theory]
[InlineData(@"<i Condition='false' Include='somedir\**\*.cs'/>")]
[InlineData(@"<i Condition='false' Include='somedir/**/*.cs'/>")]
[InlineData(@"<i Condition='false' Include='somedir/**\*.cs'/>")]
[InlineData(@"<i Condition='false' Include='somedir\**/*.cs'/>")]
public void PartialFileSystemScanGlobWithFalseCondition(string itemDefinition)
{
using (TestEnvironment env = TestEnvironment.Create())
{
TransientTestFolder directory = env.CreateFolder(createFolder: true);
TransientTestFile file = env.CreateFile(directory, "a.cs", String.Empty);
IList<ProjectItem> items = ObjectModelHelpers.GetItemsFromFragment(itemDefinition.Replace("somedir", directory.Path), allItems: false, ignoreCondition: true);
items.ShouldNotBeEmpty();
}
}
/// <summary>
/// Basic exclude case
/// </summary>
[Fact]
public void Exclude()
{
IList<ProjectItem> items = ObjectModelHelpers.GetItemsFromFragment("<i Include='a;b' Exclude='b;c'/>");
Assert.Single(items);
Assert.Equal("a", items[0].EvaluatedInclude);
}
/// <summary>
/// Exclude against an include with item vectors in it
/// </summary>
[Fact]
public void ExcludeWithIncludeVector()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='a;b;c'>
</i>
</ItemGroup>
<ItemGroup>
<i Include='x;y;z;@(i);u;v;w' Exclude='b;y;v'>
</i>
</ItemGroup>
</Project>
";
IList<ProjectItem> items = ObjectModelHelpers.GetItems(content);
// Should contain a, b, c, x, z, a, c, u, w
Assert.Equal(9, items.Count);
ObjectModelHelpers.AssertItems(new[] { "a", "b", "c", "x", "z", "a", "c", "u", "w" }, items);
}
/// <summary>
/// Exclude with item vectors against an include with item vectors in it
/// </summary>
[Fact]
public void ExcludeVectorWithIncludeVector()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='a;b;c'>
</i>
<j Include='b;y;v' />
</ItemGroup>
<ItemGroup>
<i Include='x;y;z;@(i);u;v;w' Exclude='x;@(j);w'>
</i>
</ItemGroup>
</Project>
";
IList<ProjectItem> items = ObjectModelHelpers.GetItems(content);
// Should contain a, b, c, z, a, c, u
Assert.Equal(7, items.Count);
ObjectModelHelpers.AssertItems(new[] { "a", "b", "c", "z", "a", "c", "u" }, items);
}
[Theory]
// items as strings: escaped includes appear as unescaped
[InlineData(ItemWithIncludeAndExclude,
"%61;%62",
"b",
new string[0],
new[] { "a" })]
// items as strings: escaped include matches non-escaped exclude
[InlineData(ItemWithIncludeAndExclude,
"%61",
"a",
new string[0],
new string[0])]
// items as strings: non-escaped include matches escaped exclude
[InlineData(ItemWithIncludeAndExclude,
"a",
"%61",
new string[0],
new string[0])]
// items as files: non-escaped wildcard include matches escaped non-wildcard character
[InlineData(ItemWithIncludeAndExclude,
"a?b",
"a%40b",
new[] { "acb", "a@b" },
new[] { "acb" })]
// items as files: non-escaped non-wildcard include matches escaped non-wildcard character
[InlineData(ItemWithIncludeAndExclude,
"acb;a@b",
"a%40b",
new string[0],
new[] { "acb" })]
// items as files: escaped wildcard include matches escaped non-wildcard exclude
[InlineData(ItemWithIncludeAndExclude,
"a%40*b",
"a%40bb",
new[] { "a@b", "a@ab", "a@bb" },
new[] { "a@ab", "a@b" })]
// items as files: escaped wildcard include matches escaped wildcard exclude
[InlineData(ItemWithIncludeAndExclude,
"a%40*b",
"a%40?b",
new[] { "a@b", "a@ab", "a@bb" },
new[] { "a@b" })]
// items as files: non-escaped recursive wildcard include matches escaped recursive wildcard exclude
[InlineData(ItemWithIncludeAndExclude,
@"**\a*b",
@"**\a*%78b",
new[] { "aab", "aaxb", @"dir\abb", @"dir\abxb" },
new[] { "aab", @"dir\abb" })]
// items as files: include with non-escaped glob does not match exclude with escaped wildcard character.
// The exclude is treated as a literal, not a glob, and therefore should not match the input files
[InlineData(ItemWithIncludeAndExclude,
@"**\a*b",
@"**\a%2Axb",
new[] { "aab", "aaxb", @"dir\abb", @"dir\abxb" },
new[] { "aab", "aaxb", @"dir\abb", @"dir\abxb" })]
public void IncludeExcludeWithEscapedCharacters(string projectContents, string includeString, string excludeString, string[] inputFiles, string[] expectedInclude)
{
TestIncludeExcludeWithDifferentSlashes(projectContents, includeString, excludeString, inputFiles, expectedInclude);
}
[Theory]
// items as strings: include with both escaped and unescaped glob should be treated as literal and therefore not match against files as a glob
[InlineData(ItemWithIncludeAndExclude,
@"**\a%2Axb",
@"foo",
new[] { "aab", "aaxb", @"dir\abb", @"dir\abxb" },
new[] { @"**\a*xb" })]
// Include with both escaped and unescaped glob does not match exclude with escaped wildcard character which has a different slash orientation
// The presence of the escaped and unescaped glob should make things behave as strings-which-are-not-paths and not as strings-which-are-paths
[InlineData(ItemWithIncludeAndExclude,
@"**\a%2Axb",
@"**/a%2Axb",
new string[0],
new[] { @"**\a*xb" })]
// Slashes are not normalized when contents is not a path
[InlineData(ItemWithIncludeAndExclude,
@"a/b/foo::||bar;a/b/foo::||bar/;a/b/foo::||bar\;a/b\foo::||bar",
@"a/b/foo::||bar",
new string[0],
new [] { "a/b/foo::||bar/", @"a/b/foo::||bar\", @"a/b\foo::||bar" })]
public void IncludeExcludeWithNonPathContents(string projectContents, string includeString, string excludeString, string[] inputFiles, string[] expectedInclude)
{
TestIncludeExclude(projectContents, inputFiles, expectedInclude, includeString, excludeString, normalizeSlashes: false);
}
public static IEnumerable<object[]> IncludesAndExcludesWithWildcardsTestData => GlobbingTestData.IncludesAndExcludesWithWildcardsTestData;
[Theory]
[MemberData(nameof(IncludesAndExcludesWithWildcardsTestData))]
public void ExcludeVectorWithWildCards(string includeString, string excludeString, string[] inputFiles, string[] expectedInclude, bool makeExpectedIncludeAbsolute)
{
TestIncludeExcludeWithDifferentSlashes(ItemWithIncludeAndExclude, includeString, excludeString, inputFiles, expectedInclude, makeExpectedIncludeAbsolute);
}
[Theory]
[InlineData(ItemWithIncludeAndExclude,
@"**\*",
@"excludes\**.*",
new[]
{
@"a.cs",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
},
new[]
{
@"a.cs",
"build.proj",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
})]
[InlineData(ItemWithIncludeAndExclude,
@"**\*",
@"excludes\**..\*",
new[]
{
@"a.cs",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
},
new[]
{
@"a.cs",
"build.proj",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
})]
[InlineData(ItemWithIncludeAndExclude,
@"**\*",
@"**.*",
new[]
{
@"a.cs",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
},
new[]
{
@"a.cs",
"build.proj",
@"excludes\b.cs",
@"excludes\subdir\c.cs",
})]
[InlineData(ItemWithIncludeAndExclude,
"*;**a",
"**a",
new[]
{
"a",
},
new[]
{
"a",
"build.proj"
})]
[InlineData(ItemWithIncludeAndExclude,
@"**1;**2",
@"**1",
new[]
{
@"1",
@"2",
@"excludes\1",
@"excludes\2",
@"excludes\subdir\1",
@"excludes\subdir\2",
},
new[]
{
"**2"
})]
[InlineData(ItemWithIncludeAndExclude,
@":||;||:",
@"||:",
new string[0],
new[]
{
":||"
})]
public void ExcludeAndIncludeConsideredAsLiteralsWhenFilespecIsIllegal(string projectContents, string includeString, string excludeString, string[] inputFiles, string[] expectedInclude)
{
TestIncludeExclude(projectContents, inputFiles, expectedInclude, includeString, excludeString, normalizeSlashes: true);
}
[Theory]
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData(ItemWithIncludeAndExclude,
@"src/**/*.cs",
new[]
{
@"src/a.cs",
@"src/a/b/b.cs",
},
new[]
{
@"src/a.cs",
@"src/a/b/b.cs",
})]
[InlineData(ItemWithIncludeAndExclude,
@"src/test/**/*.cs",
new[]
{
@"src/test/a.cs",
@"src/test/a/b/c.cs",
},
new[]
{
@"src/test/a.cs",
@"src/test/a/b/c.cs",
})]
[InlineData(ItemWithIncludeAndExclude,
@"src/test/**/a/b/**/*.cs",
new[]
{
@"src/test/dir/a/b/a.cs",
@"src/test/dir/a/b/c/a.cs",
},
new[]
{
@"src/test/dir/a/b/a.cs",
@"src/test/dir/a/b/c/a.cs",
})]
public void IncludeWithWildcardShouldNotPreserveUserSlashesInFixedDirectoryPart(string projectContents, string includeString, string[] inputFiles, string[] expectedInclude)
{
Func<string, char, string> setSlashes = (s, c) => s.Replace('/', c).Replace('\\', c);
// set the include string slashes to the opposite orientation relative to the OS default slash
if (NativeMethodsShared.IsWindows)
{
includeString = setSlashes(includeString, '/');
}
else
{
includeString = setSlashes(includeString, '\\');
}
// all the slashes in the expected items should be platform specific
expectedInclude = expectedInclude.Select(p => setSlashes(p, Path.DirectorySeparatorChar)).ToArray();
TestIncludeExclude(projectContents, inputFiles, expectedInclude, includeString, "");
}
private static void TestIncludeExcludeWithDifferentSlashes(string projectContents, string includeString, string excludeString, string[] inputFiles, string[] expectedInclude, bool makeExpectedIncludeAbsolute = false)
{
Action<string, string> runTest = (include, exclude) =>
{
TestIncludeExclude(projectContents, inputFiles, expectedInclude, include, exclude, normalizeSlashes: true, makeExpectedIncludeAbsolute:makeExpectedIncludeAbsolute);
};
var includeWithForwardSlash = Helpers.ToForwardSlash(includeString);
var excludeWithForwardSlash = Helpers.ToForwardSlash(excludeString);
runTest(includeString, excludeString);
runTest(includeWithForwardSlash, excludeWithForwardSlash);
runTest(includeString, excludeWithForwardSlash);
runTest(includeWithForwardSlash, excludeString);
}
private static void TestIncludeExclude(string projectContents, string[] inputFiles, string[] expectedInclude, string include, string exclude, bool normalizeSlashes = false, bool makeExpectedIncludeAbsolute = false)
{
var formattedProjectContents = string.Format(projectContents, include, exclude);
ObjectModelHelpers.AssertItemEvaluationFromProject(formattedProjectContents, inputFiles, expectedInclude, expectedMetadataPerItem: null, normalizeSlashes: normalizeSlashes, makeExpectedIncludeAbsolute: makeExpectedIncludeAbsolute);
}
[Theory]
// exclude matches include; file is next to project file
[InlineData(ItemWithIncludeAndExclude,
@"a", // include item
@"", //path relative from projectFile. Empty string if current directory
@"a", //exclude item
"", //path relative from projectFile. Empty string if current directory
new[] //files relative to this test's root directory. The project is one level deeper than the root.
{
@"project\a",
},
false // whether the include survives the exclude (true) or not (false)
)]
// exclude matches include; file is below the project file
[InlineData(ItemWithIncludeAndExclude,
@"a",
@"dir",
@"a",
"dir",
new[]
{
@"project\dir\a",
},
false
)]
// exclude matches include; file is above the project file
[InlineData(ItemWithIncludeAndExclude,
@"a",
@"..",
@"a",
"..",
new[]
{
@"a",
},
false
)]
// exclude does not match include; file is next to project file; exclude points above the project file
[InlineData(ItemWithIncludeAndExclude,
"a",
"",
"a",
"..",
new[]
{
"a",
},
true
)]
// exclude does not match include; file is below the project file; exclude points next to the project file
[InlineData(ItemWithIncludeAndExclude,
"a",
"dir",
"a",
"",
new[]
{
@"project\dir\a",
},
true
)]
// exclude does not match include; file is above the project file; exclude points next to the project file
[InlineData(ItemWithIncludeAndExclude,
"a",
"..",
"a",
"",
new[]
{
"a",
},
true
)]
public void IncludeAndExcludeWorkWithRelativeAndAbsolutePaths(
string projectContents,
string includeItem,
string includeRelativePath,
string excludeItem,
string excludeRelativePath,
string[] inputFiles,
bool includeSurvivesExclude)
{
Func<bool, string, string, string, string> adjustFilePath = (isAbsolute, testRoot, relativeFragmentFromRootToFile, file) =>
isAbsolute
? Path.GetFullPath(Path.Combine(testRoot, relativeFragmentFromRootToFile, file))
: Path.Combine(relativeFragmentFromRootToFile, file);
Action<bool, bool> runTest = (includeIsAbsolute, excludeIsAbsolute) =>
{
using (var env = TestEnvironment.Create())
{
var projectFile = env
.CreateTestProjectWithFiles(projectContents, inputFiles, "project")
.ProjectFile;
var projectFileDir = Path.GetDirectoryName(projectFile);
var include = adjustFilePath(includeIsAbsolute, projectFileDir, includeRelativePath, includeItem);
var exclude = adjustFilePath(excludeIsAbsolute, projectFileDir, excludeRelativePath, excludeItem);
// includes and exclude may be absolute, so we can only format the project after we have the test directory paths
var formattedProject = string.Format(projectContents, include, exclude);
File.WriteAllText(projectFile, formattedProject);
var expectedInclude = includeSurvivesExclude ? new[] { include } : new string[0];
ObjectModelHelpers.AssertItems(expectedInclude, new Project(projectFile).Items.ToList());
}
};
runTest(true, false);
runTest(false, true);
runTest(true, true);
}
[Theory]
// exclude globbing cone at project level;
[InlineData(
"../a.cs;b.cs", // include string
"**/*.cs", // exclude string
new[] {"a.cs", "ProjectDir/b.cs"}, // files to create relative to the test root dir
"ProjectDir", // relative path from test root to project
new[] {"../a.cs"} // expected items
)]
// exclude globbing cone below project level;
[InlineData(
"a.cs;a/b.cs",
"a/**/*.cs",
new[] { "a.cs", "a/b.cs" },
"",
new[] {"a.cs"}
)]
// exclude globbing above project level;
[InlineData(
"a.cs;../b.cs;../../c.cs",
"../**/*.cs",
new[] { "a/ProjectDir/a.cs", "a/b.cs", "c.cs"},
"a/ProjectDir",
new[] { "../../c.cs" }
)]
public void ExcludeWithMissmatchingGlobCones(string includeString, string excludeString, string[] files, string relativePathFromRootToProject, string[] expectedInclude)
{
var projectContents = string.Format(ItemWithIncludeAndExclude, includeString, excludeString);
using (var env = TestEnvironment.Create())
using (var projectCollection = new ProjectCollection())
{
var testFiles = env.CreateTestProjectWithFiles(projectContents, files,relativePathFromRootToProject);
ObjectModelHelpers.AssertItems(expectedInclude, new Project(testFiles.ProjectFile, new Dictionary<string, string>(), MSBuildConstants.CurrentToolsVersion, projectCollection).Items.ToList());
}
}
[Theory(Skip = "https://github.com/Microsoft/msbuild/issues/1576")]
[InlineData(
"../**/*.cs", // include string
"a.cs", // exclude string
new[] {"ProjectDir/a.cs", "b.cs"}, // files to create relative to the test root dir
"ProjectDir", // relative path from test root to project
new[] {"../b.cs"} // expected items
)]
public void ExcludingRelativeItemToCurrentDirectoryShouldWorkWithAboveTheConeIncludes(string includeString, string excludeString, string[] files, string relativePathFromRootToProject, string[] expectedInclude)
{
var projectContents = string.Format(ItemWithIncludeAndExclude, includeString, excludeString);
using (var env = TestEnvironment.Create())
using (var projectCollection = new ProjectCollection())
{
var testFiles = env.CreateTestProjectWithFiles(projectContents, files, relativePathFromRootToProject);
ObjectModelHelpers.AssertItems(expectedInclude, new Project(testFiles.ProjectFile, new Dictionary<string, string>(), MSBuildConstants.CurrentToolsVersion, projectCollection).Items.ToList());
}
}
/// <summary>
/// Expression like @(x) should clone metadata, but metadata should still point at the original XML objects
/// </summary>
[Fact]
public void CopyFromWithItemListExpressionClonesMetadata()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemGroup>
<i Include='i1'>
<m>m1</m>
</i>
<j Include='@(i)'/>
</ItemGroup>
</Project>
";
Project project = new Project(XmlReader.Create(new StringReader(content)));
project.GetItems("i").First().SetMetadataValue("m", "m2");
ProjectItem item1 = project.GetItems("i").First();
ProjectItem item2 = project.GetItems("j").First();
Assert.Equal("m2", item1.GetMetadataValue("m"));
Assert.Equal("m1", item2.GetMetadataValue("m"));
// Should still point at the same XML items
Assert.True(Object.ReferenceEquals(item1.GetMetadata("m").Xml, item2.GetMetadata("m").Xml));
}
/// <summary>
/// Expression like @(x) should not clone metadata, even if the item type is different.
/// It's obvious that it shouldn't clone it if the item type is the same.
/// If it is different, it doesn't clone it for performance; even if the item definition metadata
/// changes later (this is design time), the inheritors of that item definition type
/// (even those that have subsequently been transformed to a different itemtype) should see
/// the changes, by design.
/// Just to make sure we don't change that behavior, we test it here.
/// </summary>
[Fact]
public void CopyFromWithItemListExpressionDoesNotCloneDefinitionMetadata()
{
string content = @"
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
<ItemDefinitionGroup>
<i>
<m>m1</m>
</i>
</ItemDefinitionGroup>
<ItemGroup>