Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ILLink.Tasks.Tests.cs « ILLink.Tasks.Tests « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3a9b2e51ec10a3fe22863966cd0d0afb3504134 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Linker;
using Xunit;

namespace ILLink.Tasks.Tests
{
	// These tests ensure that the task options correctly flow from
	// the task -> response file -> parsed arguments -> options on LinkContext
	public class TaskArgumentTests
	{
		public static IEnumerable<object[]> AssemblyPathsCases => new List<object[]> {
			new object [] {
				new ITaskItem [] {
					new TaskItem ("Assembly.dll", new Dictionary<string, string> { { "trimmode", "copy" } })
				}
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("Assembly.dll", new Dictionary<string, string> { { "TrimMode", "Copy" } })
				}
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("path with/spaces/Assembly.dll")
				}
			},
			new object [] {
				new ITaskItem [] {
					// same path
					new TaskItem ("path/to/Assembly1.dll"),
					new TaskItem ("path/to/Assembly2.dll")
				}
			},
			new object [] {
				new ITaskItem [] {
					// same assembly
					new TaskItem ("path/to/Assembly.dll"),
					new TaskItem ("path/to/Assembly.dll")
				}
			},
			new object [] {
				new ITaskItem [] {
					// same assembly name, different paths
					new TaskItem ("path1/Assembly.dll"),
					new TaskItem ("path2/Assembly.dll")
				}
			}
		};

		[Theory]
		[MemberData (nameof (AssemblyPathsCases))]
		public void TestAssemblyPaths (ITaskItem[] assemblyPaths)
		{
			var task = new MockTask () {
				AssemblyPaths = assemblyPaths
			};
			using (var driver = task.CreateDriver ()) {
				var context = driver.Context;

				var expectedReferences = assemblyPaths.Select (p => p.ItemSpec)
					.GroupBy (p => Path.GetFileNameWithoutExtension (p))
					.Select (g => g.First ());
				var actualReferences = driver.GetReferenceAssemblies ();
				Assert.Equal (expectedReferences.OrderBy (a => a), actualReferences.OrderBy (a => a));

				foreach (var item in assemblyPaths) {
					var assemblyPath = item.ItemSpec;
					var trimMode = item.GetMetadata ("TrimMode");
					if (String.IsNullOrEmpty (trimMode))
						continue;

					AssemblyAction expectedAction = (AssemblyAction) Enum.Parse (typeof (AssemblyAction), trimMode, ignoreCase: true);
					AssemblyAction actualAction = context.Actions[Path.GetFileNameWithoutExtension (assemblyPath)];

					Assert.Equal (expectedAction, actualAction);
				}
			}
		}

		[Fact]
		public void TestAssemblyPathsWithInvalidAction ()
		{
			var task = new MockTask () {
				AssemblyPaths = new ITaskItem[] { new TaskItem ("Assembly.dll", new Dictionary<string, string> { { "TrimMode", "invalid" } }) }
			};

			using (var driver = task.CreateDriver ()) {
				Assert.Equal (1031, driver.Logger.Messages[0].Code);
			}
		}

		// the InlineData string [] parameters are wrapped in object [] as described in https://github.com/xunit/xunit/issues/2060

		[Theory]
		[InlineData (new object[] { new string[] { "path/to/Assembly.dll" } })]
		[InlineData (new object[] { new string[] { "path with/spaces/Assembly.dll" } })]
		[InlineData (new object[] { new string[] { "path/to/Assembly With Spaces.dll" } })]
		public void TestReferenceAssemblyPaths (string[] referenceAssemblyPaths)
		{
			var task = new MockTask () {
				ReferenceAssemblyPaths = referenceAssemblyPaths.Select (p => new TaskItem (p)).ToArray ()
			};
			using (var driver = task.CreateDriver ()) {
				var expectedReferences = referenceAssemblyPaths;
				var actualReferences = driver.GetReferenceAssemblies ();
				Assert.Equal (expectedReferences.OrderBy (a => a), actualReferences.OrderBy (a => a));
				foreach (var reference in expectedReferences) {
					var referenceName = Path.GetFileNameWithoutExtension (reference);
					var actualAction = driver.Context.Actions[referenceName];
					Assert.Equal (AssemblyAction.Skip, actualAction);
				}
			}
		}

		[Theory]
		[InlineData (new object[] { new string[] { "illink.dll" } })]
		[InlineData (new object[] { new string[] { "illink" } })]
		public void TestRootEntryPointAssemblyNames (string[] rootAssemblyNames)
		{
			var task = new MockTask () {
				RootAssemblyNames = rootAssemblyNames.Select (a => new TaskItem (a)).ToArray ()
			};

			using (var driver = task.CreateDriver ()) {
				var expectedRoots = rootAssemblyNames;
				var actualRoots = driver.GetRootAssemblies ();
				Assert.Equal (rootAssemblyNames.OrderBy (r => r), actualRoots.OrderBy (r => r));
			}
		}

		[Theory]
		[InlineData ("path/to/directory")]
		[InlineData ("path with/spaces")]
		public void TestOutputDirectory (string outputDirectory)
		{
			var task = new MockTask () {
				OutputDirectory = new TaskItem (outputDirectory)
			};
			using (var driver = task.CreateDriver ()) {
				var actualOutputDirectory = driver.Context.OutputDirectory;
				Assert.Equal (outputDirectory, actualOutputDirectory);
			}
		}

		[Theory]
		[InlineData (new object[] { new string[] { "combined_output.xml" } })]
		public void TestRootDescriptorFiles (string[] rootDescriptorFiles)
		{
			var task = new MockTask () {
				RootDescriptorFiles = rootDescriptorFiles.Select (f => new TaskItem (f)).ToArray ()
			};
			using (var driver = task.CreateDriver ()) {
				var actualDescriptors = driver.GetRootDescriptors ();
				Assert.Equal (rootDescriptorFiles.OrderBy (f => f), actualDescriptors.OrderBy (f => f));
			}
		}

		public static IEnumerable<object[]> OptimizationsCases ()
		{
			foreach (var optimization in MockTask.OptimizationNames) {
				yield return new object[] { optimization, true };
				yield return new object[] { optimization, false };
			}
		}

		[Theory]
		[MemberData (nameof (OptimizationsCases))]
		public void TestGlobalOptimizations (string optimization, bool enabled)
		{
			var task = new MockTask ();
			task.SetOptimization (optimization, enabled);
			// get the corresponding CodeOptimizations value
			using (var driver = task.CreateDriver ()) {
				Assert.True (driver.GetOptimizationName (optimization, out CodeOptimizations codeOptimizations));
				var actualValue = driver.Context.Optimizations.IsEnabled (codeOptimizations, assemblyName: null);
				Assert.Equal (enabled, actualValue);
			}
		}

		public static IEnumerable<object[]> PerAssemblyOptimizationsCases ()
		{
			// test that we can individually enable/disable each optimization
			foreach (var optimization in MockTask.OptimizationNames) {
				yield return new object[] {
					new ITaskItem [] {
						new TaskItem ("path/to/Assembly.dll", new Dictionary<string, string> {
							{ optimization, "True" }
						})
					}
				};
				yield return new object[] {
					new ITaskItem [] {
						new TaskItem ("path/to/Assembly.dll", new Dictionary<string, string> {
							{ optimization, "False" }
						})
					}
				};
			}
			// complex case with multiple optimizations, assemblies
			yield return new object[] {
				new ITaskItem [] {
					new TaskItem ("path/to/Assembly1.dll", new Dictionary<string, string> {
						{ "Sealer", "True" },
						{ "BeforeFieldInit", "False" }
					}),
					new TaskItem ("path/to/Assembly2.dll", new Dictionary<string, string> {
						{ "Sealer", "False" },
						{ "BeforeFieldInit", "True" }
					})
				}
			};
		}

		[Theory]
		[MemberData (nameof (PerAssemblyOptimizationsCases))]
		public void TestPerAssemblyOptimizations (ITaskItem[] assemblyPaths)
		{
			var task = new MockTask () {
				AssemblyPaths = assemblyPaths
			};
			using (var driver = task.CreateDriver ()) {
				foreach (var item in assemblyPaths) {
					var assemblyName = Path.GetFileNameWithoutExtension (item.ItemSpec);
					foreach (var optimization in MockTask.OptimizationNames) {
						Assert.True (driver.GetOptimizationName (optimization, out CodeOptimizations codeOptimizations));
						var optimizationValue = item.GetMetadata (optimization);
						if (String.IsNullOrEmpty (optimizationValue))
							continue;
						var enabled = Boolean.Parse (optimizationValue);
						var actualValue = driver.Context.Optimizations.IsEnabled (codeOptimizations, assemblyName: assemblyName);
						Assert.Equal (enabled, actualValue);
					}
				}
			}
		}

		public static IEnumerable<object[]> SingleWarnCases => new List<object[]> {
			new object[] {
				true,
				new ITaskItem [] {
					new TaskItem ("AssemblyTrue.dll", new Dictionary<string, string> { { "TrimmerSingleWarn", "true" } } ),
					new TaskItem ("AssemblyFalse.dll", new Dictionary<string, string> { { "TrimmerSingleWarn", "false" } } )
				},
			},
			new object [] {
				false,
				new ITaskItem [] {
					new TaskItem ("AssemblyTrue.dll", new Dictionary<string, string> { { "TrimmerSingleWarn", "true" } } ),
					new TaskItem ("AssemblyFalse.dll", new Dictionary<string, string> { { "TrimmerSingleWarn", "false" } } )
				}
			}
		};

		[Theory]
		[MemberData (nameof (SingleWarnCases))]
		public void TestSingleWarn (bool singleWarn, ITaskItem[] assemblyPaths)
		{
			var task = new MockTask () {
				AssemblyPaths = assemblyPaths,
				SingleWarn = singleWarn
			};
			using (var driver = task.CreateDriver ()) {
				Assert.Equal (singleWarn, driver.Context.GeneralSingleWarn);
				var expectedSingleWarn = assemblyPaths.ToDictionary (
					p => Path.GetFileNameWithoutExtension (p.ItemSpec),
					p => bool.Parse (p.GetMetadata ("TrimmerSingleWarn"))
				);
				Assert.Equal (expectedSingleWarn, driver.Context.SingleWarn);
			}
		}

		[Fact]
		public void TestInvalidPerAssemblyOptimizations ()
		{
			var task = new MockTask () {
				AssemblyPaths = new ITaskItem[] {
					new TaskItem ("path/to/Assembly.dll", new Dictionary<string, string> {
						{ "Sealer", "invalid" }
					})
				}
			};
			Assert.Throws<ArgumentException> (() => task.CreateDriver ());
		}

		[Fact]
		public void TestOptimizationsDefaults ()
		{
			var task = new MockTask ();
			using (var driver = task.CreateDriver ()) {
				var expectedOptimizations = driver.GetDefaultOptimizations ();
				var actualOptimizations = driver.Context.Optimizations.Global;
				Assert.Equal (expectedOptimizations, actualOptimizations);
			}
		}

		[Fact]
		public void CheckGlobalOptimizationsMatchPerAssemblyOptimizations ()
		{
			var task = new MockTask ();
			var optimizationMetadataNames = MockTask.OptimizationNames;
			var optimizationPropertyNames = MockTask.GetOptimizationPropertyNames ();
			Assert.Equal (optimizationMetadataNames.OrderBy (o => o), optimizationPropertyNames.OrderBy (o => o));
		}

		[Theory]
		[InlineData ("IL2001;IL2002;IL2003;IL2004", 4)]
		[InlineData ("IL2001 IL2002 IL2003 IL2004", 4)]
		[InlineData ("IL2001,IL2002,IL2003,IL2004", 4)]
		[InlineData ("IL2001,IL2002;IL2003 IL2004", 4)]
		[InlineData ("IL2001,IL2002,IL8000,IL1003", 4)]
		[InlineData ("IL20000,IL02000", 2)]
		[InlineData ("   IL2001\n  IL2002;\n \tIL2003", 3)]
		public void TestValidNoWarn (string noWarn, int validNoWarns)
		{
			var task = new MockTask () {
				NoWarn = noWarn
			};
			using (var driver = task.CreateDriver ()) {
				var actualUsedNoWarns = driver.Context.NoWarn;
				Assert.Equal (validNoWarns, actualUsedNoWarns.Count);
			}
		}

		[Theory]
		[InlineData ("0", WarnVersion.ILLink0)]
		[InlineData ("5", WarnVersion.ILLink5)]
		[InlineData ("6", (WarnVersion) 6)]
		[InlineData ("9999", WarnVersion.Latest)]
		public void TestWarn (string warnArg, WarnVersion expectedVersion)
		{
			var task = new MockTask () {
				Warn = warnArg
			};
			using (var driver = task.CreateDriver ()) {
				Assert.Equal (expectedVersion, driver.Context.WarnVersion);
			}
		}

