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 basedir
= PathCombine ("Test", "resources", "dir");
266 string aaa
= PathCombine ("a", "aa", "aaa");
267 string bb
= Path
.Combine ("b", "bb");
269 string[] dirs
= { aaa, bb, "c" }
;
271 PathCombine (basedir
, aaa
, "foo.dll"),
272 PathCombine (basedir
, bb
, "bar.dll"),
273 PathCombine (basedir
, bb
, "sample.txt"),
274 Path
.Combine (basedir
, "xyz.dll")
277 string documentString
= @"
278 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" " + Consts
.ToolsVersionString
+ @">
280 <WC>dir\**\*.dll</WC>
281 <ExWC>*\x*.dll</ExWC>
285 <Copy SourceFiles='dir\xyz.dll' DestinationFiles='dir\abc.dll'/>
286 <CreateItem Include='dir\**\*.dll' Exclude='*\x*.dll'>
287 <Output TaskParameter='Include' ItemName='CI1' />
289 <Message Text=""CI1: %(CI1.FullPath)""/>
291 <CreateItem Include='$(WC)' Exclude='$(ExWC)'>
292 <Output TaskParameter='Include' ItemName='CI2' />
294 <Message Text=""CI2: %(CI2.FullPath)""/>
299 CreateDirectoriesAndFiles (basedir
, dirs
, files
);
300 string projectdir
= Path
.Combine ("Test", "resources");
301 File
.WriteAllText (Path
.Combine (projectdir
, "wild1.proj"), documentString
);
302 proj
.Load (Path
.Combine (projectdir
, "wild1.proj"));
303 if (!proj
.Build ("Main"))
304 Assert
.Fail ("Build failed");
306 string full_base_dir
= Path
.GetFullPath (basedir
);
307 foreach (string prefix
in new string[] { "CI1: ", "CI2: " }
) {
308 logger
.CheckLoggedAny (prefix
+ PathCombine (full_base_dir
, aaa
, "foo.dll"),
309 MessageImportance
.Normal
, "A1");
310 logger
.CheckLoggedAny (prefix
+ PathCombine (full_base_dir
, bb
, "bar.dll"), MessageImportance
.Normal
, "A2");
311 logger
.CheckLoggedAny (prefix
+ PathCombine (full_base_dir
, "abc.dll"),
312 MessageImportance
.Normal
, "A3");
315 } catch (AssertionException
) {
316 logger
.DumpMessages ();
319 Directory
.Delete (basedir
, true);
324 void CreateDirectoriesAndFiles (string basedir
, string[] dirs
, string[] files
) {
325 foreach (string dir
in dirs
)
326 Directory
.CreateDirectory (Path
.Combine (basedir
, dir
));
328 foreach (string file
in files
)
329 File
.WriteAllText (file
, String
.Empty
);
332 string PathCombine (string path1
, params string[] parts
) {
333 if (parts
== null || parts
.Length
== 0)
336 string final_path
= path1
;
337 foreach (string part
in parts
)
338 final_path
= Path
.Combine (final_path
, part
);
343 public static void CheckBuildItem (BuildItem item
, string name
, string [,] metadata
, string finalItemSpec
, string prefix
)
345 Assert
.AreEqual (name
, item
.Name
, prefix
+ "#1");
346 for (int i
= 0; i
< metadata
.GetLength (0); i
++) {
347 string key
= metadata
[i
, 0];
348 string val
= metadata
[i
, 1];
349 Assert
.IsTrue (item
.HasMetadata (key
), String
.Format ("{0}#2: Expected metadata '{1}' not found", prefix
, key
));
350 Assert
.AreEqual (val
, item
.GetMetadata (key
), String
.Format ("{0}#3: Value for metadata {1}", prefix
, key
));
351 Assert
.AreEqual (val
, item
.GetEvaluatedMetadata (key
), String
.Format ("{0}#4: Value for evaluated metadata {1}", prefix
, key
));
353 Assert
.AreEqual (finalItemSpec
, item
.FinalItemSpec
, prefix
+ "#5");