Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / UsingTaskTest.cs
1 //
2 // UsingTaskTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 using System;
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
34
35 namespace MonoTests.Microsoft.Build.BuildEngine {
36         [TestFixture]
37         public class UsingTaskTest {
38                 
39                 Engine          engine;
40                 Project         project;
41                 
42                 [Test]
43                 public void TestAssemblyFile1 ()
44                 {
45                         string documentString = @"
46                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
47                                         <UsingTask
48                                                 AssemblyFile='Test/resources/TestTasks.dll'
49                                                 TaskName='SimpleTask'
50                                                 Condition='true'
51                                         />
52                                 </Project>
53                         ";
54
55                         engine = new Engine (Consts.BinPath);
56                         project = engine.CreateNewProject ();
57                         project.LoadXml (documentString);
58                         
59                         IEnumerator en = project.UsingTasks.GetEnumerator ();
60                         en.MoveNext ();
61                         
62                         UsingTask ut = (UsingTask) en.Current;
63                         
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");
69                 }
70
71                 [Test]
72                 public void TestAssemblyFile2 ()
73                 {
74                         string documentString = @"
75                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
76                                         <UsingTask
77                                                 AssemblyFile='Test/resources/TestTasks.dll'
78                                                 TaskName='SimpleTask'
79                                         />
80                                 </Project>
81                         ";
82
83                         engine = new Engine (Consts.BinPath);
84                         project = engine.CreateNewProject ();
85                         project.LoadXml (documentString);
86                         
87                         IEnumerator en = project.UsingTasks.GetEnumerator ();
88                         en.MoveNext ();
89                         
90                         UsingTask ut = (UsingTask) en.Current;
91                         
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");
97                 }
98
99                 [Test]
100                 // NOTE: quite hacky test, it works because MSBuild doesn't check type of task at loading
101                 public void TestAssemblyName ()
102                 {
103                         string documentString = @"
104                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
105                                         <UsingTask
106                                                 AssemblyName='System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
107                                                 TaskName='System.Uri'
108                                                 Condition='true'
109                                         />
110                                 </Project>
111                         ";
112
113                         engine = new Engine (Consts.BinPath);
114                         project = engine.CreateNewProject ();
115                         project.LoadXml (documentString);
116                         
117                         IEnumerator en = project.UsingTasks.GetEnumerator ();
118                         en.MoveNext ();
119                         
120                         UsingTask ut = (UsingTask) en.Current;
121                         
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");
127                 }
128                 
129                 [Test]
130                 [ExpectedException (typeof (InvalidProjectFileException))]
131                 public void TestTaskName ()
132                 {
133                         string documentString = @"
134                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
135                                         <UsingTask
136                                                 AssemblyFile='Test/resources/TestTasks.dll'
137                                         />
138                                 </Project>
139                         ";
140
141                         engine = new Engine (Consts.BinPath);
142                         project = engine.CreateNewProject ();
143                         project.LoadXml (documentString);
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (InvalidProjectFileException))]
148                 public void TestAssemblyNameOrAssemblyFile1 ()
149                 {
150                         string documentString = @"
151                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
152                                         <UsingTask
153                                                 TaskName='SimpleTask'
154                                         />
155                                 </Project>
156                         ";
157
158                         engine = new Engine (Consts.BinPath);
159                         project = engine.CreateNewProject ();
160                         project.LoadXml (documentString);
161                 }
162
163                 [Test]
164                 public void TestAssemblyNameOrAssemblyFileConditionFalse ()
165                 {
166                         string documentString = @"
167                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
168                                         <UsingTask
169                                                 TaskName='SimpleTask'
170                                                 Condition='false'
171                                         />
172                                 </Project>
173                         ";
174
175                         engine = new Engine (Consts.BinPath);
176                         project = engine.CreateNewProject ();
177                         try {
178                                 project.LoadXml (documentString);
179                         } catch (InvalidProjectFileException) {
180                                 return;
181                         }
182                         Assert.Fail ("Project load should've failed");
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (InvalidProjectFileException))]
187                 public void TestAssemblyNameOrAssemblyFile2 ()
188                 {
189                         string documentString = @"
190                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
191                                         <UsingTask
192                                                 TaskName='SimpleTask'
193                                                 AssemblyFile='A'
194                                                 AssemblyName='B'
195                                         />
196                                 </Project>
197                         ";
198
199                         engine = new Engine (Consts.BinPath);
200                         project = engine.CreateNewProject ();
201                         project.LoadXml (documentString);
202                 }
203
204                 [Test]
205                 [Category ("NotDotNet")]
206                 public void TestDuplicate1 ()
207                 {
208                         string documentString = @"
209                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
210                                         <UsingTask
211                                                 AssemblyFile='Test/resources/TestTasks.dll'
212                                                 TaskName='TrueTestTask'
213                                         />
214                                         <UsingTask
215                                                 AssemblyFile='Test/resources/TestTasks.dll'
216                                                 TaskName='TrueTestTask'
217                                         />
218
219                                         <Target Name='1'>
220                                                 <TrueTestTask/>
221                                         </Target>
222                                 </Project>
223                         ";
224
225                         engine = new Engine (Consts.BinPath);
226                         project = engine.CreateNewProject ();
227                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
228                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
229                         engine.RegisterLogger (logger);
230
231                         project.LoadXml (documentString);
232
233                         if (!project.Build ("1")) {
234                                 logger.DumpMessages ();
235                                 Assert.Fail ("Build failed");
236                         }
237
238                         Assert.AreEqual (2, project.UsingTasks.Count, "A0");
239
240                         foreach (UsingTask ut in project.UsingTasks) {
241                                 Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
242                                 Assert.IsNull (ut.AssemblyName, "A2");
243                                 Assert.AreEqual (null, ut.Condition, "A3");
244                                 Assert.AreEqual (false, ut.IsImported, "A4");
245                                 Assert.AreEqual ("TrueTestTask", ut.TaskName, "A5");
246                         }
247                 }
248
249
250                 [Test]
251                 public void TestLazyLoad1 ()
252                 {
253                         string documentString = @"
254                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
255                                         <UsingTask
256                                                 AssemblyFile='NonExistantAssembly.dll'
257                                                 TaskName='SimpleTask'
258                                         />
259                                         <Target Name='1'>
260                                                 <Message Text='hello'/>
261                                         </Target>
262                                         <Target Name='2'>
263                                                 <SimpleTask Foo='bar'/>
264                                         </Target>
265                                 </Project>
266                         ";
267
268                         engine = new Engine (Consts.BinPath);
269                         project = engine.CreateNewProject ();
270                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
271                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
272                         engine.RegisterLogger (logger);
273
274                         project.LoadXml (documentString);
275
276                         if (!project.Build ("1")) {
277                                 logger.DumpMessages ();
278                                 Assert.Fail ("Build failed");
279                         }
280
281                         if (project.Build ("2"))
282                                 Assert.Fail ("Build should've failed, as a task from a nonexistant assembly is referenced");
283
284
285                         IEnumerator en = project.UsingTasks.GetEnumerator ();
286                         en.MoveNext ();
287
288                         UsingTask ut = (UsingTask) en.Current;
289
290                         Assert.AreEqual ("NonExistantAssembly.dll", ut.AssemblyFile, "A1");
291                         Assert.IsNull (ut.AssemblyName, "A2");
292                         Assert.AreEqual (null, ut.Condition, "A3");
293                         Assert.AreEqual (false, ut.IsImported, "A4");
294                         Assert.AreEqual ("SimpleTask", ut.TaskName, "A5");
295                 }
296
297                 [Test]
298                 [Category ("NotDotNet")]
299                 public void TestLazyLoad2 ()
300                 {
301                         string documentString = @"
302                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
303                                         <UsingTask
304                                                 AssemblyFile='Test/resources/TestTasks.dll'
305                                                 TaskName='Another.SameTask'
306                                         />
307                                         <UsingTask
308                                                 AssemblyFile='Test/resources/TestTasks.dll'
309                                                 TaskName='Other.SameTask'
310                                         />
311
312                                         <Target Name='1'>
313                                                 <Other.SameTask>
314                                                         <Output TaskParameter='OutputString' ItemName='I0'/>
315                                                 </Other.SameTask>
316                                                 <Another.SameTask>
317                                                         <Output TaskParameter='OutputString' ItemName='I1'/>
318                                                 </Another.SameTask>
319                                                 <Message Text='I0: @(I0) I1: @(I1)'/>
320                                         </Target>
321                                 </Project>
322                         ";
323
324                         engine = new Engine (Consts.BinPath);
325                         project = engine.CreateNewProject ();
326                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
327                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
328                         engine.RegisterLogger (logger);
329
330                         project.LoadXml (documentString);
331
332                         if (!project.Build ("1")) {
333                                 logger.DumpMessages ();
334                                 Assert.Fail ("Build failed");
335                         }
336
337                         logger.CheckLoggedMessageHead ("I0: Other.SameTask I1: Another.SameTask", "A1");
338                 }
339
340                 [Test]
341                 public void TestLazyLoad3 ()
342                 {
343                         string documentString = @"
344                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
345                                         <UsingTask
346                                                 AssemblyFile='Test/resources/TestTasks.dll'
347                                                 TaskName='Another.SameTask'
348                                                 Condition='false'
349                                         />
350
351                                         <Target Name='1'>
352                                                 <Another.SameTask />
353                                         </Target>
354                                 </Project>
355                         ";
356
357                         engine = new Engine (Consts.BinPath);
358                         project = engine.CreateNewProject ();
359                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
360                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
361                         engine.RegisterLogger (logger);
362
363                         project.LoadXml (documentString);
364
365                         IEnumerator en = project.UsingTasks.GetEnumerator ();
366                         en.MoveNext ();
367
368                         UsingTask ut = (UsingTask) en.Current;
369
370                         Assert.AreEqual ("Test/resources/TestTasks.dll", ut.AssemblyFile, "A1");
371                         Assert.IsNull (ut.AssemblyName, "A2");
372                         Assert.AreEqual ("false", ut.Condition, "A3");
373                         Assert.AreEqual (false, ut.IsImported, "A4");
374                         Assert.AreEqual ("Another.SameTask", ut.TaskName, "A5");
375
376                         if (project.Build ("1")) {
377                                 logger.DumpMessages ();
378                                 Assert.Fail ("Build should've failed");
379                         }
380                 }
381
382         }
383 }