Merge branch 'BigIntegerParse'
[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 (endBuildDone > waitDone, "#3");
112                 }
113                 
114                 [Test]
115                 public void BuildParameterLoggersExplicitlyRequired ()
116                 {
117             string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
118   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
119   <Target Name='Foo'>
120     <ItemGroup>
121       <Foo Condition='$(X)' Include='foo.txt' />
122     </ItemGroup>
123   </Target>
124 </Project>";
125             var xml = XmlReader.Create (new StringReader (project_xml));
126             var root = ProjectRootElement.Create (xml);
127                         root.FullPath = "BuildSubmissionTest.BuildParameterLoggersExplicitlyRequired.proj";
128                         var pc = new ProjectCollection ();
129                         var sw = new StringWriter ();
130                         pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
131                         var proj = new ProjectInstance (root);
132                         var bm = new BuildManager ();
133                         var bp = new BuildParameters (pc);
134                         var br = new BuildRequestData (proj, new string [] {"Foo"});
135                         Assert.AreEqual (BuildResultCode.Failure, bm.Build (bp, br).OverallResult, "#1");
136                         // the logger is *ignored*
137                         Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");
138                 }
139                 
140                 [Test]
141                 public void ProjectInstanceBuildLoggersExplicitlyRequired ()
142                 {
143             string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
144   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
145   <Target Name='Foo'>
146     <ItemGroup>
147       <Foo Condition='$(X)' Include='foo.txt' />
148     </ItemGroup>
149   </Target>
150 </Project>";
151             var xml = XmlReader.Create (new StringReader (project_xml));
152             var root = ProjectRootElement.Create (xml);
153                         root.FullPath = "BuildSubmissionTest.TaskOutputsToLoggers.proj";
154                         var pc = new ProjectCollection ();
155                         var sw = new StringWriter ();
156                         pc.RegisterLogger (new ConsoleLogger (LoggerVerbosity.Diagnostic, sw.WriteLine, null, null));
157                         var proj = new ProjectInstance (root);
158                         Assert.IsFalse (proj.Build (), "#1");
159                         // the logger is *ignored* again
160                         Assert.IsFalse (sw.ToString ().Contains ("$(X)"), "#2");
161                 }
162         }
163 }
164