Chromium Code Reviews| Index: pkg/scheduled_test/lib/src/descriptor/pattern.dart |
| diff --git a/pkg/scheduled_test/lib/src/descriptor/pattern.dart b/pkg/scheduled_test/lib/src/descriptor/pattern.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..47cc42b1aa06807c63e0e504ddd16a4a0ee0d3b8 |
| --- /dev/null |
| +++ b/pkg/scheduled_test/lib/src/descriptor/pattern.dart |
| @@ -0,0 +1,121 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// 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. |
| + |
| +library descriptor.pattern; |
| + |
| +import 'dart:core' as core show Pattern; |
| +import 'dart:core' hide Pattern; |
| +import 'dart:async'; |
| +import 'dart:io'; |
| + |
| +import '../../../../../pkg/pathos/lib/path.dart' as path; |
| + |
| +import '../../descriptor.dart' as descriptor; |
| +import '../../scheduled_test.dart'; |
| +import '../utils.dart'; |
| + |
| +/// A function that takes a name for a [descriptor.Entry] and returns a |
| +/// [descriptor.Entry]. This is used for [Pattern] entries, where the name isn't |
| +/// known ahead-of-time. |
| +typedef descriptor.Entry EntryCreator(String name); |
| + |
| +/// A descriptor that matches filesystem entities by [core.Pattern] rather than |
| +/// by [String]. It's used only for validation. |
| +/// |
| +/// This class takes an [EntryCreator], which should return a [descriptor.Entry] |
| +/// that will be used to validate the concrete filesystem entities that match |
| +/// the [pattern]. |
| +class Pattern extends descriptor.Entry { |
| + /// The [core.Pattern] this matches filenames against. Note that the pattern |
| + /// must match the entire basename of the file. |
| + final core.Pattern pattern; |
| + |
| + /// The function used to generate the [descriptor.Entry] for filesystem |
| + /// entities matching [pattern]. |
| + final EntryCreator _fn; |
| + |
| + Pattern(core.Pattern pattern, this._fn) |
| + : super('$pattern'), |
| + pattern = pattern; |
| + |
| + /// Validates that there is some filesystem entity in [parent] that matches |
| + /// [pattern] and the child entry. This finds all entities in [parent] |
| + /// matching [pattern], then passes each of their names to the [EntityCreator] |
| + /// and validates the result. If exactly one succeeds, [this] is considered |
| + /// valid. |
| + Future validate([String parent]) => schedule(() => validateNow(parent), |
| + "validating ${describe()}"); |
| + |
| + Future validateNow([String parent]) { |
| + if (parent == null) parent = descriptor.defaultRoot; |
| + var matchingEntries = new Directory(parent).listSync() |
| + .map((entry) => entry is File ? entry.fullPathSync() : entry.path) |
| + .where((entry) => fullMatch(path.basename(entry), pattern)) |
| + .toList(); |
| + matchingEntries.sort(); |
| + |
| + if (matchingEntries.length == 0) { |
| + throw "No entry found in '$parent' matching ${_patternDescription}."; |
| + } |
| + |
| + return Future.wait(matchingEntries.map((entry) { |
| + var descriptor = _fn(path.basename(entry)); |
| + return descriptor.validateNow(parent).then((_) { |
| + return new Pair(null, descriptor.describe()); |
| + }).catchError((e) { |
| + return new Pair(e.error.toString(), descriptor.describe()); |
| + }); |
| + })).then((results) { |
| + var matches = results.where((result) => result.first == null); |
| + // If exactly one entry matching [pattern] validated, we're happy. |
| + if (matches.length == 1) return; |
| + |
| + // If more than one entry matching [pattern] validated, that's bad. |
| + if (matches.length > 1) { |
| + var resultString = matches.map((result) { |
| + return prefixLines(result.last, firstPrefix: '* ', prefix: ' '); |
| + }).join('\n'); |
| + |
| + throw "Multiple valid entries found in '$parent' matching " |
| + "$_patternDescription:\n" |
| + "$resultString"; |
| + } |
| + |
| + // If no entries matching [pattern] validated, that's also bad. |
| + var resultString = results.map((result) { |
| + return prefixLines( |
| + "Caught error\n" |
| + "${prefixLines(result.first)}\n" |
| + "while validating\n" |
| + "${prefixLines(result.last)}", |
| + firstPrefix: '* ', prefix: ' '); |
| + }).join('\n'); |
| + |
| + throw "No valid entries found in '$parent' matching " |
| + "$_patternDescription:\n" |
| + "$resultString"; |
| + }); |
| + } |
| + |
| + String describe() => "entry matching $_patternDescription"; |
| + |
| + String get _patternDescription { |
| + if (pattern is String) return "'$pattern'"; |
| + if (pattern is! RegExp) return '$pattern'; |
| + |
| + var flags = new StringBuffer(); |
| + if (!pattern.isCaseSensitive) flags.write('i'); |
| + if (pattern.isMultiLine) flags.write('m'); |
| + return '/${pattern.pattern}/$flags'; |
| + } |
| + |
| + Future create([String parent]) { |
| + throw "Pattern descriptors don't support create()."; |
|
Bob Nystrom
2013/03/15 21:58:46
UnsupportedError.
nweiz
2013/03/15 23:17:21
Done.
|
| + } |
| + |
| + Future load(String pathToLoad) => errorStream("Pattern descriptors don't " |
| + "support load()."); |
|
Bob Nystrom
2013/03/15 21:58:46
Is there a reason this propagates the error throug
nweiz
2013/03/15 23:17:21
Good point, create() should be in a Future.
|
| + |
| + Future read() => errorStream("Pattern descriptors don't support read()."); |
| +} |