#nullable enable
		[Theory]
		[InlineData (true, null, null, new int[] { }, new int[] { })]
		[InlineData (false, "IL1001,IL2000,IL2054,IL2022", null,
			new int[] { 1001, 2000, 2054, 2022 }, new int[] { })]
		[InlineData (false, "IL2023,IL6000;IL5042 IL2040", "IL4000,IL4001;IL4002 IL4003",
			new int[] { 2023, 2040, 5042, 6000 }, new int[] { 4000, 4001, 4002, 4003 })]
		[InlineData (false, "IL3000;IL3000;ABCD", "IL2005 IL3005 IL2005",
			new int[] { 3000 }, new int[] { 2005, 3005 })]
		[InlineData (true, null, "IL2067", new int[] { }, new int[] { 2067 })]
		[InlineData (true, "IL2001", "IL2001", new int[] { }, new int[] { 2001 })]
		[InlineData (false, "IL1001;\n\t IL1002\n\t IL1003", null,
			new int[] { 1001, 1002, 1003 }, new int[] { })]
		public void TestWarningsAsErrors (bool treatWarningsAsErrors, string? warningsAsErrors, string? warningsNotAsErrors, int[] warnAsError, int[] warnNotAsError)
		{
			var task = new MockTask () {
				TreatWarningsAsErrors = treatWarningsAsErrors,
				WarningsAsErrors = warningsAsErrors,
				WarningsNotAsErrors = warningsNotAsErrors
			};

			using (var driver = task.CreateDriver ()) {
				var actualWarnAsError = driver.Context.WarnAsError;
				var actualGeneralWarnAsError = driver.Context.GeneralWarnAsError;
				Assert.Equal (warnAsError.Count () + warnNotAsError.Count (), actualWarnAsError.Count);
				Assert.Equal (treatWarningsAsErrors, actualGeneralWarnAsError);
				if (warnAsError.Length > 0) {
					foreach (var warningCode in warnAsError)
						Assert.True (actualWarnAsError.ContainsKey (warningCode) && actualWarnAsError[warningCode] == true);
				}

				if (warnNotAsError.Length > 0) {
					foreach (var warningCode in warnNotAsError)
						Assert.True (actualWarnAsError.ContainsKey (warningCode) && actualWarnAsError[warningCode] == false);
				}
			}
		}
