Fix MSBuildThisFile reserved property value (empty by default).
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Tue, 22 Oct 2013 19:22:07 +0000 (04:22 +0900)
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Tue, 3 Dec 2013 07:50:28 +0000 (16:50 +0900)
mcs/class/Microsoft.Build/Microsoft.Build.Evaluation/Project.cs
mcs/class/Microsoft.Build/Microsoft.Build.Internal/ExpressionEvaluator.cs
mcs/class/Microsoft.Build/Test/Microsoft.Build.Evaluation/ResolvedImportTest.cs

index c0d78cfe128ef8109458e47360a66299ac5995f3..77b642873e0f0424f302a0797b37a0646edc5615 100644 (file)
@@ -651,7 +651,7 @@ namespace Microsoft.Build.Evaluation
 
                internal string GetEvaluationTimeThisFile ()
                {
-                       return ProjectCollection.OngoingImports.Count > 0 ? ProjectCollection.OngoingImports.Peek () : FullPath;
+                       return ProjectCollection.OngoingImports.Count > 0 ? ProjectCollection.OngoingImports.Peek () : FullPath ?? string.Empty;
                }
        }
 }
index af57021422e1105209b28b8b6dc9afd5998c41ab..97b29a1c4a677276cae3f65f45b980502b5d8511 100644 (file)
@@ -71,7 +71,7 @@ namespace Microsoft.Build.Internal
                                throw new InvalidProjectFileException (string.Format ("Recursive reference to item '{0}' with include '{1}' was found", item.ItemType, item.UnevaluatedInclude));
                        try {
                                items.Add (item);
-                               // FIXME: needs verification if string evaluation is appropriate.
+                               // FIXME: needs verification on whether string evaluation is appropriate or not.
                                return Evaluator.Evaluate (item.UnevaluatedInclude);
                        } finally {
                                items.Remove (item);
@@ -84,7 +84,7 @@ namespace Microsoft.Build.Internal
                                throw new InvalidProjectFileException (string.Format ("Recursive reference to property '{0}' was found", prop.Name));
                        try {
                                props.Add (prop);
-                               // FIXME: needs verification if string evaluation is appropriate.
+                               // FIXME: needs verification on whether string evaluation is appropriate or not.
                                return Evaluator.Evaluate (prop.UnevaluatedValue);
                        } finally {
                                props.Remove (prop);
index 49abb7c6780754ab2816d417a8d0d8e0111db53e..10061a431d36b314b0b5b57e0f6a8d2561a3bef9 100644 (file)
@@ -13,6 +13,8 @@ namespace MonoTests.Microsoft.Build.Evaluation
        [TestFixture]
        public class ResolvedImportTest
        {
+               const string temp_file_name = "test_imported.proj";
+               
                [Test]
                public void SimpleImportAndSemanticValues ()
                {
@@ -28,12 +30,12 @@ namespace MonoTests.Microsoft.Build.Evaluation
     <X Include=""included.txt"" />
   </ItemGroup>
 </Project>";
-                       using (var ts = File.CreateText ("test_imported.proj"))
+                       using (var ts = File.CreateText (temp_file_name))
                                ts.Write (imported);
                        try {
                                var reader = XmlReader.Create (new StringReader (xml));
                                var root = ProjectRootElement.Create (reader);
-                               Assert.AreEqual ("test_imported.proj", root.Imports.First ().Project, "#1");
+                               Assert.AreEqual (temp_file_name, root.Imports.First ().Project, "#1");
                                var proj = new Project (root);
                                var prop = proj.GetProperty ("A");
                                Assert.IsNotNull (prop, "#2");
@@ -42,7 +44,7 @@ namespace MonoTests.Microsoft.Build.Evaluation
                                Assert.IsNotNull (item, "#4");
                                Assert.AreEqual ("included.txt", item.EvaluatedInclude, "#5");
                        } finally {
-                               File.Delete ("imported.proj");
+                               File.Delete (temp_file_name);
                        }
                }
 
@@ -68,7 +70,7 @@ namespace MonoTests.Microsoft.Build.Evaluation
 
                void ImportAndPropertyOverrides (string label, string condition, string valueA, string valueB, string valueAPredecessor, bool existsC)
                {
-                       using (var ts = File.CreateText ("test_imported.proj"))
+                       using (var ts = File.CreateText (temp_file_name))
                                ts.Write (import_overrides_test_imported);
                        try {
                                string xml = string.Format (import_overrides_test_xml, condition);
@@ -95,7 +97,7 @@ namespace MonoTests.Microsoft.Build.Evaluation
                                else
                                        Assert.IsNull (c, label + "#8");
                        } finally {
-                               File.Delete ("test_imported.proj");
+                               File.Delete (temp_file_name);
                        }
                }
 
@@ -107,6 +109,39 @@ namespace MonoTests.Microsoft.Build.Evaluation
                        ImportAndPropertyOverrides ("[3]", "$(B)=='Y'", "X", "Y", null, false); // evaluated as false
                        ImportAndPropertyOverrides ("[4]", "$(B)=='b'", "X", "Y", null, false); // of course not evaluated with imported value
                }
+
+               // FIXME:
+               // Looks like $(MSBuildThisFile) is available only within property value, not via .NET MSBuild API.
+               // Right now our variable is added as a Reserved property, but we will have to hide it.
+               //
+               [Test]
+               public void EvaluateMSBuildThisFileProperty ()
+               {
+                       string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+  <PropertyGroup>
+    <A>$(MSBuildThisFile)</A>
+  </PropertyGroup>
+  <Import Project='test_imported.proj' />
+</Project>";
+                       string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+  <PropertyGroup>
+    <B>$(MSBuildThisFile)</B>
+  </PropertyGroup>
+</Project>";
+                       using (var ts = File.CreateText (temp_file_name))
+                               ts.Write (imported);
+                       try {
+                               var reader = XmlReader.Create (new StringReader (xml));
+                               var root = ProjectRootElement.Create (reader);
+                               var proj = new Project (root);
+                               var a = proj.GetProperty ("A");
+                               Assert.AreEqual (string.Empty, a.EvaluatedValue, "#1");
+                               var b = proj.GetProperty ("B");
+                               Assert.AreEqual (temp_file_name, b.EvaluatedValue, "#2");
+                       } finally {
+                               File.Delete (temp_file_name);
+                       }
+               }
        }
 }