importing messaging-2008 branch to trunk.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / AssignTargetPathTest.cs
1 //
2 // AssignTargetPathTest.cs
3 //
4 // Author:
5 //   Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2008 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.Text;
30 using NUnit.Framework;
31 using Microsoft.Build.BuildEngine;
32 using System.IO;
33
34 namespace MonoTests.Microsoft.Build.Tasks
35 {
36         enum OsType {
37                 Windows,
38                 Unix,
39                 Mac
40         }
41
42         [TestFixture]
43         public class AssignTargetPathTest
44         {
45                 //inspired from PathTest.cs
46                 static OsType OS;
47                 static char DSC = Path.DirectorySeparatorChar;
48
49                 [SetUp]
50                 public void SetUp ()
51                 {
52                         if ('/' == DSC) {
53                                 OS = OsType.Unix;
54                         } else if ('\\' == DSC) {
55                                 OS = OsType.Windows;
56                         } else {
57                                 OS = OsType.Mac;
58                                 //FIXME: For Mac. figure this out when we need it
59                         }
60                 }
61
62                 [Test]
63                 public void TestExecute1()
64                 {
65                         if (OS == OsType.Unix) {
66                                 CheckTargetPath(
67                                         new string[] { "/a/b/./abc.cs", "/a/c/def.cs", "xyz.cs", "/different/xyz/foo.cs", "rel/bar.resx"},
68                                         new string[] { "b/abc.cs", "c/def.cs", "xyz.cs", "foo.cs", "bar.resx" },
69                                         "/a/./", "A");
70                         } else if (OS == OsType.Windows) {
71                                 CheckTargetPath(
72                                         new string[] { @"C:\a\b\.\abc.cs", @"C:\a\c\def.cs", "xyz.cs", @"C:\different\xyz\foo.cs", @"rel\bar.resx"},
73                                         new string[] { @"b\abc.cs", @"c\def.cs", "xyz.cs", "foo.cs", "bar.resx" },
74                                         @"C:\a\.\", "A");
75                         }
76                 }
77
78                 [Test]
79                 public void TestExecute2()
80                 {
81                         if (OS == OsType.Unix) {
82                                 CheckTargetPath(
83                                         new string[] { "//a/b/abc.cs", "k/../k/def.cs", "/xyz.cs", "/different/xyz/foo.cs"},
84                                         new string[] { "a/b/abc.cs", "def.cs", "xyz.cs", "different/xyz/foo.cs"},
85                                         "/", "A");
86                         } else if (OS == OsType.Windows) {
87                                 CheckTargetPath(
88                                         new string[] { @"C:\\a\b\abc.cs", @"k\..\def.cs", @"C:\xyz.cs", @"C:\different\xyz\foo.cs"},
89                                         new string[] { "a\\b\\abc.cs", "def.cs", "xyz.cs", "different\\xyz\\foo.cs"},
90                                         "C:\\", "A");
91                         }
92                 }
93
94                 [Test]
95                 public void TestExecute3()
96                 {
97                         if (OS == OsType.Unix) {
98                                 CheckTargetPath(
99                                         new string[] { "xyz.cs", "rel/bar.resx" },
100                                         new string[] { "xyz.cs", "bar.resx" },
101                                         "/", "A");
102                         } else if (OS == OsType.Windows) {
103                                 CheckTargetPath(
104                                         new string[] { "xyz.cs", "rel\\bar.resx" },
105                                         new string[] { "xyz.cs", "bar.resx" },
106                                         "C:\\", "A");
107                         }
108                 }
109
110                 void CheckTargetPath(string[] files, string[] assignedFiles, string rootFolder, string id)
111                 {
112                         Engine engine = new Engine(Consts.BinPath);
113                         Project project = engine.CreateNewProject();
114
115                         string projectText = CreateProjectString(files, rootFolder);
116                         project.LoadXml(projectText);
117
118                         Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
119
120                         BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
121                         Assert.AreEqual(files.Length, include.Count, id + "2");
122
123                         for (int i = 0; i < files.Length; i++) {
124                                 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
125                                 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
126                                 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
127                                 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
128                                 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
129                         }
130                 }
131
132                 string CreateProjectString(string[] files, string rootFolder)
133                 {
134                         StringBuilder sb = new StringBuilder();
135                         sb.Append(@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
136                         foreach (string file in files)
137                                 sb.AppendFormat("<FooFiles Include=\"{0}\"><Child>C{0}</Child></FooFiles>\n", file);
138
139                         sb.AppendFormat(@"</ItemGroup>
140                         <Target Name=""1"">
141                                 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""{0}"">
142                                         <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
143                                 </AssignTargetPath>
144                         </Target>
145                         <Import Project=""$(MSBuildBinPath)\Microsoft.Common.targets"" />
146                 </Project>", rootFolder);
147
148                         return sb.ToString();
149                 }
150         }
151 }