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.
31 using Microsoft.Build.Construction;
32 using Microsoft.Build.Evaluation;
33 using Microsoft.Build.Execution;
34 using NUnit.Framework;
35 using System.Collections.Generic;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Logging;
40 namespace MonoTests.Microsoft.Build.Execution
43 public class BuildManagerTest
45 Project GetDummyProject ()
47 string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
48 var path = "file://localhost/foo.xml";
49 var xml = XmlReader.Create (new StringReader (empty_project_xml), null, path);
50 var root = ProjectRootElement.Create (xml);
51 return new Project (root);
55 [ExpectedException (typeof (ArgumentNullException))]
56 public void GetProjectInstanceForBuildNullFullPath ()
58 var manager = new BuildManager ();
59 manager.GetProjectInstanceForBuild (GetDummyProject ());
63 [ExpectedException (typeof (ArgumentException))]
64 public void GetProjectInstanceForBuildEmptyFullPath ()
66 var proj = GetDummyProject ();
68 var manager = new BuildManager ();
69 manager.GetProjectInstanceForBuild (proj);
73 public void GetProjectInstanceForBuild ()
75 string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
76 var path = "file://localhost/foo.xml";
77 var xml = XmlReader.Create (new StringReader(empty_project_xml), null, path);
78 var root = ProjectRootElement.Create (xml);
80 var proj = new Project (root);
81 var manager = new BuildManager ();
82 var inst = manager.GetProjectInstanceForBuild (proj);
83 Assert.AreEqual (inst, manager.GetProjectInstanceForBuild (proj), "#1");
87 [ExpectedException (typeof (InvalidOperationException))]
88 public void PendBuildRequestBeforeBeginBuild ()
90 string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
91 var path = "file://localhost/foo.xml";
92 var xml = XmlReader.Create (new StringReader (empty_project_xml), null, path);
93 var root = ProjectRootElement.Create (xml);
94 var proj = new ProjectInstance (root);
95 new BuildManager ().PendBuildRequest (new BuildRequestData (proj, new string [0]));
99 [ExpectedException (typeof (InvalidOperationException))]
100 public void ResetCachesDuringBuildIsInvalid ()
102 // Windows does not have useful sleep or alternative, so skip it
103 bool is_windows = true;
104 switch (Environment.OSVersion.Platform) {
105 case PlatformID.Unix:
106 case PlatformID.MacOSX:
110 string project_xml = string.Format (@"<Project DefaultTargets='Wait1Sec' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
111 <Target Name='Wait1Sec'>
112 <Exec Command='{0}' />
114 </Project>", is_windows ? "powershell -command \"Start-Sleep -s 1\"" : "/bin/sleep 1");
115 var xml = XmlReader.Create (new StringReader (project_xml));
116 var root = ProjectRootElement.Create (xml);
117 var proj = new ProjectInstance (root);
118 var bm = new BuildManager ();
119 bm.BeginBuild (new BuildParameters ());
120 var sub = bm.PendBuildRequest (new BuildRequestData (proj, new string [] { "Wait1Sec" }));
121 sub.ExecuteAsync (delegate {}, null);
125 bm.EndBuild (); // yes, it should work even after invalid ResetCaches call... at least on .NET it does.
130 public void BasicManualParallelBuilds ()
132 string project_xml = @"<Project DefaultTargets='Wait1Sec' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
133 <Target Name='Wait1Sec'>
134 <!-- Exec Command='ping 10.1.1.1 -n 1 -w 1' /-->
135 <Exec Command='/bin/sleep 1' />
138 switch (Environment.OSVersion.Platform) {
139 case PlatformID.MacOSX:
140 case PlatformID.Unix:
143 Assert.Ignore (); // ignore, cannot run it
147 var xml = XmlReader.Create (new StringReader (project_xml));
148 var root = ProjectRootElement.Create (xml);
149 var proj = new ProjectInstance (root);
150 var bm = new BuildManager ();
151 bm.BeginBuild (new BuildParameters () { Loggers = new ILogger [] {new ConsoleLogger (LoggerVerbosity.Diagnostic, TextWriter.Null.WriteLine, null, null)} });
152 DateTime waitDone = DateTime.MinValue;
153 DateTime beforeExec = DateTime.Now;
154 var l = new List<BuildSubmission> ();
155 for (int i = 0; i < 10; i++) {
156 var sub = bm.PendBuildRequest (new BuildRequestData (proj, new string [] { "Wait1Sec" }));
158 sub.ExecuteAsync (delegate { waitDone = DateTime.Now; }, null);
161 Assert.IsTrue (l.All (s => s.BuildResult.OverallResult == BuildResultCode.Success), "#1");
162 DateTime endBuildDone = DateTime.Now;
163 Assert.IsTrue (endBuildDone - beforeExec >= TimeSpan.FromSeconds (1), "#2");
164 Assert.IsTrue (endBuildDone > waitDone, "#3");
168 public void BuildCommonResolveAssemblyReferences ()
170 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
171 <Import Project='$(MSBuildToolsPath)\Microsoft.Common.targets' />
173 <Reference Include='System.Core' />
174 <Reference Include='System.Xml' />
177 var xml = XmlReader.Create (new StringReader (project_xml));
178 var root = ProjectRootElement.Create (xml);
179 root.FullPath = "BuildManagerTest.BuildCommonResolveAssemblyReferences.proj";
180 var proj = new ProjectInstance (root);
181 var manager = new BuildManager ();
182 var parameters = new BuildParameters () { Loggers = new ILogger [] {new ConsoleLogger (LoggerVerbosity.Diagnostic, TextWriter.Null.WriteLine, null, null)} };
183 var request = new BuildRequestData (proj, new string [] {"ResolveAssemblyReferences"});
184 Assert.AreEqual (string.Empty, proj.GetPropertyValue ("TargetFrameworkDirectory"), "#1-1");
185 var result = manager.Build (parameters, request);
186 Assert.AreNotEqual (string.Empty, proj.GetPropertyValue ("TargetFrameworkDirectory"), "#1-2"); // filled during build.
187 Assert.IsTrue (result.ResultsByTarget.ContainsKey ("GetFrameworkPaths"), "#2-1");
188 Assert.IsTrue (result.ResultsByTarget.ContainsKey ("PrepareForBuild"), "#2-2");
189 Assert.IsTrue (result.ResultsByTarget.ContainsKey ("ResolveAssemblyReferences"), "#2-3");
190 var items = proj.GetItems ("ReferencePath");
191 Assert.AreEqual (2, items.Count (), "#3");
192 var syscore = items.FirstOrDefault (i => Path.GetFileName (i.EvaluatedInclude) == "System.Core.dll");
193 var sysxml = items.FirstOrDefault (i => Path.GetFileName (i.EvaluatedInclude) == "System.Xml.dll");
194 Assert.IsNotNull (syscore, "#4-1");
195 Assert.IsNotNull (sysxml, "#4-2");
196 Assert.IsTrue (File.Exists (syscore.EvaluatedInclude), "#5-1");
197 Assert.IsTrue (File.Exists (sysxml.EvaluatedInclude), "#5-1");
198 Assert.AreEqual (BuildResultCode.Success, result.OverallResult, "#6");