Chromium Code Reviews| Index: pkg/scheduled_test/lib/src/descriptor/utils.dart |
| diff --git a/pkg/scheduled_test/lib/src/descriptor/utils.dart b/pkg/scheduled_test/lib/src/descriptor/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..450b899bf23c819280fc47e5de735d7031cd0cef |
| --- /dev/null |
| +++ b/pkg/scheduled_test/lib/src/descriptor/utils.dart |
| @@ -0,0 +1,51 @@ |
| +// 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.utils; |
| + |
| +import 'dart:io'; |
| + |
| +import 'package:path/path.dart' as path; |
| + |
| +/// Returns a single filesystem entry within [parent] whose name matches |
| +/// [pattern]. If [pattern] is a string, looks for an exact match; otherwise, |
| +/// looks for an entry that contains [pattern]. |
| +/// |
| +/// If there are no entries in [parent] matching [pattern], or more than one, |
| +/// this will throw an exception. |
| +String entryMatchingPattern(String parent, Pattern pattern) { |
| + if (pattern is String) { |
| + var path = path.join(parent, pattern); |
| + if (new File(path).existsSync() || new Directory(path).existsSync()) { |
| + return path; |
| + } |
| + throw "Entry not found: '$path'."; |
| + } |
| + |
| + var matchingEntries = new Directory(parent).listSync() |
|
Bob Nystrom
2013/02/22 17:58:21
Yay listSync()!
|
| + .map((entry) => entry is File ? entry.fullPathSync() : entry.path) |
| + .where((entry) => entry.contains(pattern)) |
| + .toList(); |
| + |
| + if (matchingEntries.length == 0) { |
| + throw "No entry found in '$parent' matching ${describePattern(pattern)}."; |
| + } else if (matchingEntries.length > 1) { |
| + throw "Multiple entries found in '$parent' matching " |
| + "${describePattern(pattern)}:\n" |
| + "${matchingEntries.map((entry) => '* $entry').join('\n')}"; |
| + } else { |
| + return matchingEntries.first; |
| + } |
| +} |
| + |
| +/// Returns a human-readable description of [pattern]. |
| +String describePattern(Pattern pattern) { |
| + if (pattern is String) return "'$pattern'"; |
|
Bob Nystrom
2013/02/22 17:58:21
Do you care about escaping quotes here?
nweiz
2013/02/22 20:57:20
I'd *like* to escape enough stuff that it's a vali
|
| + if (pattern is! RegExp) return '$pattern'; |
| + |
| + var flags = new StringBuffer(); |
| + if (!pattern.isCaseSensitive) flags.add('i'); |
| + if (pattern.isMultiLine) flags.add('m'); |
| + return '/${pattern.pattern}/$flags'; |
| +} |