Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(655)

Unified Diff: lib/src/util/io.dart

Issue 979513002: pkg/unittest: Add a server for serving test assets to browsers. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Fix libDir. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/util/io.dart
diff --git a/lib/src/util/io.dart b/lib/src/util/io.dart
index f84796f9c3cf277b90d2ea4bdfdd30c3731ce633..9a00ad642be035a69a0a350f4eebd6ecb4b48bc3 100644
--- a/lib/src/util/io.dart
+++ b/lib/src/util/io.dart
@@ -6,12 +6,21 @@ library unittest.util.io;
import 'dart:async';
import 'dart:io';
+import 'dart:mirrors';
import 'package:path/path.dart' as p;
+import '../runner/load_exception.dart';
+
/// The root directory of the Dart SDK.
-final String sdkDir = _computeSdkDir();
-String _computeSdkDir() => p.dirname(p.dirname(Platform.executable));
+final String sdkDir =
+ p.dirname(p.dirname(Platform.executable));
+
+/// The path to the `lib` directory of the `unittest` package.
+String libDir({String packageRoot}) {
+ var pathToIo = libraryPath(#unittest.util.io, packageRoot: packageRoot);
+ return p.dirname(p.dirname(p.dirname(pathToIo)));
+}
/// Returns whether the current Dart version supports [Isolate.kill].
final bool supportsIsolateKill = _supportsIsolateKill;
@@ -67,3 +76,31 @@ Uri baseUrlForAddress(InternetAddress address, int port) {
return new Uri(scheme: "http", host: address.address, port: port);
}
+
+/// Returns the package root for a Dart entrypoint at [path].
+///
+/// If [override] is passed, that's used. If the package root doesn't exist, a
+/// [LoadException] is thrown.
+String packageRootFor(String path, [String override]) {
+ var packageRoot = override == null
+ ? p.join(p.dirname(path), 'packages')
+ : override;
+
+ if (!new Directory(packageRoot).existsSync()) {
+ throw new LoadException(path, "Directory $packageRoot does not exist.");
+ }
+
+ return packageRoot;
+}
+
+/// The library name must be globally unique, or the wrong library path may be
+/// returned.
+String libraryPath(Symbol libraryName, {String packageRoot}) {
+ var lib = currentMirrorSystem().findLibrary(libraryName);
+ if (lib.uri.scheme != 'package') return p.fromUri(lib.uri);
+
+ // TODO(nweiz): is there a way to avoid assuming this is being run next to a
+ // packages directory?.
+ if (packageRoot == null) packageRoot = p.absolute('packages');
+ return p.join(packageRoot, p.fromUri(lib.uri.path));
+}
« lib/src/runner/loader.dart ('K') | « lib/src/runner/loader.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698