[sgen] Fix function signature
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Execution / BuildManagerTest.cs
1 //
2 // BuildManagerTest.cs
3 //
4 // Author:
5 //   Atsushi Enomoto (atsushi@xamarin.com)
6 //
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.IO;
30 using System.Xml;
31 using Microsoft.Build.Construction;
32 using Microsoft.Build.Evaluation;
33 using Microsoft.Build.Execution;
34 using NUnit.Framework;
35 using System.Collections.Generic;
36 using System.Linq;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Logging;
39
40 namespace MonoTests.Microsoft.Build.Execution
41 {
42         [TestFixture]
43         public class BuildManagerTest
44         {
45                 Project GetDummyProject ()
46                 {
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);
52                 }
53                 
54                 [Test]
55                 [ExpectedException (typeof (ArgumentNullException))]
56                 public void GetProjectInstanceForBuildNullFullPath ()
57                 {
58                         var manager = new BuildManager ();
59                         manager.GetProjectInstanceForBuild (GetDummyProject ());
60                 }
61                 
62                 [Test]
63                 [ExpectedException (typeof (ArgumentException))]
64                 public void GetProjectInstanceForBuildEmptyFullPath ()
65                 {
66                         var proj = GetDummyProject ();
67                         proj.FullPath = "";
68                         var manager = new BuildManager ();
69                         manager.GetProjectInstanceForBuild (proj);
70                 }
71                 
72                 [Test]
73                 public void GetProjectInstanceForBuild ()
74                 {
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);
79             root.FullPath = path;
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");
84                 }
85                 
86                 [Test]
87                 [ExpectedException (typeof (InvalidOperationException))]
88                 public void PendBuildRequestBeforeBeginBuild ()
89                 {
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]));
96                 }
97                 
98                 [Test]
99                 [ExpectedException (typeof (InvalidOperationException))]
100                 public void ResetCachesDuringBuildIsInvalid ()
101                 {
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:
107                                 is_windows = false;
108                                 break;
109                         }
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}' />
113   </Target>
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);
122                         try {
123                                 bm.ResetCaches ();
124                         } finally {
125                                 bm.EndBuild (); // yes, it should work even after invalid ResetCaches call... at least on .NET it does.
126                         }
127                 }
128
129                 [Test]
130                 public void BasicManualParallelBuilds ()
131                 {
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' />
136   </Target>
137 </Project>";
138                         switch (Environment.OSVersion.Platform) {
139                         case PlatformID.MacOSX:
140                         case PlatformID.Unix:
141                                 break;
142                         default:
143                                 Assert.Ignore (); // ignore, cannot run it
144                                 break;
145                         }
146                         
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" }));
157                                 l.Add (sub);
158                                 sub.ExecuteAsync (delegate { waitDone = DateTime.Now; }, null);
159                         }
160                         bm.EndBuild ();
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");
165                 }
166                 
167                 [Test]
168                 public void BuildCommonResolveAssemblyReferences ()
169                 {
170                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
171   <Import Project='$(MSBuildToolsPath)\Microsoft.Common.targets' />
172   <ItemGroup>
173     <Reference Include='System.Core' />
174     <Reference Include='System.Xml' />
175   </ItemGroup>
176 </Project>";
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");
199                 }
200         }
201 }
202