f9880b01cc7a07ba90c886da89b77daef62a36b6
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildTaskTest.cs
1 //
2 // BuildTaskTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 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 System.Collections.Generic;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
35
36 namespace MonoTests.Microsoft.Build.BuildEngine {
37         [TestFixture]
38         public class BuildTaskTest {
39
40                 static BuildTask[] GetTasks (Target t)
41                 {
42                         List <BuildTask> list = new List <BuildTask> ();
43                         foreach (BuildTask bt in t)
44                                 list.Add (bt);
45                         return list.ToArray ();
46                 }
47
48                 [Test]
49                 public void TestFromXml ()
50                 {
51                         Engine engine;
52                         Project project;
53                         
54                         string documentString = @"
55                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
56                                         <PropertyGroup>
57                                                 <Property>true</Property>
58                                         </PropertyGroup>
59                                         <Target Name='T'>
60                                                 <Message Text='Text' />
61                                                 <Message Text='Text' Condition='$(Property)' ContinueOnError='$(Property)' />
62                                         </Target>
63                                 </Project>
64                         ";
65                         
66                         engine = new Engine (Consts.BinPath);
67                         project = engine.CreateNewProject ();
68                         project.LoadXml (documentString);
69
70                         Target[] t = new Target [1];
71                         BuildTask[] bt;
72                         project.Targets.CopyTo (t, 0);
73                         bt = GetTasks (t [0]);
74
75                         Assert.AreEqual (String.Empty, bt [0].Condition, "A1");
76                         Assert.IsFalse (bt [0].ContinueOnError, "A2");
77                         Assert.IsNull (bt [0].HostObject, "A3");
78                         Assert.AreEqual ("Message", bt [0].Name, "A4");
79                         Assert.IsNotNull (bt [0].Type, "A5");
80
81                         Assert.AreEqual ("$(Property)", bt [1].Condition, "A6");
82                         Assert.IsTrue (bt [1].ContinueOnError, "A7");
83                         Assert.IsNull (bt [1].HostObject, "A8");
84                         Assert.AreEqual ("Message", bt [0].Name, "A9");
85                         Assert.IsNotNull (bt [0].Type, "A10");
86                 }
87
88                 [Test]
89                 public void TestGetParameterNames ()
90                 {
91                         Engine engine;
92                         Project project;
93                         
94                         string documentString = @"
95                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
96                                         <Target Name='T'>
97                                                 <Message Text='Text' Importance='low' Condition='true' ContinueOnError='true' />
98                                         </Target>
99                                 </Project>
100                         ";
101                         
102                         engine = new Engine (Consts.BinPath);
103                         project = engine.CreateNewProject ();
104                         project.LoadXml (documentString);
105
106                         Target[] t = new Target [1];
107                         BuildTask[] bt;
108                         project.Targets.CopyTo (t, 0);
109                         bt = GetTasks (t [0]);
110
111                         string[] names = bt [0].GetParameterNames ();
112
113                         Assert.AreEqual (2, names.Length, "A1");
114                         Assert.AreEqual ("Text", names [0], "A2");
115                         Assert.AreEqual ("Importance", names [1], "A3");
116                 }
117
118                 [Test]
119                 public void TestGetParameterValue1 ()
120                 {
121                         Engine engine;
122                         Project project;
123                         
124                         string documentString = @"
125                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
126                                         <Target Name='T'>
127                                                 <Message Text='$(A)' Condition='true' />
128                                         </Target>
129                                 </Project>
130                         ";
131                         
132                         engine = new Engine (Consts.BinPath);
133                         project = engine.CreateNewProject ();
134                         project.LoadXml (documentString);
135
136                         Target[] t = new Target [1];
137                         BuildTask[] bt;
138                         project.Targets.CopyTo (t, 0);
139                         bt = GetTasks (t [0]);
140
141                         Assert.AreEqual (String.Empty, bt [0].GetParameterValue ("text"), "A1");
142                         Assert.AreEqual (String.Empty, bt [0].GetParameterValue (null), "A2");
143                         Assert.AreEqual ("$(A)", bt [0].GetParameterValue ("Text"), "A3");
144                 }
145
146                 [Test]
147                 public void TestGetParameterValue2 ()
148                 {
149                         Engine engine;
150                         Project project;
151                         
152                         string documentString = @"
153                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
154                                         <Target Name='T'>
155                                                 <Message />
156                                         </Target>
157                                 </Project>
158                         ";
159                         
160                         engine = new Engine (Consts.BinPath);
161                         project = engine.CreateNewProject ();
162                         project.LoadXml (documentString);
163
164                         Target[] t = new Target [1];
165                         BuildTask[] bt;
166                         project.Targets.CopyTo (t, 0);
167                         bt = GetTasks (t [0]);
168
169                         {
170                                 bool exception = false;
171
172                                 try {
173                                         bt [0].GetParameterValue ("Condition");
174                                 } catch (ArgumentException) {
175                                         exception = true;
176                                 }
177
178                                 Assert.IsTrue (exception, "A1");
179                         }
180                         {
181                                 bool exception = false;
182
183                                 try {
184                                         bt [0].GetParameterValue ("ContinueOnError");
185                                 } catch (ArgumentException) {
186                                         exception = true;
187                                 }
188
189                                 Assert.IsTrue (exception, "A2");
190                         }
191                 }
192
193                 [Test]
194                 public void TestSetParameterValue1 ()
195                 {
196                         Engine engine;
197                         Project project;
198                         
199                         string documentString = @"
200                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
201                                         <Target Name='T'>
202                                                 <Message />
203                                         </Target>
204                                 </Project>
205                         ";
206                         
207                         engine = new Engine (Consts.BinPath);
208                         project = engine.CreateNewProject ();
209                         project.LoadXml (documentString);
210
211                         Target[] t = new Target [1];
212                         BuildTask[] bt;
213                         project.Targets.CopyTo (t, 0);
214                         bt = GetTasks (t [0]);
215
216                         bt [0].SetParameterValue ("Text", "Value");
217                         Assert.AreEqual ("Value", bt [0].GetParameterValue ("Text"), "A1");
218                         bt [0].SetParameterValue ("text", "Value");
219                         Assert.AreEqual ("Value", bt [0].GetParameterValue ("text"), "A2");
220                         bt [0].SetParameterValue ("something", "Value");
221                         Assert.AreEqual ("Value", bt [0].GetParameterValue ("something"), "A3");
222                         bt [0].SetParameterValue ("Text", "$(A)");
223                         Assert.AreEqual ("$(A)", bt [0].GetParameterValue ("Text"), "A4");
224                 }
225
226                 [Test]
227                 public void TestSetParameterValue2 ()
228                 {
229                         Engine engine;
230                         Project project;
231                         
232                         string documentString = @"
233                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
234                                         <Target Name='T'>
235                                                 <Message />
236                                         </Target>
237                                 </Project>
238                         ";
239                         
240                         engine = new Engine (Consts.BinPath);
241                         project = engine.CreateNewProject ();
242                         project.LoadXml (documentString);
243
244                         Target[] t = new Target [1];
245                         BuildTask[] bt;
246                         project.Targets.CopyTo (t, 0);
247                         bt = GetTasks (t [0]);
248
249                         bt [0].SetParameterValue ("Text", "$(A)", true);
250                         Assert.AreEqual (Utilities.Escape ("$(A)"), bt [0].GetParameterValue ("Text"), "A1");
251                         bt [0].SetParameterValue ("Text", "$(A)", false);
252                         Assert.AreEqual ("$(A)", bt [0].GetParameterValue ("Text"), "A2");
253                 }
254
255                 [Test]
256                 public void TestProperties ()
257                 {
258                         Engine engine;
259                         Project project;
260                         
261                         string documentString = @"
262                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
263                                         <Target Name='T'>
264                                                 <Message />
265                                         </Target>
266                                 </Project>
267                         ";
268                         
269                         engine = new Engine (Consts.BinPath);
270                         project = engine.CreateNewProject ();
271                         project.LoadXml (documentString);
272
273                         Target[] t = new Target [1];
274                         BuildTask[] bt;
275                         project.Targets.CopyTo (t, 0);
276                         bt = GetTasks (t [0]);
277
278                         bt [0].Condition = null;
279                         Assert.AreEqual (String.Empty, bt [0].Condition, "A1");
280                         bt [0].Condition = "something";
281                         Assert.AreEqual ("something", bt [0].Condition, "A2");
282                         
283                         bt [0].ContinueOnError = true;
284                         Assert.IsTrue (bt [0].ContinueOnError, "A3");
285                         bt [0].ContinueOnError = false;
286                         Assert.IsFalse (bt [0].ContinueOnError, "A4");
287                 }
288
289                 [Test]
290                 public void TestAddOutputItem1 ()
291                 {
292                         Engine engine;
293                         Project project;
294
295                         string documentString = @"
296                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
297                                         <Target Name='T'>
298                                                 <Message />
299                                         </Target>
300                                 </Project>
301                         ";
302
303                         engine = new Engine (Consts.BinPath);
304                         project = engine.CreateNewProject ();
305                         project.LoadXml (documentString);
306
307                         Target [] t = new Target [1];
308                         BuildTask [] bt;
309                         project.Targets.CopyTo (t, 0);
310                         bt = GetTasks (t [0]);
311
312                         bt [0].AddOutputItem (null, null);
313                 }
314
315                 [Test]
316                 public void TestAddOutputItem2 ()
317                 {
318                         Engine engine;
319                         Project project;
320
321                         string documentString = @"
322                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
323                                         <UsingTask
324                                                 AssemblyFile='Test\resources\TestTasks.dll'
325                                                 TaskName='OutputTestTask'
326                                         />
327                                         <Target Name='T'>
328                                                 <OutputTestTask />
329                                         </Target>
330                                 </Project>
331                         ";
332
333                         engine = new Engine (Consts.BinPath);
334                         project = engine.CreateNewProject ();
335                         project.LoadXml (documentString);
336
337                         Target [] t = new Target [1];
338                         BuildTask [] bt;
339                         project.Targets.CopyTo (t, 0);
340                         bt = GetTasks (t [0]);
341
342                         bt [0].AddOutputItem ("Property", "ItemName");
343                         project.Build ("T");
344
345                         Assert.AreEqual ("ItemName", project.EvaluatedItems [0].Name, "A1");
346                         Assert.AreEqual ("some_text", project.EvaluatedItems [0].FinalItemSpec, "A2");
347         }
348
349         [Test]
350         public void TestTaskInNamespace()
351         {
352             Engine engine;
353             Project project;
354
355             string documentString = @"
356                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
357                                         <UsingTask
358                                                 AssemblyFile='Test\resources\TestTasks.dll'
359                                                 TaskName='NamespacedOutputTestTask'
360                                         />
361                                         <Target Name='T'>
362                                                 <NamespacedOutputTestTask />
363                                         </Target>
364                                 </Project>
365                         ";
366
367             engine = new Engine(Consts.BinPath);
368             project = engine.CreateNewProject();
369             project.LoadXml(documentString);
370
371             Target[] t = new Target[1];
372             BuildTask[] bt;
373             project.Targets.CopyTo(t, 0);
374             bt = GetTasks(t[0]);
375
376             bt[0].AddOutputItem("Property", "ItemName");
377             project.Build("T");
378
379             Assert.AreEqual("ItemName", project.EvaluatedItems[0].Name, "A1");
380             Assert.AreEqual("some_text", project.EvaluatedItems[0].FinalItemSpec, "A2");
381         }
382
383                 [Test]
384                 public void TestAddOutputProperty1 ()
385                 {
386                         Engine engine;
387                         Project project;
388
389                         string documentString = @"
390                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
391                                         <Target Name='T'>
392                                                 <Message />
393                                         </Target>
394                                 </Project>
395                         ";
396
397                         engine = new Engine (Consts.BinPath);
398                         project = engine.CreateNewProject ();
399                         project.LoadXml (documentString);
400
401                         Target [] t = new Target [1];
402                         BuildTask [] bt;
403                         project.Targets.CopyTo (t, 0);
404                         bt = GetTasks (t [0]);
405
406                         bt [0].AddOutputProperty (null, null);
407                 }
408
409                 [Test]
410                 public void TestAddOutputProperty2 ()
411                 {
412                         Engine engine;
413                         Project project;
414
415                         string documentString = @"
416                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
417                                         <UsingTask
418                                                 AssemblyFile='Test\resources\TestTasks.dll'
419                                                 TaskName='OutputTestTask'
420                                         />
421                                         <Target Name='T'>
422                                                 <OutputTestTask />
423                                         </Target>
424                                 </Project>
425                         ";
426
427                         engine = new Engine (Consts.BinPath);
428                         project = engine.CreateNewProject ();
429                         project.LoadXml (documentString);
430
431                         Target [] t = new Target [1];
432                         BuildTask [] bt;
433                         project.Targets.CopyTo (t, 0);
434                         bt = GetTasks (t [0]);
435                         bt [0].AddOutputProperty ("Property", "PropertyName");
436                         project.Build ("T");
437
438                         Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].Value, "A1");
439                         Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].FinalValue, "A1");
440                 }
441
442                 // FIXME: edit
443                 public void TestPublish1 ()
444                 {
445                         Engine engine;
446                         Project project;
447
448                         string documentString = @"
449                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
450                                         <UsingTask
451                                                 AssemblyFile='Test\resources\TestTasks.dll'
452                                                 TaskName='OutputTestTask'
453                                         />
454                                         <Target Name='T'>
455                                                 <OutputTestTask />
456                                         </Target>
457                                 </Project>
458                         ";
459
460                         engine = new Engine (Consts.BinPath);
461                         project = engine.CreateNewProject ();
462                         project.LoadXml (documentString);
463
464                         Target [] t = new Target [1];
465                         BuildTask [] bt;
466                         project.Targets.CopyTo (t, 0);
467                         bt = GetTasks (t [0]);
468                         bt [0].AddOutputProperty ("Property", "PropertyName");
469                         project.Build ("T");
470
471                         Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].Value, "A1");
472                         Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].FinalValue, "A1");
473                 }
474         }
475 }