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;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
37 namespace MonoTests.Microsoft.Build.Tasks {
40 public class CreateItemTest {
42 public void TestAssignment ()
44 CreateItem ci = new CreateItem ();
46 ci.AdditionalMetadata = new string [1] { "a=1" };
47 ci.Include = new ITaskItem [1] { new TaskItem ("1") };
48 ci.Exclude = new ITaskItem [1] { new TaskItem ("2") };
50 Assert.AreEqual ("a=1", ci.AdditionalMetadata [0], "A1");
51 Assert.AreEqual ("1", ci.Include [0].ItemSpec, "A2");
52 Assert.AreEqual ("2", ci.Exclude [0].ItemSpec, "A3");
56 public void TestExecution1 ()
61 string documentString = @"
62 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
76 AdditionalMetadata='a=1; b = 2 '
81 TaskParameter='Include'
89 engine = new Engine (Consts.BinPath);
90 project = engine.CreateNewProject ();
91 project.LoadXml (documentString);
92 Assert.IsTrue (project.Build ("1"), "A1");
94 BuildItemGroup include = project.GetEvaluatedItemsByName ("NewItem");
95 Assert.AreEqual (2, include.Count, "A2");
97 string [,] additional_metadata = new string [,] { { "a", "1" }, { "b", "2" }, { "Sub", "fooA" } };
98 CheckBuildItem (include [0], "NewItem", additional_metadata, "2", "A");
100 additional_metadata = new string [,] { { "a", "1" }, { "b", "2" }, { "Sub", "fooC" } };
101 CheckBuildItem (include [1], "NewItem", additional_metadata, "4", "B");
105 public void TestExcludeAndCondition ()
110 string documentString = @"
111 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
125 AdditionalMetadata='a=1;b=2'
128 Condition=""'%(Sub)' == 'fooA'""
131 TaskParameter='Include'
139 engine = new Engine (Consts.BinPath);
140 project = engine.CreateNewProject ();
141 project.LoadXml (documentString);
142 Assert.IsTrue (project.Build ("1"), "A1");
144 BuildItemGroup include = project.GetEvaluatedItemsByName ("NewItem");
145 Assert.AreEqual (3, include.Count, "A2");
147 string [,] additional_metadata = new string [,] { { "a", "1" }, {"b", "2"}, {"Sub", "fooA" } };
148 CheckBuildItem (include [0], "NewItem", additional_metadata, "1", "A");
149 CheckBuildItem (include [1], "NewItem", additional_metadata, "2", "B");
150 CheckBuildItem (include [2], "NewItem", additional_metadata, "5", "C");
154 public void TestNullFields ()
159 string documentString = @"
160 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
167 <CreateItem Include='@(A)' >
169 TaskParameter='Include'
176 engine = new Engine (Consts.BinPath);
177 project = engine.CreateNewProject ();
178 project.LoadXml (documentString);
179 Assert.IsTrue (project.Build ("1"), "A1, Build failed");
181 BuildItemGroup include = project.GetEvaluatedItemsByName ("NewItem");
182 Assert.AreEqual (3, include.Count, "A2");
184 string [,] additional_metadata = new string [0, 0];
185 CheckBuildItem (include [0], "NewItem", additional_metadata, "1", "A");
186 CheckBuildItem (include [1], "NewItem", additional_metadata, "2", "B");
187 CheckBuildItem (include [2], "NewItem", additional_metadata, "5", "C");
191 public void TestVariableExpansion ()
196 string documentString = @"
197 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
202 <P3>@(Nine->'%(Identity)')</P3>
205 <Nine Include=""Nine""/>
206 <Eight Include=""Eight""/>
207 <Seven Include=""@(Eight)""/>
208 <Six Include=""@(Seven);$(P3)""/>
209 <Third Include=""Abc""/>
210 <Fourth Include=""$(P2)""/>
211 <Second Include=""@(Third);$(P1);@(Fourth);@(Six)""/>
212 <IG Include=""@(Second)""/>
216 <CreateItem Include='$(C)' >
218 TaskParameter='Include'
223 <Message Text=""C: $(C)""/>
224 <Message Text=""items: @(items)""/>
229 engine = new Engine (Consts.BinPath);
231 TestMessageLogger testLogger = new TestMessageLogger ();
232 engine.RegisterLogger (testLogger);
234 project = engine.CreateNewProject ();
235 project.LoadXml (documentString);
236 if (!project.Build ("1")) {
237 testLogger.DumpMessages ();
238 Assert.Fail ("Build failed");
241 BuildItemGroup include = project.GetEvaluatedItemsByName ("Items");
242 Assert.AreEqual (5, include.Count, "A2");
244 Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A#3");
245 Assert.AreEqual ("FooP1", include[1].FinalItemSpec, "A#4");
246 Assert.AreEqual ("FooP2", include[2].FinalItemSpec, "A#5");
247 Assert.AreEqual ("Eight", include[3].FinalItemSpec, "A#6");
248 Assert.AreEqual ("Nine", include[4].FinalItemSpec, "A#7");
250 testLogger.CheckLoggedMessageHead ("C: Abc;FooP1;FooP2;Eight;Nine", "A#9");
251 testLogger.CheckLoggedMessageHead ("items: Abc;FooP1;FooP2;Eight;Nine", "A#10");
255 #if NET_3_5 || NET_4_0
257 public void TestItemsWithWildcards () {
258 Engine engine = new Engine (Consts.BinPath);
259 Project proj = engine.CreateNewProject ();
260 TestMessageLogger logger = new TestMessageLogger ();
261 engine.RegisterLogger (logger);
265 string projectdir = Path.Combine ("Test", "resources");
266 string basedir = "dir";
267 string aaa = PathCombine (basedir, "a", "aa", "aaa");
268 string bb = PathCombine (basedir, "b", "bb");
269 string c = PathCombine (basedir, "c");
271 string[] dirs = { aaa, bb, c };
273 PathCombine (aaa, "foo.dll"),
274 PathCombine (bb, "bar.dll"),
275 PathCombine (bb, "sample.txt"),
276 Path.Combine (basedir, "xyz.dll")
279 string documentString = @"
280 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts.ToolsVersionString + @">
282 <CreateItem Include='dir\**'>
283 <Output TaskParameter='Include' ItemName='CI1' />
285 <Message Text=""CI1: @(CI1)""/>
290 CreateDirectoriesAndFiles (projectdir, dirs, files);
291 File.WriteAllText (Path.Combine (projectdir, "wild1.proj"), documentString);
292 proj.Load (Path.Combine (projectdir, "wild1.proj"));
293 if (!proj.Build ("Main"))
294 Assert.Fail ("Build failed");
296 string full_base_dir = Path.GetFullPath (basedir);
297 logger.CheckLoggedAny ("CI1: " + String.Join (";", files), MessageImportance.Normal, "A1");
298 } catch (AssertionException) {
299 logger.DumpMessages ();
302 Directory.Delete (Path.Combine (projectdir, basedir), true);
307 public void TestItemsWithWildcards2 () {
308 Engine engine = new Engine (Consts.BinPath);
309 Project proj = engine.CreateNewProject ();
310 TestMessageLogger logger = new TestMessageLogger ();
311 engine.RegisterLogger (logger);
315 string basedir = PathCombine ("Test", "resources", "dir");
316 string aaa = PathCombine ("a", "aa", "aaa");
317 string bb = Path.Combine ("b", "bb");
319 string[] dirs = { aaa, bb, "c" };
321 PathCombine (aaa, "foo.dll"),
322 PathCombine (bb, "bar.dll"),
323 PathCombine (bb, "sample.txt"),
324 PathCombine ("xyz.dll")
327 string documentString = @"
328 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts.ToolsVersionString + @">
330 <WC>dir\**\*.dll</WC>
331 <ExWC>*\x*.dll</ExWC>
335 <Copy SourceFiles='dir\xyz.dll' DestinationFiles='dir\abc.dll'/>
336 <CreateItem Include='dir\**\*.dll' Exclude='*\x*.dll'>
337 <Output TaskParameter='Include' ItemName='CI1' />
339 <Message Text=""CI1: %(CI1.FullPath)""/>
341 <CreateItem Include='$(WC)' Exclude='$(ExWC)'>
342 <Output TaskParameter='Include' ItemName='CI2' />
344 <Message Text=""CI2: %(CI2.FullPath)""/>
349 CreateDirectoriesAndFiles (basedir, dirs, files);
350 string projectdir = Path.Combine ("Test", "resources");
351 File.WriteAllText (Path.Combine (projectdir, "wild1.proj"), documentString);
352 proj.Load (Path.Combine (projectdir, "wild1.proj"));
353 if (!proj.Build ("Main"))
354 Assert.Fail ("Build failed");
356 string full_base_dir = Path.GetFullPath (basedir);
357 foreach (string prefix in new string[] { "CI1: ", "CI2: " }) {
358 logger.CheckLoggedAny (prefix + PathCombine (full_base_dir, aaa, "foo.dll"),
359 MessageImportance.Normal, "A1");
360 logger.CheckLoggedAny (prefix + PathCombine (full_base_dir, bb, "bar.dll"), MessageImportance.Normal, "A2");
361 logger.CheckLoggedAny (prefix + PathCombine (full_base_dir, "abc.dll"),
362 MessageImportance.Normal, "A3");
365 } catch (AssertionException) {
366 logger.DumpMessages ();
369 Directory.Delete (basedir, true);
374 void CreateDirectoriesAndFiles (string basedir, string[] dirs, string[] files) {
375 foreach (string dir in dirs)
376 Directory.CreateDirectory (Path.Combine (basedir, dir));
378 foreach (string file in files)
379 File.WriteAllText (Path.Combine (basedir, file), String.Empty);
382 string PathCombine (string path1, params string[] parts) {
383 if (parts == null || parts.Length == 0)
386 string final_path = path1;
387 foreach (string part in parts)
388 final_path = Path.Combine (final_path, part);
393 public static void CheckBuildItem (BuildItem item, string name, string [,] metadata, string finalItemSpec, string prefix)
395 Assert.AreEqual (name, item.Name, prefix + "#1");
396 for (int i = 0; i < metadata.GetLength (0); i ++) {
397 string key = metadata [i, 0];
398 string val = metadata [i, 1];
399 Assert.IsTrue (item.HasMetadata (key), String.Format ("{0}#2: Expected metadata '{1}' not found", prefix, key));
400 Assert.AreEqual (val, item.GetMetadata (key), String.Format ("{0}#3: Value for metadata {1}", prefix, key));
401 Assert.AreEqual (val, item.GetEvaluatedMetadata (key), String.Format ("{0}#4: Value for evaluated metadata {1}", prefix, key));
403 Assert.AreEqual (finalItemSpec, item.FinalItemSpec, prefix + "#5");