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.
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;
39 namespace MonoTests.Microsoft.Build.Evaluation
42 public class ProjectTest
45 public void EscapeDoesWTF ()
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'>
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");
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");
69 Assert.AreEqual (value, Project.GetPropertyValueEscaped (prop), "#6");
72 Assert.AreEqual (escaped, ProjectCollection.Escape (value), "#7");
76 public void FullPath ()
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");
88 public void BuildEmptyProject ()
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";
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");
108 [ExpectedException (typeof (InvalidProjectFileException))]
109 public void LoadInvalidProjectForBadCondition ()
111 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
113 <Foo>What are 'ESCAPE' & ""EVALUATE"" ? $ # % ^</Foo>
114 <!-- Note that this contains invalid Condition expression, yet ProjectElement.Create() does NOT fail. -->
115 <Baz Condition=""$(Void)=="">$(FOO)</Baz>
118 var reader = XmlReader.Create (new StringReader (xml));
119 var root = ProjectRootElement.Create (reader);
124 public void ExpandString ()
126 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
128 <Foo>What are 'ESCAPE' & ""EVALUATE"" ? $ # % ^</Foo>
130 <Baz Condition=""$(Void)==''"">$(FOO)</Baz>
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
143 public void BuildCSharpTargetGetFrameworkPaths ()
145 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
146 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
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 ()*/}));
156 public void BuildCSharpTargetBuild ()
158 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
160 <AssemblyName>Foo</AssemblyName>
162 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
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
172 public void EvaluateItemConditionThenIgnored ()
174 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
179 <Foo Condition='' Include='x' />
180 <Bar Include='$(P)' />
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");
194 public void EvaluateSamePropertiesInOrder ()
196 // used in Microsoft.Common.targets
197 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
199 <BaseIntermediateOutputPath Condition=""'$(BaseIntermediateOutputPath)' == ''"">obj\</BaseIntermediateOutputPath>
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");
209 public void DirtyMarking ()
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");
217 Assert.IsTrue (proj.IsDirty, "#2");
221 public void DirtyMarking2 ()
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;
229 Assert.IsFalse (proj.IsDirty, "#1"); // not rejected, just ignored.
230 proj.DisableMarkDirty = false;
231 Assert.IsFalse (proj.IsDirty, "#2"); // not like status pending
233 Assert.IsTrue (proj.IsDirty, "#3");
237 public void CreateProjectInstance ()
239 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
241 <AssemblyName>Foo</AssemblyName>
243 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
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");