[xbuild] Fix bug #671700, resource naming in presence of "Link".
[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", "a/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                         string root = Path.GetPathRoot (Environment.CurrentDirectory);
82                         string cur_dir_minus_root = Environment.CurrentDirectory.Substring (root.Length);
83
84                         if (OS == OsType.Unix) {
85                                 CheckTargetPath(
86                                         new string[] { "//a/b/abc.cs", "k/../k/def.cs", "/xyz.cs", "/different/xyz/foo.cs"},
87                                         new string[] { "a/b/abc.cs", Path.Combine (cur_dir_minus_root, "k/def.cs"), "xyz.cs", "different/xyz/foo.cs"},
88                                         "/", "A");
89                         } else if (OS == OsType.Windows) {
90                                 CheckTargetPath(
91                                         new string[] { root + @"a\b\abc.cs", @"k\..\k\def.cs", root + @"xyz.cs", root + @"different\xyz\foo.cs"},
92                                         new string[] { "a\\b\\abc.cs", cur_dir_minus_root + "\\k\\def.cs", "xyz.cs", "different\\xyz\\foo.cs"},
93                                         root, "A");
94                         }
95                 }
96
97                 [Test]
98                 public void TestExecute3()
99                 {
100                         string root = Path.GetPathRoot (Environment.CurrentDirectory);
101                         string cur_dir_minus_root = Environment.CurrentDirectory.Substring (root.Length);
102
103                         if (OS == OsType.Unix) {
104                                 CheckTargetPath(
105                                         new string[] { "xyz.cs", "rel/bar.resx" },
106                                         new string[] { Path.Combine (cur_dir_minus_root, "xyz.cs"),
107                                                 Path.Combine (cur_dir_minus_root, "rel/bar.resx") },
108                                         "/", "A");
109                         } else if (OS == OsType.Windows) {
110                                 CheckTargetPath(
111                                         new string[] { "xyz.cs", "rel\\bar.resx" },
112                                         new string[] { Path.Combine (cur_dir_minus_root, "xyz.cs"),
113                                                 Path.Combine (cur_dir_minus_root, "rel\\bar.resx") },
114                                         root, "A");
115                         }
116                 }
117
118                 [Test]
119                 public void TestLink ()
120                 {
121                         string projectText = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
122                                 <ItemGroup>
123                                         <FooFiles Include=""xyz.cs"">
124                                                 <Child>Cxyz.cs</Child>
125                                                 <Link>Test\Link\xyz.cs</Link>
126                                         </FooFiles>
127                                         <FooFiles Include=""rel\bar.resx"">
128                                                 <Child>Crel\bar.resx</Child>
129                                                 <Link>Test\Link\bar.resx</Link>
130                                         </FooFiles>
131                                         <FooFiles Include=""rel\qwe.txt"">
132                                                 <Child>Crel\qwe.txt</Child>
133                                                 <Link>..\Test\Link\qwe.txt</Link>
134                                         </FooFiles>
135                                 </ItemGroup>
136                                 <Target Name=""1"">
137                                         <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""/"">
138                                                 <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
139                                         </AssignTargetPath>
140                                 </Target>
141                         </Project>";
142                         Engine engine = new Engine(Consts.BinPath);
143                         Project project = engine.CreateNewProject();
144
145                         project.LoadXml(projectText);
146
147                         string id = "A";
148                         Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
149
150                         string [] files = new string [] { "xyz.cs", "rel/bar.resx", "rel/qwe.txt"};
151                         string [] assignedFiles = new string [] { "Test/Link/xyz.cs", "Test/Link/bar.resx", "../Test/Link/qwe.txt"};
152
153                         BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
154                         Assert.AreEqual(files.Length, include.Count, id + "2");
155
156                         for (int i = 0; i < files.Length; i++) {
157                                 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
158                                 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
159                                 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
160                                 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
161                                 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
162                         }
163                 }
164
165                 void CheckTargetPath(string[] files, string[] assignedFiles, string rootFolder, string id)
166                 {
167                         Engine engine = new Engine(Consts.BinPath);
168                         Project project = engine.CreateNewProject();
169
170                         string projectText = CreateProjectString(files, rootFolder);
171                         project.LoadXml(projectText);
172
173                         Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
174
175                         BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
176                         Assert.AreEqual(files.Length, include.Count, id + "2");
177
178                         for (int i = 0; i < files.Length; i++) {
179                                 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
180                                 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
181                                 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
182                                 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
183                                 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
184                         }
185                 }
186
187                 string CreateProjectString(string[] files, string rootFolder)
188                 {
189                         StringBuilder sb = new StringBuilder();
190                         sb.Append(@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
191                         foreach (string file in files)
192                                 sb.AppendFormat("<FooFiles Include=\"{0}\"><Child>C{0}</Child></FooFiles>\n", file);
193
194                         sb.AppendFormat(@"</ItemGroup>
195                         <Target Name=""1"">
196                                 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""{0}"">
197                                         <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
198                                 </AssignTargetPath>
199                         </Target>
200                 </Project>", rootFolder);
201
202                         return sb.ToString();
203                 }
204         }
205 }