5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2009 Novell, Inc (http://www.novell.com)
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.
30 using Microsoft.Build.BuildEngine;
31 using NUnit.Framework;
33 using System.Threading;
34 using Microsoft.Build.Tasks;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace MonoTests.Microsoft.Build.Tasks {
41 public class CopyTest {
42 string source_path, target_path;
45 public void CreateDir ()
47 source_path = Path.Combine (Path.Combine ("Test", "resources"), "Copy");
48 Directory.CreateDirectory (source_path);
49 target_path = Path.Combine (Path.Combine ("Test", "resources"), "Target");
50 Directory.CreateDirectory (target_path);
54 public void RemoveDirectories ()
56 Directory.Delete (source_path, true);
57 Directory.Delete (target_path, true);
61 public void TestCopy_MissingSourceFile ()
63 Copy copy = new Copy ();
64 copy.BuildEngine = new TestEngine ();
65 copy.SourceFiles = new ITaskItem [1];
66 copy.SourceFiles [0] = new TaskItem ("SourceDoesNotExist");
67 copy.DestinationFiles = new ITaskItem [1];
68 copy.DestinationFiles [0] = new TaskItem ("DestDoesNotExist");
69 Assert.IsFalse (copy.Execute ());
73 public void TestCopy1 ()
77 string file_path = Path.Combine (source_path, "copy.txt");
78 string target_file = Path.Combine (target_path, "copy.txt");
80 using (File.CreateText (file_path)) { }
82 string documentString = @"
83 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
84 <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
86 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
87 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
90 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)' SkipUnchangedFiles='true' >
91 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
92 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
94 <Message Text=""I0 : @(I0), I1: @(I1)""/>
99 engine = new Engine (Consts.BinPath);
100 project = engine.CreateNewProject ();
102 TestMessageLogger testLogger = new TestMessageLogger ();
103 engine.RegisterLogger (testLogger);
105 project.LoadXml (documentString);
107 if (!project.Build ("1")) {
108 var sb = new StringBuilder ();
109 testLogger.DumpMessages (sb);
110 Assert.Fail ("Build failed " + sb.ToString ());
112 Assert.IsTrue (File.Exists (target_file), "A2");
114 BuildItemGroup big = project.GetEvaluatedItemsByName ("I0");
115 Assert.AreEqual (1, big.Count, "A3");
116 BuildItem bi = big [0];
117 Assert.AreEqual (target_file, bi.FinalItemSpec, "A4");
118 Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A4");
119 Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A5");
121 big = project.GetEvaluatedItemsByName ("I1");
122 Assert.AreEqual (1, big.Count, "A10");
124 Assert.AreEqual (target_file, bi.FinalItemSpec, "A11");
125 Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A12");
126 Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A13");
128 // build again, this time files won't get copied because
129 // of SkipUnchangedFiles=true
130 if (!project.Build ("1")) {
131 testLogger.DumpMessages ();
132 Assert.Fail ("Build failed #2");
134 Assert.IsTrue (File.Exists (target_file), "A20");
136 big = project.GetEvaluatedItemsByName ("I0");
137 Assert.AreEqual (1, big.Count, "A21");
139 Assert.AreEqual (target_file, bi.FinalItemSpec, "A22");
140 Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A23");
141 Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A24");
143 big = project.GetEvaluatedItemsByName ("I1");
144 Assert.AreEqual (1, big.Count, "A25");
146 Assert.AreEqual (target_file, bi.FinalItemSpec, "A26");
147 Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A27");
148 Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A28");
152 public void TestCopy2 ()
156 string [] file_paths = new string [] {
157 Path.Combine (source_path, "copy1.txt"),
158 Path.Combine (source_path, "copy2.txt")
161 using (File.CreateText (file_paths[0])) { }
162 using (File.CreateText (file_paths[1])) { }
164 string documentString = @"
165 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
166 <PropertyGroup><TargetPath>" + target_path + @"</TargetPath></PropertyGroup>
168 <SFiles Include='" + file_paths [0] + @"'><Md>1</Md></SFiles>
169 <SFiles Include='" + file_paths [1] + @"'><Md>2</Md></SFiles>
172 <Copy SourceFiles='@(SFiles)' DestinationFolder='$(TargetPath)' SkipUnchangedFiles='true' >
173 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
174 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
179 engine = new Engine (Consts.BinPath);
180 project = engine.CreateNewProject ();
182 TestMessageLogger testLogger = new TestMessageLogger ();
183 engine.RegisterLogger (testLogger);
185 project.LoadXml (documentString);
187 if (!project.Build ("1")) {
188 testLogger.DumpMessages ();
189 Assert.Fail ("Build failed");
192 CheckCopyBuildItems (project, file_paths, target_path, "A1");
194 // build again, this time files won't get copied because
195 // of SkipUnchangedFiles=true
196 if (!project.Build ("1")) {
197 testLogger.DumpMessages ();
198 Assert.Fail ("Build failed #2");
200 CheckCopyBuildItems (project, file_paths, target_path, "A2");
204 public void TestCopy_EmptySources () {
208 string documentString = @"
209 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
211 <Copy SourceFiles='@(NonExistantSourceFiles)' DestinationFolder='$(TargetPath)' SkipUnchangedFiles='true' >
212 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
213 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
218 engine = new Engine (Consts.BinPath);
219 project = engine.CreateNewProject ();
221 TestMessageLogger testLogger = new TestMessageLogger ();
222 engine.RegisterLogger (testLogger);
224 project.LoadXml (documentString);
227 if (!project.Build ("1")) {
228 testLogger.DumpMessages ();
229 Assert.Fail ("Build failed");
234 public void TestCopy_EmptyDestFolder () {
238 string documentString = @"
239 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
241 <SFiles Include='foo.txt'><Md>1</Md></SFiles>
244 <Copy SourceFiles='@(SFiles)' DestinationFolder='@(NonExistant)' DestinationFiles='@(NonExistant)' SkipUnchangedFiles='true' >
245 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
246 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
251 engine = new Engine (Consts.BinPath);
252 project = engine.CreateNewProject ();
254 TestMessageLogger testLogger = new TestMessageLogger ();
255 engine.RegisterLogger (testLogger);
257 project.LoadXml (documentString);
258 if (project.Build ("1")) {
259 testLogger.DumpMessages ();
260 Assert.Fail ("Build should have failed");
265 public void TestCopy_ReadOnlyUpdate ()
269 string file_path = Path.Combine (source_path, "copyro.txt");
270 string target_file = Path.Combine (target_path, "copyro.txt");
272 using (File.CreateText (file_path)) { }
274 string documentString = @"
275 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
276 <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
278 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
279 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
282 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)' >
283 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
284 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
286 <Message Text=""I0 : @(I0), I1: @(I1)""/>
291 engine = new Engine (Consts.BinPath);
292 project = engine.CreateNewProject ();
294 TestMessageLogger testLogger = new TestMessageLogger ();
295 engine.RegisterLogger (testLogger);
297 project.LoadXml (documentString);
299 if (!project.Build ("1")) {
300 testLogger.DumpMessages ();
301 Assert.Fail ("Build failed");
303 Assert.IsTrue (File.Exists (target_file), "A2");
304 Assert.AreEqual (FileAttributes.Normal, File.GetAttributes (target_file), "A3");
308 public void TestCopy_OverwriteReadOnlyTrue ()
312 string file_path = Path.Combine (source_path, "copyro1.txt");
313 string target_file = Path.Combine (target_path, "copyro1.txt");
315 using (File.CreateText (file_path)) { }
316 using (File.CreateText (target_file)) { }
318 File.SetAttributes (target_file, FileAttributes.ReadOnly);
319 Assert.AreEqual (FileAttributes.ReadOnly, File.GetAttributes (target_file), "A1");
321 string documentString = @"
322 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
323 <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
325 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
326 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
329 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)' OverwriteReadOnlyFiles='true'>
330 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
331 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
333 <Message Text=""I0 : @(I0), I1: @(I1)""/>
338 engine = new Engine (Consts.BinPath);
339 project = engine.CreateNewProject ();
341 TestMessageLogger testLogger = new TestMessageLogger ();
342 engine.RegisterLogger (testLogger);
344 project.LoadXml (documentString);
346 if (!project.Build ("1")) {
347 var sb = new StringBuilder ();
348 testLogger.DumpMessages (sb);
349 Assert.Fail ("Build failed " + sb.ToString ());
351 Assert.IsTrue (File.Exists (target_file), "A2");
352 Assert.AreEqual (FileAttributes.Normal, File.GetAttributes (target_file), "A3");
356 public void TestCopy_OverwriteReadOnlyFalse ()
360 string file_path = Path.Combine (source_path, "copyro2.txt");
361 string target_file = Path.Combine (target_path, "copyro2.txt");
363 using (File.CreateText (file_path)) { }
364 using (File.CreateText (target_file)) { }
366 File.SetAttributes (target_file, FileAttributes.ReadOnly);
367 Assert.AreEqual (FileAttributes.ReadOnly, File.GetAttributes (target_file), "A1");
369 string documentString = @"
370 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
371 <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
373 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
374 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
377 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)'>
378 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
379 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
381 <Message Text=""I0 : @(I0), I1: @(I1)""/>
386 engine = new Engine (Consts.BinPath);
387 project = engine.CreateNewProject ();
389 TestMessageLogger testLogger = new TestMessageLogger ();
390 engine.RegisterLogger (testLogger);
392 project.LoadXml (documentString);
394 // build should fail because of the readonly target file
395 Assert.IsFalse (project.Build ("1"));
397 File.SetAttributes (target_file, FileAttributes.Normal);
401 public void TestCopy_Retries ()
405 string file_path = Path.Combine (source_path, "copyretries.txt");
406 string target_file = Path.Combine (target_path, "copyretries.txt");
408 using (File.CreateText (file_path)) { }
409 using (File.CreateText (target_file)) { }
411 File.SetAttributes (target_file, FileAttributes.ReadOnly);
412 Assert.AreEqual (FileAttributes.ReadOnly, File.GetAttributes (target_file), "A1");
414 string documentString = @"
415 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
416 <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
418 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
419 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
422 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)' Retries='3' RetryDelayMilliseconds='2000'>
423 <Output TaskParameter='CopiedFiles' ItemName='I0'/>
424 <Output TaskParameter='DestinationFiles' ItemName='I1'/>
426 <Message Text=""I0 : @(I0), I1: @(I1)""/>
431 engine = new Engine (Consts.BinPath);
432 project = engine.CreateNewProject ();
434 TestMessageLogger testLogger = new TestMessageLogger ();
435 engine.RegisterLogger (testLogger);
437 project.LoadXml (documentString);
439 // remove the read-only flag from the file after a few secs,
440 // so copying works after retries
443 File.SetAttributes (target_file, FileAttributes.Normal);
446 if (!project.Build ("1")) {
447 var sb = new StringBuilder ();
448 testLogger.DumpMessages (sb);
449 Assert.Fail ("Build failed " + sb.ToString ());
452 testLogger.CheckLoggedAny ("Copying failed. Retries left: 3.", MessageImportance.Normal, "A2");
455 void CheckCopyBuildItems (Project project, string [] source_files, string destination_folder, string prefix)
457 int num = source_files.Length;
458 for (int i = 0; i < num; i ++)
459 Assert.IsTrue (File.Exists (source_files [i]), prefix + " C1");
461 BuildItemGroup big = project.GetEvaluatedItemsByName ("I0");
463 Assert.AreEqual (num, big.Count, prefix + " C2");
464 for (int i = 0; i < num; i++) {
465 string suffix = (i + 1).ToString ();
466 BuildItem bi = big [i];
467 Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])),
468 bi.FinalItemSpec, prefix + " C3 #" + suffix);
470 Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C4 #" + suffix);
473 big = project.GetEvaluatedItemsByName ("I1");
474 Assert.AreEqual (num, big.Count, prefix + " C6");
475 for (int i = 0; i < num; i++) {
476 string suffix = (i + 1).ToString ();
477 BuildItem bi = big [i];
478 Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])),
479 bi.FinalItemSpec, prefix + " C7 #" + suffix);
480 Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C8 #" + suffix);