Revert "Merge pull request #5330 from alexanderkyte/dedup_mkbundle"
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Construction / ProjectRootElementTest.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Xml;
5 using Microsoft.Build.Construction;
6 using NUnit.Framework;
7 using Microsoft.Build.Evaluation;
8 using Microsoft.Build.Exceptions;
9
10 namespace MonoTests.Microsoft.Build.Construction
11 {
12         [TestFixture]
13         public class ProjectRootElementTest
14         {
15                 const string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
16
17                 [Test]
18                 [ExpectedException (typeof (UriFormatException))]
19                 [Category ("NotWorking")] // URL is constructed for ElementLocation, which we don't support yet.
20                 public void CreateExpectsAbsoluteUri ()
21                 {
22                         var xml = XmlReader.Create (new StringReader (empty_project_xml), null, "foo.xml");
23                         ProjectRootElement.Create (xml);
24                 }
25
26                 [Test]
27                 public void CreateAndPaths ()
28                 {
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");
35                 }
36
37                 [Test]
38                 public void FullPathSetter ()
39                 {
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");
45                 }
46
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void FullPathSetNull ()
50                 {
51                         ProjectRootElement.Create ().FullPath = null;
52                 }
53
54                 [Test]
55                 public void InvalidProject ()
56                 {
57                         try {
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");
64                         }
65                 }
66
67                 [Test]
68                 public void CreateWithXmlLoads ()
69                 {
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");
74                 }
75                 
76                 [Test]
77                 public void ToolsVersionDefault ()
78                 {
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");
83                 }
84                 
85                 [Test]
86                 public void ToolsVersionIsEmptyWithXml ()
87                 {
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");
92                 }
93
94                 [Test]
95                 public void LoadUnknownChild ()
96                 {
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");
99                         try {
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");
106                         }
107                 }
108
109                 [Test]
110                 public void LoadUnregisteredItem ()
111                 {
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");
116                 }
117                 
118                 [Test]
119                 public void LoadInvalidProjectForBadCondition ()
120                 {
121                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
122   <PropertyGroup>
123     <Foo>What are 'ESCAPE' &amp; ""EVALUATE"" ? $ # % ^</Foo>
124     <!-- Note that this contains invalid Condition expression. Project.ctor() fails to load. -->
125     <Baz Condition=""$(Void)=="">$(FOO)</Baz>
126   </PropertyGroup>
127 </Project>";
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");
132                 }
133                 
134                 [Test]
135                 [ExpectedException (typeof (InvalidProjectFileException))]
136                 public void LoadInvalidProjectGroupInProjectGroup ()
137                 {
138             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
139   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
140   <PropertyGroup>
141     <Foo>Bar</Foo>
142     <PropertyGroup>
143       <X>x</X>
144       <Y>y</Y>
145       <Z>z</Z>
146     </PropertyGroup>
147   </PropertyGroup>
148 </Project>";
149             var xml = XmlReader.Create (new StringReader (project_xml));
150             ProjectRootElement.Create (xml);
151                 }
152                 
153                 [Test]
154                 [ExpectedException (typeof (InvalidProjectFileException))]
155                 public void LoadInvalidItemGroupInProjectGroup ()
156                 {
157             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
158   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
159   <PropertyGroup>
160     <Foo>Bar</Foo>
161     <ItemGroup/>
162   </PropertyGroup>
163 </Project>";
164             var xml = XmlReader.Create (new StringReader (project_xml));
165             ProjectRootElement.Create (xml);
166                 }
167                 
168                 [Test]
169                 public void ChildAndAllChildren ()
170                 {
171             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
172   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
173   <PropertyGroup>
174     <Foo>Bar</Foo>
175     <Item/>
176   </PropertyGroup>
177 </Project>";
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");
183                 }
184                 
185                 [Test]
186                 [ExpectedException (typeof (InvalidOperationException))]
187                 public void SaveWithoutFullPath ()
188                 {
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);
192                         root.Save ();
193                 }
194                 
195                 [Test]
196                 public void SaveToWriter ()
197                 {
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 ();
202                         root.Save (sw);
203                         // CRLF? mmm, k...
204                         Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + project_xml.Replace ('\'', '"'), sw.ToString (), "#1");
205                 }
206                 
207                 [Test]
208                 [ExpectedException (typeof (InvalidProjectFileException))]
209                 public void ImportsMissingProject ()
210                 {
211                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
212   <Import Project='' />
213 </Project>";
214                         var xml = XmlReader.Create (new StringReader (project_xml));
215                         ProjectRootElement.Create (xml);
216                 }
217         }
218 }