5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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.
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
36 namespace MonoTests.Microsoft.Build.BuildEngine {
38 public class ImportTest {
44 public void TestAdd1 ()
47 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
48 <Import Project='second.proj'/>
51 using (StreamWriter sw = new StreamWriter ("Test/resources/first.proj")) {
56 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
59 using (StreamWriter sw = new StreamWriter ("Test/resources/second.proj")) {
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'/>
70 engine = new Engine (Consts.BinPath);
72 project = engine.CreateNewProject ();
73 project.LoadXml (documentString);
75 Import[] t = new Import [2];
76 Assert.AreEqual (2, project.Imports.Count, "Number of imports");
77 project.Imports.CopyTo (t, 0);
79 string base_dir = Path.Combine (Environment.CurrentDirectory, Path.Combine ("Test", "resources"));
81 Assert.IsNull (t [0].Condition, "A1");
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");
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");
93 [ExpectedException (typeof (InvalidProjectFileException))]
94 public void TestAdd2 ()
96 string documentString = @"
97 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
102 engine = new Engine (Consts.BinPath);
104 project = engine.CreateNewProject ();
105 project.LoadXml (documentString);
109 [Category ("NotWorking")]
110 public void TestAdd3 ()
112 string documentString = @"
113 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
114 <Import Project='Test/resources/SelfImport.csproj'/>
118 engine = new Engine (Consts.BinPath);
120 project = engine.CreateNewProject ();
121 project.LoadXml (documentString);
123 Assert.AreEqual (1, project.Imports.Count, "A1");
127 public void TestRelativeImport1 ()
129 string documentString = @"
130 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
131 <Import Project='Test/resources/RelativeImport1.csproj'/>
135 engine = new Engine (Consts.BinPath);
137 project = engine.CreateNewProject ();
138 project.LoadXml (documentString);
140 Assert.AreEqual ("B", project.EvaluatedProperties ["A"].FinalValue, "A1");
144 public void TestItems1 ()
146 string documentString = @"
147 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
148 <Import Project='Test/resources/Items.csproj'/>
152 engine = new Engine (Consts.BinPath);
154 project = engine.CreateNewProject ();
155 project.LoadXml (documentString);
157 BuildItemGroup [] groups = new BuildItemGroup [1];
158 project.ItemGroups.CopyTo (groups, 0);
160 Assert.IsTrue (groups [0].IsImported, "A1");
161 Assert.AreEqual (1, groups [0].Count, "A2");
165 [ExpectedException (typeof (InvalidProjectFileException))]
166 public void TestMissingImport1 ()
168 string documentString = @"
169 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
170 <Import Project='Test/resources/NonExistantProject.proj'/>
173 engine = new Engine (Consts.BinPath);
175 project = engine.CreateNewProject ();
176 project.LoadXml (documentString, ProjectLoadSettings.None);
180 public void TestMissingImport2 ()
182 string documentString = @"
183 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
184 <Import Project='Test/resources/NonExistantProject.proj'/>
188 engine = new Engine (Consts.BinPath);
190 project = engine.CreateNewProject ();
191 project.LoadXml (documentString, ProjectLoadSettings.IgnoreMissingImports);
193 Assert.AreEqual (1, project.Imports.Count, "A1");
197 [ExpectedException (typeof (InvalidProjectFileException))]
198 public void TestMissingImportDefault ()
200 string documentString = @"
201 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
202 <Import Project='Test/resources/NonExistantProject.proj'/>
206 engine = new Engine (Consts.BinPath);
208 project = engine.CreateNewProject ();
209 project.LoadXml (documentString);
214 public void TestImportWildcard ()
216 string main_project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
218 <FooItem Include=""From main.proj""/>
221 <Import Project=""tmp\*""/>
223 <Target Name=""Build"">
224 <Message Text=""FooItem: @(FooItem)""/>
228 string other_project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
230 <FooItem Include=""From $(MSBuildThisFile)""/>
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);
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;
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");
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);
251 Project project = engine.CreateNewProject ();
252 project.Load (main_project);
254 Assert.IsTrue (project.Build ("Build"), "Build failed");
256 logger.CheckLoggedMessageHead ("FooItem: From main.proj;From first.proj;From second.proj", "A1");
257 Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
259 logger.DumpMessages ();
262 File.Delete (main_project);
263 File.Delete (first_project);
264 File.Delete (second_project);