Skip to content

Commit 44cfc06

Browse files
authored
Remove registerTestPlatform() (#695)
Now that the Loader is the canonical place for determining which TestPlatforms are valid, we no longer need a separate mechanism for registering platforms. Adding a plugin to the loader is sufficient. See #391
1 parent 0a3f688 commit 44cfc06

File tree

8 files changed

+105
-126
lines changed

8 files changed

+105
-126
lines changed

lib/src/backend/test_platform.dart

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,52 @@
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-
import 'dart:collection';
6-
75
// TODO(nweiz): support pluggable platforms.
86
/// An enum of all platforms on which tests can run.
97
class TestPlatform {
108
// When adding new platforms, be sure to update the baseline and derived
119
// variable tests in test/backend/platform_selector/evaluate_test.
1210

1311
/// The command-line Dart VM.
14-
static const TestPlatform vm =
15-
const TestPlatform._("VM", "vm", isDartVM: true);
12+
static const TestPlatform vm = const TestPlatform("VM", "vm", isDartVM: true);
1613

1714
/// Dartium.
18-
static const TestPlatform dartium = const TestPlatform._("Dartium", "dartium",
15+
static const TestPlatform dartium = const TestPlatform("Dartium", "dartium",
1916
isBrowser: true, isBlink: true, isDartVM: true);
2017

2118
/// Dartium content shell.
22-
static const TestPlatform contentShell = const TestPlatform._(
19+
static const TestPlatform contentShell = const TestPlatform(
2320
"Dartium Content Shell", "content-shell",
2421
isBrowser: true, isBlink: true, isDartVM: true, isHeadless: true);
2522

2623
/// Google Chrome.
27-
static const TestPlatform chrome = const TestPlatform._("Chrome", "chrome",
24+
static const TestPlatform chrome = const TestPlatform("Chrome", "chrome",
2825
isBrowser: true, isJS: true, isBlink: true);
2926

3027
/// PhantomJS.
31-
static const TestPlatform phantomJS = const TestPlatform._(
28+
static const TestPlatform phantomJS = const TestPlatform(
3229
"PhantomJS", "phantomjs",
3330
isBrowser: true, isJS: true, isBlink: true, isHeadless: true);
3431

3532
/// Mozilla Firefox.
3633
static const TestPlatform firefox =
37-
const TestPlatform._("Firefox", "firefox", isBrowser: true, isJS: true);
34+
const TestPlatform("Firefox", "firefox", isBrowser: true, isJS: true);
3835

3936
/// Apple Safari.
4037
static const TestPlatform safari =
41-
const TestPlatform._("Safari", "safari", isBrowser: true, isJS: true);
38+
const TestPlatform("Safari", "safari", isBrowser: true, isJS: true);
4239

4340
/// Microsoft Internet Explorer.
44-
static const TestPlatform internetExplorer = const TestPlatform._(
41+
static const TestPlatform internetExplorer = const TestPlatform(
4542
"Internet Explorer", "ie",
4643
isBrowser: true, isJS: true);
4744

4845
/// The command-line Node.js VM.
4946
static const TestPlatform nodeJS =
50-
const TestPlatform._("Node.js", "node", isJS: true);
51-
52-
/// A list of all instances of [TestPlatform].
53-
static final UnmodifiableListView<TestPlatform> all =
54-
new UnmodifiableListView<TestPlatform>(_allPlatforms);
55-
56-
/// Finds a platform by its identifier string.
57-
///
58-
/// If no platform is found, returns `null`.
59-
static TestPlatform find(String identifier) =>
60-
all.firstWhere((platform) => platform.identifier == identifier,
61-
orElse: () => null);
47+
const TestPlatform("Node.js", "node", isJS: true);
6248

63-
static Set<TestPlatform> _builtIn = new Set.from([
49+
/// The platforms that are supported by the test runner by default.
50+
static const List<TestPlatform> builtIn = const [
6451
TestPlatform.vm,
6552
TestPlatform.dartium,
6653
TestPlatform.contentShell,
@@ -70,7 +57,7 @@ class TestPlatform {
7057
TestPlatform.safari,
7158
TestPlatform.internetExplorer,
7259
TestPlatform.nodeJS
73-
]);
60+
];
7461

7562
/// The human-friendly name of the platform.
7663
final String name;
@@ -93,7 +80,7 @@ class TestPlatform {
9380
/// Whether this platform has no visible window.
9481
final bool isHeadless;
9582

96-
const TestPlatform._(this.name, this.identifier,
83+
const TestPlatform(this.name, this.identifier,
9784
{this.isDartVM: false,
9885
this.isBrowser: false,
9986
this.isJS: false,
@@ -103,10 +90,13 @@ class TestPlatform {
10390
/// Converts a JSON-safe representation generated by [serialize] back into a
10491
/// [TestPlatform].
10592
factory TestPlatform.deserialize(Object serialized) {
106-
if (serialized is String) return find(serialized);
93+
if (serialized is String) {
94+
return builtIn
95+
.firstWhere((platform) => platform.identifier == serialized);
96+
}
10797

10898
var map = serialized as Map;
109-
return new TestPlatform._(map["name"], map["identifier"],
99+
return new TestPlatform(map["name"], map["identifier"],
110100
isDartVM: map["isDartVM"],
111101
isBrowser: map["isBrowser"],
112102
isJS: map["isJS"],
@@ -117,7 +107,7 @@ class TestPlatform {
117107
/// Converts [this] into a JSON-safe object that can be converted back to a
118108
/// [TestPlatform] using [new TestPlatform.deserialize].
119109
Object serialize() {
120-
if (_builtIn.contains(this)) return identifier;
110+
if (builtIn.contains(this)) return identifier;
121111

122112
return {
123113
"name": name,
@@ -132,26 +122,3 @@ class TestPlatform {
132122

133123
String toString() => name;
134124
}
135-
136-
final List<TestPlatform> _allPlatforms = TestPlatform._builtIn.toList();
137-
138-
/// **Do not call this function without express permission from the test package
139-
/// authors**.
140-
///
141-
/// This constructs and globally registers a new TestPlatform with the provided
142-
/// details.
143-
TestPlatform registerTestPlatform(String name, String identifier,
144-
{bool isDartVM: false,
145-
bool isBrowser: false,
146-
bool isJS: false,
147-
bool isBlink: false,
148-
bool isHeadless: false}) {
149-
var platform = new TestPlatform._(name, identifier,
150-
isDartVM: isDartVM,
151-
isBrowser: isBrowser,
152-
isJS: isJS,
153-
isBlink: isBlink,
154-
isHeadless: isHeadless);
155-
_allPlatforms.add(platform);
156-
return platform;
157-
}

lib/src/runner.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Runner {
130130
if (testOn == PlatformSelector.all) return;
131131

132132
var unsupportedPlatforms = _config.suiteDefaults.platforms
133-
.map(TestPlatform.find)
133+
.map(_loader.findTestPlatform)
134134
.where((platform) =>
135135
platform != null && !testOn.evaluate(platform, os: currentOS))
136136
.toList();
@@ -145,7 +145,7 @@ class Runner {
145145
var unsupportedBrowsers =
146146
unsupportedPlatforms.where((platform) => platform.isBrowser);
147147
if (unsupportedBrowsers.isNotEmpty) {
148-
var supportsAnyBrowser = TestPlatform.all
148+
var supportsAnyBrowser = _loader.allPlatforms
149149
.where((platform) => platform.isBrowser)
150150
.any((platform) => testOn.evaluate(platform));
151151

lib/src/runner/configuration/args.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import 'values.dart';
1818
final ArgParser _parser = (() {
1919
var parser = new ArgParser(allowTrailingOptions: true);
2020

21-
var allPlatforms = TestPlatform.all.toList()..remove(TestPlatform.vm);
21+
var allPlatforms = TestPlatform.builtIn.toList()..remove(TestPlatform.vm);
2222
if (!Platform.isMacOS) allPlatforms.remove(TestPlatform.safari);
2323
if (!Platform.isWindows) allPlatforms.remove(TestPlatform.internetExplorer);
2424

lib/src/runner/loader.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Loader {
4343
/// These are passed to the plugins' async memoizers when a plugin is needed.
4444
final _platformCallbacks = <TestPlatform, AsyncFunction>{};
4545

46+
/// A map of all platforms registered in [_platformCallbacks], indexed by
47+
/// their string identifiers.
48+
final _platformsByIdentifier = <String, TestPlatform>{};
49+
4650
/// All plaforms supported by this [Loader].
4751
List<TestPlatform> get allPlatforms =>
4852
new List.unmodifiable(_platformCallbacks.keys);
@@ -98,9 +102,15 @@ class Loader {
98102
for (var platform in platforms) {
99103
_platformPlugins[platform] = memoizer;
100104
_platformCallbacks[platform] = getPlugin;
105+
_platformsByIdentifier[platform.identifier] = platform;
101106
}
102107
}
103108

109+
/// Returns the [TestPlatform] registered with this loader that's identified
110+
/// by [identifier], or `null` if none can be found.
111+
TestPlatform findTestPlatform(String identifier) =>
112+
_platformsByIdentifier[identifier];
113+
104114
/// Loads all test suites in [dir] according to [suiteConfig].
105115
///
106116
/// This will load tests from files that match the global configuration's
@@ -156,7 +166,8 @@ class Loader {
156166
}
157167

158168
for (var platformName in suiteConfig.platforms) {
159-
var platform = TestPlatform.find(platformName);
169+
var platform = findTestPlatform(platformName);
170+
assert(platform != null, 'Unknown platform "$platformName".');
160171

161172
if (!suiteConfig.metadata.testOn.evaluate(platform, os: currentOS)) {
162173
continue;

lib/src/runner/remote_listener.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ class RemoteListener {
7373
}
7474

7575
var message = await channel.stream.first;
76+
var platforms = message['testPlatforms']
77+
.map((platform) => new TestPlatform.deserialize(platform))
78+
.toList();
7679

7780
if (message['asciiGlyphs'] ?? false) glyph.ascii = true;
7881
var metadata = new Metadata.deserialize(message['metadata']);
7982
verboseChain = metadata.verboseTrace;
80-
var declarer = new Declarer(
81-
message['testPlatforms']
82-
.map((platform) => new TestPlatform.deserialize(platform))
83-
.toList(),
83+
var declarer = new Declarer(platforms,
8484
metadata: metadata,
8585
collectTraces: message['collectTraces'],
8686
noRetry: message['noRetry']);
@@ -94,7 +94,8 @@ class RemoteListener {
9494

9595
var os =
9696
message['os'] == null ? null : OperatingSystem.find(message['os']);
97-
var platform = TestPlatform.find(message['platform']);
97+
var platform = platforms
98+
.firstWhere((platform) => platform.identifier == message['platform']);
9899
var suite = new Suite(declarer.build(),
99100
platform: platform, os: os, path: message['path']);
100101
new RemoteListener._(suite, printZone)._listen(channel);

lib/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Declarer get _declarer {
5353
// In order to run the tests, we set up our own Declarer via
5454
// [_globalDeclarer], and schedule a microtask to run the tests once they're
5555
// finished being defined.
56-
_globalDeclarer = new Declarer(TestPlatform.all);
56+
_globalDeclarer = new Declarer(TestPlatform.builtIn);
5757
scheduleMicrotask(() async {
5858
var suite = new RunnerSuite(const PluginEnvironment(),
5959
SuiteConfiguration.empty, _globalDeclarer.build(),

0 commit comments

Comments
 (0)