[sgen] Fix function signature
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Execution / BuildSubmissionTest.cs
1 //
2 // BuildSubmissionTest.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.Linq;
31 using System.Xml;
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;
39
40 namespace MonoTests.Microsoft.Build.Execution
41 {
42         [TestFixture]
43         public class BuildSubmissionTest
44         {
45                 [Test]
46                 public void ResultBeforeExecute ()
47                 {
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");
57                 }
58                 
59                 // This checks if the build output for each task is written to the loggers and not directly thrown as a Project loader error.
60                 [Test]
61                 public void TaskOutputsToLoggers ()
62                 {
63             string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
64   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
65   <Target Name='Foo'>
66     <ItemGroup>
67       <Foo Condition='$(X)' Include='foo.txt' />
68     </ItemGroup>
69   </Target>
70 </Project>";
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");
79                 }
80                 
81                 [Test]
82                 public void EndBuildWaitsForSubmissionCompletion ()
83                 {
84                         // Windows does not have useful sleep or alternative, so skip it
85                         bool is_windows = true;
86                         switch (Environment.OSVersion.Platform) {
87                         case PlatformID.Unix:
88                         case PlatformID.MacOSX:
89                                 is_windows = false;
90                                 break;
91                         }
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}' />
95   </Target>
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);
107                         bm.EndBuild ();
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");
113                 }
114                 
115                 [Test]
116                 public void BuildParameterLoggersExplicitlyRequired ()
117                 {
118             string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
119   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
120   <Target Name='Foo'>
121     <ItemGroup>
122       <Foo Condition='$(X)' Include='foo.txt' />
123     </ItemGroup>
124   </Target>
125 </Project>";
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");
139                 }
140                 
141                 [Test]
142                 public void ProjectInstanceBuildLoggersExplicitlyRequired ()
143                 {
144             string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
145   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
146   <Target Name='Foo'>
147     <ItemGroup>
148       <Foo Condition='$(X)' Include='foo.txt' />
149     </ItemGroup>
150   </Target>
151 </Project>";
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");
162                 }
163         }
164 }
165