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 [Category ("NotDotNet")]
279 [ExpectedException (typeof (ArgumentException))]
280 public void TestBuildProject1 ()
282 engine = new Engine (Consts.BinPath);
283 engine.BuildProject (null);
287 public void TestBuildProject2 ()
289 engine = new Engine (Consts.BinPath);
290 Project project = engine.CreateNewProject ();
292 Assert.IsFalse (engine.BuildProject (project, (string)null), "#A1");
293 Assert.IsFalse (engine.BuildProject (project, (string [])null), "#A2");
294 Assert.IsFalse (engine.BuildProject (project, (string [])null, null), "#A3");
295 Assert.IsFalse (engine.BuildProject (project, (string [])null, null, BuildSettings.None), "#A4");
297 bool caught_exception = false;
299 //null string in targetNames [] param
300 engine.BuildProject (project, new string [] {null}, null);
302 caught_exception = true;
304 if (!caught_exception)
305 Assert.Fail ("Expected exception for Engine.BuildProject");
309 [Category ("NotDotNet")]
310 [ExpectedException (typeof (ArgumentException))]
311 public void TestBuildProjectNull1 ()
313 engine = new Engine (Consts.BinPath);
314 engine.BuildProject (null, "foo");
318 [Category ("NotDotNet")]
319 [ExpectedException (typeof (ArgumentException))]
320 public void TestBuildProjectNull2 ()
322 engine = new Engine (Consts.BinPath);
323 engine.BuildProject (null, (string)null);
326 // Tests to check global properties behavior
328 public void TestGlobalProperties1 ()
330 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
331 + GetUsingTask ("MSBuild")
333 <Target Name=""main"">
334 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
335 <Message Text=""second""/>
336 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
340 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
341 + GetUsingTask ("MSBuild")
343 <Target Name = ""1"">
344 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
345 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
348 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
349 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
350 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
351 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
356 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
359 "(TargetA) foo: bar A: External: ",
360 "(TargetB) foo: foofoo A: External: ",
361 "(TargetA) foo: A: External: ",
362 "(TargetB) foo: foofoo1 A: External: ",
367 public void TestGlobalProperties1a ()
369 Directory.CreateDirectory ("Test/resources/foo");
370 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
371 + GetUsingTask ("MSBuild")
373 <Target Name=""main"">
374 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
375 <Message Text=""second""/>
376 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
380 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
381 + GetUsingTask ("MSBuild")
383 <Target Name = ""1"">
384 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
385 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
388 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
389 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
390 <MSBuild Projects=""second.proj"" Targets = ""TargetA"" Properties=""foo=bar""/>
391 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
392 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
396 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
399 "(TargetA) foo: bar A: External: ",
400 "(TargetB) foo: foofoo A: External: ",
401 "(TargetA) foo: A: External: ",
402 "(TargetB) foo: foofoo1 A: External: ",
407 public void TestGlobalProperties1b ()
409 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
410 + GetUsingTask ("MSBuild")
412 <Target Name=""main"">
413 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
414 <Message Text=""second""/>
415 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
419 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
420 + GetUsingTask ("MSBuild")
422 <Target Name = ""1"">
423 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
424 <MSBuild Projects=""second.proj""/>
425 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
428 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
429 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
430 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
431 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
435 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
438 "(TargetA) foo: bar A: External: ",
439 "(TargetA) foo: A: External: ",
440 "(TargetB) foo: foofoo A: External: ",
441 "(TargetB) foo: foofoo1 A: External: ",
446 public void TestGlobalProperties2 ()
448 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
449 + GetUsingTask ("MSBuild")
451 <Target Name=""main"">
452 <MSBuild Projects=""first.proj"" Targets = ""1""/>
453 <MSBuild Projects=""first.proj"" Targets = ""2""/>
454 <Message Text=""second""/>
455 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
459 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
460 + GetUsingTask ("MSBuild")
462 <Target Name = ""1"">
463 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
464 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
467 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
468 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
469 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
470 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
475 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
478 "(TargetA) foo: bar A: External: ",
479 "(TargetB) foo: foofoo A: External: ",
480 "(TargetA) foo: A: External: ",
481 "(TargetB) foo: foofoo1 A: External: ",
486 public void TestGlobalProperties3 ()
488 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
489 + GetUsingTask ("MSBuild")
491 <Target Name=""main"">
492 <MSBuild Projects=""first.proj"" Targets = ""1""/>
493 <CallTarget Targets=""Call2""/>
494 <Message Text=""second""/>
495 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
497 <Target Name=""Call2"">
498 <MSBuild Projects=""first.proj"" Targets = ""2""/>
502 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
503 + GetUsingTask ("MSBuild")
505 <Target Name = ""1"">
506 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
507 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
510 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
511 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
512 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
513 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
518 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
521 "(TargetA) foo: bar A: External: ",
522 "(TargetB) foo: foofoo A: External: ",
523 "(TargetA) foo: A: External: ",
524 "(TargetB) foo: foofoo1 A: External: ",
528 //externally set global properties
530 public void TestGlobalProperties4 ()
532 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
533 + GetUsingTask ("MSBuild")
535 <Target Name=""main"">
536 <MSBuild Projects=""first.proj"" Targets = ""1""/>
537 <CallTarget Targets=""Call2""/>
538 <Message Text=""second""/>
539 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
541 <Target Name=""Call2"">
542 <MSBuild Projects=""first.proj"" Targets = ""2""/>
546 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
547 + GetUsingTask ("MSBuild")
549 <Target Name = ""1"">
550 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
551 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
554 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
555 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
556 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
557 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
562 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
563 globalprops.SetProperty ("foo", "hello");
564 engine.GlobalProperties = globalprops;
566 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
567 globalprops, null, 10, 8, 15,
569 "(TargetA) foo: bar A: External: ",
570 "(TargetB) foo: foofoo A: External: ",
571 "(TargetA) foo: hello A: FooWasHello External: ",
572 "(TargetB) foo: foofoo1 A: External: ",
576 //externally set global properties, merge with explicit
578 public void TestGlobalProperties4a ()
580 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
581 + GetUsingTask ("MSBuild")
583 <Target Name=""main"">
584 <MSBuild Projects=""first.proj"" Targets = ""1""/>
585 <CallTarget Targets=""Call2""/>
586 <Message Text=""second""/>
587 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
589 <Target Name=""Call2"">
590 <MSBuild Projects=""first.proj"" Targets = ""2""/>
594 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
595 + GetUsingTask ("MSBuild")
597 <Target Name = ""1"">
598 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
599 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
602 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
603 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
604 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
605 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
610 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
611 globalprops.SetProperty ("external", "ExternalValue");
613 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
617 "(TargetA) foo: bar A: External: ExternalValue",
618 "(TargetB) foo: foofoo A: External: ExternalValue",
619 "(TargetA) foo: A: External: ExternalValue",
620 "(TargetB) foo: foofoo1 A: External: ExternalValue",
624 //set global properties on _project_, merge with explicit
626 public void TestGlobalProperties4b ()
628 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
629 + GetUsingTask ("MSBuild")
631 <Target Name=""main"">
632 <MSBuild Projects=""first.proj"" Targets = ""1""/>
633 <CallTarget Targets=""Call2""/>
634 <Message Text=""second""/>
635 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
637 <Target Name=""Call2"">
638 <MSBuild Projects=""first.proj"" Targets = ""2""/>
642 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
643 + GetUsingTask ("MSBuild")
645 <Target Name = ""1"">
646 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
647 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
650 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
651 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
652 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
653 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
658 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
659 globalprops.SetProperty ("external", "ExternalValue");
661 BuildPropertyGroup project_globalprops = new BuildPropertyGroup ();
662 project_globalprops.SetProperty ("external", "ProjExternalValue");
663 project_globalprops.SetProperty ("foo", "ProjFooValue");
665 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
666 globalprops, project_globalprops,
669 "(TargetA) foo: bar A: External: ProjExternalValue",
670 "(TargetB) foo: foofoo A: External: ProjExternalValue",
671 "(TargetA) foo: ProjFooValue A: External: ProjExternalValue",
672 "(TargetB) foo: foofoo1 A: External: ProjExternalValue",
676 //set global properties on _project_, and engine and explicit via msbuild
678 public void TestGlobalProperties4c ()
680 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
681 + GetUsingTask ("MSBuild")
683 <Target Name=""main"">
684 <MSBuild Projects=""first.proj"" Targets = ""1""/>
685 <CallTarget Targets=""Call2""/>
686 <Message Text=""second""/>
687 <MSBuild Projects=""first.proj"" Targets = ""1;2""/>
689 <Target Name=""Call2"">
690 <MSBuild Projects=""first.proj"" Targets = ""2""/>
694 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
695 + GetUsingTask ("MSBuild")
697 <Target Name = ""1"">
698 <MSBuild Projects=""second.proj"" Properties=""foo=bar""/>
699 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo""/>
702 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
703 <MSBuild Projects=""second.proj"" Targets = ""TargetA""/>
704 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo"" />
705 <MSBuild Projects=""second.proj"" Targets = ""TargetB"" Properties=""foo=foofoo1"" />
710 BuildPropertyGroup globalprops = new BuildPropertyGroup ();
711 globalprops.SetProperty ("foo", "EngineFooValue");
713 BuildPropertyGroup project_globalprops = new BuildPropertyGroup ();
714 project_globalprops.SetProperty ("foo", "ProjFooValue");
716 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
717 globalprops, project_globalprops,
720 "(TargetA) foo: bar A: External: ",
721 "(TargetB) foo: foofoo A: External: ",
722 "(TargetA) foo: ProjFooValue A: External: ",
723 "(TargetB) foo: foofoo1 A: External: ",
727 // Check for global properties in case of Import
730 public void TestGlobalPropertiesImport1 ()
732 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
733 <Target Name=""main"">
734 <MSBuild Projects=""first.proj"" Targets = ""1"" Properties='Prop=test'/>
738 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
739 <Target Name = ""1"">
740 <Message Text='Prop: $(Prop)'/>
742 <Import Project='$(Prop).proj'/>
745 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
749 public void TestGlobalPropertiesImport2 ()
751 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
752 <Target Name=""main"">
753 <MSBuild Projects=""first.proj"" Targets = ""1"" Properties='Prop=test'/>
757 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
761 <Target Name = ""1"">
762 <Message Text='Prop: $(Prop)'/>
764 <Import Project='$(Prop).proj'/>
767 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
771 public void TestGlobalPropertiesImport3()
773 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
774 <Target Name=""main"">
775 <MSBuild Projects=""first.proj"" Targets = ""1""/>
779 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
783 <Target Name = ""1"">
784 <Message Text='Prop: $(Prop)'/>
786 <Import Project='$(Prop).proj'/>
789 CreateAndCheckGlobalPropertiesImportTest (mainProject, firstProject);
793 public void TestMSBuildOutputs ()
795 string mainProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">"
796 + GetUsingTask ("MSBuild")
799 <ProjectRef Include=""first.proj"">
801 <Unique>true</Unique>
803 <ProjectRef Include=""first.proj"">
804 <Prop3>value2</Prop3>
805 <Unique>false</Unique>
808 <ProjectRef Include=""second.proj"">
809 <Prop3>value3</Prop3>
810 <Unique>unique</Unique>
816 <MSBuild Projects=""@(ProjectRef)"" Targets=""GetData"">
817 <Output TaskParameter=""TargetOutputs"" ItemName=""F""/>
819 <Message Text=""@(F): F.Unique: %(F.Unique)""/>
820 <Message Text=""@(F): F.Prop1: %(F.Prop1)""/>
821 <Message Text=""@(F): F.Prop2: %(F.Prop2)""/>
822 <Message Text=""@(F): F.Prop3: %(F.Prop3)""/>
826 string firstProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
828 <A Include=""foofoo"">
831 <Prop3>foo value</Prop3>
834 <A Include=""barbar"">
835 <Prop1>bar_false</Prop1>
836 <Prop2>bar_false</Prop2>
837 <Prop3>bar value</Prop3>
842 <Target Name=""GetData"" Outputs=""@(AbcOutputs)"">
843 <CreateItem Include=""@(A)"">
844 <Output TaskParameter=""Include"" ItemName=""AbcOutputs""/>
849 string secondProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
851 <A Include=""from_second"">
854 <Prop3>new value</Prop3>
858 <Target Name=""GetData"" Outputs=""@(AbcOutputs)"">
859 <CreateItem Include=""@(A)"">
860 <Output TaskParameter=""Include"" ItemName=""AbcOutputs""/>
866 CreateAndCheckGlobalPropertiesTest (mainProject, firstProject, secondProject,
870 "foofoo;barbar: F.Unique: true",
871 "foofoo;barbar: F.Unique: false",
872 "from_second: F.Unique: unique",
873 "foofoo;foofoo;from_second: F.Prop1: false",
874 "barbar;barbar: F.Prop1: bar_false",
875 "foofoo;foofoo;from_second: F.Prop2: false",
876 "barbar;barbar: F.Prop2: bar_false",
877 "foofoo;foofoo: F.Prop3: foo value",
878 "barbar;barbar: F.Prop3: bar value",
879 "from_second: F.Prop3: new value",
884 public void TestGetLoadedProject1()
886 Project project = Engine.GlobalEngine.GetLoadedProject("foo.proj");
887 Assert.IsNull(project);
890 void CreateAndCheckGlobalPropertiesImportTest (string main, string first)
892 string basePath = Path.Combine ("Test", "resources");
894 string testProject = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
897 File.WriteAllText (Path.Combine (basePath, "main.proj"), main);
898 File.WriteAllText (Path.Combine (basePath, "first.proj"), first);
899 File.WriteAllText (Path.Combine (basePath, "test.proj"), testProject);
902 Engine engine = new Engine ();
903 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
904 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
905 engine.RegisterLogger (logger);
907 Project project = engine.CreateNewProject ();
908 project.Load (Path.Combine (basePath, "main.proj"));
910 bool result = project.Build ();
912 logger.DumpMessages ();
913 Assert.Fail ("Build failed");
916 logger.CheckAny ("Prop: test", MessageImportance.Normal);
917 Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
919 File.Delete (Path.Combine (basePath, "main.proj"));
920 File.Delete (Path.Combine (basePath, "first.proj"));
921 File.Delete (Path.Combine (basePath, "test.proj"));
925 // Helper Methods for TestGlobalProperties*
927 void CreateAndCheckGlobalPropertiesTest (string main, string first, string second,
928 int project_count, int target_count, int task_count, string [] messages)
930 CreateAndCheckGlobalPropertiesTest (main, first, second, null, null,
931 project_count, target_count, task_count, messages);
934 void CreateAndCheckGlobalPropertiesTest (string main, string first, string second,
935 BuildPropertyGroup engine_globals, BuildPropertyGroup project_globals,
936 int project_count, int target_count, int task_count, string [] messages)
938 WriteGlobalPropertiesProjects (main, first, second);
940 Engine engine = new Engine (Consts.BinPath);
941 if (engine_globals != null)
942 engine.GlobalProperties = engine_globals;
943 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
944 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
945 engine.RegisterLogger (logger);
947 Project project = engine.CreateNewProject ();
948 project.Load (Path.Combine ("Test", Path.Combine ("resources", "main.proj")));
949 if (project_globals != null)
950 project.GlobalProperties = project_globals;
952 bool result = project.Build ();
954 logger.DumpMessages ();
955 Assert.Fail ("Build failed");
958 CheckEventCounts (logger, project_count, target_count, task_count);
960 CheckLoggedMessages (logger, messages, "A1");
963 void CheckEventCounts (MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger,
964 int project, int target, int task)
967 Assert.AreEqual (project, logger.ProjectStarted, "#project started events");
968 Assert.AreEqual (project, logger.ProjectFinished, "#project finished events");
969 Assert.AreEqual (target, logger.TargetStarted, "#target started events");
970 Assert.AreEqual (target, logger.TargetFinished, "#target finished events");
971 Assert.AreEqual (task, logger.TaskStarted, "#task started events");
972 Assert.AreEqual (task, logger.TaskFinished, "#task finished events");
973 Assert.AreEqual (1, logger.BuildStarted, "#build started events");
974 Assert.AreEqual (1, logger.BuildFinished, "#build finished events");
975 } catch (AssertionException) {
976 logger.DumpMessages ();
981 void CheckLoggedMessages (MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger, string [] messages,
985 for (int i = 0; i < messages.Length; i++) {
986 logger.CheckLoggedMessageHead (messages [i], String.Format ("{0} #{1}", prefix, i));
989 logger.DumpMessages ();
993 Assert.AreEqual (0, logger.NormalMessageCount, "Number of remaining messages");
996 // helper methods for TestGlobalProperties*
997 void WriteGlobalPropertiesProjects (string mainProject, string firstProject, string secondProject)
999 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "main.proj")))) {
1000 sw.Write (mainProject);
1003 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "first.proj")))) {
1004 sw.Write (firstProject);
1007 using (StreamWriter sw = new StreamWriter (Path.Combine ("Test", Path.Combine ("resources", "second.proj")))) {
1008 sw.Write (secondProject);
1012 public static string GetUsingTask (string taskName)
1014 return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";