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