2 // ResolveAssemblyReferenceTest.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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.
29 using System.Collections;
31 using System.Reflection;
32 using Microsoft.Build.BuildEngine;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Tasks;
35 using Microsoft.Build.Utilities;
36 using NUnit.Framework;
38 namespace MonoTests.Microsoft.Build.Tasks {
41 public class ResolveAssemblyReferenceTest {
48 public void TestGac1 ()
50 var gacDir = GetGacDir ();
52 if (gacDir == null || !System.IO.Directory.Exists (gacDir))
53 Assert.Ignore ("GAC not found.");
55 engine = new Engine (Consts.BinPath);
56 project = engine.CreateNewProject ();
57 project.LoadXml (ResolveAssembly ("System", null));
59 Assert.IsTrue (project.Build ("A"), "A1");
60 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
61 Assert.AreEqual (1, big.Count, "A2");
62 Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3");
66 public void TestHintPath1 ()
68 engine = new Engine (Consts.BinPath);
69 project = engine.CreateNewProject ();
70 project.LoadXml (ResolveAssembly (null, @"Test\resources\test.dll"));
72 Assert.IsTrue (project.Build ("A"), "A1");
73 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
74 Assert.AreEqual (1, big.Count, "A2");
75 Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3");
78 [Test, Category("NotWorking")]
79 public void ResolveBinary_FancyStuff ()
81 engine = new Engine (Consts.BinPath);
82 project = engine.CreateNewProject ();
83 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll"));
85 Assert.IsTrue (project.Build ("A"), "A1");
86 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
87 Assert.AreEqual (1, big.Count, "A2");
88 Assert.IsTrue (big[0].Include.EndsWith ("FancyStuff.dll"), "A3");
90 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
91 Assert.AreEqual (1, big.Count, "A4");
92 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
95 [Test, Category("NotWorking")]
96 public void ResolveBinary_SimpleWrite ()
98 engine = new Engine (Consts.BinPath);
99 project = engine.CreateNewProject ();
100 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\SimpleWrite.dll"));
102 Assert.IsTrue (project.Build ("A"), "A1");
103 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
104 Assert.AreEqual (1, big.Count, "A2");
105 Assert.IsTrue (big[0].Include.EndsWith ("SimpleWrite.dll"), "A3");
107 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
108 Assert.AreEqual (0, big.Count, "A4");
111 [Test, Category("NotWorking")]
112 public void ResolveBinary_Testing ()
114 engine = new Engine (Consts.BinPath);
115 project = engine.CreateNewProject ();
116 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\Testing.dll"));
118 Assert.IsTrue (project.Build ("A"), "A1");
119 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
120 Assert.AreEqual (1, big.Count, "A2");
121 Assert.IsTrue (big[0].Include.EndsWith ("Testing.dll"), "A3");
123 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
124 Assert.AreEqual (0, big.Count, "A4");
127 [Test, Category("NotWorking")]
128 public void ResolveBinary_XbuildReferenceBugTest ()
130 engine = new Engine (Consts.BinPath);
131 project = engine.CreateNewProject ();
132 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\XbuildReferenceBugTest.exe"));
134 Assert.IsTrue (project.Build ("A"), "A1");
135 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
136 Assert.AreEqual (1, big.Count, "A2");
137 Assert.IsTrue (big[0].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
139 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
140 Assert.AreEqual (3, big.Count, "A4");
141 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
142 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("FancyStuff.dll")), "A6");
143 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A7");
146 [Test, Category("NotWorking")]
147 public void ResolveBinary_FancyStuffAndXbuild ()
149 engine = new Engine (Consts.BinPath);
150 project = engine.CreateNewProject ();
151 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll", @"Test\resources\binary\XbuildReferenceBugTest.exe"));
153 Assert.IsTrue (project.Build ("A"), "A1");
154 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
155 Assert.AreEqual (2, big.Count, "A2");
156 Assert.IsTrue (big[0].Include.EndsWith ("FancyStuff.dll"), "A3");
157 Assert.IsTrue (big[1].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
159 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
160 Assert.AreEqual (2, big.Count, "A4");
161 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
162 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A6");
165 [Test, Category("NotWorking")]
166 public void ResolveBinary_SameAssemblyTwice ()
168 engine = new Engine (Consts.BinPath);
169 project = engine.CreateNewProject ();
170 project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\XbuildReferenceBugTest.exe", @"Test\resources\binary\XbuildReferenceBugTest2.exe"));
172 Assert.IsTrue (project.Build ("A"), "A1");
173 big = project.GetEvaluatedItemsByName ("ResolvedFiles");
174 Assert.AreEqual (2, big.Count, "A2");
175 Assert.IsTrue (big[0].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
176 Assert.IsTrue (big[1].Include.EndsWith ("XbuildReferenceBugTest2.exe"), "A3b");
178 big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
179 Assert.AreEqual (3, big.Count, "A4");
180 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
181 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("FancyStuff.dll")), "A6");
182 Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A7");
185 string ResolveAssembly (string gacAssembly, params string[] hintPaths)
187 string reference = "";
188 if (string.IsNullOrEmpty (gacAssembly)) {
189 foreach (var hintPath in hintPaths) {
190 reference += string.Format (
191 @"<Reference Include='{0}'>
192 <HintPath>{1}</HintPath>
193 </Reference>", System.IO.Path.GetFileNameWithoutExtension (hintPath), hintPath);
196 reference += string.Format (
197 @"<Reference Include='{0}'/>", gacAssembly);
201 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
207 {CandidateAssemblyFiles};
210 {TargetFrameworkDirectory};
218 <ResolveAssemblyReference
219 Assemblies='@(Reference)'
220 SearchPaths='$(SearchPaths)'
222 <Output TaskParameter='ResolvedFiles' ItemName='ResolvedFiles'/>
223 <Output TaskParameter='ResolvedDependencyFiles' ItemName='ResolvedDependencyFiles'/>
224 </ResolveAssemblyReference>
232 // copied from mcs/tools/gacutil/driver.cs
233 PropertyInfo gac = typeof (System.Environment).GetProperty ("GacPath", BindingFlags.Static|BindingFlags.NonPublic);
238 MethodInfo get_gac = gac.GetGetMethod (true);
239 return (string) get_gac.Invoke (null, null);