5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Generic;
33 using Microsoft.Build.BuildEngine;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using NUnit.Framework;
39 using MBT = MonoTests.Microsoft.Build.Tasks;
41 namespace MonoTests.Microsoft.Build.BuildEngine {
43 class TestLogger : Logger {
44 int target_started_events = 0;
45 int target_finished_events = 0;
47 public override void Initialize (IEventSource eventSource)
49 eventSource.TargetStarted += new TargetStartedEventHandler(TargetStarted);
50 eventSource.TargetFinished += new TargetFinishedEventHandler(TargetFinished);
51 eventSource.MessageRaised += new BuildMessageEventHandler(Message);
52 eventSource.WarningRaised += new BuildWarningEventHandler(Warning);
55 void TargetStarted (object sender, TargetStartedEventArgs args)
57 target_started_events++;
60 void TargetFinished (object sender, TargetFinishedEventArgs args)
62 target_finished_events++;
65 void Message (object sender, BuildMessageEventArgs args)
69 void Warning (object sender, BuildWarningEventArgs args)
73 public int TargetStartedEvents { get { return target_started_events; } }
75 public int TargetFinishedEvents { get { return target_finished_events; } }
79 public class ProjectTest {
82 Import [] GetImports (ImportCollection ic)
84 List<Import> list = new List<Import> ();
85 foreach (Import i in ic)
87 return list.ToArray ();
92 public void TestAssignment1 ()
96 string documentString =
97 "<Project></Project>";
99 engine = new Engine (Consts.BinPath);
100 DateTime time = DateTime.Now;
101 project = engine.CreateNewProject ();
103 project.LoadXml (documentString);
104 } catch (InvalidProjectFileException) {
105 Assert.AreEqual (true, project.BuildEnabled, "A1");
106 Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
107 Assert.AreEqual (String.Empty, project.FullFileName, "A3");
108 Assert.AreEqual (false, project.IsDirty, "A4");
109 Assert.AreEqual (false, project.IsValidated, "A5");
110 Assert.AreEqual (engine, project.ParentEngine, "A6");
111 Console.WriteLine ("time: {0} p.t: {1}", time, project.TimeOfLastDirty);
112 Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
113 Assert.IsTrue (String.Empty != project.Xml, "A8");
117 Assert.Fail ("Expected InvalidProjectFileException");
121 public void TestAssignment2 ()
125 string documentString =
126 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>";
128 engine = new Engine (Consts.BinPath);
129 DateTime time = DateTime.Now;
130 project = engine.CreateNewProject ();
131 project.LoadXml (documentString);
133 Assert.AreEqual (true, project.BuildEnabled, "A1");
134 Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
135 Assert.AreEqual (String.Empty, project.FullFileName, "A3");
136 Assert.AreEqual (true, project.IsDirty, "A4");
137 Assert.AreEqual (false, project.IsValidated, "A5");
138 Assert.AreEqual (engine, project.ParentEngine, "A6");
139 Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
140 Assert.IsTrue (String.Empty != project.Xml, "A8");
144 [Category ("NotWorking")]
145 public void TestAddNewImport1 ()
150 string documentString = @"
151 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
155 <Import Project='Test/resources/Import.csproj' />
159 engine = new Engine (Consts.BinPath);
160 project = engine.CreateNewProject ();
161 project.LoadXml (documentString);
163 project.AddNewImport ("a", "true");
164 // reevaluation wasn't caused by anything so it has only old import
165 Assert.AreEqual (1, project.Imports.Count, "A1");
169 [Ignore ("Too detailed probably (implementation specific)")]
170 public void TestAddNewItem1 ()
174 BuildItemGroup [] groups = new BuildItemGroup [1];
176 string documentString = @"
177 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
181 engine = new Engine (Consts.BinPath);
182 project = engine.CreateNewProject ();
183 project.LoadXml (documentString);
185 BuildItem item = project.AddNewItem ("A", "B");
187 Assert.AreEqual (1, project.ItemGroups.Count, "A1");
188 project.ItemGroups.CopyTo (groups, 0);
189 Assert.AreEqual (1, groups [0].Count, "A2");
190 Assert.AreEqual ("B", groups [0] [0].Include, "A3");
191 Assert.AreEqual ("B", groups [0] [0].FinalItemSpec, "A4");
192 Assert.AreEqual ("A", groups [0] [0].Name, "A5");
193 //Assert.AreNotSame (item, groups [0] [0], "A6");
194 Assert.IsFalse (object.ReferenceEquals (item, groups [0] [0]), "A6");
196 Assert.AreEqual (1, project.EvaluatedItems.Count, "A7");
197 Assert.AreEqual ("B", project.EvaluatedItems [0].Include, "A8");
198 Assert.AreEqual ("B", project.EvaluatedItems [0].FinalItemSpec, "A9");
199 Assert.AreEqual ("A", project.EvaluatedItems [0].Name, "A10");
200 //Assert.AreNotSame (item, project.EvaluatedItems [0], "A11");
201 Assert.IsFalse (object.ReferenceEquals (item, project.EvaluatedItems [0]), "A11");
205 [Category ("NotWorking")]
206 public void TestAddNewItem2 ()
211 engine = new Engine (Consts.BinPath);
212 project = engine.CreateNewProject ();
214 BuildItem item = project.AddNewItem ("A", "a;b;c");
215 Assert.AreEqual ("a;b;c", item.Include, "A1");
216 Assert.AreEqual ("a", item.FinalItemSpec, "A2");
218 Assert.AreEqual (3, project.EvaluatedItems.Count, "A3");
222 public void TestAddNewItem3 ()
226 BuildItemGroup [] groups = new BuildItemGroup [4];
228 string documentString = @"
229 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
243 engine = new Engine (Consts.BinPath);
244 project = engine.CreateNewProject ();
245 project.LoadXml (documentString);
247 project.AddNewItem ("B", "b");
249 project.ItemGroups.CopyTo (groups, 0);
250 Assert.AreEqual (0, groups [0].Count, "A1");
251 Assert.AreEqual (1, groups [1].Count, "A2");
252 Assert.AreEqual (1, groups [2].Count, "A3");
253 Assert.AreEqual (2, groups [3].Count, "A4");
256 public void TestAddNewItemGroup ()
261 string documentString = @"
262 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
266 engine = new Engine (Consts.BinPath);
267 project = engine.CreateNewProject ();
268 project.LoadXml (documentString);
270 BuildItemGroup big = project.AddNewItemGroup ();
271 Assert.IsNotNull (big, "A1");
272 Assert.AreEqual (String.Empty, big.Condition, "A2");
273 Assert.AreEqual (0, big.Count, "A3");
274 Assert.AreEqual (false, big.IsImported, "A4");
275 Assert.IsTrue (project.IsDirty, "A5");
279 public void TestAddNewPropertyGroup ()
284 string documentString = @"
285 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
289 engine = new Engine (Consts.BinPath);
290 project = engine.CreateNewProject ();
291 project.LoadXml (documentString);
293 BuildPropertyGroup bpg = project.AddNewPropertyGroup (false);
294 Assert.IsNotNull (bpg, "A1");
295 Assert.AreEqual (String.Empty, bpg.Condition, "A2");
296 Assert.AreEqual (0, bpg.Count, "A3");
297 Assert.AreEqual (false, bpg.IsImported, "A4");
298 Assert.IsTrue (project.IsDirty, "A5");
302 public void TestBuild0 ()
306 IDictionary hashtable = new Hashtable ();
308 string documentString = @"
309 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
319 engine = new Engine (Consts.BinPath);
320 project = engine.CreateNewProject ();
321 project.LoadXml (documentString);
323 Assert.AreEqual (true, project.Build (new string [] { "Main" }, hashtable), "A1");
324 Assert.AreEqual (1, hashtable.Count, "A2");
326 IDictionaryEnumerator e = hashtable.GetEnumerator ();
329 string name = (string) e.Key;
330 Assert.AreEqual ("Main", name, "A3");
331 ITaskItem [] arr = (ITaskItem []) e.Value;
333 Assert.AreEqual (3, arr.Length, "A4");
334 Assert.AreEqual ("d", arr [0].ItemSpec, "A5");
335 Assert.AreEqual ("e", arr [1].ItemSpec, "A6");
336 Assert.AreEqual ("f", arr [2].ItemSpec, "A7");
340 public void TestBuild1 ()
344 IDictionary hashtable = new Hashtable ();
346 string documentString = @"
347 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
349 <Microsoft.Build.Tasks.Message Text='Text' />
354 engine = new Engine (Consts.BinPath);
355 project = engine.CreateNewProject ();
356 project.LoadXml (documentString);
358 Assert.AreEqual (true, project.Build (new string[] { "Main" }, hashtable), "A1");
359 Assert.AreEqual (1, hashtable.Count, "A2");
361 IDictionaryEnumerator e = hashtable.GetEnumerator ();
364 string name = (string) e.Key;
365 Assert.AreEqual ("Main", name, "A3");
366 Assert.IsNotNull ((ITaskItem []) e.Value, "A4");
370 public void TestBuild2 ()
375 string documentString = @"
376 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
378 <Message Text='text' />
383 engine = new Engine (Consts.BinPath);
384 MBT.TestMessageLogger tl = new MBT.TestMessageLogger();
385 engine.RegisterLogger (tl);
386 project = engine.CreateNewProject ();
387 project.LoadXml (documentString);
392 Assert.AreEqual (2, tl.TargetStarted, "A1");
393 Assert.AreEqual (2, tl.TargetFinished, "A2");
394 Assert.AreEqual (2, tl.TaskStarted, "A3");
395 Assert.AreEqual (2, tl.TaskFinished, "A4");
399 public void TestBuild3 ()
404 string documentString = @"
405 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
407 <Message Text='text' />
412 engine = new Engine (Consts.BinPath);
413 MBT.TestMessageLogger tl = new MBT.TestMessageLogger ();
414 engine.RegisterLogger (tl);
415 project = engine.CreateNewProject ();
416 project.LoadXml (documentString);
418 project.Build (new string [1] { "T" }, null, BuildSettings.None);
419 project.Build (new string [1] { "T" }, null, BuildSettings.None);
421 Assert.AreEqual (2, tl.TargetStarted, "A1");
422 Assert.AreEqual (2, tl.TargetFinished, "A2");
423 Assert.AreEqual (2, tl.TaskStarted, "A3");
424 Assert.AreEqual (2, tl.TaskFinished, "A4");
428 public void TestBuild4 ()
433 string documentString = @"
434 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
436 <Message Text='text' />
441 engine = new Engine (Consts.BinPath);
442 MBT.TestMessageLogger tl = new MBT.TestMessageLogger ();
443 engine.RegisterLogger (tl);
444 project = engine.CreateNewProject ();
445 project.LoadXml (documentString);
447 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
448 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
450 Assert.AreEqual (1, tl.TargetStarted, "A1");
451 Assert.AreEqual (1, tl.TargetFinished, "A2");
452 Assert.AreEqual (1, tl.TaskStarted, "A3");
453 Assert.AreEqual (1, tl.TaskFinished, "A4");
457 public void TestBuild5 ()
462 string documentString = @"
463 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
467 engine = new Engine (Consts.BinPath);
468 project = engine.CreateNewProject ();
469 project.LoadXml (documentString);
471 Assert.IsFalse (project.Build ("target_that_doesnt_exist"));
475 public void TestEvaluatedItems1 ()
480 string documentString = @"
481 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
484 <B Include='b' Condition='false' />
489 engine = new Engine (Consts.BinPath);
490 project = engine.CreateNewProject ();
491 project.LoadXml (documentString);
493 Assert.AreEqual (1, project.EvaluatedItems.Count, "A1");
495 BuildItem bi = project.EvaluatedItems [0];
500 BuildItemGroup [] big = new BuildItemGroup [1];
501 project.ItemGroups.CopyTo (big, 0);
502 Assert.AreEqual ("C", big [0] [0].Name, "A2");
503 Assert.AreEqual ("c", big [0] [0].Include, "A3");
507 public void TestEvaluatedItems2 ()
512 string documentString = @"
513 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
515 <A Include='a;b;c' />
520 engine = new Engine (Consts.BinPath);
521 project = engine.CreateNewProject ();
522 project.LoadXml (documentString);
524 BuildItemGroup [] big = new BuildItemGroup [1];
525 project.ItemGroups.CopyTo (big, 0);
527 Assert.AreEqual (3, project.EvaluatedItems.Count, "A1");
528 Assert.AreEqual ("a;b;c", big [0] [0].Include, "A2");
529 Assert.AreEqual (1, big [0].Count, "A3");
531 BuildItem bi = project.EvaluatedItems [0];
535 Assert.AreEqual (3, big [0].Count, "A4");
536 Assert.AreEqual ("d", big [0] [0].Include, "A5");
537 Assert.AreEqual ("b", big [0] [1].Include, "A6");
538 Assert.AreEqual ("c", big [0] [2].Include, "A7");
542 [Category ("NotWorking")]
543 public void TestGetConditionedPropertyValues ()
548 string documentString = @"
549 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
550 <PropertyGroup Condition='true'>
552 <B Condition='true'>A</B>
555 <C Condition='true'>A</C>
556 <C Condition='false'>B</C>
557 <C Condition='!false'>C</C>
559 <E Condition="" '$(C)' == 'A' "">E</E>
564 engine = new Engine (Consts.BinPath);
565 project = engine.CreateNewProject ();
566 project.LoadXml (documentString);
568 Assert.AreEqual (0, project.GetConditionedPropertyValues ("A").Length, "A1");
569 Assert.AreEqual (0, project.GetConditionedPropertyValues ("B").Length, "A2");
570 Assert.AreEqual (1, project.GetConditionedPropertyValues ("C").Length, "A3");
571 Assert.AreEqual (0, project.GetConditionedPropertyValues ("D").Length, "A4");
572 Assert.AreEqual (0, project.GetConditionedPropertyValues ("E").Length, "A5");
573 Assert.AreEqual ("A", project.GetConditionedPropertyValues ("C") [0], "A6");
577 [ExpectedException (typeof (ArgumentNullException))]
578 public void TestGetEvaluatedItemsByName1 ()
583 string documentString = @"
584 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
588 engine = new Engine (Consts.BinPath);
589 project = engine.CreateNewProject ();
590 project.LoadXml (documentString);
592 project.GetEvaluatedItemsByName (null);
596 public void TestGetEvaluatedItemsByName2 ()
601 string documentString = @"
602 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
605 <B Include='2' Condition='true' />
606 <C Include='3' Condition='false' />
611 engine = new Engine (Consts.BinPath);
612 project = engine.CreateNewProject ();
613 project.LoadXml (documentString);
617 big = project.GetEvaluatedItemsByName (String.Empty);
619 Assert.AreEqual (0, big.Count, "A1");
621 big = project.GetEvaluatedItemsByName ("A");
623 Assert.AreEqual (1, big.Count, "A2");
624 Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
626 big = project.GetEvaluatedItemsByName ("B");
628 Assert.AreEqual (1, big.Count, "A4");
629 Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
631 big = project.GetEvaluatedItemsByName ("C");
633 Assert.AreEqual (0, big.Count, "A6");
637 [ExpectedException (typeof (ArgumentNullException))]
638 public void TestGetEvaluatedItemsByNameIgnoringCondition1 ()
643 string documentString = @"
644 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
648 engine = new Engine (Consts.BinPath);
649 project = engine.CreateNewProject ();
650 project.LoadXml (documentString);
652 project.GetEvaluatedItemsByNameIgnoringCondition (null);
656 public void TestGetEvaluatedItemsByNameIgnoringCondition2 ()
661 string documentString = @"
662 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
665 <B Include='2' Condition='true' />
666 <C Include='3' Condition='false' />
671 engine = new Engine (Consts.BinPath);
672 project = engine.CreateNewProject ();
673 project.LoadXml (documentString);
677 big = project.GetEvaluatedItemsByNameIgnoringCondition (String.Empty);
679 Assert.AreEqual (0, big.Count, "A1");
681 big = project.GetEvaluatedItemsByNameIgnoringCondition ("A");
683 Assert.AreEqual (1, big.Count, "A2");
684 Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
686 big = project.GetEvaluatedItemsByNameIgnoringCondition ("B");
688 Assert.AreEqual (1, big.Count, "A4");
689 Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
691 big = project.GetEvaluatedItemsByNameIgnoringCondition ("C");
693 Assert.AreEqual (1, big.Count, "A6");
694 Assert.AreEqual ("3", big [0].FinalItemSpec, "A7");
698 [ExpectedException (typeof (ArgumentNullException))]
699 public void TestGetEvaluatedProperty1 ()
704 string documentString = @"
705 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
709 engine = new Engine (Consts.BinPath);
710 project = engine.CreateNewProject ();
711 project.LoadXml (documentString);
713 project.GetEvaluatedProperty (null);
716 public void TestGetEvaluatedProperty2 ()
721 string documentString = @"
722 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
725 <B Condition='true'>2</B>
726 <C Condition='false'>3</C>
731 engine = new Engine (Consts.BinPath);
732 project = engine.CreateNewProject ();
733 project.LoadXml (documentString);
735 Assert.AreEqual ("1", project.GetEvaluatedProperty ("A"), "A1");
736 Assert.AreEqual ("2", project.GetEvaluatedProperty ("B"), "A2");
737 Assert.IsNull (project.GetEvaluatedProperty ("C"), "A3");
741 public void TestGetProjectExtensions ()
746 string documentString = @"
747 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
754 engine = new Engine (Consts.BinPath);
755 project = engine.CreateNewProject ();
756 project.LoadXml (documentString);
758 Assert.AreEqual (String.Empty, project.GetProjectExtensions (null), "A1");
759 Assert.AreEqual (String.Empty, project.GetProjectExtensions (String.Empty), "A2");
760 Assert.AreEqual (String.Empty, project.GetProjectExtensions ("something"), "A3");
761 Assert.AreEqual ("Text", project.GetProjectExtensions ("Node"), "A4");
765 public void TestGlobalProperties1 ()
770 string documentString = @"
771 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
775 engine = new Engine (Consts.BinPath);
776 project = engine.CreateNewProject ();
777 project.LoadXml (documentString);
779 Assert.AreEqual (0, project.GlobalProperties.Count, "A1");
783 public void TestGlobalProperties2 ()
788 string documentString = @"
789 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
793 engine = new Engine (Consts.BinPath);
794 engine.GlobalProperties.SetProperty ("Property", "Value");
796 project = engine.CreateNewProject ();
797 project.LoadXml (documentString);
799 Assert.AreEqual (1, project.GlobalProperties.Count, "A1");
800 Assert.AreEqual ("Property", project.GlobalProperties ["Property"].Name, "A2");
801 Assert.AreEqual ("Value", project.GlobalProperties ["Property"].Value, "A3");
802 Assert.AreEqual ("Value", project.GlobalProperties ["Property"].FinalValue, "A4");
803 Assert.AreEqual ("Property", project.EvaluatedProperties ["Property"].Name, "A2");
804 Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].Value, "A3");
805 Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].FinalValue, "A4");
809 [Category ("NotDotNet")]
810 [ExpectedException (typeof (ArgumentNullException))]
811 public void TestGlobalProperties3 ()
816 string documentString = @"
817 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
821 engine = new Engine (Consts.BinPath);
822 project = engine.CreateNewProject ();
823 project.LoadXml (documentString);
825 project.GlobalProperties = null;
829 [Ignore ("needs rewriting")]
830 public void TestGlobalProperties4 ()
835 string documentString = @"
836 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
838 <Property>a</Property>
843 engine = new Engine (Consts.BinPath);
844 project = engine.CreateNewProject ();
845 project.LoadXml (documentString);
847 BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
848 project.PropertyGroups.CopyTo (groups, 0);
850 project.GlobalProperties = groups [0];
851 project.GlobalProperties = project.EvaluatedProperties;
855 [Category ("NotDotNet")]
856 [ExpectedException (typeof (InvalidOperationException))]
857 public void TestGlobalProperties5 ()
862 string documentString = @"
863 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
865 <Property>a</Property>
870 engine = new Engine (Consts.BinPath);
871 project = engine.CreateNewProject ();
872 project.LoadXml (documentString);
874 BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
875 project.PropertyGroups.CopyTo (groups, 0);
876 project.GlobalProperties = groups [0];
880 [ExpectedException (typeof (InvalidProjectFileException))]
881 public void TestLoad1 ()
886 string documentString = @"
887 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
892 engine = new Engine (Consts.BinPath);
893 project = engine.CreateNewProject ();
894 project.LoadXml (documentString);
898 [ExpectedException (typeof (InvalidProjectFileException))]
899 public void TestLoad2 ()
904 engine = new Engine (Consts.BinPath);
905 project = engine.CreateNewProject ();
906 project.LoadXml ("project_file_that_doesnt_exist");
910 public void TestParentEngine ()
915 engine = new Engine (Consts.BinPath);
916 project = engine.CreateNewProject ();
918 Assert.AreEqual (engine, project.ParentEngine, "A1");
922 [Category ("NotDotNet")]
923 [ExpectedException (typeof (ArgumentNullException))]
924 public void TestRemoveItemGroup1 ()
929 engine = new Engine (Consts.BinPath);
930 p1 = engine.CreateNewProject ();
932 p1.RemoveItemGroup (null);
935 // The "BuildItemGroup" object specified does not belong to the correct "Project" object.
937 [ExpectedException (typeof (InvalidOperationException))]
938 [Category ("NotWorking")]
939 public void TestRemoveItemGroup2 ()
944 BuildItemGroup [] groups = new BuildItemGroup [1];
946 engine = new Engine (Consts.BinPath);
947 p1 = engine.CreateNewProject ();
948 p2 = engine.CreateNewProject ();
950 p1.AddNewItem ("A", "B");
951 p1.ItemGroups.CopyTo (groups, 0);
953 p2.RemoveItemGroup (groups [0]);
957 [Category ("NotDotNet")]
958 [ExpectedException (typeof (ArgumentNullException))]
959 public void TestRemoveItem1 ()
964 engine = new Engine (Consts.BinPath);
965 project = engine.CreateNewProject ();
967 project.RemoveItem (null);
970 // The object passed in is not part of the project.
972 [ExpectedException (typeof (InvalidOperationException))]
973 public void TestRemoveItem2 ()
978 engine = new Engine (Consts.BinPath);
979 project = engine.CreateNewProject ();
981 project.RemoveItem (new BuildItem ("name", "include"));
984 // The "BuildItemGroup" object specified does not belong to the correct "Project" object.
986 [ExpectedException (typeof (InvalidOperationException))]
987 public void TestRemoveItem3 ()
993 engine = new Engine (Consts.BinPath);
994 p1 = engine.CreateNewProject ();
995 p2 = engine.CreateNewProject ();
997 p1.AddNewItem ("A", "B");
999 p2.RemoveItem (p1.EvaluatedItems [0]);
1003 [Category ("NotDotNet")]
1004 [ExpectedException (typeof (InvalidOperationException))]
1005 public void TestRemoveItem4 ()
1011 engine = new Engine (Consts.BinPath);
1012 p1 = engine.CreateNewProject ();
1013 p2 = engine.CreateNewProject ();
1015 p1.AddNewItem ("A", "B");
1016 p1.AddNewItem ("A", "C");
1018 p2.RemoveItem (p1.EvaluatedItems [0]);
1022 public void TestRemoveItem5 ()
1026 BuildItemGroup [] groups = new BuildItemGroup [1];
1028 string documentString = @"
1029 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1036 engine = new Engine (Consts.BinPath);
1037 project = engine.CreateNewProject ();
1038 project.LoadXml (documentString);
1040 project.RemoveItem (project.EvaluatedItems [0]);
1041 Assert.AreEqual (0, project.EvaluatedItems.Count, "A1");
1042 project.ItemGroups.CopyTo (groups, 0);
1043 Assert.IsNull (groups [0], "A2");
1044 Assert.AreEqual (0, project.ItemGroups.Count, "A3");
1048 public void TestResetBuildStatus ()
1053 string documentString = @"
1054 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1055 <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
1056 <Message Text='text' />
1061 engine = new Engine (Consts.BinPath);
1062 TestLogger tl = new TestLogger ();
1063 engine.RegisterLogger (tl);
1064 project = engine.CreateNewProject ();
1065 project.LoadXml (documentString);
1067 project.Build ("T");
1068 project.ResetBuildStatus ();
1069 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
1070 project.ResetBuildStatus ();
1071 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
1073 Assert.AreEqual (3, tl.TargetStartedEvents, "A1");
1074 Assert.AreEqual (3, tl.TargetFinishedEvents, "A1");
1078 public void TestSchemaFile ()
1083 string documentString = @"
1084 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1088 engine = new Engine (Consts.BinPath);
1089 project = engine.CreateNewProject ();
1090 project.LoadXml (documentString);
1092 Assert.IsNull (project.SchemaFile, "A1");
1095 [Category ("NotDotNet")]
1096 [ExpectedException (typeof (ArgumentNullException))]
1097 public void TestSetProjectExtensions1 ()
1102 string documentString = @"
1103 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1107 engine = new Engine (Consts.BinPath);
1108 project = engine.CreateNewProject ();
1109 project.LoadXml (documentString);
1111 project.SetProjectExtensions (null, null);
1115 public void TestSetProjectExtensions2 ()
1120 string documentString = @"
1121 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1125 engine = new Engine (Consts.BinPath);
1126 project = engine.CreateNewProject ();
1127 project.LoadXml (documentString);
1129 project.SetProjectExtensions ("name", "1");
1130 Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1131 project.SetProjectExtensions ("name", "2");
1132 Assert.AreEqual ("2", project.GetProjectExtensions ("name"), "A2");
1133 Assert.IsTrue (project.IsDirty, "A3");
1137 public void TestSetProjectExtensions3 ()
1142 string documentString = @"
1143 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1145 </ProjectExtensions>
1149 engine = new Engine (Consts.BinPath);
1150 project = engine.CreateNewProject ();
1151 project.LoadXml (documentString);
1153 project.SetProjectExtensions ("name", "1");
1154 Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1155 Assert.IsTrue (project.IsDirty, "A2");
1159 public void TestBuildProjectError1 ()
1161 Engine engine = new Engine (Consts.BinPath);
1162 Project project = engine.CreateNewProject ();
1164 Assert.IsFalse (project.Build ((string) null), "A1");
1165 Assert.IsFalse (project.Build ((string[]) null), "A2");
1166 Assert.IsFalse (project.Build ((string []) null, null), "A3");
1167 Assert.IsFalse (project.Build ((string []) null, null, BuildSettings.None), "A4");
1171 public void TestBuildProjectError2 ()
1173 Engine engine = new Engine (Consts.BinPath);
1174 Project project = engine.CreateNewProject ();
1177 project.Build (new string [] { null });
1181 Assert.Fail ("Expected exception for project.Build, null string in targetNames []");
1185 public void TestBuildProjectFile1 ()
1187 Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPF1");
1188 CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF1");
1192 public void TestBuildProjectFileXml1 ()
1194 Project project = CreateAndLoadProject (null, false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPFX1");
1195 CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX1");
1199 public void TestBuildProjectFile2 ()
1201 Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF2");
1202 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF2");
1206 public void TestBuildProjectFileXml2 ()
1208 Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX2");
1209 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX2");
1213 public void TestBuildProjectFile3 ()
1215 Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF3");
1216 CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPF3");
1220 public void TestBuildProjectFileXml3 ()
1222 Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX3");
1223 CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPFX3");
1227 public void TestBuildProjectFile4 ()
1229 Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF4");
1230 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF4");
1234 public void TestBuildProjectFileXml4 ()
1236 Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX4");
1237 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX4");
1240 //Run separate tests
1244 public void TestBuildProjectFile5 ()
1246 Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF5");
1247 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF5");
1251 public void TestBuildProjectFileXml5 ()
1253 Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX5");
1254 CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX5");
1258 public void TestBuildProjectFile6 ()
1260 Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF6");
1261 CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF6");
1265 public void TestBuildProjectFileXml6 ()
1267 Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX6");
1268 CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX6");
1271 // run multiple targets
1273 public void TestBuildProjectFile7 ()
1275 Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF7");
1276 CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPF7");
1280 public void TestBuildProjectFileXml7 ()
1282 Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX7");
1283 CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPFX7");
1287 public void TestBuildProjectFile8 ()
1289 Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPF8");
1290 CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPF8");
1294 public void TestBuildProjectFileXml8 ()
1296 Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPFX8");
1297 CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPFX8");
1301 public void TestBatchedMetadataRef1 ()
1303 //test for multiple items with same metadata also
1304 string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1305 <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1307 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1308 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1309 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1310 <Coll1 Include=""A4""><Name>Xyz</Name></Coll1>
1311 <Coll2 Include=""B1""></Coll2>
1313 <Target Name=""ShowMessage"">
1314 <BatchingTestTask Sources=""%(Coll1.Name)"">
1315 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1317 <Message Text=""Msg: %(Coll1.Name)"" />
1321 Engine engine = new Engine (Consts.BinPath);
1322 Project project = engine.CreateNewProject ();
1324 project.LoadXml (projectString);
1325 Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1327 BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1328 Assert.AreEqual (3, include.Count, "A2");
1330 Assert.AreEqual ("FinalList", include [0].Name, "A3");
1331 Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A4");
1333 Assert.AreEqual ("FinalList", include [1].Name, "A5");
1334 Assert.AreEqual ("Def", include [1].FinalItemSpec, "A6");
1336 Assert.AreEqual ("FinalList", include [2].Name, "A7");
1337 Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A8");
1341 public void TestBatchedMetadataRef2 ()
1343 string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1344 <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1346 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1347 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1348 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1349 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1350 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1351 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1353 <Target Name=""ShowMessage"">
1354 <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll2)"">
1355 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1357 <Message Text=""Msg: %(Coll1.Name)"" />
1359 <Target Name=""ShowMessage2"">
1360 <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll1)"">
1361 <Output TaskParameter=""Output"" ItemName=""FinalList2"" />
1363 <Message Text=""Msg: %(Coll1.Name)"" />
1367 Engine engine = new Engine (Consts.BinPath);
1368 Project project = engine.CreateNewProject ();
1370 project.LoadXml (projectString);
1371 Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1373 BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1374 Assert.AreEqual (1, include.Count, "A2");
1376 Assert.AreEqual ("FinalList", include [0].Name, "A3");
1377 Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1379 Assert.IsTrue (project.Build ("ShowMessage2"), "A1: Build failed");
1380 include = project.GetEvaluatedItemsByName ("FinalList2");
1381 Assert.AreEqual (3, include.Count, "A5");
1383 Assert.AreEqual ("FinalList2", include [0].Name, "A6");
1384 Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A7");
1386 Assert.AreEqual ("FinalList2", include [1].Name, "A8");
1387 Assert.AreEqual ("Def", include [1].FinalItemSpec, "A9");
1389 Assert.AreEqual ("FinalList2", include [2].Name, "A10");
1390 Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A11");
1394 public void TestBatchedMetadataRef3 ()
1396 string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1397 <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1399 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1400 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1401 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1402 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1403 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1404 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1406 <Target Name=""ShowMessage"">
1407 <BatchingTestTask SingleTaskItem=""%(Coll2.Name)"" >
1408 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1410 <Message Text=""Msg: %(Coll1.Name)"" />
1414 Engine engine = new Engine (Consts.BinPath);
1415 Project project = engine.CreateNewProject ();
1417 project.LoadXml (projectString);
1418 Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1420 BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1421 Assert.AreEqual (1, include.Count, "A2");
1423 Assert.AreEqual ("FinalList", include [0].Name, "A3");
1424 Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1429 public void TestBatchedMetadataRef4 ()
1431 string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1432 <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1434 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1435 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1436 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1437 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1439 <Target Name=""ShowMessage"">
1440 <BatchingTestTask SingleTaskItem=""%(Coll3.Name)"" >
1441 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1446 Engine engine = new Engine (Consts.BinPath);
1447 Project project = engine.CreateNewProject ();
1449 project.LoadXml (projectString);
1450 Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1452 BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1453 Assert.AreEqual (0, include.Count, "A2");
1457 public void TestBatchedMetadataRef5 ()
1459 string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1460 <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1462 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1463 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1464 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1465 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1467 <Target Name=""ShowMessage"">
1468 <Message Text=""Coll1: @(Coll1->'Foo%(Name)Bar')"" />
1469 <BatchingTestTask Sources=""@(Coll1->'Foo%(Name)Bar')"" >
1470 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1475 Engine engine = new Engine (Consts.BinPath);
1476 Project project = engine.CreateNewProject ();
1477 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1478 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1479 engine.RegisterLogger (logger);
1481 project.LoadXml (projectString);
1482 bool result = project.Build ("ShowMessage");
1484 logger.DumpMessages ();
1485 Assert.Fail ("A1: Build failed");
1487 logger.DumpMessages ();
1488 BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1489 Assert.AreEqual (3, include.Count, "A2");
1493 public void TestInitialTargets ()
1495 Engine engine = new Engine (Consts.BinPath);
1496 Project project = engine.CreateNewProject ();
1498 project.LoadXml (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" InitialTargets=""pre "">
1499 <Target Name=""boo"">
1500 <Message Text=""Executing boo target""/>
1502 <Target Name=""pre"">
1503 <Message Text=""Executing pre target""/>
1507 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1508 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1509 engine.RegisterLogger (logger);
1512 Assert.IsTrue (project.Build (), "Build failed");
1514 Assert.AreEqual (0, logger.CheckHead ("Executing pre target", MessageImportance.Normal), "A1");
1515 Assert.AreEqual (0, logger.CheckHead ("Executing boo target", MessageImportance.Normal), "A2");
1516 Assert.AreEqual (0, logger.Count, "A3");
1518 logger.DumpMessages ();
1524 public void TestRequiredTask_String1 ()
1526 CheckProjectForRequiredTests ("RequiredTestTask_String", "@(NonExistant)",
1527 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1531 public void TestRequiredTask_String2 ()
1533 CheckProjectForRequiredTests ("RequiredTestTask_String", "$(NonExistant)",
1534 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1538 public void TestRequiredTask_TaskItem1 ()
1540 Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "@(NonExistant)",
1541 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1545 public void TestRequiredTask_TaskItem2 ()
1547 Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "$(NonExistant)",
1548 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1552 public void TestRequiredTask_TaskItemArray1 ()
1554 Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "@(NonExistant)",
1555 true, "Build failed");
1557 BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1558 Assert.AreEqual (1, group.Count, "A2");
1559 Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1563 public void TestRequiredTask_TaskItemArray2 ()
1565 Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "$(NonExistant)",
1566 true, "Build failed");
1568 BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1569 Assert.AreEqual (1, group.Count, "A2");
1570 Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1574 public void TestRequiredTask_TaskItemArray3 ()
1576 Project p = CheckProjectForRequiredTests ("RequiredTestTask_IntArray", "$(NonExistant)",
1577 true, "Build failed");
1579 BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1580 Assert.AreEqual (1, group.Count, "A2");
1581 Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1584 // full solution test
1586 public void TestBuildSolutionProject ()
1588 string basepath = Path.Combine ("Test", Path.Combine ("resources", "Project01"));
1589 string [] project_dirs = new string [] {
1590 Path.Combine (basepath, "Lib4"),
1591 Path.Combine (basepath, "Lib3"),
1592 Path.Combine (basepath, "Lib2"),
1593 Path.Combine (basepath, "Lib1"),
1594 Path.Combine (basepath, "Project01")
1596 string debug_extn = Consts.RunningOnMono () ? ".dll.mdb" : ".pdb";
1598 // List of expected output files
1600 string [] [] project_files = new string [5][] {
1601 new string [] { "Lib4.dll", "Lib4" + debug_extn },
1602 new string [] { "Lib3.dll" , "Lib3" + debug_extn },
1605 "Lib2.dll", "Lib2" + debug_extn,
1606 "lib2_folder/Lib2.deploy.txt",
1607 Path.Combine ("fr-CA", "Lib2.resources.dll"),
1608 Path.Combine ("fr-FR", "Lib2.resources.dll"),
1609 "Lib4.dll", "Lib4" + debug_extn
1615 "Lib1.dll", "Lib2" + debug_extn,
1617 Path.Combine ("fr-CA", "Lib1.resources.dll"),
1618 Path.Combine ("fr-FR", "Lib1.resources.dll"),
1619 Path.Combine ("en-US", "Lib1.resources.dll"),
1621 "Lib2.dll", "Lib2" + debug_extn,
1622 "lib2_folder/Lib2.deploy.txt",
1623 Path.Combine ("fr-CA", "Lib2.resources.dll"),
1624 Path.Combine ("fr-FR", "Lib2.resources.dll"),
1626 "Lib3.dll", "Lib3" + debug_extn,
1627 "Lib4.dll", "Lib4" + debug_extn
1632 "Project01" + (Consts.RunningOnMono () ? ".exe.mdb" : ".pdb"),
1634 "Lib1.dll", "Lib1" + debug_extn,
1636 Path.Combine ("fr-CA", "Lib1.resources.dll"),
1637 Path.Combine ("fr-FR", "Lib1.resources.dll"),
1638 Path.Combine ("en-US", "Lib1.resources.dll"),
1640 "Lib2.dll", "Lib2" + debug_extn,
1641 "lib2_folder/Lib2.deploy.txt",
1642 Path.Combine ("fr-CA", "Lib2.resources.dll"),
1643 Path.Combine ("fr-FR", "Lib2.resources.dll"),
1644 "Lib4.dll", "Lib4" + debug_extn,
1649 for (int i = 0; i < project_dirs.Length; i ++) {
1650 string bin_path = Path.Combine (project_dirs [i], Path.Combine ("bin", "Debug"));
1651 string obj_path = Path.Combine (project_dirs [i], Path.Combine ("obj", "Debug"));
1653 DeleteAllInDir (bin_path);
1655 DeleteAllInDir (obj_path);
1658 Engine engine = new Engine (Consts.BinPath);
1659 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1660 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1661 engine.RegisterLogger (logger);
1663 engine.GlobalProperties = new BuildPropertyGroup ();
1664 engine.GlobalProperties.SetProperty ("TreatWarningsAsErrors", "false");
1666 Project project = engine.CreateNewProject ();
1667 project.Load (Path.Combine (basepath, "Project01.sln.proj"));
1669 bool result = project.Build ();
1671 logger.DumpMessages ();
1672 Assert.Fail ("Build failed");
1675 // We check only the output dir, not the 'obj'
1676 string debug = Path.Combine ("bin", "Debug");
1677 for (int i = 0; i < project_dirs.Length; i++) {
1678 CheckFilesExistInDir (Path.Combine (project_dirs [i], debug),
1683 void DeleteAllInDir (string path)
1685 if (!Directory.Exists (path))
1688 foreach (string file in Directory.GetFiles (path))
1690 Directory.Delete (path, true);
1693 void CheckFilesExistInDir (string dir, params string [] files)
1695 foreach (string file in files) {
1696 string path = Path.Combine (dir, file);
1697 Assert.IsTrue (File.Exists (path),
1698 String.Format ("Expected to find file {0}", path));
1702 Project CheckProjectForRequiredTests (string taskname, string property_arg, bool expected_result, string error_msg)
1704 string projectString = String.Format (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1705 <UsingTask TaskName=""{0}"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1706 <Target Name=""foo"">
1707 <{0} Property=""{1}"">
1708 <Output TaskParameter=""Output"" ItemName=""OutItem""/>
1711 </Project>", taskname, property_arg);
1713 Engine engine = new Engine (Consts.BinPath);
1714 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1715 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1716 engine.RegisterLogger (logger);
1717 Project project = engine.CreateNewProject ();
1718 project.LoadXml (projectString);
1721 Assert.AreEqual (expected_result, project.Build (), error_msg);
1723 logger.DumpMessages ();
1728 static void CheckBuildItem (BuildItem item, string name, string [,] metadata, string finalItemSpec, string prefix)
1730 Assert.AreEqual (name, item.Name, prefix + "#1");
1731 for (int i = 0; i < metadata.GetLength (0); i++) {
1732 string key = metadata [i, 0];
1733 string val = metadata [i, 1];
1734 Assert.IsTrue (item.HasMetadata (key), String.Format ("{0}#2: Expected metadata '{1}' not found", prefix, key));
1735 Assert.AreEqual (val, item.GetMetadata (key), String.Format ("{0}#3: Value for metadata {1}", prefix, key));
1736 Assert.AreEqual (val, item.GetEvaluatedMetadata (key), String.Format ("{0}#4: Value for evaluated metadata {1}", prefix, key));
1738 Assert.AreEqual (finalItemSpec, item.FinalItemSpec, prefix + "#5");
1741 void CheckProjectBuild (Project project, string [] targetNames, bool result, string [] outputNames, string prefix)
1743 IDictionary targetOutputs = new Hashtable ();
1745 Assert.AreEqual (result, project.Build (targetNames, targetOutputs), prefix + "A1");
1746 Assert.AreEqual (outputNames.Length, targetOutputs.Keys.Count, prefix + "A2");
1748 foreach (string outputName in outputNames) {
1749 Assert.IsTrue (targetOutputs.Contains (outputName), prefix + " A3: target " + outputName);
1751 object o = targetOutputs [outputName];
1752 Assert.IsTrue (typeof (ITaskItem []).IsAssignableFrom (o.GetType ()), prefix + " A4: target " + outputName);
1754 ITaskItem [] items = (ITaskItem [])o;
1755 Assert.AreEqual (0, items.Length, prefix + "A5: target " + outputName);
1759 string CreateProjectString (bool run_separate, string [] targets, bool [] results, string prefix)
1761 StringBuilder sb = new StringBuilder ();
1762 sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">");
1763 sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", "main", prefix);
1765 sb.AppendFormat ("<CallTarget Targets=\"");
1766 for (int i = 0; i < targets.Length; i++)
1767 sb.AppendFormat ("{0};", targets [i]);
1768 sb.AppendFormat ("\" ");
1771 sb.AppendFormat (" RunEachTargetSeparately=\"true\" ");
1772 sb.AppendFormat ("/></Target>\n");
1774 for (int i = 0; i < targets.Length; i++) {
1775 sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", targets [i], prefix);
1777 sb.AppendFormat ("<Error Text = \"#Error message for target {0}:{1}\"/>", prefix, targets [i]);
1778 sb.Append ("</Target>\n");
1781 sb.Append ("</Project>");
1783 return sb.ToString ();
1786 void CreateProjectFile (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1788 using (StreamWriter sw = new StreamWriter (fname))
1789 sw.Write (CreateProjectString (run_separate, targets, results, prefix));
1792 Project CreateAndLoadProject (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1794 Engine engine = new Engine (Consts.BinPath);
1795 Project project = engine.CreateNewProject ();
1797 string projectXml = CreateProjectString (run_separate, targets, results, prefix);
1798 if (fname == null) {
1799 project.LoadXml (projectXml);
1801 using (StreamWriter sw = new StreamWriter (fname))
1802 sw.Write (projectXml);
1803 project.Load (fname);
1804 File.Delete (fname);