2009-01-30 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / ProjectTest.cs
1 //
2 // ProjectTest.cs:
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Xml;
33 using Microsoft.Build.BuildEngine;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using NUnit.Framework;
37 using System.Text;
38
39 namespace MonoTests.Microsoft.Build.BuildEngine {
40
41         class TestLogger : Logger {
42                 int target_started_events = 0;
43                 int target_finished_events = 0;
44
45                 public override void Initialize (IEventSource eventSource)
46                 {
47                         eventSource.TargetStarted += new TargetStartedEventHandler(TargetStarted);
48                         eventSource.TargetFinished += new TargetFinishedEventHandler(TargetFinished);
49                         eventSource.MessageRaised += new BuildMessageEventHandler(Message);
50                         eventSource.WarningRaised += new BuildWarningEventHandler(Warning);
51                 }
52
53                 void TargetStarted (object sender, TargetStartedEventArgs args)
54                 {
55                         target_started_events++;
56                 }
57
58                 void TargetFinished (object sender, TargetFinishedEventArgs args)
59                 {
60                         target_finished_events++;
61                 }
62
63                 void Message (object sender, BuildMessageEventArgs args)
64                 {
65                 }
66                 
67                 void Warning (object sender, BuildWarningEventArgs args)
68                 {
69                 }
70
71                 public int TargetStartedEvents { get { return target_started_events; } }
72
73                 public int TargetFinishedEvents { get { return target_finished_events; } }
74         }
75
76         [TestFixture]
77         public class ProjectTest {
78
79                 /*
80                 Import [] GetImports (ImportCollection ic)
81                 {
82                         List<Import> list = new List<Import> ();
83                         foreach (Import i in ic)
84                                 list.Add (i);
85                         return list.ToArray ();
86                 }
87                 */
88
89                 [Test]
90                 [ExpectedException (typeof (InvalidProjectFileException),
91                 @"The default XML namespace of the project must be the MSBuild XML namespace." + 
92                 " If the project is authored in the MSBuild 2003 format, please add " +
93                 "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" to the <Project> element. " +
94                 "If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.  ")]
95                 public void TestAssignment1 ()
96                 {
97                         Engine engine;
98                         Project project;
99                         string documentString =
100                                 "<Project></Project>";
101                         
102                         engine = new Engine (Consts.BinPath);
103                         DateTime time = DateTime.Now;
104                         project = engine.CreateNewProject ();
105                         project.LoadXml (documentString);
106
107                         Assert.AreEqual (true, project.BuildEnabled, "A1");
108                         Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
109                         Assert.AreEqual (String.Empty, project.FullFileName, "A3");
110                         Assert.AreEqual (false, project.IsDirty, "A4");
111                         Assert.AreEqual (false, project.IsValidated, "A5");
112                         Assert.AreEqual (engine, project.ParentEngine, "A6");
113                         Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
114                         Assert.IsTrue (String.Empty != project.Xml, "A8");
115                 }
116
117                 [Test]
118                 public void TestAssignment2 ()
119                 {
120                         Engine engine;
121                         Project project;
122                         string documentString =
123                                 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>";
124                         
125                         engine = new Engine (Consts.BinPath);
126                         DateTime time = DateTime.Now;
127                         project = engine.CreateNewProject ();
128                         project.LoadXml (documentString);
129
130                         Assert.AreEqual (true, project.BuildEnabled, "A1");
131                         Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
132                         Assert.AreEqual (String.Empty, project.FullFileName, "A3");
133                         Assert.AreEqual (true, project.IsDirty, "A4");
134                         Assert.AreEqual (false, project.IsValidated, "A5");
135                         Assert.AreEqual (engine, project.ParentEngine, "A6");
136                         Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
137                         Assert.IsTrue (String.Empty != project.Xml, "A8");
138                 }
139
140                 [Test]
141                 [Category ("NotWorking")]
142                 public void TestAddNewImport1 ()
143                 {
144                         Engine engine;
145                         Project project;
146
147                         string documentString = @"
148                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
149                                         <PropertyGroup />
150                                         <ItemGroup />
151                                         <Target Name='a' />
152                                         <Import Project='Test/resources/Import.csproj' />
153                                 </Project>
154                         ";
155
156                         engine = new Engine (Consts.BinPath);
157                         project = engine.CreateNewProject ();
158                         project.LoadXml (documentString);
159
160                         project.AddNewImport ("a", "true");
161                         // reevaluation wasn't caused by anything so it has only old import
162                         Assert.AreEqual (1, project.Imports.Count, "A1");
163                 }
164
165                 [Test]
166                 [Ignore ("Too detailed probably (implementation specific)")]
167                 public void TestAddNewItem1 ()
168                 {
169                         Engine engine;
170                         Project project;
171                         BuildItemGroup [] groups = new BuildItemGroup [1];
172
173                         string documentString = @"
174                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
175                                 </Project>
176                         ";
177
178                         engine = new Engine (Consts.BinPath);
179                         project = engine.CreateNewProject ();
180                         project.LoadXml (documentString);
181
182                         BuildItem item = project.AddNewItem ("A", "B");
183
184                         Assert.AreEqual (1, project.ItemGroups.Count, "A1");
185                         project.ItemGroups.CopyTo (groups, 0);
186                         Assert.AreEqual (1, groups [0].Count, "A2");
187                         Assert.AreEqual ("B", groups [0] [0].Include, "A3");
188                         Assert.AreEqual ("B", groups [0] [0].FinalItemSpec, "A4");
189                         Assert.AreEqual ("A", groups [0] [0].Name, "A5");
190                         //Assert.AreNotSame (item, groups [0] [0], "A6");
191                         Assert.IsFalse (object.ReferenceEquals (item, groups [0] [0]), "A6");
192
193                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A7");
194                         Assert.AreEqual ("B", project.EvaluatedItems [0].Include, "A8");
195                         Assert.AreEqual ("B", project.EvaluatedItems [0].FinalItemSpec, "A9");
196                         Assert.AreEqual ("A", project.EvaluatedItems [0].Name, "A10");
197                         //Assert.AreNotSame (item, project.EvaluatedItems [0], "A11");
198                         Assert.IsFalse (object.ReferenceEquals (item, project.EvaluatedItems [0]), "A11");
199                 }
200
201                 [Test]
202                 [Category ("NotWorking")]
203                 public void TestAddNewItem2 ()
204                 {
205                         Engine engine;
206                         Project project;
207
208                         engine = new Engine (Consts.BinPath);
209                         project = engine.CreateNewProject ();
210
211                         BuildItem item = project.AddNewItem ("A", "a;b;c");
212                         Assert.AreEqual ("a;b;c", item.Include, "A1");
213                         Assert.AreEqual ("a", item.FinalItemSpec, "A2");
214
215                         Assert.AreEqual (3, project.EvaluatedItems.Count, "A3");
216                 }
217
218                 [Test]
219                 public void TestAddNewItem3 ()
220                 {
221                         Engine engine;
222                         Project project;
223                         BuildItemGroup [] groups = new BuildItemGroup [4];
224
225                         string documentString = @"
226                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
227                                         <ItemGroup />
228                                         <ItemGroup>
229                                                 <A Include='a'/>
230                                         </ItemGroup>
231                                         <ItemGroup>
232                                                 <B Include='a'/>
233                                         </ItemGroup>
234                                         <ItemGroup>
235                                                 <B Include='a'/>
236                                         </ItemGroup>
237                                 </Project>
238                         ";
239
240                         engine = new Engine (Consts.BinPath);
241                         project = engine.CreateNewProject ();
242                         project.LoadXml (documentString);
243
244                         project.AddNewItem ("B", "b");
245
246                         project.ItemGroups.CopyTo (groups, 0);
247                         Assert.AreEqual (0, groups [0].Count, "A1");
248                         Assert.AreEqual (1, groups [1].Count, "A2");
249                         Assert.AreEqual (1, groups [2].Count, "A3");
250                         Assert.AreEqual (2, groups [3].Count, "A4");
251                 }
252                 [Test]
253                 public void TestAddNewItemGroup ()
254                 {
255                         Engine engine;
256                         Project project;
257
258                         string documentString = @"
259                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
260                                 </Project>
261                         ";
262
263                         engine = new Engine (Consts.BinPath);
264                         project = engine.CreateNewProject ();
265                         project.LoadXml (documentString);
266
267                         BuildItemGroup big = project.AddNewItemGroup ();
268                         Assert.IsNotNull (big, "A1");
269                         Assert.AreEqual (String.Empty, big.Condition, "A2");
270                         Assert.AreEqual (0, big.Count, "A3");
271                         Assert.AreEqual (false, big.IsImported, "A4");
272                         Assert.IsTrue (project.IsDirty, "A5");
273                 }
274
275                 [Test]
276                 public void TestAddNewPropertyGroup ()
277                 {
278                         Engine engine;
279                         Project project;
280
281                         string documentString = @"
282                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
283                                 </Project>
284                         ";
285
286                         engine = new Engine (Consts.BinPath);
287                         project = engine.CreateNewProject ();
288                         project.LoadXml (documentString);
289
290                         BuildPropertyGroup bpg = project.AddNewPropertyGroup (false);
291                         Assert.IsNotNull (bpg, "A1");
292                         Assert.AreEqual (String.Empty, bpg.Condition, "A2");
293                         Assert.AreEqual (0, bpg.Count, "A3");
294                         Assert.AreEqual (false, bpg.IsImported, "A4");
295                         Assert.IsTrue (project.IsDirty, "A5");
296                 }
297
298                 [Test]
299                 public void TestBuild0 ()
300                 {
301                         Engine engine;
302                         Project project;
303                         IDictionary hashtable = new Hashtable ();
304
305                         string documentString = @"
306                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
307                                         <Target 
308                                                 Name='Main'
309                                                 Inputs='a;b;c'
310                                                 Outputs='d;e;f'
311                                         >
312                                         </Target>
313                                 </Project>
314                         ";
315
316                         engine = new Engine (Consts.BinPath);
317                         project = engine.CreateNewProject ();
318                         project.LoadXml (documentString);
319
320                         Assert.AreEqual (true, project.Build (new string [] { "Main" }, hashtable), "A1");
321                         Assert.AreEqual (1, hashtable.Count, "A2");
322
323                         IDictionaryEnumerator e = hashtable.GetEnumerator ();
324                         e.MoveNext ();
325
326                         string name = (string) e.Key;
327                         Assert.AreEqual ("Main", name, "A3");
328                         ITaskItem [] arr = (ITaskItem []) e.Value;
329
330                         Assert.AreEqual (3, arr.Length, "A4");
331                         Assert.AreEqual ("d", arr [0].ItemSpec, "A5");
332                         Assert.AreEqual ("e", arr [1].ItemSpec, "A6");
333                         Assert.AreEqual ("f", arr [2].ItemSpec, "A7");
334                 }
335
336                 [Test]
337                 public void TestBuild1 ()
338                 {
339                         Engine engine;
340                         Project project;
341                         IDictionary hashtable = new Hashtable ();
342                         
343                         string documentString = @"
344                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
345                                         <Target Name='Main'>
346                                                 <Microsoft.Build.Tasks.Message Text='Text' />
347                                         </Target>
348                                 </Project>
349                         ";
350                         
351                         engine = new Engine (Consts.BinPath);
352                         project = engine.CreateNewProject ();
353                         project.LoadXml (documentString);
354
355                         Assert.AreEqual (true, project.Build (new string[] { "Main" }, hashtable), "A1");
356                         Assert.AreEqual (1, hashtable.Count, "A2");
357
358                         IDictionaryEnumerator e = hashtable.GetEnumerator ();
359                         e.MoveNext ();
360
361                         string name = (string) e.Key;
362                         Assert.AreEqual ("Main", name, "A3");
363                         Assert.IsNotNull ((ITaskItem []) e.Value, "A4");
364                 }
365
366                 [Test]
367                 public void TestBuild2 ()
368                 {
369                         Engine engine;
370                         Project project;
371
372                         string documentString = @"
373                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
374                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
375                                                 <Message Text='text' />
376                                         </Target>
377                                 </Project>
378                         ";
379
380                         engine = new Engine (Consts.BinPath);
381                         TestLogger tl = new TestLogger ();
382                         engine.RegisterLogger (tl);
383                         project = engine.CreateNewProject ();
384                         project.LoadXml (documentString);
385
386                         project.Build ("T");
387                         project.Build ("T");
388
389                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
390                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
391                 }
392
393                 [Test]
394                 public void TestBuild3 ()
395                 {
396                         Engine engine;
397                         Project project;
398
399                         string documentString = @"
400                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
401                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
402                                                 <Message Text='text' />
403                                         </Target>
404                                 </Project>
405                         ";
406
407                         engine = new Engine (Consts.BinPath);
408                         TestLogger tl = new TestLogger ();
409                         engine.RegisterLogger (tl);
410                         project = engine.CreateNewProject ();
411                         project.LoadXml (documentString);
412
413                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
414                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
415
416                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
417                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
418                 }
419
420                 [Test]
421                 [Category ("NotWorking")]
422                 public void TestBuild4 ()
423                 {
424                         Engine engine;
425                         Project project;
426
427                         string documentString = @"
428                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
429                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
430                                                 <Message Text='text' />
431                                         </Target>
432                                 </Project>
433                         ";
434
435                         engine = new Engine (Consts.BinPath);
436                         TestLogger tl = new TestLogger ();
437                         engine.RegisterLogger (tl);
438                         project = engine.CreateNewProject ();
439                         project.LoadXml (documentString);
440
441                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
442                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
443
444                         Assert.AreEqual (1, tl.TargetStartedEvents, "A1");
445                         Assert.AreEqual (1, tl.TargetFinishedEvents, "A2");
446                 }
447
448                 [Test]
449                 public void TestBuild5 ()
450                 {
451                         Engine engine;
452                         Project project;
453
454                         string documentString = @"
455                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
456                                 </Project>
457                         ";
458
459                         engine = new Engine (Consts.BinPath);
460                         project = engine.CreateNewProject ();
461                         project.LoadXml (documentString);
462
463                         Assert.IsFalse (project.Build ("target_that_doesnt_exist"));
464                 }
465
466                 [Test]
467                 public void TestEvaluatedItems1 ()
468                 {
469                         Engine engine;
470                         Project project;
471
472                         string documentString = @"
473                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
474                                         <ItemGroup>
475                                                 <A Include='a' />
476                                                 <B Include='b' Condition='false' />
477                                         </ItemGroup>
478                                 </Project>
479                         ";
480
481                         engine = new Engine (Consts.BinPath);
482                         project = engine.CreateNewProject ();
483                         project.LoadXml (documentString);
484
485                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A1");
486
487                         BuildItem bi = project.EvaluatedItems [0];
488
489                         bi.Name = "C";
490                         bi.Include = "c";
491
492                         BuildItemGroup [] big = new BuildItemGroup [1];
493                         project.ItemGroups.CopyTo (big, 0);
494                         Assert.AreEqual ("C", big [0] [0].Name, "A2");
495                         Assert.AreEqual ("c", big [0] [0].Include, "A3");
496                 }
497
498                 [Test]
499                 public void TestEvaluatedItems2 ()
500                 {
501                         Engine engine;
502                         Project project;
503
504                         string documentString = @"
505                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
506                                         <ItemGroup>
507                                                 <A Include='a;b;c' />
508                                         </ItemGroup>
509                                 </Project>
510                         ";
511
512                         engine = new Engine (Consts.BinPath);
513                         project = engine.CreateNewProject ();
514                         project.LoadXml (documentString);
515
516                         BuildItemGroup [] big = new BuildItemGroup [1];
517                         project.ItemGroups.CopyTo (big, 0);
518
519                         Assert.AreEqual (3, project.EvaluatedItems.Count, "A1");
520                         Assert.AreEqual ("a;b;c", big [0] [0].Include, "A2");
521                         Assert.AreEqual (1, big [0].Count, "A3");
522
523                         BuildItem bi = project.EvaluatedItems [0];
524
525                         bi.Include = "d";
526
527                         Assert.AreEqual (3, big [0].Count, "A4");
528                         Assert.AreEqual ("d", big [0] [0].Include, "A5");
529                         Assert.AreEqual ("b", big [0] [1].Include, "A6");
530                         Assert.AreEqual ("c", big [0] [2].Include, "A7");
531                 }
532
533                 [Test]
534                 [Category ("NotWorking")]
535                 public void TestGetConditionedPropertyValues ()
536                 {
537                         Engine engine;
538                         Project project;
539
540                         string documentString = @"
541                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
542                                         <PropertyGroup Condition='true'>
543                                                 <A>A</A>
544                                                 <B Condition='true'>A</B>
545                                         </PropertyGroup>
546                                         <PropertyGroup>
547                                                 <C Condition='true'>A</C>
548                                                 <C Condition='false'>B</C>
549                                                 <C Condition='!false'>C</C>
550                                                 <D>A</D>
551                                                 <E Condition="" '$(C)' == 'A' "">E</E>
552                                         </PropertyGroup>
553                                 </Project>
554                         ";
555
556                         engine = new Engine (Consts.BinPath);
557                         project = engine.CreateNewProject ();
558                         project.LoadXml (documentString);
559
560                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("A").Length, "A1");
561                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("B").Length, "A2");
562                         Assert.AreEqual (1, project.GetConditionedPropertyValues ("C").Length, "A3");
563                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("D").Length, "A4");
564                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("E").Length, "A5");
565                         Assert.AreEqual ("A", project.GetConditionedPropertyValues ("C") [0], "A6");
566                 }
567
568                 [Test]
569                 [ExpectedException (typeof (ArgumentNullException))]
570                 public void TestGetEvaluatedItemsByName1 ()
571                 {
572                         Engine engine;
573                         Project project;
574
575                         string documentString = @"
576                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
577                                 </Project>
578                         ";
579
580                         engine = new Engine (Consts.BinPath);
581                         project = engine.CreateNewProject ();
582                         project.LoadXml (documentString);
583
584                         project.GetEvaluatedItemsByName (null);
585                 }
586
587                 [Test]
588                 public void TestGetEvaluatedItemsByName2 ()
589                 {
590                         Engine engine;
591                         Project project;
592
593                         string documentString = @"
594                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
595                                         <ItemGroup>
596                                                 <A Include='1' />
597                                                 <B Include='2' Condition='true' />
598                                                 <C Include='3' Condition='false' />
599                                         </ItemGroup>
600                                 </Project>
601                         ";
602
603                         engine = new Engine (Consts.BinPath);
604                         project = engine.CreateNewProject ();
605                         project.LoadXml (documentString);
606
607                         BuildItemGroup big;
608
609                         big = project.GetEvaluatedItemsByName (String.Empty);
610
611                         Assert.AreEqual (0, big.Count, "A1");
612
613                         big = project.GetEvaluatedItemsByName ("A");
614
615                         Assert.AreEqual (1, big.Count, "A2");
616                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
617
618                         big = project.GetEvaluatedItemsByName ("B");
619
620                         Assert.AreEqual (1, big.Count, "A4");
621                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
622
623                         big = project.GetEvaluatedItemsByName ("C");
624
625                         Assert.AreEqual (0, big.Count, "A6");
626                 }
627
628                 [Test]
629                 [ExpectedException (typeof (ArgumentNullException))]
630                 public void TestGetEvaluatedItemsByNameIgnoringCondition1 ()
631                 {
632                         Engine engine;
633                         Project project;
634
635                         string documentString = @"
636                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
637                                 </Project>
638                         ";
639
640                         engine = new Engine (Consts.BinPath);
641                         project = engine.CreateNewProject ();
642                         project.LoadXml (documentString);
643
644                         project.GetEvaluatedItemsByNameIgnoringCondition (null);
645                 }
646
647                 [Test]
648                 public void TestGetEvaluatedItemsByNameIgnoringCondition2 ()
649                 {
650                         Engine engine;
651                         Project project;
652
653                         string documentString = @"
654                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
655                                         <ItemGroup>
656                                                 <A Include='1' />
657                                                 <B Include='2' Condition='true' />
658                                                 <C Include='3' Condition='false' />
659                                         </ItemGroup>
660                                 </Project>
661                         ";
662
663                         engine = new Engine (Consts.BinPath);
664                         project = engine.CreateNewProject ();
665                         project.LoadXml (documentString);
666
667                         BuildItemGroup big;
668
669                         big = project.GetEvaluatedItemsByNameIgnoringCondition (String.Empty);
670
671                         Assert.AreEqual (0, big.Count, "A1");
672
673                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("A");
674
675                         Assert.AreEqual (1, big.Count, "A2");
676                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
677
678                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("B");
679
680                         Assert.AreEqual (1, big.Count, "A4");
681                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
682
683                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("C");
684
685                         Assert.AreEqual (1, big.Count, "A6");
686                         Assert.AreEqual ("3", big [0].FinalItemSpec, "A7");
687                 }
688
689                 [Test]
690                 [ExpectedException (typeof (ArgumentNullException))]
691                 public void TestGetEvaluatedProperty1 ()
692                 {
693                         Engine engine;
694                         Project project;
695
696                         string documentString = @"
697                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
698                                 </Project>
699                         ";
700
701                         engine = new Engine (Consts.BinPath);
702                         project = engine.CreateNewProject ();
703                         project.LoadXml (documentString);
704
705                         project.GetEvaluatedProperty (null);
706                 }
707                 [Test]
708                 public void TestGetEvaluatedProperty2 ()
709                 {
710                         Engine engine;
711                         Project project;
712
713                         string documentString = @"
714                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
715                                         <PropertyGroup>
716                                                 <A>1</A>
717                                                 <B Condition='true'>2</B>
718                                                 <C Condition='false'>3</C>
719                                         </PropertyGroup>
720                                 </Project>
721                         ";
722
723                         engine = new Engine (Consts.BinPath);
724                         project = engine.CreateNewProject ();
725                         project.LoadXml (documentString);
726
727                         Assert.AreEqual ("1", project.GetEvaluatedProperty ("A"), "A1");
728                         Assert.AreEqual ("2", project.GetEvaluatedProperty ("B"), "A2");
729                         Assert.IsNull (project.GetEvaluatedProperty ("C"), "A3");
730                 }
731
732                 [Test]
733                 public void TestGetProjectExtensions ()
734                 {
735                         Engine engine;
736                         Project project;
737
738                         string documentString = @"
739                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
740                                         <ProjectExtensions>
741                                                 <Node>Text</Node>
742                                         </ProjectExtensions>
743                                 </Project>
744                         ";
745
746                         engine = new Engine (Consts.BinPath);
747                         project = engine.CreateNewProject ();
748                         project.LoadXml (documentString);
749
750                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (null), "A1");
751                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (String.Empty), "A2");
752                         Assert.AreEqual (String.Empty, project.GetProjectExtensions ("something"), "A3");
753                         Assert.AreEqual ("Text", project.GetProjectExtensions ("Node"), "A4");
754                 }
755
756                 [Test]
757                 public void TestGlobalProperties1 ()
758                 {
759                         Engine engine;
760                         Project project;
761                         
762                         string documentString = @"
763                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
764                                 </Project>
765                         ";
766                         
767                         engine = new Engine (Consts.BinPath);
768                         project = engine.CreateNewProject ();
769                         project.LoadXml (documentString);
770
771                         Assert.AreEqual (0, project.GlobalProperties.Count, "A1");
772                 }
773
774                 [Test]
775                 public void TestGlobalProperties2 ()
776                 {
777                         Engine engine;
778                         Project project;
779                         
780                         string documentString = @"
781                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
782                                 </Project>
783                         ";
784                         
785                         engine = new Engine (Consts.BinPath);
786                         engine.GlobalProperties.SetProperty ("Property", "Value");
787                         
788                         project = engine.CreateNewProject ();
789                         project.LoadXml (documentString);
790
791                         Assert.AreEqual (1, project.GlobalProperties.Count, "A1");
792                         Assert.AreEqual ("Property", project.GlobalProperties ["Property"].Name, "A2");
793                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].Value, "A3");
794                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].FinalValue, "A4");
795                         Assert.AreEqual ("Property", project.EvaluatedProperties ["Property"].Name, "A2");
796                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].Value, "A3");
797                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].FinalValue, "A4");
798                 }
799
800                 [Test]
801                 [Category ("NotDotNet")]
802                 [ExpectedException (typeof (ArgumentNullException))]
803                 public void TestGlobalProperties3 ()
804                 {
805                         Engine engine;
806                         Project project;
807                         
808                         string documentString = @"
809                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
810                                 </Project>
811                         ";
812                         
813                         engine = new Engine (Consts.BinPath);
814                         project = engine.CreateNewProject ();
815                         project.LoadXml (documentString);
816
817                         project.GlobalProperties = null;
818                 }
819
820                 [Test]
821                 [Ignore ("needs rewriting")]
822                 public void TestGlobalProperties4 ()
823                 {
824                         Engine engine;
825                         Project project;
826                         
827                         string documentString = @"
828                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
829                                         <PropertyGroup>
830                                                 <Property>a</Property>
831                                         </PropertyGroup>
832                                 </Project>
833                         ";
834                         
835                         engine = new Engine (Consts.BinPath);
836                         project = engine.CreateNewProject ();
837                         project.LoadXml (documentString);
838
839                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
840                         project.PropertyGroups.CopyTo (groups, 0);
841
842                         project.GlobalProperties = groups [0];
843                         project.GlobalProperties = project.EvaluatedProperties;
844                 }
845
846                 [Test]
847                 [Category ("NotDotNet")]
848                 [ExpectedException (typeof (InvalidOperationException))]
849                 public void TestGlobalProperties5 ()
850                 {
851                         Engine engine;
852                         Project project;
853                         
854                         string documentString = @"
855                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
856                                         <PropertyGroup>
857                                                 <Property>a</Property>
858                                         </PropertyGroup>
859                                 </Project>
860                         ";
861                         
862                         engine = new Engine (Consts.BinPath);
863                         project = engine.CreateNewProject ();
864                         project.LoadXml (documentString);
865
866                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
867                         project.PropertyGroups.CopyTo (groups, 0);
868                         project.GlobalProperties = groups [0];
869                 }
870
871                 [Test]
872                 [ExpectedException (typeof (InvalidProjectFileException))]
873                 public void TestLoad1 ()
874                 {
875                         Engine engine;
876                         Project project;
877
878                         string documentString = @"
879                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
880                                         <PropertyGroup>
881                                 </Project>
882                         ";
883
884                         engine = new Engine (Consts.BinPath);
885                         project = engine.CreateNewProject ();
886                         project.LoadXml (documentString);
887                 }
888
889                 [Test]
890                 [ExpectedException (typeof (InvalidProjectFileException))]
891                 public void TestLoad2 ()
892                 {
893                         Engine engine;
894                         Project project;
895
896                         engine = new Engine (Consts.BinPath);
897                         project = engine.CreateNewProject ();
898                         project.LoadXml ("project_file_that_doesnt_exist");
899                 }
900
901                 [Test]
902                 public void TestParentEngine ()
903                 {
904                         Engine engine;
905                         Project project;
906                         
907                         engine = new Engine (Consts.BinPath);
908                         project = engine.CreateNewProject ();
909
910                         Assert.AreEqual (engine, project.ParentEngine, "A1");
911                 }
912
913                 [Test]
914                 [Category ("NotDotNet")]
915                 [ExpectedException (typeof (ArgumentNullException))]
916                 public void TestRemoveItemGroup1 ()
917                 {
918                         Engine engine;
919                         Project p1;
920
921                         engine = new Engine (Consts.BinPath);
922                         p1 = engine.CreateNewProject ();
923
924                         p1.RemoveItemGroup (null);
925                 }
926
927                 [Test]
928                 [ExpectedException (typeof (InvalidOperationException),
929                                         "The \"BuildItemGroup\" object specified does not belong to the correct \"Project\" object.")]
930                 [Category ("NotWorking")]
931                 public void TestRemoveItemGroup2 ()
932                 {
933                         Engine engine;
934                         Project p1;
935                         Project p2;
936                         BuildItemGroup [] groups  = new BuildItemGroup [1];
937
938                         engine = new Engine (Consts.BinPath);
939                         p1 = engine.CreateNewProject ();
940                         p2 = engine.CreateNewProject ();
941
942                         p1.AddNewItem ("A", "B");
943                         p1.ItemGroups.CopyTo (groups, 0);
944
945                         p2.RemoveItemGroup (groups [0]);
946                 }
947
948                 [Test]
949                 [Category ("NotDotNet")]
950                 [ExpectedException (typeof (ArgumentNullException))]
951                 public void TestRemoveItem1 ()
952                 {
953                         Engine engine;
954                         Project project;
955
956                         engine = new Engine (Consts.BinPath);
957                         project = engine.CreateNewProject ();
958
959                         project.RemoveItem (null);
960                 }
961
962                 [Test]
963                 [ExpectedException (typeof (InvalidOperationException),
964                         "The object passed in is not part of the project.")]
965                 public void TestRemoveItem2 ()
966                 {
967                         Engine engine;
968                         Project project;
969
970                         engine = new Engine (Consts.BinPath);
971                         project = engine.CreateNewProject ();
972
973                         project.RemoveItem (new BuildItem ("name", "include"));
974                 }
975
976                 [Test]
977                 [ExpectedException (typeof (InvalidOperationException),
978                                         "The \"BuildItemGroup\" object specified does not belong to the correct \"Project\" object.")]
979                 public void TestRemoveItem3 ()
980                 {
981                         Engine engine;
982                         Project p1;
983                         Project p2;
984
985                         engine = new Engine (Consts.BinPath);
986                         p1 = engine.CreateNewProject ();
987                         p2 = engine.CreateNewProject ();
988
989                         p1.AddNewItem ("A", "B");
990
991                         p2.RemoveItem (p1.EvaluatedItems [0]);
992                 }
993
994                 [Test]
995                 [Category ("NotDotNet")]
996                 [ExpectedException (typeof (InvalidOperationException))]
997                 public void TestRemoveItem4 ()
998                 {
999                         Engine engine;
1000                         Project p1;
1001                         Project p2;
1002
1003                         engine = new Engine (Consts.BinPath);
1004                         p1 = engine.CreateNewProject ();
1005                         p2 = engine.CreateNewProject ();
1006
1007                         p1.AddNewItem ("A", "B");
1008                         p1.AddNewItem ("A", "C");
1009
1010                         p2.RemoveItem (p1.EvaluatedItems [0]);
1011                 }
1012
1013                 [Test]
1014                 public void TestRemoveItem5 ()
1015                 {
1016                         Engine engine;
1017                         Project project;
1018                         BuildItemGroup [] groups = new BuildItemGroup [1];
1019
1020                         string documentString = @"
1021                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1022                                         <ItemGroup>
1023                                                 <A Include='a'/>
1024                                         </ItemGroup>
1025                                 </Project>
1026                         ";
1027
1028                         engine = new Engine (Consts.BinPath);
1029                         project = engine.CreateNewProject ();
1030                         project.LoadXml (documentString);
1031
1032                         project.RemoveItem (project.EvaluatedItems [0]);
1033                         Assert.AreEqual (0, project.EvaluatedItems.Count, "A1");
1034                         project.ItemGroups.CopyTo (groups, 0);
1035                         Assert.IsNull (groups [0], "A2");
1036                         Assert.AreEqual (0, project.ItemGroups.Count, "A3");
1037                 }
1038
1039                 [Test]
1040                 [Category ("NotWorking")]
1041                 public void TestResetBuildStatus ()
1042                 {
1043                         Engine engine;
1044                         Project project;
1045
1046                         string documentString = @"
1047                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1048                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
1049                                                 <Message Text='text' />
1050                                         </Target>
1051                                 </Project>
1052                         ";
1053
1054                         engine = new Engine (Consts.BinPath);
1055                         TestLogger tl = new TestLogger ();
1056                         engine.RegisterLogger (tl);
1057                         project = engine.CreateNewProject ();
1058                         project.LoadXml (documentString);
1059
1060                         project.Build ("T");
1061                         project.ResetBuildStatus ();
1062                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
1063
1064                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
1065                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A1");
1066                 }
1067                 
1068                 [Test]
1069                 public void TestSchemaFile ()
1070                 {
1071                         Engine engine;
1072                         Project project;
1073                         
1074                         string documentString = @"
1075                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1076                                 </Project>
1077                         ";
1078                         
1079                         engine = new Engine (Consts.BinPath);
1080                         project = engine.CreateNewProject ();
1081                         project.LoadXml (documentString);
1082
1083                         Assert.IsNull (project.SchemaFile, "A1");
1084                 }
1085                 [Test]
1086                 [Category ("NotDotNet")]
1087                 [ExpectedException (typeof (ArgumentNullException))]
1088                 public void TestSetProjectExtensions1 ()
1089                 {
1090                         Engine engine;
1091                         Project project;
1092
1093                         string documentString = @"
1094                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1095                                 </Project>
1096                         ";
1097
1098                         engine = new Engine (Consts.BinPath);
1099                         project = engine.CreateNewProject ();
1100                         project.LoadXml (documentString);
1101
1102                         project.SetProjectExtensions (null, null);
1103                 }
1104
1105                 [Test]
1106                 public void TestSetProjectExtensions2 ()
1107                 {
1108                         Engine engine;
1109                         Project project;
1110
1111                         string documentString = @"
1112                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1113                                 </Project>
1114                         ";
1115
1116                         engine = new Engine (Consts.BinPath);
1117                         project = engine.CreateNewProject ();
1118                         project.LoadXml (documentString);
1119
1120                         project.SetProjectExtensions ("name", "1");
1121                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1122                         project.SetProjectExtensions ("name", "2");
1123                         Assert.AreEqual ("2", project.GetProjectExtensions ("name"), "A2");
1124                         Assert.IsTrue (project.IsDirty, "A3");
1125                 }
1126
1127                 [Test]
1128                 public void TestSetProjectExtensions3 ()
1129                 {
1130                         Engine engine;
1131                         Project project;
1132
1133                         string documentString = @"
1134                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1135                                         <ProjectExtensions>
1136                                         </ProjectExtensions>
1137                                 </Project>
1138                         ";
1139
1140                         engine = new Engine (Consts.BinPath);
1141                         project = engine.CreateNewProject ();
1142                         project.LoadXml (documentString);
1143
1144                         project.SetProjectExtensions ("name", "1");
1145                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1146                         Assert.IsTrue (project.IsDirty, "A2");
1147                 }
1148
1149                 [Test]
1150                 public void TestBuildProjectError1 ()
1151                 {
1152                         Engine engine = new Engine (Consts.BinPath);
1153                         Project project = engine.CreateNewProject ();
1154
1155                         Assert.IsFalse (project.Build ((string) null), "A1");
1156                         Assert.IsFalse (project.Build ((string[]) null), "A2");
1157                         Assert.IsFalse (project.Build ((string []) null, null), "A3");
1158                         Assert.IsFalse (project.Build ((string []) null, null, BuildSettings.None), "A4");
1159                 }
1160
1161                 [Test]
1162                 public void TestBuildProjectError2 ()
1163                 {
1164                         Engine engine = new Engine (Consts.BinPath);
1165                         Project project = engine.CreateNewProject ();
1166
1167                         try {
1168                                 project.Build (new string [] { null });
1169                         } catch {
1170                                 return;
1171                         }
1172                         Assert.Fail ("Expected exception for project.Build, null string in targetNames []");
1173                 }
1174
1175                 [Test]
1176                 public void TestBuildProjectFile1 ()
1177                 {
1178                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPF1");
1179                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF1");
1180                 }
1181
1182                 [Test]
1183                 public void TestBuildProjectFileXml1 ()
1184                 {
1185                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPFX1");
1186                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX1");
1187                 }
1188
1189                 [Test]
1190                 public void TestBuildProjectFile2 ()
1191                 {
1192                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF2");
1193                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF2");
1194                 }
1195
1196                 [Test]
1197                 public void TestBuildProjectFileXml2 ()
1198                 {
1199                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX2");
1200                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX2");
1201                 }
1202
1203                 [Test]
1204                 public void TestBuildProjectFile3 ()
1205                 {
1206                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF3");
1207                         CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPF3");
1208                 }
1209
1210                 [Test]
1211                 public void TestBuildProjectFileXml3 ()
1212                 {
1213                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX3");
1214                         CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPFX3");
1215                 }
1216
1217                 [Test]
1218                 public void TestBuildProjectFile4 ()
1219                 {
1220                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF4");
1221                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF4");
1222                 }
1223
1224                 [Test]
1225                 public void TestBuildProjectFileXml4 ()
1226                 {
1227                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX4");
1228                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX4");
1229                 }
1230
1231                 //Run separate tests
1232
1233                 //Run single target
1234                 [Test]
1235                 public void TestBuildProjectFile5 ()
1236                 {
1237                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF5");
1238                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF5");
1239                 }
1240
1241                 [Test]
1242                 public void TestBuildProjectFileXml5 ()
1243                 {
1244                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX5");
1245                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX5");
1246                 }
1247
1248                 [Test]
1249                 public void TestBuildProjectFile6 ()
1250                 {
1251                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF6");
1252                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF6");
1253                 }
1254
1255                 [Test]
1256                 public void TestBuildProjectFileXml6 ()
1257                 {
1258                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX6");
1259                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX6");
1260                 }
1261
1262                 // run multiple targets
1263                 [Test]
1264                 public void TestBuildProjectFile7 ()
1265                 {
1266                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF7");
1267                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPF7");
1268                 }
1269
1270                 [Test]
1271                 public void TestBuildProjectFileXml7 ()
1272                 {
1273                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX7");
1274                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPFX7");
1275                 }
1276
1277                 [Test]
1278                 public void TestBuildProjectFile8 ()
1279                 {
1280                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPF8");
1281                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPF8");
1282                 }
1283
1284                 [Test]
1285                 public void TestBuildProjectFileXml8 ()
1286                 {
1287                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPFX8");
1288                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPFX8");
1289                 }
1290
1291                 [Test]
1292                 public void TestBatchedMetadataRef1 ()
1293                 {
1294                         //test for multiple items with same metadata also
1295                          string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1296                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1297                         <ItemGroup>
1298                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1299                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1300                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1301                                 <Coll1 Include=""A4""><Name>Xyz</Name></Coll1>
1302                                 <Coll2 Include=""B1""></Coll2>
1303                         </ItemGroup>
1304                                 <Target Name=""ShowMessage"">
1305                                         <BatchingTestTask Sources=""%(Coll1.Name)"">
1306                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1307                                         </BatchingTestTask>
1308                                         <Message Text=""Msg: %(Coll1.Name)"" />
1309                                 </Target>
1310                  </Project>";
1311
1312                         Engine engine = new Engine (Consts.BinPath);
1313                         Project project = engine.CreateNewProject ();
1314
1315                         project.LoadXml (projectString);
1316                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1317
1318                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1319                         Assert.AreEqual (3, include.Count, "A2");
1320
1321                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1322                         Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A4");
1323
1324                         Assert.AreEqual ("FinalList", include [1].Name, "A5");
1325                         Assert.AreEqual ("Def", include [1].FinalItemSpec, "A6");
1326
1327                         Assert.AreEqual ("FinalList", include [2].Name, "A7");
1328                         Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A8");
1329                 }
1330
1331                 [Test]
1332                 public void TestBatchedMetadataRef2 ()
1333                 {
1334                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1335                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1336                         <ItemGroup>
1337                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1338                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1339                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1340                                 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1341                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1342                                 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1343                         </ItemGroup>
1344                                 <Target Name=""ShowMessage"">
1345                                         <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll2)"">
1346                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1347                                         </BatchingTestTask>
1348                                         <Message Text=""Msg: %(Coll1.Name)"" />
1349                                 </Target>
1350                                 <Target Name=""ShowMessage2"">
1351                                         <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll1)"">
1352                                                 <Output TaskParameter=""Output"" ItemName=""FinalList2"" />
1353                                         </BatchingTestTask>
1354                                         <Message Text=""Msg: %(Coll1.Name)"" />
1355                                 </Target>
1356                  </Project>";
1357
1358                         Engine engine = new Engine (Consts.BinPath);
1359                         Project project = engine.CreateNewProject ();
1360
1361                         project.LoadXml (projectString);
1362                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1363
1364                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1365                         Assert.AreEqual (1, include.Count, "A2");
1366
1367                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1368                         Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1369
1370                         Assert.IsTrue (project.Build ("ShowMessage2"), "A1: Build failed");
1371                         include = project.GetEvaluatedItemsByName ("FinalList2");
1372                         Assert.AreEqual (3, include.Count, "A5");
1373
1374                         Assert.AreEqual ("FinalList2", include [0].Name, "A6");
1375                         Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A7");
1376
1377                         Assert.AreEqual ("FinalList2", include [1].Name, "A8");
1378                         Assert.AreEqual ("Def", include [1].FinalItemSpec, "A9");
1379
1380                         Assert.AreEqual ("FinalList2", include [2].Name, "A10");
1381                         Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A11");
1382                 }
1383
1384                 [Test]
1385                 public void TestBatchedMetadataRef3 ()
1386                 {
1387                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1388                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1389                         <ItemGroup>
1390                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1391                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1392                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1393                                 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1394                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1395                                 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1396                         </ItemGroup>
1397                                 <Target Name=""ShowMessage"">
1398                                         <BatchingTestTask SingleTaskItem=""%(Coll2.Name)"" >
1399                                                 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1400                                         </BatchingTestTask>
1401                                         <Message Text=""Msg: %(Coll1.Name)"" />
1402                                 </Target>
1403                  </Project>";
1404
1405                         Engine engine = new Engine (Consts.BinPath);
1406                         Project project = engine.CreateNewProject ();
1407
1408                         project.LoadXml (projectString);
1409                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1410
1411                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1412                         Assert.AreEqual (1, include.Count, "A2");
1413
1414                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1415                         Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1416
1417                 }
1418
1419                 [Test]
1420                 public void TestBatchedMetadataRef4 ()
1421                 {
1422                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1423                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1424                         <ItemGroup>
1425                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1426                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1427                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1428                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1429                         </ItemGroup>
1430                                 <Target Name=""ShowMessage"">
1431                                         <BatchingTestTask SingleTaskItem=""%(Coll3.Name)"" >
1432                                                 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1433                                         </BatchingTestTask>
1434                                 </Target>
1435                  </Project>";
1436
1437                         Engine engine = new Engine (Consts.BinPath);
1438                         Project project = engine.CreateNewProject ();
1439
1440                         project.LoadXml (projectString);
1441                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1442
1443                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1444                         Assert.AreEqual (0, include.Count, "A2");
1445                 }
1446
1447                 [Test]
1448                 public void TestBatchedMetadataRef5 ()
1449                 {
1450                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1451                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1452                         <ItemGroup>
1453                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1454                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1455                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1456                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1457                         </ItemGroup>
1458                                 <Target Name=""ShowMessage"">
1459                                         <Message Text=""Coll1: @(Coll1->'Foo%(Name)Bar')"" />
1460                                         <BatchingTestTask Sources=""@(Coll1->'Foo%(Name)Bar')"" >
1461                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1462                                         </BatchingTestTask>
1463                                 </Target>
1464                  </Project>";
1465
1466                         Engine engine = new Engine (Consts.BinPath);
1467                         Project project = engine.CreateNewProject ();
1468                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1469                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1470                         engine.RegisterLogger (logger);
1471
1472                         project.LoadXml (projectString);
1473                         bool result = project.Build ("ShowMessage");
1474                         if (!result) {
1475                                 logger.DumpMessages ();
1476                                 Assert.Fail ("A1: Build failed");
1477                         }
1478                         logger.DumpMessages ();
1479                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1480                         Assert.AreEqual (3, include.Count, "A2");
1481                 }
1482
1483                 [Test]
1484                 public void TestInitialTargets ()
1485                 {
1486                         Engine engine = new Engine (Consts.BinPath);
1487                         Project project = engine.CreateNewProject ();
1488
1489                         project.LoadXml (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" InitialTargets=""pre  "">
1490                                 <Target Name=""boo"">
1491                                         <Message Text=""Executing boo target""/>
1492                                 </Target>
1493                                 <Target Name=""pre"">
1494                                         <Message Text=""Executing pre target""/>
1495                                 </Target>
1496                         </Project>");
1497
1498                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1499                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1500                         engine.RegisterLogger (logger);
1501
1502                         try {
1503                                 Assert.IsTrue (project.Build (), "Build failed");
1504
1505                                 Assert.AreEqual (0, logger.CheckHead ("Executing pre target", MessageImportance.Normal), "A1");
1506                                 Assert.AreEqual (0, logger.CheckHead ("Executing boo target", MessageImportance.Normal), "A2");
1507                                 Assert.AreEqual (0, logger.Count, "A3");
1508                         } catch {
1509                                 logger.DumpMessages ();
1510                                 throw;
1511                         }
1512                 }
1513
1514                 [Test]
1515                 public void TestRequiredTask_String1 ()
1516                 {
1517                         CheckProjectForRequiredTests ("RequiredTestTask_String", "@(NonExistant)",
1518                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1519                 }
1520
1521                 [Test]
1522                 public void TestRequiredTask_String2 ()
1523                 {
1524                         CheckProjectForRequiredTests ("RequiredTestTask_String", "$(NonExistant)",
1525                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1526                 }
1527
1528                 [Test]
1529                 public void TestRequiredTask_TaskItem1 ()
1530                 {
1531                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "@(NonExistant)",
1532                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1533                 }
1534
1535                 [Test]
1536                 public void TestRequiredTask_TaskItem2 ()
1537                 {
1538                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "$(NonExistant)",
1539                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1540                 }
1541
1542                 [Test]
1543                 public void TestRequiredTask_TaskItemArray1 ()
1544                 {
1545                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "@(NonExistant)",
1546                                 true, "Build failed");
1547
1548                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1549                         Assert.AreEqual (1, group.Count, "A2");
1550                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1551                 }
1552
1553                 [Test]
1554                 public void TestRequiredTask_TaskItemArray2 ()
1555                 {
1556                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "$(NonExistant)",
1557                                 true, "Build failed");
1558
1559                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1560                         Assert.AreEqual (1, group.Count, "A2");
1561                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1562                 }
1563
1564                 [Test]
1565                 public void TestRequiredTask_TaskItemArray3 ()
1566                 {
1567                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_IntArray", "$(NonExistant)",
1568                                 true, "Build failed");
1569
1570                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1571                         Assert.AreEqual (1, group.Count, "A2");
1572                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1573                 }
1574
1575
1576                 Project CheckProjectForRequiredTests (string taskname, string property_arg, bool expected_result, string error_msg)
1577                 {
1578                         string projectString = String.Format (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1579                                 <UsingTask TaskName=""{0}"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1580                                 <Target Name=""foo"">
1581                                         <{0} Property=""{1}"">
1582                                                 <Output TaskParameter=""Output"" ItemName=""OutItem""/>
1583                                         </{0}>
1584                                 </Target>
1585                         </Project>", taskname, property_arg);
1586
1587                         Engine engine = new Engine (Consts.BinPath);
1588                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1589                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1590                         engine.RegisterLogger (logger);
1591                         Project project = engine.CreateNewProject ();
1592                         project.LoadXml (projectString);
1593
1594                         try {
1595                                 Assert.AreEqual (expected_result, project.Build (), error_msg);
1596                         } finally {
1597                                 logger.DumpMessages ();
1598                         }
1599                         return project;
1600                 }
1601
1602                 static void CheckBuildItem (BuildItem item, string name, string [,] metadata, string finalItemSpec, string prefix)
1603                 {
1604                         Assert.AreEqual (name, item.Name, prefix + "#1");
1605                         for (int i = 0; i < metadata.GetLength (0); i++) {
1606                                 string key = metadata [i, 0];
1607                                 string val = metadata [i, 1];
1608                                 Assert.IsTrue (item.HasMetadata (key), String.Format ("{0}#2: Expected metadata '{1}' not found", prefix, key));
1609                                 Assert.AreEqual (val, item.GetMetadata (key), String.Format ("{0}#3: Value for metadata {1}", prefix, key));
1610                                 Assert.AreEqual (val, item.GetEvaluatedMetadata (key), String.Format ("{0}#4: Value for evaluated metadata {1}", prefix, key));
1611                         }
1612                         Assert.AreEqual (finalItemSpec, item.FinalItemSpec, prefix + "#5");
1613                 }
1614
1615                 void CheckProjectBuild (Project project, string [] targetNames, bool result, string [] outputNames, string prefix)
1616                 {
1617                         IDictionary targetOutputs = new Hashtable ();
1618
1619                         Assert.AreEqual (result, project.Build (targetNames, targetOutputs), prefix + "A1");
1620                         Assert.AreEqual (outputNames.Length, targetOutputs.Keys.Count, prefix + "A2");
1621
1622                         foreach (string outputName in outputNames) {
1623                                 Assert.IsTrue (targetOutputs.Contains (outputName), prefix + " A3: target " + outputName);
1624
1625                                 object o = targetOutputs [outputName];
1626                                 Assert.IsTrue (typeof (ITaskItem []).IsAssignableFrom (o.GetType ()), prefix + " A4: target " + outputName);
1627
1628                                 ITaskItem [] items = (ITaskItem [])o;
1629                                 Assert.AreEqual (0, items.Length, prefix + "A5: target " + outputName);
1630                         }
1631                 }
1632
1633                 string CreateProjectString (bool run_separate, string [] targets, bool [] results, string prefix)
1634                 {
1635                         StringBuilder sb = new StringBuilder ();
1636                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">");
1637                         sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", "main", prefix);
1638
1639                         sb.AppendFormat ("<CallTarget Targets=\"");
1640                         for (int i = 0; i < targets.Length; i++)
1641                                 sb.AppendFormat ("{0};", targets [i]);
1642                         sb.AppendFormat ("\" ");
1643
1644                         if (run_separate)
1645                                 sb.AppendFormat (" RunEachTargetSeparately=\"true\" ");
1646                         sb.AppendFormat ("/></Target>\n");
1647
1648                         for (int i = 0; i < targets.Length; i++) {
1649                                 sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", targets [i], prefix);
1650                                 if (!results [i])
1651                                         sb.AppendFormat ("<Error Text = \"#Error message for target {0}:{1}\"/>", prefix, targets [i]);
1652                                 sb.Append ("</Target>\n");
1653                         }
1654
1655                         sb.Append ("</Project>");
1656
1657                         return sb.ToString ();
1658                 }
1659
1660                 void CreateProjectFile (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1661                 {
1662                         using (StreamWriter sw = new StreamWriter (fname))
1663                                 sw.Write (CreateProjectString (run_separate, targets, results, prefix));
1664                 }
1665
1666                 Project CreateAndLoadProject (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1667                 {
1668                         Engine engine = new Engine (Consts.BinPath);
1669                         Project project = engine.CreateNewProject ();
1670
1671                         string projectXml = CreateProjectString (run_separate, targets, results, prefix);
1672                         if (fname == null) {
1673                                 project.LoadXml (projectXml);
1674                         } else {
1675                                 using (StreamWriter sw = new StreamWriter (fname))
1676                                         sw.Write (projectXml);
1677                                 project.Load (fname);
1678                                 File.Delete (fname);
1679                         }
1680
1681                         return project;
1682                 }
1683         }
1684 }