2 // BuildSubmissionTest.cs
5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
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.
32 using Microsoft.Build.Construction;
33 using Microsoft.Build.Evaluation;
34 using Microsoft.Build.Execution;
35 using NUnit.Framework;
36 using Microsoft.Build.Logging;
37 using Microsoft.Build.Framework;
38 using System.Collections.Generic;
40 namespace MonoTests.Microsoft.Build.Execution
43 public class BuildSubmissionTest
46 public void ResultBeforeExecute ()
48 string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
49 var path = "file://localhost/foo.xml";
50 var xml = XmlReader.Create (new StringReader (empty_project_xml), null, path);
51 var root = ProjectRootElement.Create (xml);
52 var proj = new ProjectInstance (root);
53 var bm = new BuildManager ();
54 bm.BeginBuild (new BuildParameters ());
55 var sub = bm.PendBuildRequest (new BuildRequestData (proj, new string [0]));
56 Assert.IsNull (sub.BuildResult, "#1");
59 // This checks if the build output for each task is written to the loggers and not directly thrown as a Project loader error.
61 public void TaskOutputsToLoggers ()
63 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
64 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
67 <Foo Condition='$(X)' Include='foo.txt' />
71 var xml = XmlReader.Create (new StringReader (project_xml));
72 var root = ProjectRootElement.Create (xml);
73 root.FullPath = "BuildSubmissionTest.TaskOutputsToLoggers.proj";
74 var proj = new ProjectInstance (root);
75 Assert.AreEqual ("$(X)", root.Targets.First ().ItemGroups.First ().Items.First ().Condition, "#0");
76 var sw = new StringWriter ();
77 Assert.IsFalse (proj.Build (new ILogger [] {new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null)}), "#1");
78 Assert.IsTrue (sw.ToString ().Contains ("$(X)"), "#2");
82 public void EndBuildWaitsForSubmissionCompletion ()
84 string project_xml = string.Format (@"<Project DefaultTargets='Wait1Sec' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
85 <Target Name='Wait1Sec'>
86 <Exec Command='{0}' />
88 </Project>", Environment.OSVersion.Platform == PlatformID.Win32NT ? "powershell -command \"Start-Sleep -s 1\"" : "/bin/sleep 1");
89 var xml = XmlReader.Create (new StringReader (project_xml));
90 var root = ProjectRootElement.Create (xml);
91 root.FullPath = "BuildSubmissionTest.EndBuildWaitsForSubmissionCompletion.proj";
92 var proj = new ProjectInstance (root);
93 var bm = new BuildManager ();
94 bm.BeginBuild (new BuildParameters ());
95 var waitDone = TimeSpan.MinValue;
96 var sw = System.Diagnostics.Stopwatch.StartNew ();
97 var sub = bm.PendBuildRequest (new BuildRequestData (proj, new string [] { "Wait1Sec" }));
98 sub.ExecuteAsync (cb => waitDone = sw.Elapsed, null);
100 Assert.AreEqual (BuildResultCode.Success, sub.BuildResult.OverallResult, "#1");
101 var endBuildDone = sw.Elapsed;
102 AssertHelper.GreaterOrEqual (endBuildDone, TimeSpan.FromSeconds (1), "#2");
103 AssertHelper.GreaterOrEqual (waitDone, TimeSpan.FromSeconds (1), "#3");
104 AssertHelper.GreaterOrEqual (endBuildDone, waitDone, "#4");
105 AssertHelper.LessOrEqual (endBuildDone, TimeSpan.FromSeconds (1.5), "#5");
106 AssertHelper.LessOrEqual (waitDone, TimeSpan.FromSeconds (1.5), "#6");
110 public void BuildParameterLoggersExplicitlyRequired ()
112 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
113 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
116 <Foo Condition='$(X)' Include='foo.txt' />
120 var xml = XmlReader.Create (new StringReader (project_xml));
121 var root = ProjectRootElement.Create (xml);
122 root.FullPath = "BuildSubmissionTest.BuildParameterLoggersExplicitlyRequired.proj";
123 var pc = new ProjectCollection ();
124 var sw = new StringWriter ();
125 pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
126 var proj = new ProjectInstance (root);
127 var bm = new BuildManager ();
128 var bp = new BuildParameters (pc);
129 var br = new BuildRequestData (proj, new string [] {"Foo"});
130 Assert.AreEqual (BuildResultCode.Failure, bm.Build (bp, br).OverallResult, "#1");
131 // the logger is *ignored*
132 Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");
136 public void ProjectInstanceBuildLoggersExplicitlyRequired ()
138 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
139 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
142 <Foo Condition='$(X)' Include='foo.txt' />
146 var xml = XmlReader.Create (new StringReader (project_xml));
147 var root = ProjectRootElement.Create (xml);
148 root.FullPath = "BuildSubmissionTest.TaskOutputsToLoggers.proj";
149 var pc = new ProjectCollection ();
150 var sw = new StringWriter ();
151 pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
152 var proj = new ProjectInstance (root);
153 Assert.IsFalse (proj.Build (), "#1");
154 // the logger is *ignored* again
155 Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");