Simplify test detection (#2682)

* Simplify test detection

* Fix 7zip behavior
diff --git a/lib/src/command_runner.dart b/lib/src/command_runner.dart
index d5e98a0..68d8da6 100644
--- a/lib/src/command_runner.dart
+++ b/lib/src/command_runner.dart
@@ -235,6 +235,7 @@
     var depsRev = match[1];
 
     String actualRev;
+    final pubRoot = p.dirname(p.dirname(p.fromUri(Platform.script)));
     try {
       actualRev =
           git.runSync(['rev-parse', 'HEAD'], workingDir: pubRoot).single;
diff --git a/lib/src/io.dart b/lib/src/io.dart
index 0303e11..18da24a 100644
--- a/lib/src/io.dart
+++ b/lib/src/io.dart
@@ -461,31 +461,12 @@
   createSymlink(target, symlink, relative: relative);
 }
 
-/// Whether the current process is one of pub's test files.
-///
-/// This works because an actual pub executable that imports this will always
-/// start with "pub".
-final bool runningAsTest =
-    !path.url.basename(Platform.script.path).startsWith('pub.');
-
-// TODO(nweiz): Use the test API when test#48 is fixed.
-/// Whether the current process is one of pub's test files being run through the
-/// test package's test runner.
-///
-/// The test runner starts all tests from a `data:` URI.
-final bool _runningAsTestRunner = Platform.script.scheme == 'data';
-
 /// Whether the current process is a pub subprocess being run from a test.
 ///
 /// The "_PUB_TESTING" variable is automatically set for all the test code's
 /// invocations of pub.
 final bool runningFromTest = Platform.environment.containsKey('_PUB_TESTING');
 
-/// Whether pub is running from within the Dart SDK, as opposed to from the Dart
-/// source repository.
-final bool _runningFromSdk =
-    !runningFromTest && Platform.script.path.endsWith('.snapshot');
-
 /// A regular expression to match the script path of a pub script running from
 /// source in the Dart repo.
 final _dartRepoRegExp = RegExp(r'/third_party/pkg/pub/('
@@ -498,52 +479,7 @@
 ///
 /// This can happen when running tests against the repo, as well as when
 /// building Observatory.
-final bool runningFromDartRepo = (() {
-  if (_runningAsTestRunner) {
-    // When running from the test runner, we can't find our location via
-    // Platform.script since the runner munges that. However, it guarantees that
-    // the working directory is <repo>/third_party/pkg/pub.
-    return path.current.contains(RegExp(r'[/\\]third_party[/\\]pkg[/\\]pub$'));
-  } else {
-    return Platform.script.path.contains(_dartRepoRegExp);
-  }
-})();
-
-/// Resolves [target] relative to the Dart SDK's `asset` directory.
-///
-/// Throws a [StateError] if called from within the Dart repo.
-String _sdkAssetPath(String target) {
-  if (runningFromDartRepo) {
-    throw StateError("Can't get SDK assets from within the Dart repo.");
-  }
-
-  return path.join(
-      sdk.rootDirectory, 'lib', '_internal', 'pub', 'asset', target);
-}
-
-/// The path to the root of pub's sources in the pub repo.
-///
-/// This throws a [StateError] if it's called when running pub from the SDK.
-final String pubRoot = (() {
-  if (_runningFromSdk) {
-    throw StateError("Can't get pub's root from the SDK.");
-  }
-
-  // The test runner always runs from the working directory.
-  if (_runningAsTestRunner) return path.current;
-
-  var script = path.fromUri(Platform.script);
-  if (runningAsTest) {
-    // Running from "test/../some_test.dart".
-    var components = path.split(script);
-    var testIndex = components.indexOf('test');
-    if (testIndex == -1) throw StateError("Can't find pub's root.");
-    return path.joinAll(components.take(testIndex));
-  }
-
-  // Pub is run from "bin/pub.dart".
-  return path.dirname(path.dirname(script));
-})();
+final bool runningFromDartRepo = Platform.script.path.contains(_dartRepoRegExp);
 
 /// The path to the root of the Dart repo.
 ///
@@ -554,12 +490,6 @@
     throw StateError('Not running from source in the Dart repo.');
   }
 
-  if (_runningAsTestRunner) {
-    // When running in test code started by the test runner, the working
-    // directory will always be <repo>/third_party/pkg/pub.
-    return path.dirname(path.dirname(path.dirname(path.current)));
-  }
-
   // Get the URL of the repo root in a way that works when either both running
   // as a test or as a pub executable.
   var url = Platform.script
@@ -899,8 +829,19 @@
 }
 
 final String _pathTo7zip = (() {
-  if (!runningFromDartRepo) return _sdkAssetPath(path.join('7zip', '7za.exe'));
-  return path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe');
+  final candidate = runningFromDartRepo
+      ? path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe')
+      : path.join(
+          sdk.rootDirectory,
+          'lib',
+          '_internal',
+          'pub',
+          'asset',
+          '7zip',
+          '7za.exe',
+        );
+  if (fileExists(candidate)) return candidate;
+  throw StateError('Could not find 7zip.');
 })();
 
 /// Create a .tar.gz archive from a list of entries.
