1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
using System;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.Build.Construction;
using NUnit.Framework;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;
namespace MonoTests.Microsoft.Build.Construction
{
[TestFixture]
public class ProjectRootElementTest
{
const string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
[Test]
[ExpectedException (typeof (UriFormatException))]
[Category ("NotWorking")] // URL is constructed for ElementLocation, which we don't support yet.
public void CreateExpectsAbsoluteUri ()
{
var xml = XmlReader.Create (new StringReader (empty_project_xml), null, "foo.xml");
ProjectRootElement.Create (xml);
}
[Test]
public void CreateAndPaths ()
{
Assert.IsNull (ProjectRootElement.Create ().FullPath, "#1");
var xml = XmlReader.Create (new StringReader (empty_project_xml), null, "file:///foo.xml");
// This creator does not fill FullPath...
var root = ProjectRootElement.Create (xml);
Assert.IsNull (root.FullPath, "#2");
Assert.AreEqual (Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath), root.DirectoryPath, "#3");
}
[Test]
public void FullPathSetter ()
{
var root = ProjectRootElement.Create ();
root.FullPath = "test" + Path.DirectorySeparatorChar + "foo.xml";
var full = Path.Combine (Path.GetDirectoryName (new Uri (GetType ().Assembly.CodeBase).LocalPath), "test", "foo.xml");
Assert.AreEqual (full, root.FullPath, "#1");
Assert.AreEqual (Path.GetDirectoryName (full), root.DirectoryPath, "#1");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void FullPathSetNull ()
{
ProjectRootElement.Create ().FullPath = null;
}
[Test]
public void InvalidProject ()
{
try {
ProjectRootElement.Create (XmlReader.Create (new StringReader (" <root/>")));
Assert.Fail ("should throw InvalidProjectFileException");
} catch (InvalidProjectFileException ex) {
#if NET_4_5
Assert.AreEqual (1, ex.LineNumber, "#1");
// it is very interesting, but unlike XmlReader.LinePosition it returns the position for '<'.
Assert.AreEqual (2, ex.ColumnNumber, "#2");
#endif
}
}
[Test]
public void CreateWithXmlLoads ()
{
string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
var root = ProjectRootElement.Create (xml);
Assert.AreEqual (1, root.Items.Count, "#1");
}
[Test]
public void ToolsVersionDefault ()
{
var g = ProjectCollection.GlobalProjectCollection;
var root = ProjectRootElement.Create ();
// this will be wrong in the future version, but since .NET 4.5 still expects "4.0" we can't say for sure.
Assert.AreEqual ("4.0", root.ToolsVersion, "#1");
}
[Test]
public void ToolsVersionIsEmptyWithXml ()
{
string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><None Include='bar.txt' /></ItemGroup></Project>";
var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
var root = ProjectRootElement.Create (xml);
Assert.AreEqual (string.Empty, root.ToolsVersion, "#1");
}
[Test]
public void LoadUnknownChild ()
{
string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><Unknown /></Project>";
var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
try {
ProjectRootElement.Create (xml);
Assert.Fail ("should throw InvalidProjectFileException");
} catch (InvalidProjectFileException ex) {
#if NET_4_5
Assert.AreEqual (1, ex.LineNumber, "#1");
// unlike unexpected element case which returned the position for '<', it does return the name start char...
Assert.AreEqual (70, ex.ColumnNumber, "#2");
#endif
}
}
[Test]
public void LoadUnregisteredItem ()
{
string project_xml_1 = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'><ItemGroup><UnregisteredItem Include='bar.txt' /></ItemGroup></Project>";
var xml = XmlReader.Create (new StringReader (project_xml_1), null, "file://localhost/foo.xml");
var root = ProjectRootElement.Create (xml);
Assert.AreEqual (1, root.Items.Count, "#1");
}
[Test]
public void LoadInvalidProjectForBadCondition ()
{
string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<Foo>What are 'ESCAPE' & ""EVALUATE"" ? $ # % ^</Foo>
<!-- Note that this contains invalid Condition expression. Project.ctor() fails to load. -->
<Baz Condition=""$(Void)=="">$(FOO)</Baz>
</PropertyGroup>
</Project>";
var path = "file://localhost/foo.xml";
var reader = XmlReader.Create (new StringReader (xml), null, path);
var root = ProjectRootElement.Create (reader);
Assert.AreEqual (2, root.Properties.Count, "#1");
}
[Test]
[ExpectedException (typeof (InvalidProjectFileException))]
public void LoadInvalidProjectGroupInProjectGroup ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
<PropertyGroup>
<Foo>Bar</Foo>
<PropertyGroup>
<X>x</X>
<Y>y</Y>
<Z>z</Z>
</PropertyGroup>
</PropertyGroup>
</Project>";
var xml = XmlReader.Create (new StringReader (project_xml));
ProjectRootElement.Create (xml);
}
[Test]
[ExpectedException (typeof (InvalidProjectFileException))]
public void LoadInvalidItemGroupInProjectGroup ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
<PropertyGroup>
<Foo>Bar</Foo>
<ItemGroup/>
</PropertyGroup>
</Project>";
var xml = XmlReader.Create (new StringReader (project_xml));
ProjectRootElement.Create (xml);
}
[Test]
public void ChildAndAllChildren ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
<PropertyGroup>
<Foo>Bar</Foo>
<Item/>
</PropertyGroup>
</Project>";
var xml = XmlReader.Create (new StringReader (project_xml));
var root = ProjectRootElement.Create (xml);
Assert.AreEqual (2, root.Children.Count, "#1");
// AllChildren expands descendants
Assert.AreEqual (4, root.AllChildren.Count (), "#2");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void SaveWithoutFullPath ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
var root = ProjectRootElement.Create (xml);
root.Save ();
}
[Test]
public void SaveToWriter ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
var root = ProjectRootElement.Create (xml);
var sw = new StringWriter ();
root.Save (sw);
// CRLF? mmm, k...
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + project_xml.Replace ('\'', '"'), sw.ToString (), "#1");
}
[Test]
[ExpectedException (typeof (InvalidProjectFileException))]
public void ImportsMissingProject ()
{
string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Import Project='' />
</Project>";
var xml = XmlReader.Create (new StringReader (project_xml));
ProjectRootElement.Create (xml);
}
}
}
|