From df2ede529a99aa1fe8765da9657fd2ccd977cd2b Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum <nweiz@google.com> Date: Wed, 7 Mar 2018 12:53:47 -0800 Subject: [PATCH] Stop importing dart:io from browser libraries (#782) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I forgot that this wasn't supported in Dart 1.24 ðŸ˜. Closes #781 --- CHANGELOG.md | 4 +++ lib/src/backend/suite_platform.dart | 14 -------- lib/src/runner.dart | 6 ++-- lib/src/runner/browser/browser_manager.dart | 4 +-- lib/src/runner/load_suite.dart | 3 +- lib/src/runner/loader.dart | 3 +- lib/src/util/io.dart | 13 ++++++++ lib/src/utils.dart | 36 +++++++++------------ lib/test.dart | 3 +- pubspec.yaml | 2 +- 10 files changed, 42 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e5325a..7ecfd445 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.12.32+1 + +* Fix a bug that broke content shell on Dart 1.24. + ## 0.12.32 * Add an `include` configuration field which specifies the path to another diff --git a/lib/src/backend/suite_platform.dart b/lib/src/backend/suite_platform.dart index 5ac8e333..a4cb7181 100644 --- a/lib/src/backend/suite_platform.dart +++ b/lib/src/backend/suite_platform.dart @@ -2,9 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// Prefix this import to avoid accidentally using IO stuff in cross-platform -// contexts. -import '../util/io.dart' as io; import 'operating_system.dart'; import 'runtime.dart'; @@ -34,17 +31,6 @@ class SuitePlatform { } } - /// Creates a new platform with the given [runtime] and [os] and [inGoogle] - /// determined using `dart:io`. - /// - /// If [runtime] is a browser, this will set [os] to [OperatingSystem.none]. - /// - /// Throws an [UnsupportedError] if called in a context where `dart:io` is - /// unavailable. - SuitePlatform.current(this.runtime) - : os = runtime.isBrowser ? OperatingSystem.none : io.currentOS, - inGoogle = io.inGoogle; - /// Converts a JSON-safe representation generated by [serialize] back into a /// [SuitePlatform]. factory SuitePlatform.deserialize(Object serialized) { diff --git a/lib/src/runner.dart b/lib/src/runner.dart index 236ab0f2..02e6bef0 100644 --- a/lib/src/runner.dart +++ b/lib/src/runner.dart @@ -131,8 +131,7 @@ class Runner { var unsupportedRuntimes = _config.suiteDefaults.runtimes .map(_loader.findRuntime) .where((runtime) => - runtime != null && - !testOn.evaluate(new SuitePlatform.current(runtime))) + runtime != null && !testOn.evaluate(currentPlatform(runtime))) .toList(); if (unsupportedRuntimes.isEmpty) return; @@ -147,8 +146,7 @@ class Runner { if (unsupportedBrowsers.isNotEmpty) { var supportsAnyBrowser = _loader.allRuntimes .where((runtime) => runtime.isBrowser) - .any( - (runtime) => testOn.evaluate(new SuitePlatform.current(runtime))); + .any((runtime) => testOn.evaluate(currentPlatform(runtime))); if (supportsAnyBrowser) { unsupportedNames diff --git a/lib/src/runner/browser/browser_manager.dart b/lib/src/runner/browser/browser_manager.dart index a2d80c43..834ffca4 100644 --- a/lib/src/runner/browser/browser_manager.dart +++ b/lib/src/runner/browser/browser_manager.dart @@ -11,7 +11,7 @@ import 'package:stream_channel/stream_channel.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import '../../backend/runtime.dart'; -import '../../backend/suite_platform.dart'; +import '../../util/io.dart'; import '../../util/stack_trace_mapper.dart'; import '../application_exception.dart'; import '../configuration/suite.dart'; @@ -242,7 +242,7 @@ class BrowserManager { }); try { - controller = deserializeSuite(path, new SuitePlatform.current(_runtime), + controller = deserializeSuite(path, currentPlatform(_runtime), suiteConfig, await _environment, suiteChannel, message); controller.channel("test.browser.mapper").sink.add(mapper?.serialize()); diff --git a/lib/src/runner/load_suite.dart b/lib/src/runner/load_suite.dart index 3743c8be..fc8b428d 100644 --- a/lib/src/runner/load_suite.dart +++ b/lib/src/runner/load_suite.dart @@ -15,6 +15,7 @@ import '../backend/suite.dart'; import '../backend/suite_platform.dart'; import '../backend/test.dart'; import '../backend/runtime.dart'; +import '../util/io.dart'; import '../utils.dart'; import 'configuration/suite.dart'; import 'load_exception.dart'; @@ -122,7 +123,7 @@ class LoadSuite extends Suite implements RunnerSuite { return new LoadSuite( "loading ${exception.path}", config ?? SuiteConfiguration.empty, - platform ?? new SuitePlatform.current(Runtime.vm), + platform ?? currentPlatform(Runtime.vm), () => new Future.error(exception, stackTrace), path: exception.path); } diff --git a/lib/src/runner/loader.dart b/lib/src/runner/loader.dart index cb3cec2b..61ce9e17 100644 --- a/lib/src/runner/loader.dart +++ b/lib/src/runner/loader.dart @@ -14,7 +14,6 @@ import 'package:yaml/yaml.dart'; import '../backend/group.dart'; import '../backend/invoker.dart'; import '../backend/runtime.dart'; -import '../backend/suite_platform.dart'; import '../util/io.dart'; import 'browser/platform.dart'; import 'configuration.dart'; @@ -223,7 +222,7 @@ class Loader { var runtime = findRuntime(runtimeName); assert(runtime != null, 'Unknown platform "$runtimeName".'); - var platform = new SuitePlatform.current(runtime); + var platform = currentPlatform(runtime); if (!suiteConfig.metadata.testOn.evaluate(platform)) { continue; } diff --git a/lib/src/util/io.dart b/lib/src/util/io.dart index c2883d22..e79eae0a 100644 --- a/lib/src/util/io.dart +++ b/lib/src/util/io.dart @@ -10,6 +10,8 @@ import 'package:async/async.dart'; import 'package:path/path.dart' as p; import '../backend/operating_system.dart'; +import '../backend/runtime.dart'; +import '../backend/suite_platform.dart'; import '../utils.dart'; /// The ASCII code for a newline character. @@ -50,6 +52,17 @@ final OperatingSystem currentOS = (() { throw new UnsupportedError('Unsupported operating system "$name".'); })(); +// TODO(nweiz): Make this `new SuitePlatform.current()` once we only support +// Dart 2 and we can import `dart:io` from within cross-platform libraries. See +// commit 4ffda6d2. +/// Returns a [SuitePlatform] with the given [runtime], and with [os] and +/// [inGoogle] determined automatically. +/// +/// If [runtime] is a browser, this will set [os] to [OperatingSystem.none]. +SuitePlatform currentPlatform(Runtime runtime) => new SuitePlatform(runtime, + os: runtime.isBrowser ? OperatingSystem.none : currentOS, + inGoogle: inGoogle); + /// A queue of lines of standard input. final stdinLines = new StreamQueue(lineSplitter.bind(stdin)); diff --git a/lib/src/utils.dart b/lib/src/utils.dart index bb5f785c..60d55e5e 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -10,14 +10,12 @@ import 'dart:typed_data'; import 'package:async/async.dart'; import 'package:collection/collection.dart'; import 'package:matcher/matcher.dart'; +import 'package:path/path.dart' as p; import 'package:stream_channel/stream_channel.dart'; import 'package:term_glyph/term_glyph.dart' as glyph; import 'backend/invoker.dart'; import 'backend/operating_system.dart'; -// Prefix this import to avoid accidentally using IO stuff in cross-platform -// contexts. -import 'util/io.dart' as io; /// A typedef for a possibly-asynchronous function. /// @@ -51,16 +49,24 @@ final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); /// A regular expression matching a single vowel. final _vowel = new RegExp('[aeiou]'); -/// Returns the best guess for the current operating system without relying on +/// Directories that are specific to OS X. +/// +/// This is used to try to distinguish OS X and Linux in [currentOSGuess]. +final _macOSDirectories = new Set<String>.from( + ["/Applications", "/Library", "/Network", "/System", "/Users"]); + +/// Returns the best guess for the current operating system without using /// `dart:io`. /// /// This is useful for running test files directly and skipping tests as -/// appropriate. -final currentOSGuess = _ifSupported(() => io.currentOS, OperatingSystem.none); - -/// Returns the best guess for whether we're running on internal Google -/// infrastructure without relying on `dart:io`. -final inGoogleGuess = _ifSupported(() => io.inGoogle, false); +/// appropriate. The only OS-specific information we have is the current path, +/// which we try to use to figure out the OS. +final OperatingSystem currentOSGuess = (() { + if (p.style == p.Style.url) return OperatingSystem.none; + if (p.style == p.Style.windows) return OperatingSystem.windows; + if (_macOSDirectories.any(p.current.startsWith)) return OperatingSystem.macOS; + return OperatingSystem.linux; +})(); /// A regular expression matching a hyphenated identifier. /// @@ -90,16 +96,6 @@ class Pair<E, F> { int get hashCode => first.hashCode ^ last.hashCode; } -/// Returns [callback]'s return value, unless it throws an [UnsupportedError] in -/// which case returns [fallback]. -T _ifSupported<T>(T callback(), T fallback) { - try { - return callback(); - } on UnsupportedError { - return fallback; - } -} - /// Get a string description of an exception. /// /// Many exceptions include the exception class name at the beginning of their diff --git a/lib/test.dart b/lib/test.dart index 9dbafe32..cc84803e 100644 --- a/lib/test.dart +++ b/lib/test.dart @@ -62,8 +62,7 @@ Declarer get _declarer { const PluginEnvironment(), SuiteConfiguration.empty, _globalDeclarer.build(), - new SuitePlatform(Runtime.vm, - os: currentOSGuess, inGoogle: inGoogleGuess), + new SuitePlatform(Runtime.vm, os: currentOSGuess), path: p.prettyUri(Uri.base)); var engine = new Engine(); diff --git a/pubspec.yaml b/pubspec.yaml index 4e3ee3cf..ad261879 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: test -version: 0.12.32 +version: 0.12.32+1 author: Dart Team <misc@dartlang.org> description: A library for writing dart unit tests. homepage: https://github.com/dart-lang/test -- GitLab