Add [Category ("NotWorking")] to failing test.
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Evaluation / ProjectTest.cs
1 //
2 // ProjectTest.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 NUnit.Framework;
35 using Microsoft.Build.Exceptions;
36 using Microsoft.Build.Logging;
37 using Microsoft.Build.Framework;
38
39 namespace MonoTests.Microsoft.Build.Evaluation
40 {
41         [TestFixture]
42         public class ProjectTest
43         {
44                 [Test]
45                 public void EscapeDoesWTF ()
46                 {
47                         string value_xml = "What are 'ESCAPE' & \"EVALUATE\" ? $ # % ^";
48                         string value = "What are 'ESCAPE' & \"EVALUATE\" ? $ # % ^";
49                         string escaped = "What are %27ESCAPE%27 & \"EVALUATE\" %3f %24 # %25 ^";
50                         string xml = string.Format (@"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
51         <PropertyGroup>
52                 <Foo>{0}</Foo>
53                 <Baz>$(FOO)</Baz>
54         </PropertyGroup>
55 </Project>", value_xml);
56                         var path = "file://localhost/foo.xml";
57                         var reader = XmlReader.Create (new StringReader (xml), null, path);
58                         var root = ProjectRootElement.Create (reader);
59                         var proj = new Project (root);
60                         var prop = proj.Properties.First (p => p.Name == "Foo");
61                         Assert.AreEqual (value, prop.UnevaluatedValue, "#1");
62                         Assert.AreEqual (value, prop.EvaluatedValue, "#2");
63                         // eh?
64                         Assert.AreEqual (value, Project.GetPropertyValueEscaped (prop), "#3");
65                         prop = proj.Properties.First (p => p.Name == "Baz");
66                         Assert.AreEqual ("$(FOO)", prop.UnevaluatedValue, "#4");
67                         Assert.AreEqual (value, prop.EvaluatedValue, "#5");
68                         // eh?
69                         Assert.AreEqual (value, Project.GetPropertyValueEscaped (prop), "#6");
70                         
71                         // OK you are fine.
72                         Assert.AreEqual (escaped, ProjectCollection.Escape (value), "#7");
73                 }
74                 
75                 [Test]
76                 public void FullPath ()
77                 {
78                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
79                         var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
80                         var root = ProjectRootElement.Create (xml);
81                         var proj = new Project (root);
82                         proj.FullPath = "ABC";
83                         Assert.IsTrue (proj.FullPath.EndsWith (Path.DirectorySeparatorChar + "ABC"), "#1");
84                         Assert.AreEqual (root.FullPath, proj.FullPath, "#2");
85                 }
86                 
87                 [Test]
88                 public void BuildEmptyProject ()
89                 {
90                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
91                         var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
92                         var root = ProjectRootElement.Create (xml);
93                         root.FullPath = "ProjectTest.BuildEmptyProject.proj";
94             
95                         // This seems to do nothing and still returns true
96                         Assert.IsTrue (new Project (root) { FullPath = "ProjectTest.BuildEmptyProject.1.proj" }.Build (), "#1");
97                         // This seems to fail to find the appropriate target
98                         Assert.IsFalse (new Project (root) { FullPath = "ProjectTest.BuildEmptyProject.2.proj" }.Build ("Build", null), "#2");
99                         // Thus, this tries to build all the targets (empty) and no one failed, so returns true(!)
100                         Assert.IsTrue (new Project (root) { FullPath = "ProjectTest.BuildEmptyProject.3.proj" }.Build (new string [0], null), "#3");
101                         // Actially null "targets" is accepted and returns true(!!)
102                         Assert.IsTrue (new Project (root) { FullPath = "ProjectTest.BuildEmptyProject.4.proj" }.Build ((string []) null, null), "#4");
103                         // matching seems to be blindly done, null string also results in true(!!)
104                         Assert.IsTrue (new Project (root) { FullPath = "ProjectTest.BuildEmptyProject.5.proj" }.Build ((string) null, null), "#5");
105                 }
106                 
107                 [Test]
108                 [ExpectedException (typeof (InvalidProjectFileException))]
109                 public void LoadInvalidProjectForBadCondition ()
110                 {
111                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
112   <PropertyGroup>
113     <Foo>What are 'ESCAPE' &amp; ""EVALUATE"" ? $ # % ^</Foo>
114     <!-- Note that this contains invalid Condition expression, yet ProjectElement.Create() does NOT fail. -->
115     <Baz Condition=""$(Void)=="">$(FOO)</Baz>
116   </PropertyGroup>
117 </Project>";
118                         var reader = XmlReader.Create (new StringReader (xml));
119                         var root = ProjectRootElement.Create (reader);
120                         new Project (root);
121                 }
122                 
123                 [Test]
124                 public void ExpandString ()
125                 {
126                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
127   <PropertyGroup>
128     <Foo>What are 'ESCAPE' &amp; ""EVALUATE"" ? $ # % ^</Foo>
129     <Bar>y</Bar>
130     <Baz Condition=""$(Void)==''"">$(FOO)</Baz>
131   </PropertyGroup>
132 </Project>";
133                         var reader = XmlReader.Create (new StringReader (xml));
134                         var root = ProjectRootElement.Create (reader);
135                         var proj = new Project (root);
136                         root.FullPath = "ProjectTest.ExpandString.proj";
137                         Assert.AreEqual ("xyz", proj.ExpandString ("x$(BAR)z"), "#1");
138                         Assert.AreEqual ("x$(BARz", proj.ExpandString ("x$(BARz"), "#2"); // incomplete
139                         Assert.AreEqual ("xz", proj.ExpandString ("x@(BAR)z"), "#3"); // not an item
140                 }
141                 
142                 [Test]
143                 public void BuildCSharpTargetGetFrameworkPaths ()
144                 {
145             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
146   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
147 </Project>";
148             var xml = XmlReader.Create (new StringReader (project_xml));
149             var root = ProjectRootElement.Create (xml);
150             var proj = new Project (root);
151                         root.FullPath = "ProjectTest.BuildCSharpTargetGetFrameworkPaths.proj";
152                         Assert.IsTrue (proj.Build ("GetFrameworkPaths", new ILogger [] {/*new ConsoleLogger ()*/}));
153                 }
154                 
155                 [Test]
156                 public void BuildCSharpTargetBuild ()
157                 {
158             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
159   <PropertyGroup>
160     <AssemblyName>Foo</AssemblyName>
161   </PropertyGroup>
162   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
163 </Project>";
164             var xml = XmlReader.Create (new StringReader (project_xml));
165             var root = ProjectRootElement.Create (xml);
166                         root.FullPath = "ProjectTest.BuildCSharpTargetBuild.proj";
167                         var proj = new Project (root, null, "4.0");
168                         Assert.IsFalse (proj.Build ("Build", new ILogger [] {/*new ConsoleLogger (LoggerVerbosity.Diagnostic)*/})); // missing mandatory properties
169                 }
170                 
171                 [Test]
172                 public void EvaluateItemConditionThenIgnored ()
173                 {
174             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
175   <PropertyGroup>
176     <P></P>
177   </PropertyGroup>
178   <ItemGroup>
179     <Foo Condition='' Include='x' />
180     <Bar Include='$(P)' />
181     <Baz Include='z' />
182   </ItemGroup>
183 </Project>";
184                         var xml = XmlReader.Create (new StringReader (project_xml));
185                         var root = ProjectRootElement.Create (xml);
186                         var proj = new Project (root);
187                         // note that Foo is ignored BUT Bar is NOT ignored.
188                         Assert.AreEqual (2, proj.ItemsIgnoringCondition.Count, "#1");
189                         Assert.IsNotNull ("Bar", proj.ItemsIgnoringCondition.First ().ItemType, "#2");
190                         Assert.IsNotNull ("Baz", proj.ItemsIgnoringCondition.Last ().ItemType, "#3");
191                 }
192                 
193                 [Test]
194                 public void EvaluateSamePropertiesInOrder ()
195                 {
196                         // used in Microsoft.Common.targets
197             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
198   <PropertyGroup>
199     <BaseIntermediateOutputPath Condition=""'$(BaseIntermediateOutputPath)' == ''"">obj\</BaseIntermediateOutputPath>
200   </PropertyGroup>
201 </Project>";
202                         var xml = XmlReader.Create (new StringReader (project_xml));
203                         var root = ProjectRootElement.Create (xml);
204                         var proj = new Project (root);
205                         Assert.AreEqual ("obj\\", proj.GetPropertyValue ("BaseIntermediateOutputPath"), "#1");
206                 }
207                 
208                 [Test]
209                 public void DirtyMarking ()
210                 {
211                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
212                         var xml = XmlReader.Create (new StringReader (project_xml));
213                         var root = ProjectRootElement.Create (xml);
214                         var proj = new Project (root);
215                         Assert.IsFalse (proj.IsDirty, "#1");
216                         proj.MarkDirty ();
217                         Assert.IsTrue (proj.IsDirty, "#2");
218                 }
219                 
220                 [Test]
221                 public void DirtyMarking2 ()
222                 {
223                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
224                         var xml = XmlReader.Create (new StringReader (project_xml));
225                         var root = ProjectRootElement.Create (xml);
226                         var proj = new Project (root);
227                         proj.DisableMarkDirty = true;
228                         proj.MarkDirty ();
229                         Assert.IsFalse (proj.IsDirty, "#1"); // not rejected, just ignored.
230                         proj.DisableMarkDirty = false;
231                         Assert.IsFalse (proj.IsDirty, "#2"); // not like status pending
232                         proj.MarkDirty ();
233                         Assert.IsTrue (proj.IsDirty, "#3");
234                 }
235                 
236                 [Test]
237                 public void CreateProjectInstance ()
238                 {
239             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
240   <PropertyGroup>
241     <AssemblyName>Foo</AssemblyName>
242   </PropertyGroup>
243   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
244 </Project>";
245             var xml = XmlReader.Create (new StringReader (project_xml));
246             var root = ProjectRootElement.Create (xml);
247                         var proj = new Project (root, null, "4.0");
248                         var inst = proj.CreateProjectInstance ();
249                         Assert.AreEqual ("4.0", inst.ToolsVersion, "#1");
250                 }
251         }
252 }
253