2 // AssignTargetPathTest.cs
5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2008 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 NUnit.Framework;
31 using Microsoft.Build.BuildEngine;
34 namespace MonoTests.Microsoft.Build.Tasks
43 public class AssignTargetPathTest
45 //inspired from PathTest.cs
47 static char DSC = Path.DirectorySeparatorChar;
54 } else if ('\\' == DSC) {
58 //FIXME: For Mac. figure this out when we need it
63 public void TestExecute1()
65 if (OS == OsType.Unix) {
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" },
70 } else if (OS == OsType.Windows) {
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" },
79 public void TestExecute2()
81 string root = Path.GetPathRoot (Environment.CurrentDirectory);
82 string cur_dir_minus_root = Environment.CurrentDirectory.Substring (root.Length);
84 if (OS == OsType.Unix) {
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"},
89 } else if (OS == OsType.Windows) {
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"},
98 public void TestExecute3()
100 string root = Path.GetPathRoot (Environment.CurrentDirectory);
101 string cur_dir_minus_root = Environment.CurrentDirectory.Substring (root.Length);
103 if (OS == OsType.Unix) {
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") },
109 } else if (OS == OsType.Windows) {
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") },
119 public void TestLink ()
121 string projectText = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
123 <FooFiles Include=""xyz.cs"">
124 <Child>Cxyz.cs</Child>
125 <Link>Test\Link\xyz.cs</Link>
127 <FooFiles Include=""rel\bar.resx"">
128 <Child>Crel\bar.resx</Child>
129 <Link>Test\Link\bar.resx</Link>
131 <FooFiles Include=""rel\qwe.txt"">
132 <Child>Crel\qwe.txt</Child>
133 <Link>..\Test\Link\qwe.txt</Link>
137 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""/"">
138 <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
142 Engine engine = new Engine(Consts.BinPath);
143 Project project = engine.CreateNewProject();
145 project.LoadXml(projectText);
148 Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
150 string [] files = new string [] { "xyz.cs", "rel/bar.resx", "rel/qwe.txt"};
151 string [] assignedFiles = new string [] {
152 PathCombine ("Test", "Link", "xyz.cs"),
153 PathCombine ("Test", "Link", "bar.resx"),
154 PathCombine ("..", "Test", "Link", "qwe.txt")
157 BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
158 Assert.AreEqual(files.Length, include.Count, id + "2");
160 for (int i = 0; i < files.Length; i++) {
161 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
162 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
163 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
164 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
165 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
169 string PathCombine (string path1, params string[] parts)
171 if (parts == null || parts.Length == 0)
174 string final_path = path1;
175 foreach (string part in parts)
176 final_path = Path.Combine (final_path, part);
181 void CheckTargetPath(string[] files, string[] assignedFiles, string rootFolder, string id)
183 Engine engine = new Engine(Consts.BinPath);
184 Project project = engine.CreateNewProject();
186 string projectText = CreateProjectString(files, rootFolder);
187 project.LoadXml(projectText);
189 Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
191 BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
192 Assert.AreEqual(files.Length, include.Count, id + "2");
194 for (int i = 0; i < files.Length; i++) {
195 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
196 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
197 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
198 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
199 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
203 string CreateProjectString(string[] files, string rootFolder)
205 StringBuilder sb = new StringBuilder();
206 sb.Append(@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
207 foreach (string file in files)
208 sb.AppendFormat("<FooFiles Include=\"{0}\"><Child>C{0}</Child></FooFiles>\n", file);
210 sb.AppendFormat(@"</ItemGroup>
212 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""{0}"">
213 <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
216 </Project>", rootFolder);
218 return sb.ToString();