5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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.
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.BuildEngine {
37 public class UsingTaskTest {
43 public void TestAssemblyFile1 ()
45 string documentString = @"
46 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
48 AssemblyFile='Test/resources/TestTasks.dll'
55 engine = new Engine (Consts.BinPath);
56 project = engine.CreateNewProject ();
57 project.LoadXml (documentString);
59 IEnumerator en = project.UsingTasks.GetEnumerator ();
62 UsingTask ut = (UsingTask) en.Current;
64 Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
65 Assert.IsNull (ut.AssemblyName, "A2");
66 Assert.AreEqual ("true", ut.Condition, "A3");
67 Assert.AreEqual (false, ut.IsImported, "A4");
68 Assert.AreEqual ("SimpleTask", ut.TaskName, "A5");
72 public void TestAssemblyFile2 ()
74 string documentString = @"
75 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
77 AssemblyFile='Test/resources/TestTasks.dll'
83 engine = new Engine (Consts.BinPath);
84 project = engine.CreateNewProject ();
85 project.LoadXml (documentString);
87 IEnumerator en = project.UsingTasks.GetEnumerator ();
90 UsingTask ut = (UsingTask) en.Current;
92 Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
93 Assert.IsNull (ut.AssemblyName, "A2");
94 Assert.AreEqual (null, ut.Condition, "A3");
95 Assert.AreEqual (false, ut.IsImported, "A4");
96 Assert.AreEqual ("SimpleTask", ut.TaskName, "A5");
100 // NOTE: quite hacky test, it works because MSBuild doesn't check type of task at loading
101 public void TestAssemblyName ()
103 string documentString = @"
104 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
106 AssemblyName='System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
107 TaskName='System.Uri'
113 engine = new Engine (Consts.BinPath);
114 project = engine.CreateNewProject ();
115 project.LoadXml (documentString);
117 IEnumerator en = project.UsingTasks.GetEnumerator ();
120 UsingTask ut = (UsingTask) en.Current;
122 Assert.AreEqual ("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", ut.AssemblyName, "A1");
123 Assert.IsNull (ut.AssemblyFile, "A2");
124 Assert.AreEqual ("true", ut.Condition, "A3");
125 Assert.AreEqual (false, ut.IsImported, "A4");
126 Assert.AreEqual ("System.Uri", ut.TaskName, "A5");
130 [ExpectedException (typeof (InvalidProjectFileException))]
131 public void TestTaskName ()
133 string documentString = @"
134 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
136 AssemblyFile='Test/resources/TestTasks.dll'
141 engine = new Engine (Consts.BinPath);
142 project = engine.CreateNewProject ();
143 project.LoadXml (documentString);
147 [ExpectedException (typeof (InvalidProjectFileException))]
148 public void TestAssemblyNameOrAssemblyFile1 ()
150 string documentString = @"
151 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
153 TaskName='SimpleTask'
158 engine = new Engine (Consts.BinPath);
159 project = engine.CreateNewProject ();
160 project.LoadXml (documentString);
164 public void TestAssemblyNameOrAssemblyFileConditionFalse ()
166 string documentString = @"
167 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
169 TaskName='SimpleTask'
175 engine = new Engine (Consts.BinPath);
176 project = engine.CreateNewProject ();
178 project.LoadXml (documentString);
179 } catch (InvalidProjectFileException) {
182 Assert.Fail ("Project load should've failed");
186 [ExpectedException (typeof (InvalidProjectFileException))]
187 public void TestAssemblyNameOrAssemblyFile2 ()
189 string documentString = @"
190 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
192 TaskName='SimpleTask'
199 engine = new Engine (Consts.BinPath);
200 project = engine.CreateNewProject ();
201 project.LoadXml (documentString);
205 public void TestDuplicate1 ()
207 string documentString = @"
208 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
210 AssemblyFile='Test/resources/TestTasks.dll'
211 TaskName='TrueTestTask'
214 AssemblyFile='Test/resources/TestTasks.dll'
215 TaskName='TrueTestTask'
224 engine = new Engine (Consts.BinPath);
225 project = engine.CreateNewProject ();
226 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
227 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
228 engine.RegisterLogger (logger);
230 project.LoadXml (documentString);
232 if (!project.Build ("1")) {
233 logger.DumpMessages ();
234 Assert.Fail ("Build failed");
237 Assert.AreEqual (2, project.UsingTasks.Count, "A0");
239 foreach (UsingTask ut in project.UsingTasks) {
240 Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
241 Assert.IsNull (ut.AssemblyName, "A2");
242 Assert.AreEqual (null, ut.Condition, "A3");
243 Assert.AreEqual (false, ut.IsImported, "A4");
244 Assert.AreEqual ("TrueTestTask", ut.TaskName, "A5");
250 public void TestLazyLoad1 ()
252 string documentString = @"
253 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
255 AssemblyFile='NonExistantAssembly.dll'
256 TaskName='SimpleTask'
259 <Message Text='hello'/>
262 <SimpleTask Foo='bar'/>
267 engine = new Engine (Consts.BinPath);
268 project = engine.CreateNewProject ();
269 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
270 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
271 engine.RegisterLogger (logger);
273 project.LoadXml (documentString);
275 if (!project.Build ("1")) {
276 logger.DumpMessages ();
277 Assert.Fail ("Build failed");
280 if (project.Build ("2"))
281 Assert.Fail ("Build should've failed, as a task from a nonexistant assembly is referenced");
284 IEnumerator en = project.UsingTasks.GetEnumerator ();
287 UsingTask ut = (UsingTask) en.Current;
289 Assert.AreEqual ("NonExistantAssembly.dll", ut.AssemblyFile, "A1");
290 Assert.IsNull (ut.AssemblyName, "A2");
291 Assert.AreEqual (null, ut.Condition, "A3");
292 Assert.AreEqual (false, ut.IsImported, "A4");
293 Assert.AreEqual ("SimpleTask", ut.TaskName, "A5");
297 public void TestLazyLoad2 ()
299 string documentString = @"
300 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
302 AssemblyFile='Test/resources/TestTasks.dll'
303 TaskName='Another.SameTask'
306 AssemblyFile='Test/resources/TestTasks.dll'
307 TaskName='Other.SameTask'
312 <Output TaskParameter='OutputString' ItemName='I0'/>
315 <Output TaskParameter='OutputString' ItemName='I1'/>
317 <Message Text='I0: @(I0) I1: @(I1)'/>
322 engine = new Engine (Consts.BinPath);
323 project = engine.CreateNewProject ();
324 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
325 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
326 engine.RegisterLogger (logger);
328 project.LoadXml (documentString);
330 if (!project.Build ("1")) {
331 logger.DumpMessages ();
332 Assert.Fail ("Build failed");
335 logger.CheckLoggedMessageHead ("I0: Other.SameTask I1: Another.SameTask", "A1");
339 public void TestLazyLoad3 ()
341 string documentString = @"
342 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
344 AssemblyFile='Test/resources/TestTasks.dll'
345 TaskName='Another.SameTask'
355 engine = new Engine (Consts.BinPath);
356 project = engine.CreateNewProject ();
357 MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
358 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
359 engine.RegisterLogger (logger);
361 project.LoadXml (documentString);
363 IEnumerator en = project.UsingTasks.GetEnumerator ();
366 UsingTask ut = (UsingTask) en.Current;
368 Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
369 Assert.IsNull (ut.AssemblyName, "A2");
370 Assert.AreEqual ("false", ut.Condition, "A3");
371 Assert.AreEqual (false, ut.IsImported, "A4");
372 Assert.AreEqual ("Another.SameTask", ut.TaskName, "A5");
374 if (project.Build ("1")) {
375 logger.DumpMessages ();
376 Assert.Fail ("Build should've failed");