#nullable disable

		public static IEnumerable<object[]> CustomDataCases => new List<object[]> {
			new object [] {
				new ITaskItem [] {
					new TaskItem ("DataName", new Dictionary<string, string> { { "Value", "DataValue" } })
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("DataName", new Dictionary<string, string> { { "Value", "DataValue" } }),
					new TaskItem ("DataName", new Dictionary<string, string> { { "Value", "DataValue2" } })
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("DataName1", new Dictionary<string, string> { { "Value", "DataValue1" } }),
					new TaskItem ("DataName2", new Dictionary<string, string> { { "Value", "DataValue2" } })
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("DataName", new Dictionary<string, string> { { "Value", "data value with spaces" } })
				},
			},
		};

		[Theory]
		[MemberData (nameof (CustomDataCases))]
		public void TestCustomData (ITaskItem[] customData)
		{
			var task = new MockTask () {
				CustomData = customData
			};
			using (var driver = task.CreateDriver ()) {
				var expectedCustomData = customData.Select (c => new { Key = c.ItemSpec, Value = c.GetMetadata ("Value") })
					.GroupBy (c => c.Key)
					.Select (c => c.Last ())
					.ToDictionary (c => c.Key, c => c.Value);
				var actualCustomData = driver.GetCustomData ();
				Assert.Equal (expectedCustomData, actualCustomData);
			}
		}

		public static IEnumerable<object[]> FeatureSettingsCases => new List<object[]> {
			new object [] {
				new ITaskItem [] {
					new TaskItem ("FeatureName", new Dictionary<string, string> { { "Value", "true" } })
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("FeatureName", new Dictionary<string, string> { { "Value", "true" } }),
					new TaskItem ("FeatureName", new Dictionary<string, string> { { "Value", "false" } })
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem ("FeatureName1", new Dictionary<string, string> { { "value", "true" } }),
					new TaskItem ("FeatureName2", new Dictionary<string, string> { { "value", "false" } }),
				},
			},
		};

		[Theory]
		[MemberData (nameof (FeatureSettingsCases))]
		public void TestFeatureSettings (ITaskItem[] featureSettings)
		{
			var task = new MockTask () {
				FeatureSettings = featureSettings
			};
			using (var driver = task.CreateDriver ()) {
				var expectedSettings = featureSettings.Select (f => new { Feature = f.ItemSpec, Value = f.GetMetadata ("Value") })
					.GroupBy (f => f.Feature)
					.Select (f => f.Last ())
					.ToDictionary (f => f.Feature, f => bool.Parse (f.Value));
				var actualSettings = driver.Context.FeatureSettings;
				Assert.Equal (expectedSettings, actualSettings);
			}
		}

		[Fact]
		public void TestInvalidFeatureSettings ()
		{
			var task = new MockTask () {
				FeatureSettings = new ITaskItem[] { new TaskItem ("FeatureName") }
			};
			Assert.Throws<ArgumentException> (() => task.CreateDriver ());
		}

		[Fact]
		public void TestExtraArgs ()
		{
			var task = new MockTask () {
				TrimMode = "copy",
				ExtraArgs = "--trim-mode copyused"
			};
			using (var driver = task.CreateDriver ()) {
				Assert.Equal (AssemblyAction.Link, driver.Context.DefaultAction);
				// Check that ExtraArgs can override TrimMode
				Assert.Equal (AssemblyAction.CopyUsed, driver.Context.TrimAction);
			}
		}

		[Theory]
		[InlineData (true)]
		[InlineData (false)]
		public void TestDumpDependencies (bool dumpDependencies)
		{
			var task = new MockTask () {
				DumpDependencies = dumpDependencies
			};
			using (var driver = task.CreateDriver ()) {
				Assert.Equal (dumpDependencies, driver.GetDependencyRecorders ()?.Single () == MockXmlDependencyRecorder.Singleton);
			}
		}

		[Theory]
		[InlineData (true)]
		[InlineData (false)]
		public void TestRemoveSymbols (bool removeSymbols)
		{
			var task = new MockTask () {
				RemoveSymbols = removeSymbols
			};
			using (var driver = task.CreateDriver ()) {
				Assert.NotEqual (removeSymbols, driver.Context.LinkSymbols);
			}
		}

		[Fact]
		public void TestRemoveSymbolsDefault ()
		{
			var task = new MockTask ();
			using (var driver = task.CreateDriver ()) {
				Assert.False (driver.Context.LinkSymbols);
			}
		}


		[Fact]
		public void TestKeepCustomMetadata ()
		{
			var task = new MockTask () {
				KeepMetadata = new ITaskItem[] { new TaskItem ("parametername") }
			};

			using (var driver = task.CreateDriver ()) {
				Assert.Equal (MetadataTrimming.None, driver.Context.MetadataTrimming);
			}
		}

		[Theory]
		[InlineData ("copy")]
		[InlineData ("link")]
		[InlineData ("copyused")]
		public void TestGlobalTrimMode (string trimMode)
		{
			var task = new MockTask () {
				TrimMode = trimMode
			};
			using (var driver = task.CreateDriver ()) {
				var expectedAction = (AssemblyAction) Enum.Parse (typeof (AssemblyAction), trimMode, ignoreCase: true);
				Assert.Equal (expectedAction, driver.Context.TrimAction);
				Assert.Equal (AssemblyAction.Link, driver.Context.DefaultAction);
			}
		}

		[Theory]
		[InlineData ("copy")]
		[InlineData ("link")]
		[InlineData ("copyused")]
		public void TestDefaultAction (string defaultAction)
		{
			var task = new MockTask () {
				DefaultAction = defaultAction
			};
			using (var driver = task.CreateDriver ()) {
				var expectedAction = (AssemblyAction) Enum.Parse (typeof (AssemblyAction), defaultAction, ignoreCase: true);
				Assert.Equal (expectedAction, driver.Context.DefaultAction);
				Assert.Equal (AssemblyAction.Link, driver.Context.TrimAction);
			}
		}

		[Fact]
		public void TestInvalidDefaultAction ()
		{
			var task = new MockTask () {
				TrimMode = "invalid"
			};

			using (var driver = task.CreateDriver ()) {
				Assert.Equal (1031, driver.Logger.Messages[0].Code);
			}
		}

		public static IEnumerable<object[]> CustomStepsCases => new List<object[]> {
			new object [] {
				new ITaskItem [] {
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "Type", "ILLink.Tasks.Tests.MockCustomStep" }
					})
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
						{ "BeforeStep", "MarkStep" }
					})
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "type", "ILLink.Tasks.Tests.MockCustomStep" },
						{ "beforebtep", "MarkStep" }
					})
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
						{ "AfterStep", "MarkStep" }
					})
				},
			},
			new object [] {
				new ITaskItem [] {
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
						{ "BeforeStep", "MarkStep" }
					}),
					new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
						{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
						{ "AfterStep", "MarkStep" }
					})
				},
			}
		};

		[Theory]
		[MemberData (nameof (CustomStepsCases))]
		public void TestCustomSteps (ITaskItem[] customSteps)
		{
			var task = new MockTask () {
				CustomSteps = customSteps
			};
			using (var driver = task.CreateDriver ()) {
				foreach (var customStep in customSteps) {
					var stepType = customStep.GetMetadata ("Type");
					var stepName = stepType.Substring (stepType.LastIndexOf (Type.Delimiter) + 1);
					var beforeStepName = customStep.GetMetadata ("BeforeStep");
					var afterStepName = customStep.GetMetadata ("AfterStep");
					Assert.True (String.IsNullOrEmpty (beforeStepName) || String.IsNullOrEmpty (afterStepName));

					var actualStepNames = driver.Context.Pipeline.GetSteps ().Select (s => s.GetType ().Name);
					if (!String.IsNullOrEmpty (beforeStepName)) {
						Assert.Contains (beforeStepName, actualStepNames);
						Assert.Equal (stepName, actualStepNames.TakeWhile (s => s != beforeStepName).Last ());
					} else if (!String.IsNullOrEmpty (afterStepName)) {
						Assert.Contains (afterStepName, actualStepNames);
						Assert.Equal (stepName, actualStepNames.SkipWhile (s => s != afterStepName).ElementAt (1));
					} else {
						Assert.Equal (stepName, actualStepNames.Last ());
					}
				}
			}
		}

		[Fact]
		public void TestCustomStepsWithBeforeAndAfterSteps ()
		{
			var customSteps = new ITaskItem[] {
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
					{ "BeforeStep", "MarkStep" },
					{ "AfterStep", "MarkStep" }
				})
			};
			var task = new MockTask () {
				CustomSteps = customSteps
			};
			Assert.Throws<ArgumentException> (() => task.CreateDriver ());
		}

		[Fact]
		public void TestCustomStepOrdering ()
		{
			var customSteps = new ITaskItem[] {
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep" },
					{ "BeforeStep", "MarkStep" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep2" },
					{ "BeforeStep", "MarkStep" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep3" },
					{ "AfterStep", "MarkStep" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep4" },
					{ "AfterStep", "MarkStep" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep5" },
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockCustomStep6" },
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockMarkHandler" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockMarkHandler2" },
					{ "BeforeStep", "MockMarkHandler" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockMarkHandler3" },
					{ "AfterStep", "MockMarkHandler2" }
				}),
				new TaskItem (Assembly.GetExecutingAssembly ().Location, new Dictionary<string, string> {
					{ "Type", "ILLink.Tasks.Tests.MockMarkHandler4" }
				}),
			};
			var task = new MockTask () {
				CustomSteps = customSteps
			};
			using (var driver = task.CreateDriver ()) {
				var actualSteps = driver.Context.Pipeline.GetSteps ().Select (s => s.GetType ().Name).ToList ();
				Assert.Equal (new List<string> {
					"MockCustomStep",
					"MockCustomStep2",
					"MarkStep",
					"MockCustomStep4",
					"MockCustomStep3",
				}, actualSteps.Skip (actualSteps.IndexOf ("MarkStep") - 2).Take (5).ToList ());
				Assert.Equal (new List<string> {
					"MockCustomStep5",
					"MockCustomStep6"
				}, actualSteps.TakeLast (2).ToList ());
				var actualMarkHandlers = driver.Context.Pipeline.MarkHandlers.Select (h => h.GetType ().Name).ToList ();
				Assert.Equal (new List<string> {
					"MockMarkHandler2",
					"MockMarkHandler3",
					"MockMarkHandler",
					"MockMarkHandler4"
				}, actualMarkHandlers.TakeLast (4).ToList ());
			}
		}

		[Fact]
		public void TestCustomStepsMissingType ()
		{
			var customSteps = new ITaskItem[] {
				new TaskItem (Assembly.GetExecutingAssembly ().Location)
			};
			var task = new MockTask () {
				CustomSteps = customSteps
			};

			Assert.Throws<ArgumentException> (() => task.CreateDriver ());
		}

		[Fact]
		public void TestErrorHandling ()
		{
			var task = new MockTask () {
				RootAssemblyNames = new ITaskItem[0]
			};
			task.BuildEngine = new MockBuildEngine ();
			Assert.False (task.Execute ());
			Assert.Contains (task.Messages, message =>
				message.Line.Contains ("No input files were specified"));
		}
	}
}