Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / Mono.XBuild.Tasks / Test / Mono.XBuild.Tasks / PcFileCacheTest.cs
1 //
2 // PcFileCacheTest.cs
3 //
4 // Author:
5 //       Antonius Riha <antoniusriha@gmail.com>
6 //
7 // Copyright (c) 2013 Antonius Riha
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 using System;
27 using System.Collections.Generic;
28 using System.IO;
29 using Mono.PkgConfig;
30 using NUnit.Framework;
31
32 namespace MonoTests.Mono.PkgConfig
33 {
34         [TestFixture]
35         public class PcFileCacheTest
36         {
37                 static readonly string cacheDir = "testcache";
38                 static readonly string pcCacheFileName = "pkgconfig-cache-2.xml";
39                 static readonly string pcCacheFilePath = Path.Combine (cacheDir, pcCacheFileName);
40                 static readonly string pkgConfigDir = "testpkgconfig";
41
42                 [SetUp]
43                 public void Setup ()
44                 {
45                         Directory.CreateDirectory (cacheDir);
46                         Directory.CreateDirectory (pkgConfigDir);
47                 }
48
49                 [TearDown]
50                 public void Teardown ()
51                 {
52                         if (Directory.Exists (cacheDir))
53                                 Directory.Delete (cacheDir, true);
54                         if (Directory.Exists (pkgConfigDir))
55                                 Directory.Delete (pkgConfigDir, true);
56                 }
57
58                 [Test]
59                 public void CreatePcFileCache ()
60                 {
61                         PcFileCacheStub.Create (cacheDir);
62
63                         // cache dir should exist
64                         Assert.IsTrue (Directory.Exists (cacheDir), "A1");
65
66                         // cache file should not exist
67                         Assert.IsFalse (File.Exists (pcCacheFilePath), "A2");
68                 }
69
70                 [Test]
71                 public void CreatePcFileCacheWithExistingEmptyCacheFile ()
72                 {
73                         // Create pc cache file
74                         WritePcCacheFileContent ("");
75                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
76
77                         // cache should be empty
78                         string[] pkgConfigDirs = { pkgConfigDir };
79                         CollectionAssert.IsEmpty (cache.GetPackages (pkgConfigDirs), "A1");
80                 }
81
82                 [Test]
83                 public void CreatePcFileCacheWithCacheFileContaining1EntryForAnExistingPcFile ()
84                 {
85                         // Create pc cache file with an entry and corresponding pc file
86                         string pkgConfigFileName = "gtk-sharp-2.0.pc";
87                         string pkgConfigFullFilePath = Path.GetFullPath (Path.Combine (pkgConfigDir, pkgConfigFileName));
88                         string pcCacheFileContent = @"<PcFileCache>
89   <File path=""" + pkgConfigFullFilePath + @""" lastWriteTime=""2013-11-23T21:18:31+01:00"" />
90 </PcFileCache>
91 ";
92
93                         string pkgConfigFileContent = @"prefix=${pcfiledir}/../..
94 exec_prefix=${prefix}
95 libdir=${exec_prefix}/lib
96 gapidir=${prefix}/share/gapi-2.0
97
98
99 Name: Gtk#
100 Description: Gtk# - GNOME .NET Binding
101 Version: 2.12.10
102 Cflags: -I:${gapidir}/pango-api.xml -I:${gapidir}/atk-api.xml -I:${gapidir}/gdk-api.xml -I:${gapidir}/gtk-api.xml
103 Libs: -r:${libdir}/cli/pango-sharp-2.0/pango-sharp.dll -r:${libdir}/cli/atk-sharp-2.0/atk-sharp.dll -r:${libdir}/cli/gdk-sharp-2.0/gdk-sharp.dll -r:${libdir}/cli/gtk-sharp-2.0/gtk-sharp.dll
104 Requires: glib-sharp-2.0
105 ";
106
107                         AddPkgConfigFile (pkgConfigFileName, pkgConfigFileContent);
108                         WritePcCacheFileContent (pcCacheFileContent);
109
110                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
111
112                         // cache should contain entry of pc file
113                         Assert.IsNotNull (cache.GetPackageInfo (pkgConfigFullFilePath), "A1");
114                 }
115
116                 [Test]
117                 public void CreatePcFileCacheWithCacheFileContainingOneOrphanedEntry ()
118                 {
119                         string pkgConfigFileName = "gtk-sharp-2.0.pc";
120                         string pkgConfigFullFilePath = Path.GetFullPath (Path.Combine (pkgConfigDir, pkgConfigFileName));
121                         string pcCacheFileContent = @"<PcFileCache>
122   <File path=""" + pkgConfigFullFilePath + @""" lastWriteTime=""2013-11-23T21:18:31+01:00"" />
123 </PcFileCache>
124 ";
125                         WritePcCacheFileContent (pcCacheFileContent);
126
127                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
128
129                         // cache should contain orphaned entry
130                         Assert.IsNotNull (cache.GetPackageInfo (pkgConfigFullFilePath), "A1");
131                 }
132
133                 [Test]
134                 public void CreatePcFileCacheWithoutCacheFileButWithPcFile ()
135                 {
136                         string pkgConfigFileName = "gtk-sharp-2.0.pc";
137                         string pkgConfigFileContent = @"prefix=${pcfiledir}/../..
138 exec_prefix=${prefix}
139 libdir=${exec_prefix}/lib
140 gapidir=${prefix}/share/gapi-2.0
141
142
143 Name: Gtk#
144 Description: Gtk# - GNOME .NET Binding
145 Version: 2.12.10
146 Cflags: -I:${gapidir}/pango-api.xml -I:${gapidir}/atk-api.xml -I:${gapidir}/gdk-api.xml -I:${gapidir}/gtk-api.xml
147 Libs: -r:${libdir}/cli/pango-sharp-2.0/pango-sharp.dll -r:${libdir}/cli/atk-sharp-2.0/atk-sharp.dll -r:${libdir}/cli/gdk-sharp-2.0/gdk-sharp.dll -r:${libdir}/cli/gtk-sharp-2.0/gtk-sharp.dll
148 Requires: glib-sharp-2.0
149 ";
150                         AddPkgConfigFile (pkgConfigFileName, pkgConfigFileContent);
151
152                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
153
154                         // cache file should exist
155                         Assert.IsFalse (File.Exists (pcCacheFilePath), "A1");
156
157                         // cache should be empty
158                         string[] pkgConfigDirs = { pkgConfigDir };
159                         CollectionAssert.IsEmpty (cache.GetPackages (pkgConfigDirs), "A2");
160                 }
161
162                 [Test]
163                 public void GetPackagesOrderedByFolder ()
164                 {
165                         string pkgConfigDir1 = "testpkgconfigdir1";
166                         string pkgConfigDir2 = "testpkgconfigdir2";
167                         Directory.CreateDirectory (pkgConfigDir1);
168                         Directory.CreateDirectory (pkgConfigDir2);
169
170                         string pkgConfigFile11NameAttr = "gtk-sharp-2.0";
171                         string pkgConfigFile11FullPath = Path.GetFullPath (Path.Combine (pkgConfigDir1, "gtk-sharp-2.0.pc"));
172
173                         string pkgConfigFile21NameAttr = "art-sharp-2.0";
174                         string pkgConfigFile21FullPath = Path.GetFullPath (Path.Combine (pkgConfigDir2, "art-sharp-2.0.pc"));
175
176                         string pkgConfigFile12NameAttr = "cecil";
177                         string pkgConfigFile12FullPath = Path.GetFullPath (Path.Combine (pkgConfigDir1, "cecil.pc"));
178
179                         string pcCacheFileContent = @"<PcFileCache>
180   <File path=""" + pkgConfigFile11FullPath + @""" lastWriteTime=""2013-11-23T21:18:31+01:00"" name=""" + pkgConfigFile11NameAttr + @""" />
181   <File path=""" + pkgConfigFile21FullPath + @""" lastWriteTime=""2011-07-12T12:04:53+02:00"" name=""" + pkgConfigFile21NameAttr + @""" />
182   <File path=""" + pkgConfigFile12FullPath + @""" lastWriteTime=""2012-07-24T22:28:30+02:00"" name=""" + pkgConfigFile12NameAttr + @""" />
183 </PcFileCache>
184 ";
185
186                         WritePcCacheFileContent (pcCacheFileContent);
187
188                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
189                         string[] pkgConfigDirs = { pkgConfigDir1, pkgConfigDir2 };
190                         IEnumerable<PackageInfo> packages = cache.GetPackages (pkgConfigDirs);
191
192                         PackageInfo[] packageArray = new PackageInfo [3];
193                         int i = 0;
194                         foreach (PackageInfo package in packages)
195                                 packageArray [i++] = package;
196
197                         Assert.AreEqual (pkgConfigFile11NameAttr, packageArray [0].Name, "A1");
198                         Assert.AreEqual (pkgConfigFile12NameAttr, packageArray [1].Name, "A2");
199                         Assert.AreEqual (pkgConfigFile21NameAttr, packageArray [2].Name, "A3");
200
201                         Directory.Delete (pkgConfigDir1, true);
202                         Directory.Delete (pkgConfigDir2, true);
203                 }
204
205                 [Test]
206                 public void UpdatePcFileCacheWithOrphanedEntry ()
207                 {
208                         string pkgConfigFileNameAttr = "gtk-sharp-2.0";
209                         string pkgConfigFileName = "gtk-sharp-2.0.pc";
210                         string pkgConfigFullFilePath = Path.GetFullPath (Path.Combine (pkgConfigDir, pkgConfigFileName));
211                         string pcCacheFileContent = @"<PcFileCache>
212   <File path=""" + pkgConfigFullFilePath + @""" lastWriteTime=""2013-11-23T21:18:31+01:00"" name=""" + pkgConfigFileNameAttr + @""" />
213 </PcFileCache>
214 ";
215
216                         WritePcCacheFileContent (pcCacheFileContent);
217
218                         PcFileCache cache = PcFileCacheStub.Create (cacheDir);
219
220                         // precondition
221                         string[] pkgConfigDirs = { pkgConfigDir };
222                         Assert.IsNotNull (cache.GetPackageInfoByName (pkgConfigFileNameAttr, pkgConfigDirs), "A1");
223
224                         cache.Update (pkgConfigDirs);
225                         Assert.IsNull (cache.GetPackageInfoByName (pkgConfigFileNameAttr, pkgConfigDirs), "A2");
226                 }
227
228                 static void WritePcCacheFileContent (string content)
229                 {
230                         File.WriteAllText (pcCacheFilePath, content);
231                 }
232
233                 static void AddPkgConfigFile (string fileName, string content)
234                 {
235                         AddPkgConfigFile (fileName, content, pkgConfigDir);
236                 }
237
238                 static void AddPkgConfigFile (string fileName, string content, string pkgConfigDir)
239                 {
240                         string path = Path.Combine (pkgConfigDir, fileName);
241                         File.WriteAllText (path, content);
242                 }
243
244                 class PcFileCacheContextStub : IPcFileCacheContext
245                 {
246                         public void StoreCustomData (PcFile pcfile, PackageInfo pkg)
247                         {
248                         }
249
250                         public bool IsCustomDataComplete (string pcfile, PackageInfo pkg)
251                         {
252                                 return false;
253                         }
254
255                         public void ReportError (string message, Exception ex)
256                         {
257                         }
258                 }
259
260                 class PcFileCacheStub : PcFileCache
261                 {
262                         static string initCacheDirectory;
263                         readonly string cacheDirectory;
264
265                         PcFileCacheStub (string cacheDirectory) : base (new PcFileCacheContextStub ())
266                         {
267                                 if (cacheDirectory == null)
268                                         throw new ArgumentNullException ("cacheDirectory");
269                                 this.cacheDirectory = cacheDirectory;
270                         }
271
272                         protected override string CacheDirectory {
273                                 get { return initCacheDirectory == null ? cacheDirectory : initCacheDirectory; }
274                         }
275
276                         public static PcFileCache Create (string cacheDirectory)
277                         {
278                                 initCacheDirectory = cacheDirectory;
279                                 PcFileCache cache = new PcFileCacheStub (cacheDirectory);
280                                 initCacheDirectory = null;
281                                 return cache;
282                         }
283                 }
284         }
285 }