[MSBuild] Fixed a test that failed when the GAC wasn't available
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / ResolveAssemblyReferenceTest.cs
1 //
2 // ResolveAssemblyReferenceTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
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.Collections;
30 using System.Linq;
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;
37
38 namespace MonoTests.Microsoft.Build.Tasks {
39         
40         [TestFixture]
41         public class ResolveAssemblyReferenceTest {
42                 
43                 Engine engine;
44                 Project project;
45                 BuildItemGroup big;
46
47                 [Test]
48                 public void TestGac1 ()
49                 {
50                         var gacDir = GetGacDir ();
51
52                         if (gacDir == null || !System.IO.Directory.Exists (gacDir))
53                                 Assert.Ignore ("GAC not found.");
54
55                         engine = new Engine (Consts.BinPath);
56                         project = engine.CreateNewProject ();
57                         project.LoadXml (ResolveAssembly ("System", null));
58                         
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");
63                 }
64                 
65                 [Test]
66                 public void TestHintPath1 ()
67                 {
68                         engine = new Engine (Consts.BinPath);
69                         project = engine.CreateNewProject ();
70                         project.LoadXml (ResolveAssembly (null, @"Test\resources\test.dll"));
71                         
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");
76                 }
77                 
78                 [Test, Category("NotWorking")]
79                 public void ResolveBinary_FancyStuff ()
80                 {
81                         engine = new Engine (Consts.BinPath);
82                         project = engine.CreateNewProject ();
83                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll"));
84                         
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");
89                         
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");
93                 }
94                 
95                 [Test, Category("NotWorking")]
96                 public void ResolveBinary_SimpleWrite ()
97                 {
98                         engine = new Engine (Consts.BinPath);
99                         project = engine.CreateNewProject ();
100                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\SimpleWrite.dll"));
101                         
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");
106                         
107                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
108                         Assert.AreEqual (0, big.Count, "A4");
109                 }
110                 
111                 [Test, Category("NotWorking")]
112                 public void ResolveBinary_Testing ()
113                 {
114                         engine = new Engine (Consts.BinPath);
115                         project = engine.CreateNewProject ();
116                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\Testing.dll"));
117                         
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");
122                         
123                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
124                         Assert.AreEqual (0, big.Count, "A4");
125                 }
126                 
127                 [Test, Category("NotWorking")]
128                 public void ResolveBinary_XbuildReferenceBugTest ()
129                 {
130                         engine = new Engine (Consts.BinPath);
131                         project = engine.CreateNewProject ();
132                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\XbuildReferenceBugTest.exe"));
133                         
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");
138                         
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");
144                 }
145
146                 [Test, Category("NotWorking")]
147                 public void ResolveBinary_FancyStuffAndXbuild ()
148                 {
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"));
152                         
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");
158                         
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");
163                 }
164
165                 [Test, Category("NotWorking")]
166                 public void ResolveBinary_SameAssemblyTwice ()
167                 {
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"));
171
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");
177                         
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");
183                 }
184
185                 string ResolveAssembly (string gacAssembly, params string[] hintPaths)
186                 {
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);
194                                 }
195                         } else {
196                                 reference += string.Format (
197                                         @"<Reference Include='{0}'/>", gacAssembly);
198                         }
199                         
200                         return @"
201                   <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
202                                         <ItemGroup>
203                                         " + reference + @"      
204                                         </ItemGroup>
205                                         <PropertyGroup>
206                                                 <SearchPaths>
207                                                         {CandidateAssemblyFiles};
208                                                         $(ReferencePath);
209                                                         {HintPathFromItem};
210                                                         {TargetFrameworkDirectory};
211                                                         {AssemblyFolders};
212                                                         {GAC};
213                                                         {RawFileName};
214                                                         $(OutputPath)
215                                                 </SearchPaths>
216                                         </PropertyGroup>
217                                         <Target Name='A'>
218                                                 <ResolveAssemblyReference
219                                                         Assemblies='@(Reference)'
220                                                         SearchPaths='$(SearchPaths)'
221                                                 >
222                                                         <Output TaskParameter='ResolvedFiles' ItemName='ResolvedFiles'/>
223                                                         <Output TaskParameter='ResolvedDependencyFiles' ItemName='ResolvedDependencyFiles'/>
224                                                 </ResolveAssemblyReference>
225                                         </Target>
226                                 </Project>
227                         ";
228                 }
229
230                 string GetGacDir ()
231                 {
232                         // copied from mcs/tools/gacutil/driver.cs
233                         PropertyInfo gac = typeof (System.Environment).GetProperty ("GacPath", BindingFlags.Static|BindingFlags.NonPublic);
234
235                         if (gac == null)
236                                 return null;
237
238                         MethodInfo get_gac = gac.GetGetMethod (true);
239                         return (string) get_gac.Invoke (null, null);
240                 }
241         }
242