959c8991b771a3d91c2573521f068cb5d8093f58
[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                 void CheckTargetPath(string[] files, string[] assignedFiles, string rootFolder, string id)
119                 {
120                         Engine engine = new Engine(Consts.BinPath);
121                         Project project = engine.CreateNewProject();
122
123                         string projectText = CreateProjectString(files, rootFolder);
124                         project.LoadXml(projectText);
125
126                         Assert.IsTrue(project.Build("1"), id + "1 : Error in building");
127
128                         BuildItemGroup include = project.GetEvaluatedItemsByName("FooPath");
129                         Assert.AreEqual(files.Length, include.Count, id + "2");
130
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");
137                         }
138                 }
139
140                 string CreateProjectString(string[] files, string rootFolder)
141                 {
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);
146
147                         sb.AppendFormat(@"</ItemGroup>
148                         <Target Name=""1"">
149                                 <AssignTargetPath Files=""@(FooFiles)"" RootFolder=""{0}"">
150                                         <Output TaskParameter=""AssignedFiles"" ItemName=""FooPath"" />
151                                 </AssignTargetPath>
152                         </Target>
153                 </Project>", rootFolder);
154
155                         return sb.ToString();
156                 }
157         }
158 }