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);
33 Assert.IsNull (root.FullPath, "#2");
34 Assert.AreEqual (Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath), root.DirectoryPath, "#3");
38 public void FullPathSetter ()
40 var root = ProjectRootElement.Create ();
41 root.FullPath = "test" + Path.DirectorySeparatorChar + "foo.xml";
42 var full = Path.Combine (Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath), "test", "foo.xml");
43 Assert.AreEqual (full, root.FullPath, "#1");
44 Assert.AreEqual (Path.GetDirectoryName (full), root.DirectoryPath, "#1");
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void FullPathSetNull ()
51 ProjectRootElement.Create ().FullPath = null;
55 public void InvalidProject ()
58 ProjectRootElement.Create (XmlReader.Create (new StringReader (" <root/>")));
59 Assert.Fail ("should throw InvalidProjectFileException");
60 } catch (InvalidProjectFileException ex) {
61 Assert.AreEqual (1, ex.LineNumber, "#1");
62 // it is very interesting, but unlike XmlReader.LinePosition it returns the position for '<'.
63 Assert.AreEqual (2, ex.ColumnNumber, "#2");
68 public void CreateWithXmlLoads ()
70 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
71 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
72 var root = ProjectRootElement.Create (xml);
73 Assert.AreEqual (1, root.Items.Count, "#1");
77 public void ToolsVersionDefault ()
79 var g = ProjectCollection.GlobalProjectCollection;
80 var root = ProjectRootElement.Create ();
81 // this will be wrong in the future version, but since .NET 4.5 still expects "4.0" we can't say for sure.
82 Assert.AreEqual ("4.0", root.ToolsVersion, "#1");
86 public void ToolsVersionIsEmptyWithXml ()
88 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
89 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
90 var root = ProjectRootElement.Create (xml);
91 Assert.AreEqual (string.Empty, root.ToolsVersion, "#1");
95 public void LoadUnknownChild ()
97 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><Unknown /></Project>";
98 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
100 ProjectRootElement.Create (xml);
101 Assert.Fail ("should throw InvalidProjectFileException");
102 } catch (InvalidProjectFileException ex) {
103 Assert.AreEqual (1, ex.LineNumber, "#1");
104 // unlike unexpected element case which returned the position for '<', it does return the name start char...
105 Assert.AreEqual (70, ex.ColumnNumber, "#2");
110 public void LoadUnregisteredItem ()
112 string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><UnregisteredItem Include='bar.txt' /></ItemGroup></Project>";
113 var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
114 var root = ProjectRootElement.Create (xml);
115 Assert.AreEqual (1, root.Items.Count, "#1");
119 public void LoadInvalidProjectForBadCondition ()
121 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
123 <Foo>What are 'ESCAPE' & ""EVALUATE"" ? $ # % ^</Foo>
124 <!-- Note that this contains invalid Condition expression. Project.ctor() fails to load. -->
125 <Baz Condition=""$(Void)=="">$(FOO)</Baz>
128 var path = "file://localhost/foo.xml";
129 var reader = XmlReader.Create (new StringReader (xml), null, path);
130 var root = ProjectRootElement.Create (reader);
131 Assert.AreEqual (2, root.Properties.Count, "#1");
135 [ExpectedException (typeof (InvalidProjectFileException))]
136 public void LoadInvalidProjectGroupInProjectGroup ()
138 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
139 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
149 var xml = XmlReader.Create (new StringReader (project_xml));
150 ProjectRootElement.Create (xml);
154 [ExpectedException (typeof (InvalidProjectFileException))]
155 public void LoadInvalidItemGroupInProjectGroup ()
157 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
158 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
164 var xml = XmlReader.Create (new StringReader (project_xml));
165 ProjectRootElement.Create (xml);
169 public void ChildAndAllChildren ()
171 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
172 <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
178 var xml = XmlReader.Create (new StringReader (project_xml));
179 var root = ProjectRootElement.Create (xml);
180 Assert.AreEqual (2, root.Children.Count, "#1");
181 // AllChildren expands descendants
182 Assert.AreEqual (4, root.AllChildren.Count (), "#2");
186 [ExpectedException (typeof (InvalidOperationException))]
187 public void SaveWithoutFullPath ()
189 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
190 var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
191 var root = ProjectRootElement.Create (xml);
196 public void SaveToWriter ()
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);
201 var sw = new StringWriter ();
204 Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + project_xml.Replace ('\'', '"'), sw.ToString (), "#1");
208 [ExpectedException (typeof (InvalidProjectFileException))]
209 public void ImportsMissingProject ()
211 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
212 <Import Project='' />
214 var xml = XmlReader.Create (new StringReader (project_xml));
215 ProjectRootElement.Create (xml);