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