[xbuild] Add support for wildcards in Import.
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / ImportTest.cs
1 //
2 // ImportTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
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.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
34 using System.IO;
35
36 namespace MonoTests.Microsoft.Build.BuildEngine {
37         [TestFixture]
38         public class ImportTest {
39                 
40                 Engine                  engine;
41                 Project                 project;
42                 
43                 [Test]
44                 public void TestAdd1 ()
45                 {
46                         string first = @"
47                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
48                                         <Import Project='second.proj'/>
49                                 </Project>
50 ";
51                         using (StreamWriter sw = new StreamWriter ("Test/resources/first.proj")) {
52                                 sw.Write (first);
53                         }
54
55                         string second = @"
56                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
57                                 </Project>
58 ";
59                         using (StreamWriter sw = new StreamWriter ("Test/resources/second.proj")) {
60                                 sw.Write (second);
61                         }
62
63                         string documentString = @"
64                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
65                                                 <Import Project='Test\resources\first.proj'/>
66                                                 <Import Project='Test\resources\Import.csproj' Condition='false'/>
67                                 </Project>
68                         ";
69
70                         engine = new Engine (Consts.BinPath);
71
72                         project = engine.CreateNewProject ();
73                         project.LoadXml (documentString);
74
75                         Import[] t = new Import [2];
76                         Assert.AreEqual (2, project.Imports.Count, "Number of imports");
77                         project.Imports.CopyTo (t, 0);
78
79                         string base_dir = Path.Combine (Environment.CurrentDirectory, Path.Combine ("Test", "resources"));
80
81                         Assert.IsNull (t [0].Condition, "A1");
82
83                         Assert.AreEqual (false, t[0].IsImported, "A5");
84                         Assert.AreEqual ("Test\\resources\\first.proj", t[0].ProjectPath, "A6");
85                         Assert.AreEqual (Path.Combine (base_dir, "first.proj"), t[0].EvaluatedProjectPath, "A7");
86
87                         Assert.AreEqual (true, t[1].IsImported, "A2");
88                         Assert.AreEqual ("second.proj", t[1].ProjectPath, "A3");
89                         Assert.AreEqual (Path.Combine (base_dir, "second.proj"), t[1].EvaluatedProjectPath, "A4");
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (InvalidProjectFileException))]
94                 public void TestAdd2 ()
95                 {
96                         string documentString = @"
97                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
98                                         <Import Project=''/>
99                                 </Project>
100                         ";
101
102                         engine = new Engine (Consts.BinPath);
103
104                         project = engine.CreateNewProject ();
105                         project.LoadXml (documentString);
106                 }
107
108                 [Test]
109                 [Category ("NotWorking")]
110                 public void TestAdd3 ()
111                 {
112                         string documentString = @"
113                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
114                                         <Import Project='Test/resources/SelfImport.csproj'/>
115                                 </Project>
116                         ";
117
118                         engine = new Engine (Consts.BinPath);
119
120                         project = engine.CreateNewProject ();
121                         project.LoadXml (documentString);
122
123                         Assert.AreEqual (1, project.Imports.Count, "A1");
124                 }
125
126                 [Test]
127                 public void TestRelativeImport1 ()
128                 {
129                         string documentString = @"
130                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
131                                         <Import Project='Test/resources/RelativeImport1.csproj'/>
132                                 </Project>
133                         ";
134
135                         engine = new Engine (Consts.BinPath);
136
137                         project = engine.CreateNewProject ();
138                         project.LoadXml (documentString);
139
140                         Assert.AreEqual ("B", project.EvaluatedProperties ["A"].FinalValue, "A1");
141                 }
142
143                 [Test]
144                 public void TestItems1 ()
145                 {
146                         string documentString = @"
147                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
148                                         <Import Project='Test/resources/Items.csproj'/>
149                                 </Project>
150                         ";
151
152                         engine = new Engine (Consts.BinPath);
153
154                         project = engine.CreateNewProject ();
155                         project.LoadXml (documentString);
156
157                         BuildItemGroup [] groups = new BuildItemGroup [1];
158                         project.ItemGroups.CopyTo (groups, 0);
159
160                         Assert.IsTrue (groups [0].IsImported, "A1");
161                         Assert.AreEqual (1, groups [0].Count, "A2");
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (InvalidProjectFileException))]
166                 public void TestMissingImport1 ()
167                 {
168                         string documentString = @"
169                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
170                                         <Import Project='Test/resources/NonExistantProject.proj'/>
171                                 </Project>";
172
173                         engine = new Engine (Consts.BinPath);
174
175                         project = engine.CreateNewProject ();
176                         project.LoadXml (documentString, ProjectLoadSettings.None);
177                 }
178
179                 [Test]
180                 public void TestMissingImport2 ()
181                 {
182                         string documentString = @"
183                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
184                                         <Import Project='Test/resources/NonExistantProject.proj'/>
185                                 </Project>
186                         ";
187
188                         engine = new Engine (Consts.BinPath);
189
190                         project = engine.CreateNewProject ();
191                         project.LoadXml (documentString, ProjectLoadSettings.IgnoreMissingImports);
192
193                         Assert.AreEqual (1, project.Imports.Count, "A1");
194                 }
195
196                 [Test]
197                 [ExpectedException (typeof (InvalidProjectFileException))]
198                 public void TestMissingImportDefault ()
199                 {
200                         string documentString = @"
201                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
202                                         <Import Project='Test/resources/NonExistantProject.proj'/>
203                                 </Project>
204                         ";
205
206                         engine = new Engine (Consts.BinPath);
207
208                         project = engine.CreateNewProject ();
209                         project.LoadXml (documentString);
210                 }
211
212 #if NET_4_0
213                 [Test]
214                 public void TestImportWildcard ()
215                 {
216                         string main_project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
217                                 <ItemGroup>
218                                         <FooItem Include=""From main.proj""/>
219                                 </ItemGroup>
220
221                                 <Import Project=""tmp\*""/>
222
223                                 <Target Name=""Build"">
224                                         <Message Text=""FooItem: @(FooItem)""/>
225                                 </Target>
226                         </Project>";
227
228                         string other_project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
229                                 <ItemGroup>
230                                         <FooItem Include=""From $(MSBuildThisFile)""/>
231                                 </ItemGroup>
232                         </Project>";
233
234                         Engine engine = new Engine (Consts.BinPath);
235                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
236                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
237                         engine.RegisterLogger (logger);
238
239                         string base_dir = Path.GetFullPath (Path.Combine ("Test", "resources")) + Path.DirectorySeparatorChar;
240                         string tmp_dir = Path.GetFullPath (Path.Combine (base_dir, "tmp")) + Path.DirectorySeparatorChar;
241
242                         string main_project = Path.Combine (base_dir, "main.proj");
243                         string first_project = Path.Combine (tmp_dir, "first.proj");
244                         string second_project = Path.Combine (tmp_dir, "second.proj");
245
246                         Directory.CreateDirectory (tmp_dir);
247                         File.WriteAllText (main_project, main_project_xml);
248                         File.WriteAllText (first_project, other_project_xml);
249                         File.WriteAllText (second_project, other_project_xml);
250
251                         Project project = engine.CreateNewProject ();
252                         project.Load (main_project);
253                         try {
254                                 Assert.IsTrue (project.Build ("Build"), "Build failed");
255
256                                 logger.CheckLoggedMessageHead ("FooItem: From main.proj;From first.proj;From second.proj", "A1");
257                                 Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
258                         } catch {
259                                 logger.DumpMessages ();
260                                 throw;
261                         } finally {
262                                 File.Delete (main_project);
263                                 File.Delete (first_project);
264                                 File.Delete (second_project);
265                         }
266                 }
267 #endif
268
269         }
270 }