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;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
36 namespace MonoTests.Microsoft.Build.BuildEngine {
38 class TestLogger : Logger {
39 int target_started_events = 0;
40 int target_finished_events = 0;
42 public override void Initialize (IEventSource eventSource)
44 eventSource.TargetStarted += new TargetStartedEventHandler(TargetStarted);
45 eventSource.TargetFinished += new TargetFinishedEventHandler(TargetFinished);
46 eventSource.MessageRaised += new BuildMessageEventHandler(Message);
47 eventSource.WarningRaised += new BuildWarningEventHandler(Warning);
50 void TargetStarted (object sender, TargetStartedEventArgs args)
52 target_started_events++;
55 void TargetFinished (object sender, TargetFinishedEventArgs args)
57 target_finished_events++;
60 void Message (object sender, BuildMessageEventArgs args)
64 void Warning (object sender, BuildWarningEventArgs args)
68 public int TargetStartedEvents { get { return target_started_events; } }
70 public int TargetFinishedEvents { get { return target_finished_events; } }
74 public class ProjectTest {
77 [ExpectedException (typeof (InvalidProjectFileException),
78 @"The default XML namespace of the project must be the MSBuild XML namespace." +
79 " If the project is authored in the MSBuild 2003 format, please add " +
80 "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" to the <Project> element. " +
81 "If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format. ")]
82 public void TestAssignment1 ()
86 string documentString =
87 "<Project></Project>";
89 engine = new Engine (Consts.BinPath);
90 DateTime time = DateTime.Now;
91 project = engine.CreateNewProject ();
92 project.LoadXml (documentString);
94 Assert.AreEqual (true, project.BuildEnabled, "A1");
95 Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
96 Assert.AreEqual (String.Empty, project.FullFileName, "A3");
97 Assert.AreEqual (false, project.IsDirty, "A4");
98 Assert.AreEqual (false, project.IsValidated, "A5");
99 Assert.AreEqual (engine, project.ParentEngine, "A6");
100 Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
101 Assert.IsTrue (String.Empty != project.Xml, "A8");
105 public void TestAssignment2 ()
109 string documentString =
110 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>";
112 engine = new Engine (Consts.BinPath);
113 DateTime time = DateTime.Now;
114 project = engine.CreateNewProject ();
115 project.LoadXml (documentString);
117 Assert.AreEqual (true, project.BuildEnabled, "A1");
118 Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
119 Assert.AreEqual (String.Empty, project.FullFileName, "A3");
120 Assert.AreEqual (true, project.IsDirty, "A4");
121 Assert.AreEqual (false, project.IsValidated, "A5");
122 Assert.AreEqual (engine, project.ParentEngine, "A6");
123 Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
124 Assert.IsTrue (String.Empty != project.Xml, "A8");
128 [Category ("NotWorking")]
129 public void TestAddNewItemGroup ()
134 string documentString = @"
135 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
139 engine = new Engine (Consts.BinPath);
140 project = engine.CreateNewProject ();
141 project.LoadXml (documentString);
143 BuildItemGroup big = project.AddNewItemGroup ();
144 Assert.IsNotNull (big, "A1");
145 Assert.AreEqual (String.Empty, big.Condition, "A2");
146 Assert.AreEqual (0, big.Count, "A3");
147 Assert.AreEqual (false, big.IsImported, "A4");
148 Assert.IsTrue (project.IsDirty, "A5");
152 [Category ("NotWorking")]
153 public void TestAddNewPropertyGroup ()
158 string documentString = @"
159 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
163 engine = new Engine (Consts.BinPath);
164 project = engine.CreateNewProject ();
165 project.LoadXml (documentString);
167 BuildPropertyGroup bpg = project.AddNewPropertyGroup (false);
168 Assert.IsNotNull (bpg, "A1");
169 Assert.AreEqual (String.Empty, bpg.Condition, "A2");
170 Assert.AreEqual (0, bpg.Count, "A3");
171 Assert.AreEqual (false, bpg.IsImported, "A4");
172 Assert.IsTrue (project.IsDirty, "A5");
176 [Category ("NotWorking")]
177 public void TestBuild1 ()
181 IDictionary hashtable = new Hashtable ();
183 string documentString = @"
184 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
186 <Microsoft.Build.Tasks.Message Text='Text' />
191 engine = new Engine (Consts.BinPath);
192 project = engine.CreateNewProject ();
193 project.LoadXml (documentString);
195 Assert.AreEqual (true, project.Build (new string[] { "Main" }, hashtable), "A1");
196 Assert.AreEqual (1, hashtable.Count, "A2");
200 public void TestBuild2 ()
205 string documentString = @"
206 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
207 <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
208 <Message Text='text' />
213 engine = new Engine (Consts.BinPath);
214 TestLogger tl = new TestLogger ();
215 engine.RegisterLogger (tl);
216 project = engine.CreateNewProject ();
217 project.LoadXml (documentString);
222 Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
223 Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
227 public void TestBuild3 ()
232 string documentString = @"
233 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
234 <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
235 <Message Text='text' />
240 engine = new Engine (Consts.BinPath);
241 TestLogger tl = new TestLogger ();
242 engine.RegisterLogger (tl);
243 project = engine.CreateNewProject ();
244 project.LoadXml (documentString);
246 project.Build (new string [1] { "T" }, null, BuildSettings.None);
247 project.Build (new string [1] { "T" }, null, BuildSettings.None);
249 Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
250 Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
254 [Category ("NotWorking")]
255 public void TestBuild4 ()
260 string documentString = @"
261 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
262 <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
263 <Message Text='text' />
268 engine = new Engine (Consts.BinPath);
269 TestLogger tl = new TestLogger ();
270 engine.RegisterLogger (tl);
271 project = engine.CreateNewProject ();
272 project.LoadXml (documentString);
274 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
275 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
277 Assert.AreEqual (1, tl.TargetStartedEvents, "A1");
278 Assert.AreEqual (1, tl.TargetFinishedEvents, "A2");
282 public void TestBuild5 ()
287 string documentString = @"
288 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
292 engine = new Engine (Consts.BinPath);
293 project = engine.CreateNewProject ();
294 project.LoadXml (documentString);
296 Assert.IsFalse (project.Build ("target_that_doesnt_exist"));
300 public void TestEvaluatedItems1 ()
305 string documentString = @"
306 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
309 <B Include='b' Condition='false' />
314 engine = new Engine (Consts.BinPath);
315 project = engine.CreateNewProject ();
316 project.LoadXml (documentString);
318 Assert.AreEqual (1, project.EvaluatedItems.Count, "A1");
320 BuildItem bi = project.EvaluatedItems [0];
325 BuildItemGroup [] big = new BuildItemGroup [1];
326 project.ItemGroups.CopyTo (big, 0);
327 Assert.AreEqual ("C", big [0] [0].Name, "A2");
328 Assert.AreEqual ("c", big [0] [0].Include, "A3");
332 public void TestEvaluatedItems2 ()
337 string documentString = @"
338 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
340 <A Include='a;b;c' />
345 engine = new Engine (Consts.BinPath);
346 project = engine.CreateNewProject ();
347 project.LoadXml (documentString);
349 BuildItemGroup [] big = new BuildItemGroup [1];
350 project.ItemGroups.CopyTo (big, 0);
352 Assert.AreEqual (3, project.EvaluatedItems.Count, "A1");
353 Assert.AreEqual ("a;b;c", big [0] [0].Include, "A2");
354 Assert.AreEqual (1, big [0].Count, "A3");
356 BuildItem bi = project.EvaluatedItems [0];
360 Assert.AreEqual (3, big [0].Count, "A4");
361 Assert.AreEqual ("d", big [0] [0].Include, "A5");
362 Assert.AreEqual ("b", big [0] [1].Include, "A6");
363 Assert.AreEqual ("c", big [0] [2].Include, "A7");
367 [Category ("NotWorking")]
368 public void TestGetConditionedPropertyValues ()
373 string documentString = @"
374 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
375 <PropertyGroup Condition='true'>
377 <B Condition='true'>A</B>
380 <C Condition='true'>A</C>
381 <C Condition='false'>B</C>
382 <C Condition='!false'>C</C>
384 <E Condition="" '$(C)' == 'A' "">E</E>
389 engine = new Engine (Consts.BinPath);
390 project = engine.CreateNewProject ();
391 project.LoadXml (documentString);
393 Assert.AreEqual (0, project.GetConditionedPropertyValues ("A").Length, "A1");
394 Assert.AreEqual (0, project.GetConditionedPropertyValues ("B").Length, "A2");
395 Assert.AreEqual (1, project.GetConditionedPropertyValues ("C").Length, "A3");
396 Assert.AreEqual (0, project.GetConditionedPropertyValues ("D").Length, "A4");
397 Assert.AreEqual (0, project.GetConditionedPropertyValues ("E").Length, "A5");
398 Assert.AreEqual ("A", project.GetConditionedPropertyValues ("C") [0], "A6");
402 [ExpectedException (typeof (ArgumentNullException))]
403 public void TestGetEvaluatedItemsByName1 ()
408 string documentString = @"
409 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
413 engine = new Engine (Consts.BinPath);
414 project = engine.CreateNewProject ();
415 project.LoadXml (documentString);
417 project.GetEvaluatedItemsByName (null);
421 public void TestGetEvaluatedItemsByName2 ()
426 string documentString = @"
427 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
430 <B Include='2' Condition='true' />
431 <C Include='3' Condition='false' />
436 engine = new Engine (Consts.BinPath);
437 project = engine.CreateNewProject ();
438 project.LoadXml (documentString);
442 big = project.GetEvaluatedItemsByName (String.Empty);
444 Assert.AreEqual (0, big.Count, "A1");
446 big = project.GetEvaluatedItemsByName ("A");
448 Assert.AreEqual (1, big.Count, "A2");
449 Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
451 big = project.GetEvaluatedItemsByName ("B");
453 Assert.AreEqual (1, big.Count, "A4");
454 Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
456 big = project.GetEvaluatedItemsByName ("C");
458 Assert.AreEqual (0, big.Count, "A6");
462 [ExpectedException (typeof (ArgumentNullException))]
463 public void TestGetEvaluatedItemsByNameIgnoringCondition1 ()
468 string documentString = @"
469 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
473 engine = new Engine (Consts.BinPath);
474 project = engine.CreateNewProject ();
475 project.LoadXml (documentString);
477 project.GetEvaluatedItemsByNameIgnoringCondition (null);
481 public void TestGetEvaluatedItemsByNameIgnoringCondition2 ()
486 string documentString = @"
487 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
490 <B Include='2' Condition='true' />
491 <C Include='3' Condition='false' />
496 engine = new Engine (Consts.BinPath);
497 project = engine.CreateNewProject ();
498 project.LoadXml (documentString);
502 big = project.GetEvaluatedItemsByNameIgnoringCondition (String.Empty);
504 Assert.AreEqual (0, big.Count, "A1");
506 big = project.GetEvaluatedItemsByNameIgnoringCondition ("A");
508 Assert.AreEqual (1, big.Count, "A2");
509 Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
511 big = project.GetEvaluatedItemsByNameIgnoringCondition ("B");
513 Assert.AreEqual (1, big.Count, "A4");
514 Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
516 big = project.GetEvaluatedItemsByNameIgnoringCondition ("C");
518 Assert.AreEqual (1, big.Count, "A6");
519 Assert.AreEqual ("3", big [0].FinalItemSpec, "A7");
523 [ExpectedException (typeof (ArgumentNullException))]
524 public void TestGetEvaluatedProperty1 ()
529 string documentString = @"
530 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
534 engine = new Engine (Consts.BinPath);
535 project = engine.CreateNewProject ();
536 project.LoadXml (documentString);
538 project.GetEvaluatedProperty (null);
541 public void TestGetEvaluatedProperty2 ()
546 string documentString = @"
547 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
550 <B Condition='true'>2</B>
551 <C Condition='false'>3</C>
556 engine = new Engine (Consts.BinPath);
557 project = engine.CreateNewProject ();
558 project.LoadXml (documentString);
560 Assert.AreEqual ("1", project.GetEvaluatedProperty ("A"), "A1");
561 Assert.AreEqual ("2", project.GetEvaluatedProperty ("B"), "A2");
562 Assert.IsNull (project.GetEvaluatedProperty ("C"), "A3");
566 public void TestGetProjectExtensions ()
571 string documentString = @"
572 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
579 engine = new Engine (Consts.BinPath);
580 project = engine.CreateNewProject ();
581 project.LoadXml (documentString);
583 Assert.AreEqual (String.Empty, project.GetProjectExtensions (null), "A1");
584 Assert.AreEqual (String.Empty, project.GetProjectExtensions (String.Empty), "A2");
585 Assert.AreEqual (String.Empty, project.GetProjectExtensions ("something"), "A3");
586 Assert.AreEqual ("Text", project.GetProjectExtensions ("Node"), "A4");
590 public void TestGlobalProperties1 ()
595 string documentString = @"
596 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
600 engine = new Engine (Consts.BinPath);
601 project = engine.CreateNewProject ();
602 project.LoadXml (documentString);
604 Assert.AreEqual (0, project.GlobalProperties.Count, "A1");
608 public void TestGlobalProperties2 ()
613 string documentString = @"
614 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
618 engine = new Engine (Consts.BinPath);
619 engine.GlobalProperties.SetProperty ("Property", "Value");
621 project = engine.CreateNewProject ();
622 project.LoadXml (documentString);
624 Assert.AreEqual (1, project.GlobalProperties.Count, "A1");
625 Assert.AreEqual ("Property", project.GlobalProperties ["Property"].Name, "A2");
626 Assert.AreEqual ("Value", project.GlobalProperties ["Property"].Value, "A3");
627 Assert.AreEqual ("Value", project.GlobalProperties ["Property"].FinalValue, "A4");
628 Assert.AreEqual ("Property", project.EvaluatedProperties ["Property"].Name, "A2");
629 Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].Value, "A3");
630 Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].FinalValue, "A4");
634 [Ignore ("NullRefException under MS .NET 2.0")]
635 public void TestGlobalProperties3 ()
640 string documentString = @"
641 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
645 engine = new Engine (Consts.BinPath);
646 project = engine.CreateNewProject ();
647 project.LoadXml (documentString);
649 project.GlobalProperties = null;
653 [Ignore ("NullRefException under MS .NET 2.0")]
654 public void TestGlobalProperties4 ()
659 string documentString = @"
660 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
662 <Property>a</Property>
667 engine = new Engine (Consts.BinPath);
668 project = engine.CreateNewProject ();
669 project.LoadXml (documentString);
671 BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
672 project.PropertyGroups.CopyTo (groups, 0);
674 project.GlobalProperties = groups [0];
675 project.GlobalProperties = project.EvaluatedProperties;
679 [Category ("NotWorking")]
680 public void TestGlobalProperties5 ()
685 string documentString = @"
686 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
688 <Property>a</Property>
693 engine = new Engine (Consts.BinPath);
694 project = engine.CreateNewProject ();
695 project.LoadXml (documentString);
697 BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
698 project.PropertyGroups.CopyTo (groups, 0);
699 project.GlobalProperties = groups [0];
703 [ExpectedException (typeof (InvalidProjectFileException))]
704 public void TestLoad1 ()
709 string documentString = @"
710 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
715 engine = new Engine (Consts.BinPath);
716 project = engine.CreateNewProject ();
717 project.LoadXml (documentString);
721 [ExpectedException (typeof (InvalidProjectFileException))]
722 public void TestLoad2 ()
727 engine = new Engine (Consts.BinPath);
728 project = engine.CreateNewProject ();
729 project.LoadXml ("project_file_that_doesnt_exist");
733 public void TestParentEngine ()
738 engine = new Engine (Consts.BinPath);
739 project = engine.CreateNewProject ();
741 Assert.AreEqual (engine, project.ParentEngine, "A1");
745 [Category ("NotWorking")]
746 public void TestResetBuildStatus ()
751 string documentString = @"
752 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
753 <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
754 <Message Text='text' />
759 engine = new Engine (Consts.BinPath);
760 TestLogger tl = new TestLogger ();
761 engine.RegisterLogger (tl);
762 project = engine.CreateNewProject ();
763 project.LoadXml (documentString);
766 project.ResetBuildStatus ();
767 project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
769 Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
770 Assert.AreEqual (2, tl.TargetFinishedEvents, "A1");
774 public void TestSchemaFile ()
779 string documentString = @"
780 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
784 engine = new Engine (Consts.BinPath);
785 project = engine.CreateNewProject ();
786 project.LoadXml (documentString);
788 Assert.IsNull (project.SchemaFile, "A1");
791 [Ignore ("NRE on .NET 2.0")]
792 public void TestSetProjectExtensions1 ()
797 string documentString = @"
798 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
802 engine = new Engine (Consts.BinPath);
803 project = engine.CreateNewProject ();
804 project.LoadXml (documentString);
806 project.SetProjectExtensions (null, null);
810 public void TestSetProjectExtensions2 ()
815 string documentString = @"
816 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
820 engine = new Engine (Consts.BinPath);
821 project = engine.CreateNewProject ();
822 project.LoadXml (documentString);
824 project.SetProjectExtensions ("name", "1");
825 Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
826 project.SetProjectExtensions ("name", "2");
827 Assert.AreEqual ("2", project.GetProjectExtensions ("name"), "A2");
828 Assert.IsTrue (project.IsDirty, "A3");
832 public void TestSetProjectExtensions3 ()
837 string documentString = @"
838 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
844 engine = new Engine (Consts.BinPath);
845 project = engine.CreateNewProject ();
846 project.LoadXml (documentString);
848 project.SetProjectExtensions ("name", "1");
849 Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
850 Assert.IsTrue (project.IsDirty, "A2");