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