2 // ResolvedImportTest.cs
5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.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.
32 using Microsoft.Build.Construction;
33 using Microsoft.Build.Evaluation;
34 using NUnit.Framework;
35 using Microsoft.Build.Exceptions;
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Execution;
39 namespace MonoTests.Microsoft.Build.Evaluation
42 public class ResolvedImportTest
44 const string temp_file_name = "test_imported.proj";
47 public void SimpleImportAndSemanticValues ()
49 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
50 <Import Project='test_imported.proj' />
52 string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
58 <X Include=""included.txt"" />
61 using (var ts = File.CreateText (temp_file_name))
64 var reader = XmlReader.Create (new StringReader (xml));
65 var root = ProjectRootElement.Create (reader);
66 Assert.AreEqual (temp_file_name, root.Imports.First ().Project, "#1");
67 var proj = new Project (root);
68 var prop = proj.GetProperty ("A");
69 Assert.IsNotNull (prop, "#2");
70 Assert.IsTrue (prop.IsImported, "#3");
71 var item = proj.GetItems ("X").FirstOrDefault ();
72 Assert.IsNotNull (item, "#4");
73 Assert.AreEqual ("included.txt", item.EvaluatedInclude, "#5");
75 File.Delete (temp_file_name);
79 string import_overrides_test_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
83 <Import Condition=""{0}"" Project='test_imported.proj' />
88 string import_overrides_test_imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
90 <C Condition='$(A)==x'>c</C>
95 <X Include=""included.txt"" />
99 void ImportAndPropertyOverrides (string label, string condition, string valueA, string valueB, string valueAPredecessor, bool existsC)
101 using (var ts = File.CreateText (temp_file_name))
102 ts.Write (import_overrides_test_imported);
104 string xml = string.Format (import_overrides_test_xml, condition);
105 var reader = XmlReader.Create (new StringReader (xml));
106 var root = ProjectRootElement.Create (reader);
107 var proj = new Project (root);
108 var a = proj.GetProperty ("A");
109 Assert.IsNotNull (a, label + "#2");
110 Assert.AreEqual (valueA, a.EvaluatedValue, label + "#3");
111 if (valueAPredecessor == null)
112 Assert.IsNull (a.Predecessor, label + "#3.1");
114 Assert.IsNotNull (a.Predecessor, label + "#3.2");
115 Assert.AreEqual (valueAPredecessor, a.Predecessor.EvaluatedValue, label + "#3.3");
117 var b = proj.GetProperty ("B");
118 Assert.IsNotNull (b, label + "#4");
119 Assert.AreEqual (valueB, b.EvaluatedValue, label + "#5");
120 var c = proj.GetProperty ("C"); // yes it can be retrieved.
122 Assert.IsNotNull (c, label + "#6");
123 Assert.AreEqual ("c", c.EvaluatedValue, label + "#7");
126 Assert.IsNull (c, label + "#8");
128 File.Delete (temp_file_name);
133 public void ImportAndPropertyOverrides ()
135 ImportAndPropertyOverrides ("[1]", "'True'", "a", "Y", "X", true);
136 ImportAndPropertyOverrides ("[2]", "$(A)=='X'", "a", "Y", "X", true); // evaluated as true
137 ImportAndPropertyOverrides ("[3]", "$(B)=='Y'", "X", "Y", null, false); // evaluated as false
138 ImportAndPropertyOverrides ("[4]", "$(B)=='b'", "X", "Y", null, false); // of course not evaluated with imported value
142 // Looks like $(MSBuildThisFile) is available only within property value, not via .NET MSBuild API.
143 // Right now our variable is added as a Reserved property, but we will have to hide it.
146 public void EvaluateMSBuildThisFileProperty ()
148 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
150 <A>$(MSBuildThisFile)</A>
152 <Import Project='test_imported.proj' />
154 string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
156 <B>$(MSBuildThisFile)</B>
159 using (var ts = File.CreateText (temp_file_name))
162 var reader = XmlReader.Create (new StringReader (xml));
163 var root = ProjectRootElement.Create (reader);
164 var proj = new Project (root);
165 var a = proj.GetProperty ("A");
166 Assert.AreEqual (string.Empty, a.EvaluatedValue, "#1");
167 var b = proj.GetProperty ("B");
168 Assert.AreEqual (temp_file_name, b.EvaluatedValue, "#2");
170 var pi = new ProjectInstance (root);
171 var ai = pi.GetProperty ("A");
172 Assert.AreEqual (string.Empty, ai.EvaluatedValue, "#3");
173 var bi = pi.GetProperty ("B");
174 Assert.AreEqual (temp_file_name, bi.EvaluatedValue, "#4");
176 File.Delete (temp_file_name);