5 using Microsoft.Build.Construction;
7 using Microsoft.Build.Evaluation;
8 using Microsoft.Build.Exceptions;
10 namespace MonoTests.Microsoft.Build.Construction
13 public class ProjectRootElementTest
15 const string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
18 [ExpectedException (typeof (UriFormatException))]
19 [Category ("NotWorking")] // URL is constructed for ElementLocation, which we don't support yet.
20 public void CreateExpectsAbsoluteUri ()
22 var xml = XmlReader.Create (new StringReader (empty_project_xml), null, "foo.xml");
23 ProjectRootElement.Create (xml);
27 public void CreateAndPaths ()
29 Assert.IsNull (ProjectRootElement.Create ().FullPath, "#1");
30 var xml = XmlReader.Create (new StringReader (empty_project_xml), null, "file:///foo.xml");
31 // This creator does not fill FullPath...
32 var root = ProjectRootElement.Create (xml);
34 // Expected to run from mcs/class/lib/profile
35 var dir_name = Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath);
36 var namespace_path = Path.Combine (Directory.GetParent (dir_name).Parent.FullName, "Microsoft.Build");
38 Assert.IsNull (root.FullPath, "#2");
39 Assert.AreEqual (namespace_path, root.DirectoryPath, "#3");
43 public void FullPathSetter ()
45 var root = ProjectRootElement.Create ();
46 root.FullPath = "test" + Path.DirectorySeparatorChar + "foo.xml";
48 // Expected to run from mcs/class/lib/profile
49 var dir_name = Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath);
50 var namespace_path = Path.Combine (Directory.GetParent (dir_name).Parent.FullName, "Microsoft.Build");
51 var full = Path.Combine (namespace_path, "test", "foo.xml");
52 Assert.AreEqual (full, root.FullPath, "#1");
53 Assert.AreEqual (Path.GetDirectoryName (full), root.DirectoryPath, "#1");
57 [ExpectedException (typeof (ArgumentNullException))]
58 public void FullPathSetNull ()
60 ProjectRootElement.Create ().FullPath = null;
64 public void InvalidProject ()
67 ProjectRootElement.Create (XmlReader.Create (new StringReader (" <root/>")));
68 Assert.Fail ("should throw InvalidProjectFileException");
69 } catch (InvalidProjectFileException ex) {
70 Assert.AreEqual (1, ex.LineNumber, "#1");
71 // it is very interesting, but unlike XmlReader.LinePosition it returns the position for '<'.
72 Assert.AreEqual (2, ex.ColumnNumber, "#2");
77 public void CreateWithXmlLoads ()
79 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
80 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
81 var root = ProjectRootElement.Create (xml);
82 Assert.AreEqual (1, root.Items.Count, "#1");
86 public void ToolsVersionDefault ()
88 var g = ProjectCollection.GlobalProjectCollection;
89 var root = ProjectRootElement.Create ();
90 // this will be wrong in the future version, but since .NET 4.5 still expects "4.0" we can't say for sure.
91 Assert.AreEqual ("4.0", root.ToolsVersion, "#1");
95 public void ToolsVersionIsEmptyWithXml ()
97 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
98 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
99 var root = ProjectRootElement.Create (xml);
100 Assert.AreEqual (string.Empty, root.ToolsVersion, "#1");
104 public void LoadUnknownChild ()
106 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><Unknown /></Project>";
107 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
109 ProjectRootElement.Create (xml);
110 Assert.Fail ("should throw InvalidProjectFileException");
111 } catch (InvalidProjectFileException ex) {
112 Assert.AreEqual (1, ex.LineNumber, "#1");
113 // unlike unexpected element case which returned the position for '<', it does return the name start char...
114 Assert.AreEqual (70, ex.ColumnNumber, "#2");
119 public void LoadUnregisteredItem ()
121 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><UnregisteredItem Include='bar.txt' /></ItemGroup></Project>";
122 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
123 var root = ProjectRootElement.Create (xml);
124 Assert.AreEqual (1, root.Items.Count, "#1");
128 public void LoadInvalidProjectForBadCondition ()
130 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
132 <Foo>What are 'ESCAPE' & ""EVALUATE"" ? $ # % ^</Foo>
133 <!-- Note that this contains invalid Condition expression. Project.ctor() fails to load. -->
134 <Baz Condition=""$(Void)=="">$(FOO)</Baz>
137 var path = "file://localhost/foo.xml";
138 var reader = XmlReader.Create (new StringReader (xml), null, path);
139 var root = ProjectRootElement.Create (reader);
140 Assert.AreEqual (2, root.Properties.Count, "#1");
144 [ExpectedException (typeof (InvalidProjectFileException))]
145 public void LoadInvalidProjectGroupInProjectGroup ()
147 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
148 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
158 var xml = XmlReader.Create (new StringReader (project_xml));
159 ProjectRootElement.Create (xml);
163 [ExpectedException (typeof (InvalidProjectFileException))]
164 public void LoadInvalidItemGroupInProjectGroup ()
166 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
167 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
173 var xml = XmlReader.Create (new StringReader (project_xml));
174 ProjectRootElement.Create (xml);
178 public void ChildAndAllChildren ()
180 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
181 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
187 var xml = XmlReader.Create (new StringReader (project_xml));
188 var root = ProjectRootElement.Create (xml);
189 Assert.AreEqual (2, root.Children.Count, "#1");
190 // AllChildren expands descendants
191 Assert.AreEqual (4, root.AllChildren.Count (), "#2");
195 [ExpectedException (typeof (InvalidOperationException))]
196 public void SaveWithoutFullPath ()
198 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
199 var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
200 var root = ProjectRootElement.Create (xml);
205 public void SaveToWriter ()
207 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
208 var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
209 var root = ProjectRootElement.Create (xml);
210 var sw = new StringWriter ();
213 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + project_xml.Replace ('\'', '"'), sw.ToString (), "#1");
217 [ExpectedException (typeof (InvalidProjectFileException))]
218 public void ImportsMissingProject ()
220 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
221 <Import Project='' />
223 var xml = XmlReader.Create (new StringReader (project_xml));
224 ProjectRootElement.Create (xml);