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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library descriptor.pattern;
6
7 import 'dart:core' as core show Pattern;
8 import 'dart:core' hide Pattern;
9 import 'dart:async';
10 import 'dart:io';
11
12 import '../../../../../pkg/pathos/lib/path.dart' as path;
13
14 import '../../descriptor.dart' as descriptor;
15 import '../../scheduled_test.dart';
16 import '../utils.dart';
17
18 /// A function that takes a name for a [descriptor.Entry] and returns a
19 /// [descriptor.Entry]. This is used for [Pattern] entries, where the name isn't
20 /// known ahead-of-time.
21 typedef descriptor.Entry EntryCreator(String name);
22
23 /// A descriptor that matches filesystem entities by [core.Pattern] rather than
24 /// by [String]. It's used only for validation.
25 ///
26 /// This class takes an [EntryCreator], which should return a [descriptor.Entry]
27 /// that will be used to validate the concrete filesystem entities that match
28 /// the [pattern].
29 class Pattern extends descriptor.Entry {
30 /// The [core.Pattern] this matches filenames against. Note that the pattern
31 /// must match the entire basename of the file.
32 final core.Pattern pattern;
33
34 /// The function used to generate the [descriptor.Entry] for filesystem
35 /// entities matching [pattern].
36 final EntryCreator _fn;
37
38 Pattern(core.Pattern pattern, this._fn)
39 : super('$pattern'),
40 pattern = pattern;
41
42 /// Validates that there is some filesystem entity in [parent] that matches
43 /// [pattern] and the child entry. This finds all entities in [parent]
44 /// matching [pattern], then passes each of their names to the [EntityCreator]
45 /// and validates the result. If exactly one succeeds, [this] is considered
46 /// valid.
47 Future validate([String parent]) => schedule(() => validateNow(parent),
48 "validating ${describe()}");
49
50 Future validateNow([String parent]) {
51 if (parent == null) parent = descriptor.defaultRoot;
52 var matchingEntries = new Directory(parent).listSync()
53 .map((entry) => entry is File ? entry.fullPathSync() : entry.path)
54 .where((entry) => fullMatch(path.basename(entry), pattern))
55 .toList();
56 matchingEntries.sort();
57
58 if (matchingEntries.length == 0) {
59 throw "No entry found in '$parent' matching ${_patternDescription}.";
60 }
61
62 return Future.wait(matchingEntries.map((entry) {
63 var descriptor = _fn(path.basename(entry));
64 return descriptor.validateNow(parent).then((_) {
65 return new Pair(null, descriptor.describe());
66 }).catchError((e) {
67 return new Pair(e.error.toString(), descriptor.describe());
68 });
69 })).then((results) {
70 var matches = results.where((result) => result.first == null);
71 // If exactly one entry matching [pattern] validated, we're happy.
72 if (matches.length == 1) return;
73
74 // If more than one entry matching [pattern] validated, that's bad.
75 if (matches.length > 1) {
76 var resultString = matches.map((result) {
77 return prefixLines(result.last, firstPrefix: '* ', prefix: ' ');
78 }).join('\n');
79
80 throw "Multiple valid entries found in '$parent' matching "
81 "$_patternDescription:\n"
82 "$resultString";
83 }
84
85 // If no entries matching [pattern] validated, that's also bad.
86 var resultString = results.map((result) {
87 return prefixLines(
88 "Caught error\n"
89 "${prefixLines(result.first)}\n"
90 "while validating\n"
91 "${prefixLines(result.last)}",
92 firstPrefix: '* ', prefix: ' ');
93 }).join('\n');
94
95 throw "No valid entries found in '$parent' matching "
96 "$_patternDescription:\n"
97 "$resultString";
98 });
99 }
100
101 String describe() => "entry matching $_patternDescription";
102
103 String get _patternDescription {
104 if (pattern is String) return "'$pattern'";
105 if (pattern is! RegExp) return '$pattern';
106
107 var flags = new StringBuffer();
108 if (!pattern.isCaseSensitive) flags.write('i');
109 if (pattern.isMultiLine) flags.write('m');
110 return '/${pattern.pattern}/$flags';
111 }
112
113 Future create([String parent]) {
114 throw "Pattern descriptors don't support create().";
Bob Nystrom 2013/03/15 21:58:46 UnsupportedError.
nweiz 2013/03/15 23:17:21 Done.
115 }
116
117 Future load(String pathToLoad) => errorStream("Pattern descriptors don't "
118 "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.
119
120 Future read() => errorStream("Pattern descriptors don't support read().");
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698