b3260b2b3a51bead453ee8213ce173371b4cb4f5
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Evaluation / ProjectTest.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Xml;
5 using Microsoft.Build.Construction;
6 using Microsoft.Build.Evaluation;
7 using NUnit.Framework;
8 using Microsoft.Build.Exceptions;
9 using Microsoft.Build.Logging;
10 using Microsoft.Build.Framework;
11
12 namespace MonoTests.Microsoft.Build.Evaluation
13 {
14         [TestFixture]
15         public class ProjectTest
16         {
17                 [Test]
18                 [Category ("NotWorking")]
19                 public void EscapeDoesWTF ()
20                 {
21                         string value = "What are 'ESCAPE' & \"EVALUATE\" ? $ # % ^";
22                         string escaped_by_collection = "What are %27ESCAPE%27 & \"EVALUATE\" %3f %24 # %25 ^";
23                         string xml = string.Format (@"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
24         <PropertyGroup>
25                 <Foo>{0}</Foo>
26                 <Baz>$(FOO)</Baz>
27         </PropertyGroup>
28 </Project>", value);
29                         var path = "file://localhost/foo.xml";
30                         var reader = XmlReader.Create (new StringReader (xml), null, path);
31                         var root = ProjectRootElement.Create (xml);
32                         var proj = new Project (root);
33                         var prop = proj.Properties.First (p => p.Name == "Foo");
34                         Assert.AreEqual (value, prop.UnevaluatedValue, "#1");
35                         Assert.AreEqual (value, prop.EvaluatedValue, "#2");
36                         // eh?
37                         Assert.AreEqual (value, Project.GetPropertyValueEscaped (prop), "#3");
38                         prop = proj.Properties.First (p => p.Name == "Baz");
39                         Assert.AreEqual ("$(FOO)", prop.UnevaluatedValue, "#4");
40                         Assert.AreEqual (value, prop.EvaluatedValue, "#5");
41                         // eh?
42                         Assert.AreEqual (value, Project.GetPropertyValueEscaped (prop), "#6");
43                         
44                         // OK you are fine.
45                         Assert.AreEqual (escaped_by_collection, ProjectCollection.Escape (value), "#7");
46                 }
47                 
48                 [Test]
49                 public void FullPath ()
50                 {
51                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
52                         var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
53                         var root = ProjectRootElement.Create (xml);
54                         var proj = new Project (root);
55                         proj.FullPath = "ABC";
56                         Assert.IsTrue (proj.FullPath.EndsWith (Path.DirectorySeparatorChar + "ABC"), "#1");
57                         Assert.AreEqual (root.FullPath, proj.FullPath, "#2");
58                 }
59                 
60                 [Test]
61                 public void BuildEmptyProject ()
62                 {
63                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
64                         var xml = XmlReader.Create (new StringReader (project_xml), null, "file://localhost/foo.xml");
65                         var root = ProjectRootElement.Create (xml);
66             
67                         // This seems to do nothing and still returns true
68                         Assert.IsTrue (new Project (root).Build (), "#1");
69                         // This seems to fail to find the appropriate target
70                         Assert.IsFalse (new Project (root).Build ("Build", null), "#2");
71                         // Thus, this tries to build all the targets (empty) and no one failed, so returns true(!)
72                         Assert.IsTrue (new Project (root).Build (new string [0], null), "#3");
73                         // Actially null "targets" is accepted and returns true(!!)
74                         Assert.IsTrue (new Project (root).Build ((string []) null, null), "#4");
75                         // matching seems to be blindly done, null string also results in true(!!)
76                         Assert.IsTrue (new Project (root).Build ((string) null, null), "#5");
77                 }
78                 
79                 [Test]
80                 [ExpectedException (typeof (InvalidProjectFileException))]
81                 [Category ("NotWorking")]
82                 public void LoadInvalidProjectForBadCondition ()
83                 {
84                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
85   <PropertyGroup>
86     <Foo>What are 'ESCAPE' &amp; ""EVALUATE"" ? $ # % ^</Foo>
87     <!-- Note that this contains invalid Condition expression, yet ProjectElement.Create() does NOT fail. -->
88     <Baz Condition=""$(Void)=="">$(FOO)</Baz>
89   </PropertyGroup>
90 </Project>";
91                         var path = "file://localhost/foo.xml";
92                         var reader = XmlReader.Create (new StringReader (xml), null, path);
93                         var root = ProjectRootElement.Create (reader);
94                         new Project (root);
95                 }
96                 
97                 [Test]
98                 [Category ("NotWorking")]
99                 public void ExpandString ()
100                 {
101                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
102   <PropertyGroup>
103     <Foo>What are 'ESCAPE' &amp; ""EVALUATE"" ? $ # % ^</Foo>
104     <Bar>y</Bar>
105     <Baz Condition=""$(Void)==''"">$(FOO)</Baz>
106   </PropertyGroup>
107 </Project>";
108                         var path = "file://localhost/foo.xml";
109                         var reader = XmlReader.Create (new StringReader (xml), null, path);
110                         var root = ProjectRootElement.Create (reader);
111                         var proj = new Project (root);
112                         Assert.AreEqual ("xyz", proj.ExpandString ("x$(BAR)z"), "#1");
113                         Assert.AreEqual ("x$(BARz", proj.ExpandString ("x$(BARz"), "#2"); // incomplete
114                         Assert.AreEqual ("xz", proj.ExpandString ("x@(BAR)z"), "#3"); // not an item
115                 }
116                 
117                 [Test]
118                 [Category ("NotWorking")]
119                 public void BuildCSharpTargetGetFrameworkPaths ()
120                 {
121             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
122   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
123 </Project>";
124             var path = "file://localhost/foo.xml";
125             var xml = XmlReader.Create (new StringReader (project_xml));
126             var root = ProjectRootElement.Create (xml);
127             var proj = new Project (root);
128                         Assert.IsTrue (proj.Build ("GetFrameworkPaths", new ILogger [] {new ConsoleLogger ()}));
129                 }
130                 
131                 [Test]
132                 [Category ("NotWorking")]
133                 public void BuildCSharpTargetBuild ()
134                 {
135             string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
136   <Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
137 </Project>";
138             var path = "file://localhost/foo.xml";
139             var xml = XmlReader.Create (new StringReader (project_xml));
140             var root = ProjectRootElement.Create (xml);
141             var proj = new Project (root);
142                         Assert.IsFalse (proj.Build ("Build", new ILogger [] {new ConsoleLogger ()})); // missing mandatory properties
143                 }
144         }
145 }
146