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") },
118 void CheckTargetPath(string[] files, string[] assignedFiles, string rootFolder, string id)
120 Engine engine = new Engine(Consts.BinPath);
121 Project project = engine.CreateNewProject();
123 string projectText = CreateProjectString(files, rootFolder);
124 project.LoadXml(projectText);
126 Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
128 BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
129 Assert.AreEqual(files.Length, include.Count, id + "2");
131 for (int i = 0; i < files.Length; i++) {
132 Assert.AreEqual (files [i], include [i].FinalItemSpec, id + "3, file #" + i);
133 Assert.IsTrue (include[i].HasMetadata ("TargetPath"), id + "4, file #" + i + ", TargetPath metadata missing");
134 Assert.AreEqual (assignedFiles [i], include[i].GetMetadata("TargetPath"), id + "5, file #" + i);
135 Assert.IsTrue (include [i].HasMetadata ("Child"), id + "6, file #" + i + ", Child metadata missing");
136 Assert.AreEqual ("C" + files [i], include [i].GetMetadata ("Child"), id + "7, file #" + i + ", Child metadata value incorrect");
140 string CreateProjectString(string[] files, string rootFolder)
142 StringBuilder sb = new StringBuilder();
143 sb.Append(@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
144 foreach (string file in files)
145 sb.AppendFormat("<FooFiles Include=\"{0}\"><Child>C{0}</Child></FooFiles>\n", file);
147 sb.AppendFormat(@"</ItemGroup>
149 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""{0}"">
150 <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
153 </Project>", rootFolder);
155 return sb.ToString();