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

Unified Diff: pkg/scheduled_test/lib/src/descriptor/pattern.dart

Issue 12853005: Change the way Patterns work in scheduled_test/descriptor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add Nothing.validateNow. Created 7 years, 9 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: 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)
Bob Nystrom 2013/03/15 21:58:46 Does this work with the new Link stuff too?
nweiz 2013/03/15 23:17:21 Probably not. I'll add a TODO.
+ .where((entry) => fullMatch(path.basename(entry), pattern))
+ .toList();
+ matchingEntries.sort();
+
+ if (matchingEntries.length == 0) {
Bob Nystrom 2013/03/15 21:58:46 .isEmpty
nweiz 2013/03/15 23:17:21 Done.
+ 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;
Bob Nystrom 2013/03/15 21:58:46 This works, but you may want to add a .toList() af
nweiz 2013/03/15 23:17:21 Done.
+
+ // 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 Throw 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 you're returning the error async
nweiz 2013/03/15 23:17:21 Good point, create() should be in a Future.
+
+ Future read() => errorStream("Pattern descriptors don't support read().");
+}

Powered by Google App Engine
This is Rietveld 408576698