Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Evaluation / ResolvedImportTest.cs
1 //
2 // ResolvedImportTest.cs
3 //
4 // Author:
5 //   Atsushi Enomoto (atsushi@xamarin.com)
6 //
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.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.IO;
30 using System.Linq;
31 using System.Xml;
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;
38
39 namespace MonoTests.Microsoft.Build.Evaluation
40 {
41         [TestFixture]
42         public class ResolvedImportTest
43         {
44                 const string temp_file_name = "test_imported.proj";
45                 
46                 [Test]
47                 public void SimpleImportAndSemanticValues ()
48                 {
49                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
50   <Import Project='test_imported.proj' />
51 </Project>";
52                         string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
53   <PropertyGroup>
54     <A>x</A>
55     <B>y</B>
56   </PropertyGroup>
57   <ItemGroup>
58     <X Include=""included.txt"" />
59   </ItemGroup>
60 </Project>";
61                         using (var ts = File.CreateText (temp_file_name))
62                                 ts.Write (imported);
63                         try {
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");
74                         } finally {
75                                 File.Delete (temp_file_name);
76                         }
77                 }
78
79                         string import_overrides_test_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
80   <PropertyGroup>
81     <A>X</A>
82   </PropertyGroup>
83   <Import Condition=""{0}"" Project='test_imported.proj' />
84   <PropertyGroup>
85     <B>Y</B>
86   </PropertyGroup>
87 </Project>";
88                         string import_overrides_test_imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
89   <PropertyGroup>
90     <C Condition='$(A)==x'>c</C>
91     <A>a</A>
92     <B>b</B>
93   </PropertyGroup>
94   <ItemGroup>
95     <X Include=""included.txt"" />
96   </ItemGroup>
97 </Project>";
98
99                 void ImportAndPropertyOverrides (string label, string condition, string valueA, string valueB, string valueAPredecessor, bool existsC)
100                 {
101                         using (var ts = File.CreateText (temp_file_name))
102                                 ts.Write (import_overrides_test_imported);
103                         try {
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");
113                                 else {
114                                         Assert.IsNotNull (a.Predecessor, label + "#3.2");
115                                         Assert.AreEqual (valueAPredecessor, a.Predecessor.EvaluatedValue, label + "#3.3");
116                                 }
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.
121                                 if (existsC) {
122                                         Assert.IsNotNull (c, label + "#6");
123                                         Assert.AreEqual ("c", c.EvaluatedValue, label + "#7");
124                                 }
125                                 else
126                                         Assert.IsNull (c, label + "#8");
127                         } finally {
128                                 File.Delete (temp_file_name);
129                         }
130                 }
131
132                 [Test]
133                 public void ImportAndPropertyOverrides ()
134                 {
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
139                 }
140
141                 // FIXME:
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.
144                 //
145                 [Test]
146                 public void EvaluateMSBuildThisFileProperty ()
147                 {
148                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
149   <PropertyGroup>
150     <A>$(MSBuildThisFile)</A>
151   </PropertyGroup>
152   <Import Project='test_imported.proj' />
153 </Project>";
154                         string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
155   <PropertyGroup>
156     <B>$(MSBuildThisFile)</B>
157   </PropertyGroup>
158 </Project>";
159                         using (var ts = File.CreateText (temp_file_name))
160                                 ts.Write (imported);
161                         try {
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");
169
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");
175                         } finally {
176                                 File.Delete (temp_file_name);
177                         }
178                 }
179         }
180 }
181