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");
47 Assert.IsTrue (g.GlobalProperties.IsReadOnly, "#2");
51 public void DefaultToolsVersion ()
53 var pc = ProjectCollection.GlobalProjectCollection;
54 Assert.AreEqual (pc.Toolsets.First ().ToolsVersion, pc.DefaultToolsVersion, "#1");
58 public void Toolsets ()
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");
69 public void BuildDoesNotIncreaseCollectionContent ()
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");
79 Assert.AreEqual (0, coll.Count, "#2");
83 public void GetLoadedProjectsWithoutFullPath ()
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 ();
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");
95 new Project (root, null, null, pc);
96 Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#2"); // huh?
100 public void GetLoadedProjectsSuccess ()
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 ();
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";
112 Assert.AreEqual (1, pc.GetLoadedProjects (path).Count, "#1"); // wow ok...
113 Assert.AreEqual (proj, pc.GetLoadedProjects (path).First (), "#2");
117 public void GetLoadedProjectsSuccess2 ()
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 ();
123 using (var fs = File.CreateText (path))
124 fs.Write (project_xml);
126 var proj = pc.LoadProject (path);
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");
136 public void GetLoadedProjectsForProjectInstance ()
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";
145 new ProjectInstance (root, null, null, pc);
146 Assert.AreEqual (0, pc.GetLoadedProjects (path).Count, "#1"); // so, ProjectInstance does not actually load Project...