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 // Windows does not have useful sleep or alternative, so skip it
85 bool is_windows = true;
86 switch (Environment.OSVersion.Platform) {
88 case PlatformID.MacOSX:
92 string project_xml = string.Format (@"<Project DefaultTargets='Wait1Sec' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
93 <Target Name='Wait1Sec'>
94 <Exec Command='{0}' />
96 </Project>", is_windows ? "powershell -command \"Start-Sleep -s 1\"" : "/bin/sleep 1");
97 var xml = XmlReader.Create (new StringReader (project_xml));
98 var root = ProjectRootElement.Create (xml);
99 root.FullPath = "BuildSubmissionTest.EndBuildWaitsForSubmissionCompletion.proj";
100 var proj = new ProjectInstance (root);
101 var bm = new BuildManager ();
102 bm.BeginBuild (new BuildParameters ());
103 DateTime waitDone = DateTime.MinValue;
104 DateTime beforeExec = DateTime.Now;
105 var sub = bm.PendBuildRequest (new BuildRequestData (proj, new string [] { "Wait1Sec" }));
106 sub.ExecuteAsync (delegate { waitDone = DateTime.Now; }, null);
108 Assert.IsTrue (sub.BuildResult.OverallResult == BuildResultCode.Success, "#1");
109 DateTime endBuildDone = DateTime.Now;
110 Assert.IsTrue (endBuildDone - beforeExec >= TimeSpan.FromSeconds (1), "#2");
111 Assert.IsTrue (waitDone >= beforeExec, "#3");
112 Assert.IsTrue (endBuildDone >= waitDone, "#4");
116 public void BuildParameterLoggersExplicitlyRequired ()
118 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
119 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
122 <Foo Condition='$(X)' Include='foo.txt' />
126 var xml = XmlReader.Create (new StringReader (project_xml));
127 var root = ProjectRootElement.Create (xml);
128 root.FullPath = "BuildSubmissionTest.BuildParameterLoggersExplicitlyRequired.proj";
129 var pc = new ProjectCollection ();
130 var sw = new StringWriter ();
131 pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
132 var proj = new ProjectInstance (root);
133 var bm = new BuildManager ();
134 var bp = new BuildParameters (pc);
135 var br = new BuildRequestData (proj, new string [] {"Foo"});
136 Assert.AreEqual (BuildResultCode.Failure, bm.Build (bp, br).OverallResult, "#1");
137 // the logger is *ignored*
138 Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");
142 public void ProjectInstanceBuildLoggersExplicitlyRequired ()
144 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
145 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
148 <Foo Condition='$(X)' Include='foo.txt' />
152 var xml = XmlReader.Create (new StringReader (project_xml));
153 var root = ProjectRootElement.Create (xml);
154 root.FullPath = "BuildSubmissionTest.TaskOutputsToLoggers.proj";
155 var pc = new ProjectCollection ();
156 var sw = new StringWriter ();
157 pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
158 var proj = new ProjectInstance (root);
159 Assert.IsFalse (proj.Build (), "#1");
160 // the logger is *ignored* again
161 Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");