Merge pull request #498 from Unroll-Me/master
[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 Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
36
37 namespace MonoTests.Microsoft.Build.Tasks {
38         
39         [TestFixture]
40         public class ResolveAssemblyReferenceTest {
41                 
42                 Engine engine;
43                 Project project;
44                 BuildItemGroup big;
45
46                 [Test]
47                 public void TestGac1 ()
48                 {
49                         engine = new Engine (Consts.BinPath);
50                         project = engine.CreateNewProject ();
51                         project.LoadXml (ResolveAssembly ("System", null));
52                         
53                         Assert.IsTrue (project.Build ("A"), "A1");
54                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
55                         Assert.AreEqual (1, big.Count, "A2");
56                         Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3");
57                 }
58                 
59                 [Test]
60                 public void TestHintPath1 ()
61                 {
62                         engine = new Engine (Consts.BinPath);
63                         project = engine.CreateNewProject ();
64                         project.LoadXml (ResolveAssembly (null, @"Test\resources\test.dll"));
65                         
66                         Assert.IsTrue (project.Build ("A"), "A1");
67                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
68                         Assert.AreEqual (1, big.Count, "A2");
69                         Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3");
70                 }
71                 
72                 [Test, Category("NotWorking")]
73                 public void ResolveBinary_FancyStuff ()
74                 {
75                         engine = new Engine (Consts.BinPath);
76                         project = engine.CreateNewProject ();
77                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll"));
78                         
79                         Assert.IsTrue (project.Build ("A"), "A1");
80                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
81                         Assert.AreEqual (1, big.Count, "A2");
82                         Assert.IsTrue (big[0].Include.EndsWith ("FancyStuff.dll"), "A3");
83                         
84                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
85                         Assert.AreEqual (1, big.Count, "A4");
86                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
87                 }
88                 
89                 [Test, Category("NotWorking")]
90                 public void ResolveBinary_SimpleWrite ()
91                 {
92                         engine = new Engine (Consts.BinPath);
93                         project = engine.CreateNewProject ();
94                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\SimpleWrite.dll"));
95                         
96                         Assert.IsTrue (project.Build ("A"), "A1");
97                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
98                         Assert.AreEqual (1, big.Count, "A2");
99                         Assert.IsTrue (big[0].Include.EndsWith ("SimpleWrite.dll"), "A3");
100                         
101                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
102                         Assert.AreEqual (0, big.Count, "A4");
103                 }
104                 
105                 [Test, Category("NotWorking")]
106                 public void ResolveBinary_Testing ()
107                 {
108                         engine = new Engine (Consts.BinPath);
109                         project = engine.CreateNewProject ();
110                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\Testing.dll"));
111                         
112                         Assert.IsTrue (project.Build ("A"), "A1");
113                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
114                         Assert.AreEqual (1, big.Count, "A2");
115                         Assert.IsTrue (big[0].Include.EndsWith ("Testing.dll"), "A3");
116                         
117                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
118                         Assert.AreEqual (0, big.Count, "A4");
119                 }
120                 
121                 [Test, Category("NotWorking")]
122                 public void ResolveBinary_XbuildReferenceBugTest ()
123                 {
124                         engine = new Engine (Consts.BinPath);
125                         project = engine.CreateNewProject ();
126                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\XbuildReferenceBugTest.exe"));
127                         
128                         Assert.IsTrue (project.Build ("A"), "A1");
129                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
130                         Assert.AreEqual (1, big.Count, "A2");
131                         Assert.IsTrue (big[0].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
132                         
133                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
134                         Assert.AreEqual (3, big.Count, "A4");
135                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
136                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("FancyStuff.dll")), "A6");
137                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A7");
138                 }
139
140                 [Test, Category("NotWorking")]
141                 public void ResolveBinary_FancyStuffAndXbuild ()
142                 {
143                         engine = new Engine (Consts.BinPath);
144                         project = engine.CreateNewProject ();
145                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll", @"Test\resources\binary\XbuildReferenceBugTest.exe"));
146                         
147                         Assert.IsTrue (project.Build ("A"), "A1");
148                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
149                         Assert.AreEqual (2, big.Count, "A2");
150                         Assert.IsTrue (big[0].Include.EndsWith ("FancyStuff.dll"), "A3");
151                         Assert.IsTrue (big[1].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
152                         
153                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
154                         Assert.AreEqual (2, big.Count, "A4");
155                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
156                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A6");
157                 }
158
159                 [Test, Category("NotWorking")]
160                 public void ResolveBinary_SameAssemblyTwice ()
161                 {
162                         engine = new Engine (Consts.BinPath);
163                         project = engine.CreateNewProject ();
164                         project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\XbuildReferenceBugTest.exe", @"Test\resources\binary\XbuildReferenceBugTest2.exe"));
165
166                         Assert.IsTrue (project.Build ("A"), "A1");
167                         big = project.GetEvaluatedItemsByName ("ResolvedFiles");
168                         Assert.AreEqual (2, big.Count, "A2");
169                         Assert.IsTrue (big[0].Include.EndsWith ("XbuildReferenceBugTest.exe"), "A3");
170                         Assert.IsTrue (big[1].Include.EndsWith ("XbuildReferenceBugTest2.exe"), "A3b");
171                         
172                         big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles");
173                         Assert.AreEqual (3, big.Count, "A4");
174                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5");
175                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("FancyStuff.dll")), "A6");
176                         Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("Testing.dll")), "A7");
177                 }
178
179                 string ResolveAssembly (string gacAssembly, params string[] hintPaths)
180                 {
181                         string reference = "";
182                         if (string.IsNullOrEmpty (gacAssembly)) {
183                                 foreach (var hintPath in hintPaths) {
184                                         reference += string.Format (
185                                                 @"<Reference Include='{0}'>
186                                                                 <HintPath>{1}</HintPath>
187                                                         </Reference>", System.IO.Path.GetFileNameWithoutExtension (hintPath), hintPath);
188                                 }
189                         } else {
190                                 reference += string.Format (
191                                         @"<Reference Include='{0}'/>", gacAssembly);
192                         }
193                         
194                         return @"
195                   <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
196                                         <ItemGroup>
197                                         " + reference + @"      
198                                         </ItemGroup>
199                                         <PropertyGroup>
200                                                 <SearchPaths>
201                                                         {CandidateAssemblyFiles};
202                                                         $(ReferencePath);
203                                                         {HintPathFromItem};
204                                                         {TargetFrameworkDirectory};
205                                                         {AssemblyFolders};
206                                                         {GAC};
207                                                         {RawFileName};
208                                                         $(OutputPath)
209                                                 </SearchPaths>
210                                         </PropertyGroup>
211                                         <Target Name='A'>
212                                                 <ResolveAssemblyReference
213                                                         Assemblies='@(Reference)'
214                                                         SearchPaths='$(SearchPaths)'
215                                                 >
216                                                         <Output TaskParameter='ResolvedFiles' ItemName='ResolvedFiles'/>
217                                                         <Output TaskParameter='ResolvedDependencyFiles' ItemName='ResolvedDependencyFiles'/>
218                                                 </ResolveAssemblyReference>
219                                         </Target>
220                                 </Project>
221                         ";
222                 }
223         }
224