Merge pull request #24 from joeconwaystk/master

Fix global_packages enumeration
diff --git a/lib/pub_cache.dart b/lib/pub_cache.dart
index 26ab298..8dd77f6 100644
--- a/lib/pub_cache.dart
+++ b/lib/pub_cache.dart
@@ -96,8 +96,11 @@
 
     Directory globalPackagesDir = _getSubDir(location, 'global_packages');
     if (globalPackagesDir.existsSync()) {
-      _applications = globalPackagesDir.listSync().map(
-          (dir) => new Application._(this, dir)).toList();
+      _applications = globalPackagesDir
+          .listSync()
+          .where((item) => item is Directory)
+          .map((dir) => new Application._(this, dir))
+          .toList();
     }
 
     // Scan hosted packages - just pub.dartlang.org for now.
diff --git a/test/pub_cache_test.dart b/test/pub_cache_test.dart
index 1fb219e..d63a5c1 100644
--- a/test/pub_cache_test.dart
+++ b/test/pub_cache_test.dart
@@ -16,12 +16,39 @@
   final String cacheDirName = Platform.isWindows ? 'Cache' : 'pub-cache';
 
   group('PubCache', () {
+    tearDown(() {
+      Directory cacheDir = PubCache.getSystemCacheLocation();
+      var globalDir = new Directory(path.join(cacheDir.path, "global_packages"));
+      var file = new File(path.join(globalDir.path, "nonsense"));
+      if (file.existsSync()) {
+        file.deleteSync();
+      }
+    });
+
     test('getSystemCacheLocation', () {
       Directory cacheDir = PubCache.getSystemCacheLocation();
       expect(cacheDir, isNotNull);
       expect(path.basename(cacheDir.path), contains(cacheDirName));
     });
 
+    test('Create PubCache when non-directories are in global_packages', () {
+      // Get cache in its current state
+      var cache = new PubCache();
+      var currentGlobalApps = cache.getGlobalApplications();
+
+      // Put a file in global_packages
+      Directory cacheDir = PubCache.getSystemCacheLocation();
+      var globalDir = new Directory(path.join(cacheDir.path, "global_packages"));
+      var file = new File(path.join(globalDir.path, "nonsense"));
+      file.writeAsStringSync("pub_cache test suite");
+
+      // Ensure that this file is not reflected in cache
+      cache = new PubCache();
+      var newGlobalApps = cache.getGlobalApplications();
+
+      expect(currentGlobalApps.length, newGlobalApps.length);
+    });
+
     test('PubCache', () {
       PubCache cache = new PubCache();
       expect(cache, isNotNull);