diff --git a/lib/src/sdk/dart.dart b/lib/src/sdk/dart.dart
index 328995d..2763f85 100644
--- a/lib/src/sdk/dart.dart
+++ b/lib/src/sdk/dart.dart
@@ -23,11 +23,6 @@
   @override
   Version get firstPubVersion => Version.none;
 
-  /// The path to the root directory of the SDK.
-  ///
-  /// Note that if pub is running from source within the Dart repo (for example
-  /// when building Observatory), this will be the repo's "sdk/" directory,
-  /// which doesn't look exactly like the built SDK.
   static final String _rootDirectory = () {
     if (runningFromDartRepo) return p.join(dartRepoRoot, 'sdk');
 
@@ -49,6 +44,11 @@
     return Version.parse(sdkVersion);
   }();
 
+  /// The path to the root directory of the SDK.
+  ///
+  /// Note that if pub is running from source within the Dart repo (for example
+  /// when building Observatory), this will be the repo's "sdk/" directory,
+  /// which doesn't look exactly like the built SDK.
   String get rootDirectory => _rootDirectory;
 
   @override
diff --git a/test/test_pub.dart b/test/test_pub.dart
index 5c71a82..45d47c2 100644
--- a/test/test_pub.dart
+++ b/test/test_pub.dart
@@ -54,9 +54,6 @@
 Matcher isUnminifiedDart2JSOutput =
     contains('// The code supports the following hooks');
 
-/// The entrypoint for pub itself.
-final _entrypoint = Entrypoint(pubRoot, SystemCache(isOffline: true));
-
 /// Converts [value] into a YAML string.
 String yaml(value) => jsonEncode(value);
 
@@ -391,6 +388,23 @@
   return environment;
 }
 
+/// The test runner starts all tests from a `data:` URI.
+final bool _runningAsTestRunner = Platform.script.scheme == 'data';
+
+/// The path to the root of pub's sources in the pub repo.
+final String _pubRoot = (() {
+  // The test runner always runs from the repo directory.
+  if (_runningAsTestRunner) return p.current;
+
+  // Running from "test/../some_test.dart".
+  var script = p.fromUri(Platform.script);
+
+  var components = p.split(script);
+  var testIndex = components.indexOf('test');
+  if (testIndex == -1) throw StateError("Can't find pub's root.");
+  return p.joinAll(components.take(testIndex));
+})();
+
 /// Starts a Pub process and returns a [PubProcess] that supports interaction
 /// with that process.
 ///
@@ -418,18 +432,14 @@
     dartBin = p.absolute(dartBin);
   }
 
-  var pubPath = p.absolute(p.join(pubRoot, 'bin/pub.dart'));
-
   // If there's a snapshot for "pub" available we use it. If the snapshot is
   // out-of-date local source the tests will be useless, therefore it is
   // recommended to use a temporary file with a unique name for each test run.
-  // Note: running tests without a snapshot is significantly slower.
-  //
-  // TODO(nweiz): When the test runner supports plugins, create one to
-  // auto-generate the snapshot before each run.
-  final snapshotPath = Platform.environment['_PUB_TEST_SNAPSHOT'] ?? '';
-  if (snapshotPath.isNotEmpty && fileExists(snapshotPath)) {
-    pubPath = snapshotPath;
+  // Note: running tests without a snapshot is significantly slower, use
+  // tool/test.dart to generate the snapshot.
+  var pubPath = Platform.environment['_PUB_TEST_SNAPSHOT'] ?? '';
+  if (pubPath.isEmpty || !fileExists(pubPath)) {
+    pubPath = p.absolute(p.join(_pubRoot, 'bin/pub.dart'));
   }
 
   final dotPackagesPath = (await Isolate.packageConfig).toString();
@@ -618,24 +628,6 @@
   return LockFile(packages);
 }
 
-/// Returns the path to the version of [package] used by pub.
-String packagePath(String package) {
-  if (runningFromDartRepo) {
-    return dirExists(p.join(dartRepoRoot, 'pkg', package))
-        ? p.join(dartRepoRoot, 'pkg', package)
-        : p.join(dartRepoRoot, 'third_party', 'pkg', package);
-  }
-
-  var id = _entrypoint.lockFile.packages[package];
-  if (id == null) {
-    throw StateError(
-        'The tests rely on "$package", but it\'s not in the lockfile.');
-  }
-
-  return p.join(
-      SystemCache.defaultDir, 'hosted/pub.dartlang.org/$package-${id.version}');
-}
-
 /// Uses [client] as the mock HTTP client for this test.
 ///
 /// Note that this will only affect HTTP requests made via http.dart in the