Add a few more tests and comments for GnWorkspace

Change-Id: I3d8cc76e166c1968049c35d383599f6bebd0d1bf
Reviewed-on: https://dart-review.googlesource.com/c/80521
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/generated/gn.dart b/pkg/analyzer/lib/src/generated/gn.dart
index fa17c4e..abbed00 100644
--- a/pkg/analyzer/lib/src/generated/gn.dart
+++ b/pkg/analyzer/lib/src/generated/gn.dart
@@ -157,7 +157,9 @@
   /**
    * Find the GN workspace that contains the given [path].
    *
-   * Return `null` if a workspace could not be found.
+   * Return `null` if a workspace could not be found. For a workspace to be
+   * found, both a `.jiri_root` file must be found, and at least one "packages"
+   * file must be found in [path]'s output directory.
    */
   static GnWorkspace find(ResourceProvider provider, String path) {
     Context context = provider.pathContext;
@@ -185,7 +187,7 @@
         return new GnWorkspace._(provider, root, packagesFiles);
       }
 
-      // Go up the folder.
+      // Go up one folder.
       folder = parent;
     }
   }
diff --git a/pkg/analyzer/test/generated/gn_test.dart b/pkg/analyzer/test/generated/gn_test.dart
index 36596ba..46ff4ea 100644
--- a/pkg/analyzer/test/generated/gn_test.dart
+++ b/pkg/analyzer/test/generated/gn_test.dart
@@ -22,6 +22,14 @@
     expect(workspace, isNull);
   }
 
+  void test_find_noPackagesFiles() {
+    newFolder('/workspace/.jiri_root');
+    newFolder('/workspace/some/code');
+    GnWorkspace workspace =
+        GnWorkspace.find(resourceProvider, convertPath('/workspace'));
+    expect(workspace, isNull);
+  }
+
   void test_find_notAbsolute() {
     expect(
         () => GnWorkspace.find(resourceProvider, convertPath('not_absolute')),
@@ -96,6 +104,23 @@
     expect(workspace.packageMap['flutter'][0].path, packageLocation);
   }
 
+  void test_packages_fallbackBuildDirWithUselessConfig() {
+    newFolder('/workspace/.jiri_root');
+    newFolder('/workspace/some/code');
+    newFile('/workspace/some/code/pubspec.yaml');
+    newFile('/workspace/.config', content: 'FOO=foo\n' + 'BAR=bar\n');
+    String packageLocation = convertPath('/workspace/this/is/the/package');
+    Uri packageUri = resourceProvider.pathContext.toUri(packageLocation);
+    newFile('/workspace/out/debug-x87_128/dartlang/gen/some/code/foo.packages',
+        content: 'flutter:$packageUri');
+    GnWorkspace workspace =
+        GnWorkspace.find(resourceProvider, convertPath('/workspace/some/code'));
+    expect(workspace, isNotNull);
+    expect(workspace.root, convertPath('/workspace'));
+    expect(workspace.packageMap.length, 1);
+    expect(workspace.packageMap['flutter'][0].path, packageLocation);
+  }
+
   void test_packages_multipleCandidates() {
     newFolder('/workspace/.jiri_root');
     newFolder('/workspace/some/code');