2 // ProjectCollectionTest.cs
5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using Microsoft.Build.Construction;
33 using Microsoft.Build.Evaluation;
34 using NUnit.Framework;
35 using Microsoft.Build.Execution;
37 namespace MonoTests.Microsoft.Build.Evaluation
40 public class ProjectCollectionTest
43 public void GlobalProperties ()
45 var g = ProjectCollection.GlobalProjectCollection;
46 Assert.AreEqual (0, g.GlobalProperties.Count, "#1");
48 Assert.IsTrue (g.GlobalProperties.IsReadOnly, "#2");
53 public void DefaultToolsVersion ()
55 var pc = ProjectCollection.GlobalProjectCollection;
56 Assert.AreEqual (pc.Toolsets.First ().ToolsVersion, pc.DefaultToolsVersion, "#1");
60 public void Toolsets ()
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");
71 public void BuildDoesNotIncreaseCollectionContent ()
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");
81 Assert.AreEqual (0, coll.Count, "#2");
85 public void GetLoadedProjectsWithoutFullPath ()
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 ();
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");
97 new Project (root, null, null, pc);
98 Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#2"); // huh?
102 public void GetLoadedProjectsSuccess ()
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 ();
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";
114 Assert.AreEqual (1, pc.GetLoadedProjects (path).Count, "#1"); // wow ok...
115 Assert.AreEqual (proj, pc.GetLoadedProjects (path).First (), "#2");
119 public void GetLoadedProjectsSuccess2 ()
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 ();
125 using (var fs = File.CreateText (path))
126 fs.Write (project_xml);
128 var proj = pc.LoadProject (path);
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");
138 public void GetLoadedProjectsForProjectInstance ()
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";
147 new ProjectInstance (root, null, null, pc);
148 Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#1"); // so, ProjectInstance does not actually load Project...