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 Microsoft.Build.BuildEngine;
30 using Microsoft.Build.Framework;
31 using Microsoft.Build.Utilities;
32 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.BuildEngine {
37 class CheckUnregisterLogger : Logger {
38 bool anything = false;
40 public override void Initialize (IEventSource eventSource)
42 eventSource.AnyEventRaised += delegate { anything = true; };
43 eventSource.BuildFinished += delegate { anything = true; };
44 eventSource.BuildStarted += delegate { anything = true; };
45 eventSource.CustomEventRaised += delegate { anything = true; };
46 eventSource.ErrorRaised += delegate { anything = true; };
47 eventSource.MessageRaised += delegate { anything = true; };
48 eventSource.ProjectFinished += delegate { anything = true; };
49 eventSource.ProjectStarted += delegate { anything = true; };
50 eventSource.StatusEventRaised += delegate { anything = true; };
51 eventSource.TargetFinished += delegate { anything = true; };
52 eventSource.TargetStarted += delegate { anything = true; };
53 eventSource.TaskFinished += delegate { anything = true; };
54 eventSource.TaskStarted += delegate { anything = true; };
55 eventSource.WarningRaised += delegate { anything = true; };
58 public bool Anything { get { return anything; } }
62 public class EngineTest {
67 static string GetPropValue (BuildPropertyGroup bpg, string name)
69 foreach (BuildProperty bp in bpg) {
70 if (bp.Name == name) {
80 secondProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
81 <PropertyGroup Condition=""'$(foo)' == 'hello'"">
84 <Target Name=""TargetA"">
85 <Message Text=""(TargetA) foo: $(foo) A: $(A) External: $(External)""/>
88 <Target Name=""TargetB"">
89 <Message Text=""(TargetB) foo: $(foo) A: $(A) External: $(External)""/>
96 public void TestCtor ()
98 engine = new Engine (Consts.BinPath);
101 // Before a project can be instantiated, Engine.BinPath must be set to the location on disk where MSBuild is installed.
102 // This is used to evaluate $(MSBuildBinPath).
103 /* This isn't valid for 3.5
106 [ExpectedException (typeof (InvalidOperationException))]
107 public void TestNewProject ()
109 engine = new Engine ();
111 engine.CreateNewProject ();
115 public void TestBinPath ()
117 engine = new Engine (Consts.BinPath);
119 Assert.AreEqual (Consts.BinPath, engine.BinPath, "A1");
123 public void TestBuildEnabled ()
125 engine = new Engine (Consts.BinPath);
127 Assert.AreEqual (true, engine.BuildEnabled, "A1");
131 public void TestOnlyLogCriticalEvents ()
133 engine = new Engine (Consts.BinPath);
135 Assert.AreEqual (false, engine.OnlyLogCriticalEvents, "A1");
139 public void TestGlobalProperties ()
141 engine = new Engine (Consts.BinPath);
144 Assert.IsNotNull (engine.GlobalProperties, "A1");
145 Assert.AreEqual (0, engine.GlobalProperties.Count, "A2");
146 Assert.AreEqual (String.Empty, engine.GlobalProperties.Condition, "A3");
147 Assert.IsFalse (engine.GlobalProperties.IsImported, "A4");
149 engine.GlobalProperties.SetProperty ("GlobalA", "value1");
150 Assert.AreEqual (1, engine.GlobalProperties.Count, "A5");
151 engine.GlobalProperties.SetProperty ("GlobalB", "value1");
152 Assert.AreEqual (2, engine.GlobalProperties.Count, "A6");
153 engine.GlobalProperties.SetProperty ("GlobalA", "value2");
154 Assert.AreEqual (2, engine.GlobalProperties.Count, "A7");
156 project = engine.CreateNewProject ();
157 Assert.AreEqual (2, project.GlobalProperties.Count, "A8");
158 project.GlobalProperties.SetProperty ("GlobalC", "value3");
159 Assert.AreEqual (3, project.GlobalProperties.Count, "A9");
160 Assert.AreEqual (2, engine.GlobalProperties.Count, "A10");
162 project.GlobalProperties.SetProperty ("GlobalA", "value3");
163 Assert.AreEqual ("value2", GetPropValue(engine.GlobalProperties, "GlobalA"), "A11");
164 engine.GlobalProperties.SetProperty ("GlobalB", "value3");
165 Assert.AreEqual ("value1", GetPropValue(project.GlobalProperties, "GlobalB"), "A12");
167 engine.GlobalProperties.SetProperty ("GlobalC", "value4");
168 engine.GlobalProperties.SetProperty ("GlobalD", "value5");
169 Assert.AreEqual (4, engine.GlobalProperties.Count, "A13");
170 Assert.AreEqual (3, project.GlobalProperties.Count, "A14");
172 project = new Project (engine);
173 Assert.AreEqual (4, project.GlobalProperties.Count, "A15");
177 public void TestGlobalEngine ()
179 engine = new Engine ();
180 Assert.IsFalse (engine == Engine.GlobalEngine, "1");
181 Assert.IsNotNull (Engine.GlobalEngine, "2");
182 engine = Engine.GlobalEngine;
183 Assert.AreSame (engine, Engine.GlobalEngine, "3");
187 [ExpectedException (typeof (ArgumentNullException))]
188 [Category ("NotDotNet")]
189 public void TestRegisterLogger ()
191 engine = new Engine (Consts.BinPath);
192 engine.RegisterLogger (null);
195 // The "Project" object specified does not belong to the correct "Engine" object.
197 [ExpectedException (typeof (InvalidOperationException))]
198 public void TestUnloadProject1 ()
200 Engine a = new Engine (Consts.BinPath);
201 Engine b = new Engine (Consts.BinPath);
203 Project p = a.CreateNewProject ();
209 [ExpectedException (typeof (ArgumentNullException))]
210 [Category ("NotDotNet")]
211 public void TestUnloadProject2 ()
213 Engine a = new Engine (Consts.BinPath);
215 a.UnloadProject (null);
218 // This project object has been unloaded from the MSBuild engine and is no longer valid.
220 [ExpectedException (typeof (InvalidOperationException))]
221 public void TestUnloadProject3 ()
223 Engine a = new Engine (Consts.BinPath);
224 Project p = a.CreateNewProject ();
231 [Category ("NotWorking")]
232 public void TestUnregisterAllLoggers ()
234 engine = new Engine (Consts.BinPath);
235 CheckUnregisterLogger cul = new CheckUnregisterLogger ();
236 engine.RegisterLogger (cul);
238 engine.UnregisterAllLoggers ();
240 Assert.IsFalse (cul.Anything, "A1");
244 public void TestBuildError1 ()
246 engine = new Engine (Consts.BinPath);
247 Project project = engine.CreateNewProject ();
249 Assert.IsFalse (project.Build (), "A1");
250 Assert.IsFalse (project.Build ((string)null), "A2");
251 Assert.IsFalse (project.Build ((string [])null), "A3");
252 Assert.IsFalse (project.Build (new string [0]), "A4");
253 Assert.IsFalse (project.Build (null, null), "A5");
254 Assert.IsFalse (project.Build (null, null, BuildSettings.None), "A6");
255 //FIXME: Add test for Build (null, non-null-target)
259 public void TestBuildProjectFile1 ()
261 engine = new Engine (Consts.BinPath);
262 Project project = engine.CreateNewProject ();
263 project.LoadXml (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
265 <Message Text='Target 1 called'/>
269 Assert.IsTrue (project.Build ((string)null), "A1");
270 Assert.IsTrue (project.Build ((string [])null), "A2");
271 Assert.IsTrue (project.Build (new string [0]), "A3");
272 Assert.IsTrue (project.Build (null, null), "A4");
273 Assert.IsTrue (project.Build (null, null, BuildSettings.None), "A5");
274 //FIXME: Add test for Build (null, non-null-target)
278 [ExpectedException (typeof (ArgumentException))]
279 public void TestBuildProject1 ()
281 engine = new Engine (Consts.BinPath);
282 engine.BuildProject (null);
286 public void TestBuildProject2 ()
288 engine = new Engine (Consts.BinPath);
289 Project project = engine.CreateNewProject ();
291 Assert.IsFalse (engine.BuildProject (project, (string)null), "#A1");
292 Assert.IsFalse (engine.BuildProject (project, (string [])null), "#A2");
293 Assert.IsFalse (engine.BuildProject (project, (string [])null, null), "#A3");
294 Assert.IsFalse (engine.BuildProject (project, (string [])null, null, BuildSettings.None), "#A4");
296 bool caught_exception = false;
298 //null string in targetNames [] param
299 engine.BuildProject (project, new string [] {null}, null);
301 caught_exception = true;
303 if (!caught_exception)
304 Assert.Fail ("Expected exception for Engine.BuildProject");
308 [ExpectedException (typeof (ArgumentException))]
309 public void TestBuildProjectNull1 ()
311 engine = new Engine (Consts.BinPath);
312 engine.BuildProject (null, "foo");
316 [ExpectedException (typeof (ArgumentException))]
317 public void TestBuildProjectNull2 ()
319 engine = new Engine (Consts.BinPath);
320 engine.BuildProject (null, (string)null);
323 // Tests to check global properties behavior
325 public void TestGlobalProperties1 ()
327 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
328 + GetUsingTask ("MSBuild")
330 <Target Name=""main"">
331 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
332 <Message Text=""second""/>
333 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
337 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
338 + GetUsingTask ("MSBuild")
340 <Target Name = ""1"">
341 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
342 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
345 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
346 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
347 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
348 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
353 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
356 "(TargetA) foo: bar A: External: ",
357 "(TargetB) foo: foofoo A: External: ",
358 "(TargetA) foo: A: External: ",
359 "(TargetB) foo: foofoo1 A: External: ",
364 public void TestGlobalProperties1a ()
366 Directory.CreateDirectory ("Test/resources/foo");
367 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
368 + GetUsingTask ("MSBuild")
370 <Target Name=""main"">
371 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
372 <Message Text=""second""/>
373 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
377 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
378 + GetUsingTask ("MSBuild")
380 <Target Name = ""1"">
381 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
382 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
385 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
386 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
387 <MSBuild Projects=""second.proj"" Targets = ""TargetA"" Properties=""foo=bar""/>
388 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
389 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
393 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
396 "(TargetA) foo: bar A: External: ",
397 "(TargetB) foo: foofoo A: External: ",
398 "(TargetA) foo: A: External: ",
399 "(TargetB) foo: foofoo1 A: External: ",
404 public void TestGlobalProperties1b ()
406 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
407 + GetUsingTask ("MSBuild")
409 <Target Name=""main"">
410 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
411 <Message Text=""second""/>
412 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
416 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
417 + GetUsingTask ("MSBuild")
419 <Target Name = ""1"">
420 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
421 <MSBuild Projects=""second.proj""/>
422 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
425 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
426 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
427 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
428 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
432 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
435 "(TargetA) foo: bar A: External: ",
436 "(TargetA) foo: A: External: ",
437 "(TargetB) foo: foofoo A: External: ",
438 "(TargetB) foo: foofoo1 A: External: ",
443 public void TestGlobalProperties2 ()
445 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
446 + GetUsingTask ("MSBuild")
448 <Target Name=""main"">
449 <MSBuild Projects=""first.proj"" Targets = ""1""/>
450 <MSBuild Projects=""first.proj"" Targets = ""2""/>
451 <Message Text=""second""/>
452 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
456 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
457 + GetUsingTask ("MSBuild")
459 <Target Name = ""1"">
460 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
461 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
464 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
465 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
466 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
467 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
472 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
475 "(TargetA) foo: bar A: External: ",
476 "(TargetB) foo: foofoo A: External: ",
477 "(TargetA) foo: A: External: ",
478 "(TargetB) foo: foofoo1 A: External: ",
483 public void TestGlobalProperties3 ()
485 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
486 + GetUsingTask ("MSBuild")
488 <Target Name=""main"">
489 <MSBuild Projects=""first.proj"" Targets = ""1""/>
490 <CallTarget Targets=""Call2""/>
491 <Message Text=""second""/>
492 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
494 <Target Name=""Call2"">
495 <MSBuild Projects=""first.proj"" Targets = ""2""/>
499 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
500 + GetUsingTask ("MSBuild")
502 <Target Name = ""1"">
503 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
504 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
507 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
508 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
509 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
510 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
515 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
518 "(TargetA) foo: bar A: External: ",
519 "(TargetB) foo: foofoo A: External: ",
520 "(TargetA) foo: A: External: ",
521 "(TargetB) foo: foofoo1 A: External: ",
525 //externally set global properties
527 public void TestGlobalProperties4 ()
529 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
530 + GetUsingTask ("MSBuild")
532 <Target Name=""main"">
533 <MSBuild Projects=""first.proj"" Targets = ""1""/>
534 <CallTarget Targets=""Call2""/>
535 <Message Text=""second""/>
536 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
538 <Target Name=""Call2"">
539 <MSBuild Projects=""first.proj"" Targets = ""2""/>
543 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
544 + GetUsingTask ("MSBuild")
546 <Target Name = ""1"">
547 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
548 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
551 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
552 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
553 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
554 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
559 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
560 globalprops.SetProperty ("foo", "hello");
561 engine.GlobalProperties = globalprops;
563 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
564 globalprops, null, 10, 8, 15,
566 "(TargetA) foo: bar A: External: ",
567 "(TargetB) foo: foofoo A: External: ",
568 "(TargetA) foo: hello A: FooWasHello External: ",
569 "(TargetB) foo: foofoo1 A: External: ",
573 //externally set global properties, merge with explicit
575 public void TestGlobalProperties4a ()
577 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
578 + GetUsingTask ("MSBuild")
580 <Target Name=""main"">
581 <MSBuild Projects=""first.proj"" Targets = ""1""/>
582 <CallTarget Targets=""Call2""/>
583 <Message Text=""second""/>
584 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
586 <Target Name=""Call2"">
587 <MSBuild Projects=""first.proj"" Targets = ""2""/>
591 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
592 + GetUsingTask ("MSBuild")
594 <Target Name = ""1"">
595 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
596 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
599 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
600 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
601 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
602 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
607 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
608 globalprops.SetProperty ("external", "ExternalValue");
610 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
614 "(TargetA) foo: bar A: External: ExternalValue",
615 "(TargetB) foo: foofoo A: External: ExternalValue",
616 "(TargetA) foo: A: External: ExternalValue",
617 "(TargetB) foo: foofoo1 A: External: ExternalValue",
621 //set global properties on _project_, merge with explicit
623 public void TestGlobalProperties4b ()
625 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
626 + GetUsingTask ("MSBuild")
628 <Target Name=""main"">
629 <MSBuild Projects=""first.proj"" Targets = ""1""/>
630 <CallTarget Targets=""Call2""/>
631 <Message Text=""second""/>
632 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
634 <Target Name=""Call2"">
635 <MSBuild Projects=""first.proj"" Targets = ""2""/>
639 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
640 + GetUsingTask ("MSBuild")
642 <Target Name = ""1"">
643 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
644 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
647 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
648 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
649 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
650 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
655 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
656 globalprops.SetProperty ("external", "ExternalValue");
658 BuildPropertyGroup project_globalprops = new BuildPropertyGroup ();
659 project_globalprops.SetProperty ("external", "ProjExternalValue");
660 project_globalprops.SetProperty ("foo", "ProjFooValue");
662 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
663 globalprops, project_globalprops,
666 "(TargetA) foo: bar A: External: ProjExternalValue",
667 "(TargetB) foo: foofoo A: External: ProjExternalValue",
668 "(TargetA) foo: ProjFooValue A: External: ProjExternalValue",
669 "(TargetB) foo: foofoo1 A: External: ProjExternalValue",
673 //set global properties on _project_, and engine and explicit via msbuild
675 public void TestGlobalProperties4c ()
677 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
678 + GetUsingTask ("MSBuild")
680 <Target Name=""main"">
681 <MSBuild Projects=""first.proj"" Targets = ""1""/>
682 <CallTarget Targets=""Call2""/>
683 <Message Text=""second""/>
684 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
686 <Target Name=""Call2"">
687 <MSBuild Projects=""first.proj"" Targets = ""2""/>
691 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
692 + GetUsingTask ("MSBuild")
694 <Target Name = ""1"">
695 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
696 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
699 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
700 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
701 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
702 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
707 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
708 globalprops.SetProperty ("foo", "EngineFooValue");
710 BuildPropertyGroup project_globalprops = new BuildPropertyGroup ();
711 project_globalprops.SetProperty ("foo", "ProjFooValue");
713 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
714 globalprops, project_globalprops,
717 "(TargetA) foo: bar A: External: ",
718 "(TargetB) foo: foofoo A: External: ",
719 "(TargetA) foo: ProjFooValue A: External: ",
720 "(TargetB) foo: foofoo1 A: External: ",
724 // Check for global properties in case of Import
727 public void TestGlobalPropertiesImport1 ()
729 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
730 <Target Name=""main"">
731 <MSBuild Projects=""first.proj"" Targets = ""1"" Properties='Prop=test'/>
735 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
736 <Target Name = ""1"">
737 <Message Text='Prop: $(Prop)'/>
739 <Import Project='$(Prop).proj'/>
742 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
746 public void TestGlobalPropertiesImport2 ()
748 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
749 <Target Name=""main"">
750 <MSBuild Projects=""first.proj"" Targets = ""1"" Properties='Prop=test'/>
754 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
758 <Target Name = ""1"">
759 <Message Text='Prop: $(Prop)'/>
761 <Import Project='$(Prop).proj'/>
764 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
768 public void TestGlobalPropertiesImport3()
770 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
771 <Target Name=""main"">
772 <MSBuild Projects=""first.proj"" Targets = ""1""/>
776 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
780 <Target Name = ""1"">
781 <Message Text='Prop: $(Prop)'/>
783 <Import Project='$(Prop).proj'/>
786 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
790 public void TestMSBuildOutputs ()
792 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
793 + GetUsingTask ("MSBuild")
796 <ProjectRef Include=""first.proj"">
798 <Unique>true</Unique>
800 <ProjectRef Include=""first.proj"">
801 <Prop3>value2</Prop3>
802 <Unique>false</Unique>
805 <ProjectRef Include=""second.proj"">
806 <Prop3>value3</Prop3>
807 <Unique>unique</Unique>
813 <MSBuild Projects=""@(ProjectRef)"" Targets=""GetData"">
814 <Output TaskParameter=""TargetOutputs"" ItemName=""F""/>
816 <Message Text=""@(F): F.Unique: %(F.Unique)""/>
817 <Message Text=""@(F): F.Prop1: %(F.Prop1)""/>
818 <Message Text=""@(F): F.Prop2: %(F.Prop2)""/>
819 <Message Text=""@(F): F.Prop3: %(F.Prop3)""/>
823 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
825 <A Include=""foofoo"">
828 <Prop3>foo value</Prop3>
831 <A Include=""barbar"">
832 <Prop1>bar_false</Prop1>
833 <Prop2>bar_false</Prop2>
834 <Prop3>bar value</Prop3>
839 <Target Name=""GetData"" Outputs=""@(AbcOutputs)"">
840 <CreateItem Include=""@(A)"">
841 <Output TaskParameter=""Include"" ItemName=""AbcOutputs""/>
846 string secondProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
848 <A Include=""from_second"">
851 <Prop3>new value</Prop3>
855 <Target Name=""GetData"" Outputs=""@(AbcOutputs)"">
856 <CreateItem Include=""@(A)"">
857 <Output TaskParameter=""Include"" ItemName=""AbcOutputs""/>
863 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
867 "foofoo;barbar: F.Unique: true",
868 "foofoo;barbar: F.Unique: false",
869 "from_second: F.Unique: unique",
870 "foofoo;foofoo;from_second: F.Prop1: false",
871 "barbar;barbar: F.Prop1: bar_false",
872 "foofoo;foofoo;from_second: F.Prop2: false",
873 "barbar;barbar: F.Prop2: bar_false",
874 "foofoo;foofoo: F.Prop3: foo value",
875 "barbar;barbar: F.Prop3: bar value",
876 "from_second: F.Prop3: new value",
881 public void TestGetLoadedProject1()
883 Project project = Engine.GlobalEngine.GetLoadedProject("foo.proj");
884 Assert.IsNull(project);
887 void CreateAndCheckGlobalPropertiesImportTest (string main, string first)
889 string basePath = Path.Combine ("Test", "resources");
891 string testProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
894 File.WriteAllText (Path.Combine (basePath, "main.proj"), main);
895 File.WriteAllText (Path.Combine (basePath, "first.proj"), first);
896 File.WriteAllText (Path.Combine (basePath, "test.proj"), testProject);
899 Engine engine = new Engine ();
900 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
901 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
902 engine.RegisterLogger (logger);
904 Project project = engine.CreateNewProject ();
905 project.Load (Path.Combine (basePath, "main.proj"));
907 bool result = project.Build ();
909 logger.DumpMessages ();
910 Assert.Fail ("Build failed");
913 logger.CheckAny ("Prop: test", MessageImportance.Normal);
914 Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
916 File.Delete (Path.Combine (basePath, "main.proj"));
917 File.Delete (Path.Combine (basePath, "first.proj"));
918 File.Delete (Path.Combine (basePath, "test.proj"));
922 // Helper Methods for TestGlobalProperties*
924 void CreateAndCheckGlobalPropertiesTest (string main, string first, string second,
925 int project_count, int target_count, int task_count, string [] messages)
927 CreateAndCheckGlobalPropertiesTest (main, first, second, null, null,
928 project_count, target_count, task_count, messages);
931 void CreateAndCheckGlobalPropertiesTest (string main, string first, string second,
932 BuildPropertyGroup engine_globals, BuildPropertyGroup project_globals,
933 int project_count, int target_count, int task_count, string [] messages)
935 WriteGlobalPropertiesProjects (main, first, second);
937 Engine engine = new Engine (Consts.BinPath);
938 if (engine_globals != null)
939 engine.GlobalProperties = engine_globals;
940 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
941 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
942 engine.RegisterLogger (logger);
944 Project project = engine.CreateNewProject ();
945 project.Load (Path.Combine ("Test", Path.Combine ("resources", "main.proj")));
946 if (project_globals != null)
947 project.GlobalProperties = project_globals;
949 bool result = project.Build ();
951 logger.DumpMessages ();
952 Assert.Fail ("Build failed");
955 CheckEventCounts (logger, project_count, target_count, task_count);
957 CheckLoggedMessages (logger, messages, "A1");
960 void CheckEventCounts (MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger,
961 int project, int target, int task)
964 Assert.AreEqual (project, logger.ProjectStarted, "#project started events");
965 Assert.AreEqual (project, logger.ProjectFinished, "#project finished events");
966 Assert.AreEqual (target, logger.TargetStarted, "#target started events");
967 Assert.AreEqual (target, logger.TargetFinished, "#target finished events");
968 Assert.AreEqual (task, logger.TaskStarted, "#task started events");
969 Assert.AreEqual (task, logger.TaskFinished, "#task finished events");
970 Assert.AreEqual (1, logger.BuildStarted, "#build started events");
971 Assert.AreEqual (1, logger.BuildFinished, "#build finished events");
972 } catch (AssertionException) {
973 logger.DumpMessages ();
978 void CheckLoggedMessages (MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger, string [] messages,
982 for (int i = 0; i < messages.Length; i++) {
983 logger.CheckLoggedMessageHead (messages [i], String.Format ("{0} #{1}", prefix, i));
986 logger.DumpMessages ();
990 Assert.AreEqual (0, logger.NormalMessageCount, "Number of remaining messages");
993 // helper methods for TestGlobalProperties*
994 void WriteGlobalPropertiesProjects (string mainProject, string firstProject, string secondProject)
996 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "main.proj")))) {
997 sw.Write (mainProject);
1000 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "first.proj")))) {
1001 sw.Write (firstProject);
1004 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "second.proj")))) {
1005 sw.Write (secondProject);
1009 public static string GetUsingTask (string taskName)
1011 return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";