e3cd166b9f9ad123dfb0713299d0cb3e832d08e3
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / CopyTest.cs
1 //
2 // CopyTest.cs
3 //  
4 // Author:
5 //   Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2009 Novell, Inc (http://www.novell.com)
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.IO;
30 using Microsoft.Build.BuildEngine;
31 using NUnit.Framework;
32
33 namespace MonoTests.Microsoft.Build.Tasks {
34
35         [TestFixture]
36         public class CopyTest {
37                 string source_path, target_path;
38
39                 [SetUp]
40                 public void CreateDir ()
41                 {
42                         source_path = Path.Combine (Path.Combine ("Test", "resources"), "Copy");
43                         Directory.CreateDirectory (source_path);
44                         target_path = Path.Combine (Path.Combine ("Test", "resources"), "Target");
45                         Directory.CreateDirectory (target_path);
46                 }
47
48                 [TearDown]
49                 public void RemoveDirectories ()
50                 {
51                         Directory.Delete (source_path, true);
52                         Directory.Delete (target_path, true);
53                 }
54
55                 [Test]
56                 public void TestCopy1 ()
57                 {
58                         Engine engine;
59                         Project project;
60                         string file_path = Path.Combine(source_path, "copy.txt");
61                         string target_file = Path.Combine (target_path, "copy.txt");
62
63                         using (File.CreateText (file_path)) { }
64
65                         string documentString = @"
66                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
67                                         <PropertyGroup><DestFile>" + target_file + @"</DestFile></PropertyGroup>
68                                         <ItemGroup>
69                                                 <SFiles Include='" + file_path + @"'><Md>1</Md></SFiles>
70                                                 <DFiles Include='$(DestFile)'><Mde>2</Mde></DFiles>
71                                         </ItemGroup>
72                                         <Target Name='1'>
73                                                 <Copy SourceFiles='@(SFiles)' DestinationFiles='@(DFiles)' SkipUnchangedFiles='true' >
74                                                         <Output TaskParameter='CopiedFiles' ItemName='I0'/>
75                                                         <Output TaskParameter='DestinationFiles' ItemName='I1'/>
76                                                 </Copy>
77                                                 <Message Text=""I0 : @(I0), I1: @(I1)""/>
78                                         </Target>
79                                 </Project>
80                         ";
81
82                         engine = new Engine (Consts.BinPath);
83                         project = engine.CreateNewProject ();
84
85                         TestMessageLogger testLogger = new TestMessageLogger ();
86                         engine.RegisterLogger (testLogger);
87
88                         project.LoadXml (documentString);
89
90                         if (!project.Build ("1")) {
91                                 testLogger.DumpMessages ();
92                                 Assert.Fail ("Build failed");
93                         }
94                         Assert.IsTrue (File.Exists (target_file), "A2");
95
96                         BuildItemGroup big = project.GetEvaluatedItemsByName ("I0");
97                         Assert.AreEqual (1, big.Count, "A3");
98                         BuildItem bi = big [0];
99                         Assert.AreEqual (target_file, bi.FinalItemSpec, "A4");
100                         Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A4");
101                         Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A5");
102
103                         big = project.GetEvaluatedItemsByName ("I1");
104                         Assert.AreEqual (1, big.Count, "A10");
105                         bi = big [0];
106                         Assert.AreEqual (target_file, bi.FinalItemSpec, "A11");
107                         Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A12");
108                         Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A13");
109
110                         // build again, this time files won't get copied because
111                         // of SkipUnchangedFiles=true
112                         if (!project.Build ("1")) {
113                                 testLogger.DumpMessages ();
114                                 Assert.Fail ("Build failed #2");
115                         }
116                         Assert.IsTrue (File.Exists (target_file), "A20");
117
118                         big = project.GetEvaluatedItemsByName ("I0");
119                         Assert.AreEqual (1, big.Count, "A21");
120                         bi = big [0];
121                         Assert.AreEqual (target_file, bi.FinalItemSpec, "A22");
122                         Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A23");
123                         Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A24");
124
125                         big = project.GetEvaluatedItemsByName ("I1");
126                         Assert.AreEqual (1, big.Count, "A25");
127                         bi = big [0];
128                         Assert.AreEqual (target_file, bi.FinalItemSpec, "A26");
129                         Assert.AreEqual ("1", bi.GetMetadata ("Md"), "A27");
130                         Assert.AreEqual ("2", bi.GetMetadata ("Mde"), "A28");
131                 }
132
133                 [Test]
134                 public void TestCopy2 ()
135                 {
136                         Engine engine;
137                         Project project;
138                         string [] file_paths = new string [] {
139                                 Path.Combine (source_path, "copy1.txt"),
140                                 Path.Combine (source_path, "copy2.txt")
141                         };
142
143                         using (File.CreateText (file_paths[0])) { }
144                         using (File.CreateText (file_paths[1])) { }
145
146                         string documentString = @"
147                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
148                                         <PropertyGroup><TargetPath>" + target_path + @"</TargetPath></PropertyGroup>
149                                         <ItemGroup>
150                                                 <SFiles Include='" + file_paths [0] + @"'><Md>1</Md></SFiles>
151                                                 <SFiles Include='" + file_paths [1] + @"'><Md>2</Md></SFiles>
152                                         </ItemGroup>
153                                         <Target Name='1'>
154                                                 <Copy SourceFiles='@(SFiles)' DestinationFolder='$(TargetPath)' SkipUnchangedFiles='true' >
155                                                         <Output TaskParameter='CopiedFiles' ItemName='I0'/>
156                                                         <Output TaskParameter='DestinationFiles' ItemName='I1'/>
157                                                 </Copy>
158                                         </Target>
159                                 </Project>
160                         ";
161                         engine = new Engine (Consts.BinPath);
162                         project = engine.CreateNewProject ();
163
164                         TestMessageLogger testLogger = new TestMessageLogger ();
165                         engine.RegisterLogger (testLogger);
166
167                         project.LoadXml (documentString);
168
169                         if (!project.Build ("1")) {
170                                 testLogger.DumpMessages ();
171                                 Assert.Fail ("Build failed");
172                         }
173
174                         CheckCopyBuildItems (project, file_paths, target_path, "A1");
175
176                         // build again, this time files won't get copied because
177                         // of SkipUnchangedFiles=true
178                         if (!project.Build ("1")) {
179                                 testLogger.DumpMessages ();
180                                 Assert.Fail ("Build failed #2");
181                         }
182                         CheckCopyBuildItems (project, file_paths, target_path, "A2");
183                 }
184
185                 [Test]
186                 public void TestCopy_EmptySources () {
187                         Engine engine;
188                         Project project;
189
190                         string documentString = @"
191                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
192                                         <Target Name='1'>
193                                                 <Copy SourceFiles='@(NonExistantSourceFiles)' DestinationFolder='$(TargetPath)' SkipUnchangedFiles='true' >
194                                                         <Output TaskParameter='CopiedFiles' ItemName='I0'/>
195                                                         <Output TaskParameter='DestinationFiles' ItemName='I1'/>
196                                                 </Copy>
197                                         </Target>
198                                 </Project>
199                         ";
200                         engine = new Engine (Consts.BinPath);
201                         project = engine.CreateNewProject ();
202
203                         TestMessageLogger testLogger = new TestMessageLogger ();
204                         engine.RegisterLogger (testLogger);
205
206                         project.LoadXml (documentString);
207
208                         
209                         if (!project.Build ("1")) {
210                                 testLogger.DumpMessages ();
211                                 Assert.Fail ("Build failed");
212                         }
213                 }
214
215                 [Test]
216                 public void TestCopy_EmptyDestFolder () {
217                         Engine engine;
218                         Project project;
219
220                         string documentString = @"
221                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
222                                         <ItemGroup>
223                                                 <SFiles Include='foo.txt'><Md>1</Md></SFiles>
224                                         </ItemGroup>
225                                         <Target Name='1'>
226                                                 <Copy SourceFiles='@(SFiles)' DestinationFolder='@(NonExistant)' DestinationFiles='@(NonExistant)' SkipUnchangedFiles='true' >
227                                                         <Output TaskParameter='CopiedFiles' ItemName='I0'/>
228                                                         <Output TaskParameter='DestinationFiles' ItemName='I1'/>
229                                                 </Copy>
230                                         </Target>
231                                 </Project>
232                         ";
233                         engine = new Engine (Consts.BinPath);
234                         project = engine.CreateNewProject ();
235
236                         TestMessageLogger testLogger = new TestMessageLogger ();
237                         engine.RegisterLogger (testLogger);
238
239                         project.LoadXml (documentString);
240                         if (project.Build ("1")) {
241                                 testLogger.DumpMessages ();
242                                 Assert.Fail ("Build should have failed");
243                         }
244                 }
245
246                 void CheckCopyBuildItems (Project project, string [] source_files, string destination_folder, string prefix)
247                 {
248                         int num = source_files.Length;
249                         for (int i = 0; i < num; i ++)
250                                 Assert.IsTrue (File.Exists (source_files [i]), prefix + " C1");
251
252                         BuildItemGroup big = project.GetEvaluatedItemsByName ("I0");
253
254                         Assert.AreEqual (num, big.Count, prefix + " C2");
255                         for (int i = 0; i < num; i++) {
256                                 string suffix = (i + 1).ToString ();
257                                 BuildItem bi = big [i];
258                                 Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])),
259                                         bi.FinalItemSpec, prefix + " C3 #" + suffix);
260
261                                 Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C4 #" + suffix);
262                         }
263
264                         big = project.GetEvaluatedItemsByName ("I1");
265                         Assert.AreEqual (num, big.Count, prefix + " C6");
266                         for (int i = 0; i < num; i++) {
267                                 string suffix = (i + 1).ToString ();
268                                 BuildItem bi = big [i];
269                                 Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])),
270                                         bi.FinalItemSpec, prefix + " C7 #" + suffix);
271                                 Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C8 #" + suffix);
272                         }
273                  }
274         }
275 }