Skip to content

Commit df2ede5

Browse files
authored
Stop importing dart:io from browser libraries (#782)
I forgot that this wasn't supported in Dart 1.24 😝. Closes #781
1 parent 4ffda6d commit df2ede5

File tree

10 files changed

+42
-46
lines changed

10 files changed

+42
-46
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.32+1
2+
3+
* Fix a bug that broke content shell on Dart 1.24.
4+
15
## 0.12.32
26

37
* Add an `include` configuration field which specifies the path to another

β€Žlib/src/backend/suite_platform.dartβ€Ž

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// Prefix this import to avoid accidentally using IO stuff in cross-platform
6-
// contexts.
7-
import '../util/io.dart' as io;
85
import 'operating_system.dart';
96
import 'runtime.dart';
107

@@ -34,17 +31,6 @@ class SuitePlatform {
3431
}
3532
}
3633

37-
/// Creates a new platform with the given [runtime] and [os] and [inGoogle]
38-
/// determined using `dart:io`.
39-
///
40-
/// If [runtime] is a browser, this will set [os] to [OperatingSystem.none].
41-
///
42-
/// Throws an [UnsupportedError] if called in a context where `dart:io` is
43-
/// unavailable.
44-
SuitePlatform.current(this.runtime)
45-
: os = runtime.isBrowser ? OperatingSystem.none : io.currentOS,
46-
inGoogle = io.inGoogle;
47-
4834
/// Converts a JSON-safe representation generated by [serialize] back into a
4935
/// [SuitePlatform].
5036
factory SuitePlatform.deserialize(Object serialized) {

β€Žlib/src/runner.dartβ€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class Runner {
131131
var unsupportedRuntimes = _config.suiteDefaults.runtimes
132132
.map(_loader.findRuntime)
133133
.where((runtime) =>
134-
runtime != null &&
135-
!testOn.evaluate(new SuitePlatform.current(runtime)))
134+
runtime != null && !testOn.evaluate(currentPlatform(runtime)))
136135
.toList();
137136
if (unsupportedRuntimes.isEmpty) return;
138137

@@ -147,8 +146,7 @@ class Runner {
147146
if (unsupportedBrowsers.isNotEmpty) {
148147
var supportsAnyBrowser = _loader.allRuntimes
149148
.where((runtime) => runtime.isBrowser)
150-
.any(
151-
(runtime) => testOn.evaluate(new SuitePlatform.current(runtime)));
149+
.any((runtime) => testOn.evaluate(currentPlatform(runtime)));
152150

153151
if (supportsAnyBrowser) {
154152
unsupportedNames

β€Žlib/src/runner/browser/browser_manager.dartβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:stream_channel/stream_channel.dart';
1111
import 'package:web_socket_channel/web_socket_channel.dart';
1212

1313
import '../../backend/runtime.dart';
14-
import '../../backend/suite_platform.dart';
14+
import '../../util/io.dart';
1515
import '../../util/stack_trace_mapper.dart';
1616
import '../application_exception.dart';
1717
import '../configuration/suite.dart';
@@ -242,7 +242,7 @@ class BrowserManager {
242242
});
243243

244244
try {
245-
controller = deserializeSuite(path, new SuitePlatform.current(_runtime),
245+
controller = deserializeSuite(path, currentPlatform(_runtime),
246246
suiteConfig, await _environment, suiteChannel, message);
247247

248248
controller.channel("test.browser.mapper").sink.add(mapper?.serialize());

β€Žlib/src/runner/load_suite.dartβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '../backend/suite.dart';
1515
import '../backend/suite_platform.dart';
1616
import '../backend/test.dart';
1717
import '../backend/runtime.dart';
18+
import '../util/io.dart';
1819
import '../utils.dart';
1920
import 'configuration/suite.dart';
2021
import 'load_exception.dart';
@@ -122,7 +123,7 @@ class LoadSuite extends Suite implements RunnerSuite {
122123
return new LoadSuite(
123124
"loading ${exception.path}",
124125
config ?? SuiteConfiguration.empty,
125-
platform ?? new SuitePlatform.current(Runtime.vm),
126+
platform ?? currentPlatform(Runtime.vm),
126127
() => new Future.error(exception, stackTrace),
127128
path: exception.path);
128129
}

β€Žlib/src/runner/loader.dartβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:yaml/yaml.dart';
1414
import '../backend/group.dart';
1515
import '../backend/invoker.dart';
1616
import '../backend/runtime.dart';
17-
import '../backend/suite_platform.dart';
1817
import '../util/io.dart';
1918
import 'browser/platform.dart';
2019
import 'configuration.dart';
@@ -223,7 +222,7 @@ class Loader {
223222
var runtime = findRuntime(runtimeName);
224223
assert(runtime != null, 'Unknown platform "$runtimeName".');
225224

226-
var platform = new SuitePlatform.current(runtime);
225+
var platform = currentPlatform(runtime);
227226
if (!suiteConfig.metadata.testOn.evaluate(platform)) {
228227
continue;
229228
}

β€Žlib/src/util/io.dartβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'package:async/async.dart';
1010
import 'package:path/path.dart' as p;
1111

1212
import '../backend/operating_system.dart';
13+
import '../backend/runtime.dart';
14+
import '../backend/suite_platform.dart';
1315
import '../utils.dart';
1416

1517
/// The ASCII code for a newline character.
@@ -50,6 +52,17 @@ final OperatingSystem currentOS = (() {
5052
throw new UnsupportedError('Unsupported operating system "$name".');
5153
})();
5254

55+
// TODO(nweiz): Make this `new SuitePlatform.current()` once we only support
56+
// Dart 2 and we can import `dart:io` from within cross-platform libraries. See
57+
// commit 4ffda6d2.
58+
/// Returns a [SuitePlatform] with the given [runtime], and with [os] and
59+
/// [inGoogle] determined automatically.
60+
///
61+
/// If [runtime] is a browser, this will set [os] to [OperatingSystem.none].
62+
SuitePlatform currentPlatform(Runtime runtime) => new SuitePlatform(runtime,
63+
os: runtime.isBrowser ? OperatingSystem.none : currentOS,
64+
inGoogle: inGoogle);
65+
5366
/// A queue of lines of standard input.
5467
final stdinLines = new StreamQueue(lineSplitter.bind(stdin));
5568

β€Žlib/src/utils.dartβ€Ž

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import 'dart:typed_data';
1010
import 'package:async/async.dart';
1111
import 'package:collection/collection.dart';
1212
import 'package:matcher/matcher.dart';
13+
import 'package:path/path.dart' as p;
1314
import 'package:stream_channel/stream_channel.dart';
1415
import 'package:term_glyph/term_glyph.dart' as glyph;
1516

1617
import 'backend/invoker.dart';
1718
import 'backend/operating_system.dart';
18-
// Prefix this import to avoid accidentally using IO stuff in cross-platform
19-
// contexts.
20-
import 'util/io.dart' as io;
2119

2220
/// A typedef for a possibly-asynchronous function.
2321
///
@@ -51,16 +49,24 @@ final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): ');
5149
/// A regular expression matching a single vowel.
5250
final _vowel = new RegExp('[aeiou]');
5351

54-
/// Returns the best guess for the current operating system without relying on
52+
/// Directories that are specific to OS X.
53+
///
54+
/// This is used to try to distinguish OS X and Linux in [currentOSGuess].
55+
final _macOSDirectories = new Set<String>.from(
56+
["/Applications", "/Library", "/Network", "/System", "/Users"]);
57+
58+
/// Returns the best guess for the current operating system without using
5559
/// `dart:io`.
5660
///
5761
/// This is useful for running test files directly and skipping tests as
58-
/// appropriate.
59-
final currentOSGuess = _ifSupported(() => io.currentOS, OperatingSystem.none);
60-
61-
/// Returns the best guess for whether we're running on internal Google
62-
/// infrastructure without relying on `dart:io`.
63-
final inGoogleGuess = _ifSupported(() => io.inGoogle, false);
62+
/// appropriate. The only OS-specific information we have is the current path,
63+
/// which we try to use to figure out the OS.
64+
final OperatingSystem currentOSGuess = (() {
65+
if (p.style == p.Style.url) return OperatingSystem.none;
66+
if (p.style == p.Style.windows) return OperatingSystem.windows;
67+
if (_macOSDirectories.any(p.current.startsWith)) return OperatingSystem.macOS;
68+
return OperatingSystem.linux;
69+
})();
6470

6571
/// A regular expression matching a hyphenated identifier.
6672
///
@@ -90,16 +96,6 @@ class Pair<E, F> {
9096
int get hashCode => first.hashCode ^ last.hashCode;
9197
}
9298

93-
/// Returns [callback]'s return value, unless it throws an [UnsupportedError] in
94-
/// which case returns [fallback].
95-
T _ifSupported<T>(T callback(), T fallback) {
96-
try {
97-
return callback();
98-
} on UnsupportedError {
99-
return fallback;
100-
}
101-
}
102-
10399
/// Get a string description of an exception.
104100
///
105101
/// Many exceptions include the exception class name at the beginning of their

β€Žlib/test.dartβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ Declarer get _declarer {
6262
const PluginEnvironment(),
6363
SuiteConfiguration.empty,
6464
_globalDeclarer.build(),
65-
new SuitePlatform(Runtime.vm,
66-
os: currentOSGuess, inGoogle: inGoogleGuess),
65+
new SuitePlatform(Runtime.vm, os: currentOSGuess),
6766
path: p.prettyUri(Uri.base));
6867

6968
var engine = new Engine();

β€Žpubspec.yamlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: test
2-
version: 0.12.32
2+
version: 0.12.32+1
33
author: Dart Team <[email protected]>
44
description: A library for writing dart unit tests.
55
homepage: https://github.com/dart-lang/test

0 commit comments

Comments
Β (0)