Repository navigation
Expand file tree
/
Copy pathTaskExecutionHost_Tests.cs
More file actions
1556 lines (1338 loc) · 55.4 KB
/
Copy pathTaskExecutionHost_Tests.cs
File metadata and controls
1556 lines (1338 loc) · 55.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;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.Build.BackEnd;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Collections;
using Microsoft.Build.Construction;
using Microsoft.Build.Engine.UnitTests.TestComparers;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.Debugging;
using Shouldly;
using Xunit;
using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
#nullable disable
namespace Microsoft.Build.UnitTests.BackEnd
{
/// <summary>
/// The test class for the TaskExecutionHost
/// </summary>
public class TaskExecutionHost_Tests : ITestTaskHost, IBuildEngine2, IDisposable
{
/// <summary>
/// The set of parameters which have been initialized on the task.
/// </summary>
private Dictionary<string, object> _parametersSetOnTask;
/// <summary>
/// The set of outputs which were read from the task.
/// </summary>
private Dictionary<string, object> _outputsReadFromTask;
/// <summary>
/// The task execution host
/// </summary>
private TaskExecutionHost _host;
/// <summary>
/// The mock logging service
/// </summary>
private ILoggingService _loggingService;
/// <summary>
/// The mock logger
/// </summary>
private MockLogger _logger;
/// <summary>
/// Array containing one item, used for ITaskItem tests.
/// </summary>
private ITaskItem[] _oneItem;
/// <summary>
/// Array containing two items, used for ITaskItem tests.
/// </summary>
private ITaskItem[] _twoItems;
/// <summary>
/// The bucket which receives outputs.
/// </summary>
private ItemBucket _bucket;
/// <summary>
/// Unused.
/// </summary>
public bool IsRunningMultipleNodes
{
get { return false; }
}
/// <summary>
/// Unused.
/// </summary>
public bool ContinueOnError
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Unused.
/// </summary>
public int LineNumberOfTaskNode
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Unused.
/// </summary>
public int ColumnNumberOfTaskNode
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Unused.
/// </summary>
public string ProjectFileOfTaskNode
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Prepares the environment for the test.
/// </summary>
public TaskExecutionHost_Tests()
{
InitializeHost(false);
}
/// <summary>
/// Cleans up after the test
/// </summary>
public void Dispose()
{
if (_host != null)
{
((IDisposable)_host).Dispose();
}
_host = null;
}
/// <summary>
/// Validate that setting parameters with only the required parameters works.
/// </summary>
[Fact]
public void ValidateNoParameters()
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
parameters["ExecuteReturnParam"] = ("true", ElementLocation.Create("foo.proj"));
Assert.True(_host.SetTaskParameters(parameters));
Assert.Single(_parametersSetOnTask);
Assert.True(_parametersSetOnTask.ContainsKey("ExecuteReturnParam"));
}
/// <summary>
/// Validate that setting no parameters when a required parameter exists fails and throws an exception.
/// </summary>
[Fact]
public void ValidateNoParameters_MissingRequired()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
_host.SetTaskParameters(parameters);
});
}
/// <summary>
/// Validate that setting a non-existent parameter fails, but does not throw an exception.
/// </summary>
[Fact]
public void ValidateNonExistentParameter()
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
parameters["NonExistentParam"] = ("foo", ElementLocation.Create("foo.proj"));
Assert.False(_host.SetTaskParameters(parameters));
}
#region Bool Params
/// <summary>
/// Validate that setting a bool param works and sets the right value.
/// </summary>
[Fact]
public void TestSetBoolParam()
{
ValidateTaskParameter("BoolParam", "true", true);
}
/// <summary>
/// Validate that setting a bool param works and sets the right value.
/// </summary>
[Fact]
public void TestSetBoolParamFalse()
{
ValidateTaskParameter("BoolParam", "false", false);
}
/// <summary>
/// Validate that setting a bool param with an empty value does not cause the parameter to get set.
/// </summary>
[Fact]
public void TestSetBoolParamEmptyAttribute()
{
ValidateTaskParameterNotSet("BoolParam", "");
}
/// <summary>
/// Validate that setting a bool param with a property which evaluates to nothing does not cause the parameter to get set.
/// </summary>
[Fact]
public void TestSetBoolParamEmptyProperty()
{
ValidateTaskParameterNotSet("BoolParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting a bool param with an item which evaluates to nothing does not cause the parameter to get set.
/// </summary>
[Fact]
public void TestSetBoolParamEmptyItem()
{
ValidateTaskParameterNotSet("BoolParam", "@(NonExistentItem)");
}
#endregion
#region Bool Array Params
/// <summary>
/// Validate that setting a bool array with a single true sets the array to one 'true' value.
/// </summary>
[Fact]
public void TestSetBoolArrayParamOneItem()
{
ValidateTaskParameterArray("BoolArrayParam", "true", new bool[] { true });
}
/// <summary>
/// Validate that setting a bool array with a list of two values sets them appropriately.
/// </summary>
[Fact]
public void TestSetBoolArrayParamTwoItems()
{
ValidateTaskParameterArray("BoolArrayParam", "false;true", new bool[] { false, true });
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetBoolArrayParamEmptyAttribute()
{
ValidateTaskParameterNotSet("BoolArrayParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetBoolArrayParamEmptyProperty()
{
ValidateTaskParameterNotSet("BoolArrayParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetBoolArrayParamEmptyItem()
{
ValidateTaskParameterNotSet("BoolArrayParam", "@(NonExistentItem)");
}
#endregion
#region Int Params
/// <summary>
/// Validate that setting an int param with a value of 0 causes it to get the correct value.
/// </summary>
[Fact]
public void TestSetIntParamZero()
{
ValidateTaskParameter("IntParam", "0", 0);
}
/// <summary>
/// Validate that setting an int param with a value of 1 causes it to get the correct value.
/// </summary>
[Fact]
public void TestSetIntParamOne()
{
ValidateTaskParameter("IntParam", "1", 1);
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntParamEmptyAttribute()
{
ValidateTaskParameterNotSet("IntParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntParamEmptyProperty()
{
ValidateTaskParameterNotSet("IntParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntParamEmptyItem()
{
ValidateTaskParameterNotSet("IntParam", "@(NonExistentItem)");
}
#endregion
#region Int Array Params
/// <summary>
/// Validate that setting an int array with a single value causes it to get a single value.
/// </summary>
[Fact]
public void TestSetIntArrayParamOneItem()
{
ValidateTaskParameterArray("IntArrayParam", "0", new int[] { 0 });
}
/// <summary>
/// Validate that setting an int array with a list of values causes it to get the correct values.
/// </summary>
[Fact]
public void TestSetIntArrayParamTwoItems()
{
SetTaskParameter("IntArrayParam", "1;0");
Assert.True(_parametersSetOnTask.ContainsKey("IntArrayParam"));
Assert.Equal(1, ((int[])_parametersSetOnTask["IntArrayParam"])[0]);
Assert.Equal(0, ((int[])_parametersSetOnTask["IntArrayParam"])[1]);
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntArrayParamEmptyAttribute()
{
ValidateTaskParameterNotSet("IntArrayParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntArrayParamEmptyProperty()
{
ValidateTaskParameterNotSet("IntArrayParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetIntArrayParamEmptyItem()
{
ValidateTaskParameterNotSet("IntArrayParam", "@(NonExistentItem)");
}
#endregion
#region String Params
/// <summary>
/// Test that setting a string param sets the correct value.
/// </summary>
[Fact]
public void TestSetStringParam()
{
ValidateTaskParameter("StringParam", "0", "0");
}
/// <summary>
/// Test that setting a string param sets the correct value.
/// </summary>
[Fact]
public void TestSetStringParamOne()
{
ValidateTaskParameter("StringParam", "1", "1");
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringParamEmptyAttribute()
{
ValidateTaskParameterNotSet("StringParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringParamEmptyProperty()
{
ValidateTaskParameterNotSet("StringParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringParamEmptyItem()
{
ValidateTaskParameterNotSet("StringParam", "@(NonExistentItem)");
}
#endregion
#region String Array Params
/// <summary>
/// Validate that setting a string array with a single value sets the correct value.
/// </summary>
[Fact]
public void TestSetStringArrayParam()
{
ValidateTaskParameterArray("StringArrayParam", "0", new string[] { "0" });
}
/// <summary>
/// Validate that setting a string array with a list of two values sets the correct values.
/// </summary>
[Fact]
public void TestSetStringArrayParamOne()
{
ValidateTaskParameterArray("StringArrayParam", "1;0", new string[] { "1", "0" });
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringArrayParamEmptyAttribute()
{
ValidateTaskParameterNotSet("StringArrayParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringArrayParamEmptyProperty()
{
ValidateTaskParameterNotSet("StringArrayParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetStringArrayParamEmptyItem()
{
ValidateTaskParameterNotSet("StringArrayParam", "@(NonExistentItem)");
}
#endregion
#region ITaskItem Params
/// <summary>
/// Validate that setting an item with an item list evaluating to one item sets the value appropriately, including metadata.
/// </summary>
[Fact]
public void TestSetItemParamSingle()
{
ValidateTaskParameterItem("ItemParam", "@(ItemListContainingOneItem)", _oneItem[0]);
}
/// <summary>
/// Validate that setting an item with an item list evaluating to two items sets the value appropriately, including metadata.
/// </summary>
[Fact]
public void TestSetItemParamDouble()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
ValidateTaskParameterItems("ItemParam", "@(ItemListContainingTwoItems)", _twoItems);
});
}
/// <summary>
/// Validate that setting an item with a string results in an item with the evaluated include set to the string.
/// </summary>
[Fact]
public void TestSetItemParamString()
{
ValidateTaskParameterItem("ItemParam", "MyItemName");
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemParamEmptyAttribute()
{
ValidateTaskParameterNotSet("ItemParam", "");
}
/// <summary>
/// Validate that setting the parameter with a property which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemParamEmptyProperty()
{
ValidateTaskParameterNotSet("ItemParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemParamEmptyItem()
{
ValidateTaskParameterNotSet("ItemParam", "@(NonExistentItem)");
}
#endregion
#region ITaskItem Array Params
/// <summary>
/// Validate that setting an item array using an item list containing one item sets a single item.
/// </summary>
[Fact]
public void TestSetItemArrayParamSingle()
{
ValidateTaskParameterItems("ItemArrayParam", "@(ItemListContainingOneItem)", _oneItem);
}
/// <summary>
/// Validate that setting an item array using an item list containing two items sets both items.
/// </summary>
[Fact]
public void TestSetItemArrayParamDouble()
{
ValidateTaskParameterItems("ItemArrayParam", "@(ItemListContainingTwoItems)", _twoItems);
}
/// <summary>
/// Validate that setting an item array with
/// </summary>
[Fact]
public void TestSetItemArrayParamString()
{
ValidateTaskParameterItems("ItemArrayParam", "MyItemName");
}
/// <summary>
/// Validate that setting an item array with a list with multiple values creates multiple items.
/// </summary>
[Fact]
public void TestSetItemArrayParamTwoStrings()
{
ValidateTaskParameterItems("ItemArrayParam", "MyItemName;MyOtherItemName", new string[] { "MyItemName", "MyOtherItemName" });
}
/// <summary>
/// Validate that setting the parameter with an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemArrayParamEmptyAttribute()
{
ValidateTaskParameterNotSet("ItemArrayParam", "");
}
/// <summary>
/// Validate that setting the parameter with a parameter which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemArrayParamEmptyProperty()
{
ValidateTaskParameterNotSet("ItemArrayParam", "$(NonExistentProperty)");
}
/// <summary>
/// Validate that setting the parameter with an item which evaluates to an empty value does not cause it to be set.
/// </summary>
[Fact]
public void TestSetItemArrayParamEmptyItem()
{
ValidateTaskParameterNotSet("ItemArrayParam", "@(NonExistentItem)");
}
#endregion
#region Execute Tests
/// <summary>
/// Tests that successful execution returns true.
/// </summary>
[Fact]
public void TestExecuteTrue()
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
parameters["ExecuteReturnParam"] = ("true", ElementLocation.Create("foo.proj"));
Assert.True(_host.SetTaskParameters(parameters));
bool executeValue = _host.Execute();
Assert.True(executeValue);
}
/// <summary>
/// Tests that unsuccessful execution returns false.
/// </summary>
[Fact]
public void TestExecuteFalse()
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
parameters["ExecuteReturnParam"] = ("false", ElementLocation.Create("foo.proj"));
Assert.True(_host.SetTaskParameters(parameters));
bool executeValue = _host.Execute();
Assert.False(executeValue);
}
/// <summary>
/// Tests that when Execute throws, the exception bubbles up.
/// </summary>
[Fact]
public void TestExecuteThrow()
{
Assert.Throws<IndexOutOfRangeException>(() =>
{
var parameters = new Dictionary<string, (string, ElementLocation)>(StringComparer.OrdinalIgnoreCase);
parameters["ExecuteReturnParam"] = ("false", ElementLocation.Create("foo.proj"));
Dispose();
InitializeHost(true);
Assert.True(_host.SetTaskParameters(parameters));
_host.Execute();
});
}
#endregion
#region Bool Outputs
/// <summary>
/// Validate that boolean output to an item produces the correct evaluated include.
/// </summary>
[Fact]
public void TestOutputBoolToItem()
{
SetTaskParameter("BoolParam", "true");
ValidateOutputItem("BoolOutput", "True");
}
/// <summary>
/// Validate that boolean output to a property produces the correct evaluated value.
/// </summary>
[Fact]
public void TestOutputBoolToProperty()
{
SetTaskParameter("BoolParam", "true");
ValidateOutputProperty("BoolOutput", "True");
}
/// <summary>
/// Validate that boolean array output to an item array produces the correct evaluated includes.
/// </summary>
[Fact]
public void TestOutputBoolArrayToItems()
{
SetTaskParameter("BoolArrayParam", "false;true");
ValidateOutputItems("BoolArrayOutput", new string[] { "False", "True" });
}
/// <summary>
/// Validate that boolean array output to an item produces the correct semi-colon-delimited evaluated value.
/// </summary>
[Fact]
public void TestOutputBoolArrayToProperty()
{
SetTaskParameter("BoolArrayParam", "false;true");
ValidateOutputProperty("BoolArrayOutput", "False;True");
}
#endregion
#region Int Outputs
/// <summary>
/// Validate that an int output to an item produces the correct evaluated include
/// </summary>
[Fact]
public void TestOutputIntToItem()
{
SetTaskParameter("IntParam", "42");
ValidateOutputItem("IntOutput", "42");
}
/// <summary>
/// Validate that an int output to an property produces the correct evaluated value.
/// </summary>
[Fact]
public void TestOutputIntToProperty()
{
SetTaskParameter("IntParam", "42");
ValidateOutputProperty("IntOutput", "42");
}
/// <summary>
/// Validate that an int array output to an item produces the correct evaluated includes.
/// </summary>
[Fact]
public void TestOutputIntArrayToItems()
{
SetTaskParameter("IntArrayParam", "42;99");
ValidateOutputItems("IntArrayOutput", new string[] { "42", "99" });
}
/// <summary>
/// Validate that an int array output to a property produces the correct semi-colon-delimited evaluated value.
/// </summary>
[Fact]
public void TestOutputIntArrayToProperty()
{
SetTaskParameter("IntArrayParam", "42;99");
ValidateOutputProperty("IntArrayOutput", "42;99");
}
#endregion
#region String Outputs
/// <summary>
/// Validate that a string output to an item produces the correct evaluated include.
/// </summary>
[Fact]
public void TestOutputStringToItem()
{
SetTaskParameter("StringParam", "FOO");
ValidateOutputItem("StringOutput", "FOO");
}
/// <summary>
/// Validate that a string output to a property produces the correct evaluated value.
/// </summary>
[Fact]
public void TestOutputStringToProperty()
{
SetTaskParameter("StringParam", "FOO");
ValidateOutputProperty("StringOutput", "FOO");
}
/// <summary>
/// Validate that an empty string output overwrites the property value
/// </summary>
[Fact]
public void TestOutputEmptyStringToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("EmptyStringOutput", String.Empty);
}
/// <summary>
/// Validate that an empty string array output overwrites the property value
/// </summary>
[Fact]
public void TestOutputEmptyStringArrayToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("EmptyStringArrayOutput", String.Empty);
}
/// <summary>
/// A string output returning null should not cause any property set.
/// </summary>
[Fact]
public void TestOutputNullStringToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("NullStringOutput", "initialvalue");
}
/// <summary>
/// A string output returning null should not cause any property set.
/// </summary>
[Fact]
public void TestOutputNullITaskItemToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("NullITaskItemOutput", "initialvalue");
}
/// <summary>
/// A string output returning null should not cause any property set.
/// </summary>
[Fact]
public void TestOutputNullStringArrayToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("NullStringArrayOutput", "initialvalue");
}
/// <summary>
/// A string output returning null should not cause any property set.
/// </summary>
[Fact]
public void TestOutputNullITaskItemArrayToProperty()
{
_bucket.Lookup.SetProperty(ProjectPropertyInstance.Create("output", "initialvalue"));
ValidateOutputProperty("NullITaskItemArrayOutput", "initialvalue");
}
/// <summary>
/// Validate that a string array output to an item produces the correct evaluated includes.
/// </summary>
[Fact]
public void TestOutputStringArrayToItems()
{
SetTaskParameter("StringArrayParam", "FOO;bar");
ValidateOutputItems("StringArrayOutput", new string[] { "FOO", "bar" });
}
/// <summary>
/// Validate that a string array output to a property produces the correct semi-colon-delimited evaluated value.
/// </summary>
[Fact]
public void TestOutputStringArrayToProperty()
{
SetTaskParameter("StringArrayParam", "FOO;bar");
ValidateOutputProperty("StringArrayOutput", "FOO;bar");
}
#endregion
#region Item Outputs
/// <summary>
/// Validate that an item output to an item replicates the item, with metadata
/// </summary>
[Fact]
public void TestOutputItemToItem()
{
SetTaskParameter("ItemParam", "@(ItemListContainingOneItem)");
ValidateOutputItems("ItemOutput", _oneItem);
}
/// <summary>
/// Validate than an item output to a property produces the correct evaluated value.
/// </summary>
[Fact]
public void TestOutputItemToProperty()
{
SetTaskParameter("ItemParam", "@(ItemListContainingOneItem)");
ValidateOutputProperty("ItemOutput", _oneItem[0].ItemSpec);
}
/// <summary>
/// Validate that an item array output to an item replicates the items, with metadata.
/// </summary>
[Fact]
public void TestOutputItemArrayToItems()
{
SetTaskParameter("ItemArrayParam", "@(ItemListContainingTwoItems)");
ValidateOutputItems("ItemArrayOutput", _twoItems);
}
/// <summary>
/// Validate that an item array output to a property produces the correct semi-colon-delimited evaluated value.
/// </summary>
[Fact]
public void TestOutputItemArrayToProperty()
{
SetTaskParameter("ItemArrayParam", "@(ItemListContainingTwoItems)");
ValidateOutputProperty("ItemArrayOutput", String.Concat(_twoItems[0].ItemSpec, ";", _twoItems[1].ItemSpec));
}
#endregion
#region Other Output Tests
/// <summary>
/// Attempts to gather outputs into an item list from an string task parameter that
/// returns an empty string. This should be a no-op.
/// </summary>
[Fact]
public void TestEmptyStringInStringArrayParameterIntoItemList()
{
SetTaskParameter("StringArrayParam", "");
ValidateOutputItems("StringArrayOutput", Array.Empty<ITaskItem>());
}
/// <summary>
/// Attempts to gather outputs into an item list from an string task parameter that
/// returns an empty string. This should be a no-op.
/// </summary>
[Fact]
public void TestEmptyStringParameterIntoItemList()
{
SetTaskParameter("StringParam", "");
ValidateOutputItems("StringOutput", Array.Empty<ITaskItem>());
}
/// <summary>
/// Attempts to gather outputs from a null task parameter of type "ITaskItem[]". This should succeed.
/// </summary>
[Fact]
public void TestNullITaskItemArrayParameter()
{
ValidateOutputItems("ItemArrayNullOutput", Array.Empty<ITaskItem>());
}
/// <summary>
/// Attempts to gather outputs from a task parameter of type "ArrayList". This should fail.
/// </summary>
[Fact]
public void TestArrayListParameter()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
ValidateOutputItems("ArrayListOutput", Array.Empty<ITaskItem>());
});
}
/// <summary>
/// Attempts to gather outputs from a non-existent output. This should fail.
/// </summary>
[Fact]
public void TestNonexistantOutput()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
Assert.False(_host.GatherTaskOutputs("NonExistentOutput", ElementLocation.Create(".", 1, 1), true, "output"));
});
}
/// <summary>
/// object[] should not be a supported output type.
/// </summary>
[Fact]
public void TestOutputObjectArrayToProperty()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
ValidateOutputProperty("ObjectArrayOutput", "");
});
}
#endregion
#region Other Tests
/// <summary>
/// Test that cleanup for task clears out the task instance.
/// </summary>
[Fact]
public void TestCleanupForTask()
{
_host.CleanupForBatch();
Assert.NotNull((_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper);
_host.CleanupForTask();
Assert.Null((_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper);
}
/// <summary>
/// Test that a using task which specifies an invalid assembly produces an exception.
/// </summary>
[Fact]
public void TestTaskResolutionFailureWithUsingTask()
{
Assert.Throws<InvalidProjectFileException>(() =>
{
_loggingService = new MockLoggingService();
Dispose();
_host = new TaskExecutionHost();
TargetLoggingContext tlc = new TargetLoggingContext(_loggingService, new BuildEventContext(1, 1, BuildEventContext.InvalidProjectContextId, 1));
ProjectInstance project = CreateTestProject();
_host.InitializeForTask(
this,
tlc,
project,
"TaskWithMissingAssembly",
ElementLocation.Create("none", 1, 1),
this,
false,
#if FEATURE_APPDOMAIN
null,
#endif
false,
CancellationToken.None);
_host.FindTask(null);