Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Evaluation / ProjectCollectionTest.cs
1 //
2 // ProjectCollectionTest.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.Execution;
36
37 namespace MonoTests.Microsoft.Build.Evaluation
38 {
39         [TestFixture]
40         public class ProjectCollectionTest
41         {
42                 [Test]
43                 public void GlobalProperties ()
44                 {
45                         var g = ProjectCollection.GlobalProjectCollection;
46                         Assert.AreEqual (0, g.GlobalProperties.Count, "#1");
47                         Assert.IsTrue (g.GlobalProperties.IsReadOnly, "#2");
48                 }
49                 
50                 [Test]
51                 public void DefaultToolsVersion ()
52                 {
53                         var pc = ProjectCollection.GlobalProjectCollection;
54                         Assert.AreEqual (pc.Toolsets.First ().ToolsVersion, pc.DefaultToolsVersion, "#1");
55                 }
56                 
57                 [Test]
58                 public void Toolsets ()
59                 {
60                         var pc = ProjectCollection.GlobalProjectCollection;
61                         Assert.IsNotNull (pc.Toolsets, "#1-1");
62                         Assert.IsTrue (pc.Toolsets.Any (), "#1-2");
63                         pc = new ProjectCollection ();
64                         Assert.IsNotNull (pc.Toolsets, "#2-1");
65                         Assert.IsTrue (pc.Toolsets.Any (), "#2-2");
66                 }
67                 
68                 [Test]
69                 public void BuildDoesNotIncreaseCollectionContent ()
70                 {
71                         string empty_project_xml = "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
72                         var xml = XmlReader.Create (new StringReader (empty_project_xml));
73                         var root = ProjectRootElement.Create (xml);
74                         var coll = new ProjectCollection ();
75                         var inst = new ProjectInstance (root, null, null, coll);
76                         root.FullPath = "ProjectCollectionTest.BuildDoesNotIncreaseCollectionContent.proj";
77                         Assert.AreEqual (0, coll.Count, "#1");
78                         inst.Build ();
79                         Assert.AreEqual (0, coll.Count, "#2");
80                 }
81                 
82                 [Test]
83                 public void GetLoadedProjectsWithoutFullPath ()
84                 {
85                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
86                         var xml = XmlReader.Create (new StringReader (project_xml));
87                         var root = ProjectRootElement.Create (xml);
88                         string path = Path.GetFullPath ("foo.xml");
89                         var pc = new ProjectCollection ();
90                         
91                         pc.LoadProject (XmlReader.Create (new StringReader (project_xml), null, path));
92                         Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#1"); // huh?
93                         Assert.AreEqual (0, pc.LoadedProjects.Count, "#1.1");
94                         
95                         new Project (root, null, null, pc);
96                         Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#2"); // huh?
97                 }
98                         
99                 [Test]
100                 public void GetLoadedProjectsSuccess ()
101                 {
102                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
103                         var xml = XmlReader.Create (new StringReader (project_xml));
104                         var root = ProjectRootElement.Create (xml);
105                         string path = Path.GetFullPath ("foo.xml");
106                         var pc = new ProjectCollection ();
107                         
108                         var proj = new Project (root, null, null, pc);
109                         // this order also matters for test; It sets FullPath after Project.ctor(), and should still work.
110                         root.FullPath = "foo.xml";
111                         
112                         Assert.AreEqual (1, pc.GetLoadedProjects (path).Count, "#1"); // wow ok...
113                         Assert.AreEqual (proj, pc.GetLoadedProjects (path).First (), "#2");
114                 }
115                         
116                 [Test]
117                 public void GetLoadedProjectsSuccess2 ()
118                 {
119                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
120                         string path = Path.GetFullPath ("GetLoadedProjectsSuccess2.xml");
121                         var pc = new ProjectCollection ();
122                         
123                         using (var fs = File.CreateText (path))
124                                 fs.Write (project_xml);
125                         try {
126                                 var proj = pc.LoadProject (path);
127                                 
128                                 Assert.AreEqual (1, pc.GetLoadedProjects (path).Count, "#1"); // ok... LoadProject (with filename) adds it to the collection.
129                                 Assert.AreEqual (proj, pc.GetLoadedProjects (path).First (), "#2");
130                         } finally {
131                                 File.Delete (path);
132                         }
133                 }
134                         
135                 [Test]
136                 public void GetLoadedProjectsForProjectInstance ()
137                 {
138                         string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
139                         var xml = XmlReader.Create (new StringReader (project_xml));
140                         var root = ProjectRootElement.Create (xml);
141                         string path = Path.GetFullPath ("foo.xml");
142                         var pc = new ProjectCollection ();
143                         root.FullPath = "foo.xml";
144                         
145                         new ProjectInstance (root, null, null, pc);                     
146                         Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#1"); // so, ProjectInstance does not actually load Project...
147                 }
148         }
149 }
150