Index: pkg/scheduled_test/lib/src/descriptor/directory.dart |
diff --git a/pkg/scheduled_test/lib/src/descriptor/directory.dart b/pkg/scheduled_test/lib/src/descriptor/directory.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb37546224dc3e52ed808d4131549b50ecd46a9f |
--- /dev/null |
+++ b/pkg/scheduled_test/lib/src/descriptor/directory.dart |
@@ -0,0 +1,92 @@ |
+// 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.file; |
+ |
+import 'dart:async'; |
+import 'dart:io' as io; |
+ |
+import 'package:path/path.dart' as path; |
+ |
+import '../../descriptor.dart' as descriptor; |
+import '../../scheduled_test.dart'; |
+import '../utils.dart'; |
+import 'utils.dart'; |
+ |
+/// A descriptor describing a directory containing multiple files. |
+class Directory extends descriptor.Entry { |
+ /// The entries contained within this directory. |
+ final List<descriptor.Entry> contents; |
+ |
+ Directory(Pattern name, this.contents) |
+ : super(name); |
+ |
+ Future create([String parent]) => schedule(() { |
+ if (parent == null) parent = descriptor.defaultRoot; |
+ var fullPath = path.join(parent, stringName); |
+ return new io.Directory(fullPath).create(recursive: true).then((_) { |
+ return Future.wait( |
+ contents.map((entry) => entry.create(fullPath)).toList()); |
+ }); |
+ }, 'creating directory:\n${describe()}'); |
+ |
+ Future validate([String parent]) => schedule(() { |
+ if (parent == null) parent = descriptor.defaultRoot; |
+ var fullPath = entryMatchingPattern(parent, name); |
+ return Future.wait( |
+ contents.map((entry) => entry.validate(fullPath)).toList()); |
+ }, 'validating directory:\n${describe()}'); |
+ |
+ Stream<List<int>> load([String pathToLoad]) { |
+ return liftStream(new Future.immediate(null).then((_) { |
+ if (pathToLoad == null) { |
+ throw "Can't load the contents of $nameDescription: is a directory."; |
+ } else if (path.isAbsolute(pathToLoad)) { |
+ throw "Can't load absolute path '$pathToLoad'."; |
+ } |
+ |
+ var split = path.split(path.normalize(pathToLoad)); |
+ if (split.first == '.' || split.first == '..') { |
+ throw "Can't load '$pathToLoad' from within $nameDescription."; |
+ } |
+ |
+ var matchingEntries = contents.where((entry) => |
+ entry.stringName == split.first); |
Bob Nystrom
2013/02/22 17:58:21
You might want a .toList() here since you use .len
nweiz
2013/02/22 20:57:20
Done.
|
+ |
+ if (matchingEntries.length == 0) { |
+ throw "Couldn't find an entry named '${split.first}' within " |
+ "$nameDescription."; |
+ } else if (matchingEntries.length > 1) { |
+ throw "Found multiple entries named '${split.first}' within " |
+ "$nameDescription."; |
+ } else { |
+ var remainingPath = split.getRange(1, split.length - 1); |
+ if (remainingPath.isEmpty) { |
+ return matchingEntries.first.load(); |
+ } else { |
+ return matchingEntries.first.load(path.joinAll(remainingPath)); |
+ } |
+ } |
+ })); |
+ } |
+ |
+ String describe() { |
+ var description = name; |
+ if (name is! String) description = 'directory matching $nameDescription'; |
+ if (contents.isEmpty) return description; |
+ |
+ var buffer = new StringBuffer(); |
+ buffer..add(description)..add('\n'); |
Bob Nystrom
2013/02/22 17:58:21
buffer.writeln(description);
and below
nweiz
2013/02/22 20:57:20
Done.
|
+ for (var entry in contents.take(contents.length - 1)) { |
+ var entryString = prefixLines(entry.describe(), prefix: '| ') |
+ .replaceFirst('| ', '|-- '); |
+ buffer..add(entryString)..add('\n'); |
+ } |
+ |
+ var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') |
+ .replaceFirst(' ', "'-- "); |
+ buffer.add(lastEntryString); |
Bob Nystrom
2013/02/22 17:58:21
May want a trailing newline here.
nweiz
2013/02/22 20:57:20
Nope. This method is like toString in that it's as
|
+ return buffer.toString(); |
+ } |
+} |