2009-03-26 Jonathan Chambers <joncham@gmail.com>
[mono-project.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildTaskTest.cs
blobf9880b01cc7a07ba90c886da89b77daef62a36b6
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:
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.
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;
36 namespace MonoTests.Microsoft.Build.BuildEngine {
37 [TestFixture]
38 public class BuildTaskTest {
40 static BuildTask[] GetTasks (Target t)
42 List <BuildTask> list = new List <BuildTask> ();
43 foreach (BuildTask bt in t)
44 list.Add (bt);
45 return list.ToArray ();
48 [Test]
49 public void TestFromXml ()
51 Engine engine;
52 Project project;
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>
66 engine = new Engine (Consts.BinPath);
67 project = engine.CreateNewProject ();
68 project.LoadXml (documentString);
70 Target[] t = new Target [1];
71 BuildTask[] bt;
72 project.Targets.CopyTo (t, 0);
73 bt = GetTasks (t [0]);
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");
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");
88 [Test]
89 public void TestGetParameterNames ()
91 Engine engine;
92 Project project;
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>
102 engine = new Engine (Consts.BinPath);
103 project = engine.CreateNewProject ();
104 project.LoadXml (documentString);
106 Target[] t = new Target [1];
107 BuildTask[] bt;
108 project.Targets.CopyTo (t, 0);
109 bt = GetTasks (t [0]);
111 string[] names = bt [0].GetParameterNames ();
113 Assert.AreEqual (2, names.Length, "A1");
114 Assert.AreEqual ("Text", names [0], "A2");
115 Assert.AreEqual ("Importance", names [1], "A3");
118 [Test]
119 public void TestGetParameterValue1 ()
121 Engine engine;
122 Project project;
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>
132 engine = new Engine (Consts.BinPath);
133 project = engine.CreateNewProject ();
134 project.LoadXml (documentString);
136 Target[] t = new Target [1];
137 BuildTask[] bt;
138 project.Targets.CopyTo (t, 0);
139 bt = GetTasks (t [0]);
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");
146 [Test]
147 public void TestGetParameterValue2 ()
149 Engine engine;
150 Project project;
152 string documentString = @"
153 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
154 <Target Name='T'>
155 <Message />
156 </Target>
157 </Project>
160 engine = new Engine (Consts.BinPath);
161 project = engine.CreateNewProject ();
162 project.LoadXml (documentString);
164 Target[] t = new Target [1];
165 BuildTask[] bt;
166 project.Targets.CopyTo (t, 0);
167 bt = GetTasks (t [0]);
170 bool exception = false;
172 try {
173 bt [0].GetParameterValue ("Condition");
174 } catch (ArgumentException) {
175 exception = true;
178 Assert.IsTrue (exception, "A1");
181 bool exception = false;
183 try {
184 bt [0].GetParameterValue ("ContinueOnError");
185 } catch (ArgumentException) {
186 exception = true;
189 Assert.IsTrue (exception, "A2");
193 [Test]
194 public void TestSetParameterValue1 ()
196 Engine engine;
197 Project project;
199 string documentString = @"
200 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
201 <Target Name='T'>
202 <Message />
203 </Target>
204 </Project>
207 engine = new Engine (Consts.BinPath);
208 project = engine.CreateNewProject ();
209 project.LoadXml (documentString);
211 Target[] t = new Target [1];
212 BuildTask[] bt;
213 project.Targets.CopyTo (t, 0);
214 bt = GetTasks (t [0]);
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");
226 [Test]
227 public void TestSetParameterValue2 ()
229 Engine engine;
230 Project project;
232 string documentString = @"
233 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
234 <Target Name='T'>
235 <Message />
236 </Target>
237 </Project>
240 engine = new Engine (Consts.BinPath);
241 project = engine.CreateNewProject ();
242 project.LoadXml (documentString);
244 Target[] t = new Target [1];
245 BuildTask[] bt;
246 project.Targets.CopyTo (t, 0);
247 bt = GetTasks (t [0]);
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");
255 [Test]
256 public void TestProperties ()
258 Engine engine;
259 Project project;
261 string documentString = @"
262 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
263 <Target Name='T'>
264 <Message />
265 </Target>
266 </Project>
269 engine = new Engine (Consts.BinPath);
270 project = engine.CreateNewProject ();
271 project.LoadXml (documentString);
273 Target[] t = new Target [1];
274 BuildTask[] bt;
275 project.Targets.CopyTo (t, 0);
276 bt = GetTasks (t [0]);
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");
283 bt [0].ContinueOnError = true;
284 Assert.IsTrue (bt [0].ContinueOnError, "A3");
285 bt [0].ContinueOnError = false;
286 Assert.IsFalse (bt [0].ContinueOnError, "A4");
289 [Test]
290 public void TestAddOutputItem1 ()
292 Engine engine;
293 Project project;
295 string documentString = @"
296 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
297 <Target Name='T'>
298 <Message />
299 </Target>
300 </Project>
303 engine = new Engine (Consts.BinPath);
304 project = engine.CreateNewProject ();
305 project.LoadXml (documentString);
307 Target [] t = new Target [1];
308 BuildTask [] bt;
309 project.Targets.CopyTo (t, 0);
310 bt = GetTasks (t [0]);
312 bt [0].AddOutputItem (null, null);
315 [Test]
316 public void TestAddOutputItem2 ()
318 Engine engine;
319 Project project;
321 string documentString = @"
322 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
323 <UsingTask
324 AssemblyFile='Test\resources\TestTasks.dll'
325 TaskName='OutputTestTask'
327 <Target Name='T'>
328 <OutputTestTask />
329 </Target>
330 </Project>
333 engine = new Engine (Consts.BinPath);
334 project = engine.CreateNewProject ();
335 project.LoadXml (documentString);
337 Target [] t = new Target [1];
338 BuildTask [] bt;
339 project.Targets.CopyTo (t, 0);
340 bt = GetTasks (t [0]);
342 bt [0].AddOutputItem ("Property", "ItemName");
343 project.Build ("T");
345 Assert.AreEqual ("ItemName", project.EvaluatedItems [0].Name, "A1");
346 Assert.AreEqual ("some_text", project.EvaluatedItems [0].FinalItemSpec, "A2");
349 [Test]
350 public void TestTaskInNamespace()
352 Engine engine;
353 Project project;
355 string documentString = @"
356 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
357 <UsingTask
358 AssemblyFile='Test\resources\TestTasks.dll'
359 TaskName='NamespacedOutputTestTask'
361 <Target Name='T'>
362 <NamespacedOutputTestTask />
363 </Target>
364 </Project>
367 engine = new Engine(Consts.BinPath);
368 project = engine.CreateNewProject();
369 project.LoadXml(documentString);
371 Target[] t = new Target[1];
372 BuildTask[] bt;
373 project.Targets.CopyTo(t, 0);
374 bt = GetTasks(t[0]);
376 bt[0].AddOutputItem("Property", "ItemName");
377 project.Build("T");
379 Assert.AreEqual("ItemName", project.EvaluatedItems[0].Name, "A1");
380 Assert.AreEqual("some_text", project.EvaluatedItems[0].FinalItemSpec, "A2");
383 [Test]
384 public void TestAddOutputProperty1 ()
386 Engine engine;
387 Project project;
389 string documentString = @"
390 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
391 <Target Name='T'>
392 <Message />
393 </Target>
394 </Project>
397 engine = new Engine (Consts.BinPath);
398 project = engine.CreateNewProject ();
399 project.LoadXml (documentString);
401 Target [] t = new Target [1];
402 BuildTask [] bt;
403 project.Targets.CopyTo (t, 0);
404 bt = GetTasks (t [0]);
406 bt [0].AddOutputProperty (null, null);
409 [Test]
410 public void TestAddOutputProperty2 ()
412 Engine engine;
413 Project project;
415 string documentString = @"
416 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
417 <UsingTask
418 AssemblyFile='Test\resources\TestTasks.dll'
419 TaskName='OutputTestTask'
421 <Target Name='T'>
422 <OutputTestTask />
423 </Target>
424 </Project>
427 engine = new Engine (Consts.BinPath);
428 project = engine.CreateNewProject ();
429 project.LoadXml (documentString);
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");
438 Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].Value, "A1");
439 Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].FinalValue, "A1");
442 // FIXME: edit
443 public void TestPublish1 ()
445 Engine engine;
446 Project project;
448 string documentString = @"
449 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
450 <UsingTask
451 AssemblyFile='Test\resources\TestTasks.dll'
452 TaskName='OutputTestTask'
454 <Target Name='T'>
455 <OutputTestTask />
456 </Target>
457 </Project>
460 engine = new Engine (Consts.BinPath);
461 project = engine.CreateNewProject ();
462 project.LoadXml (documentString);
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");
471 Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].Value, "A1");
472 Assert.AreEqual ("some_text", project.EvaluatedProperties ["PropertyName"].FinalValue, "A1");