| 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..631d8734efb41cd8bca07f9fbde1a08aff425d18
|
| --- /dev/null
|
| +++ b/pkg/scheduled_test/lib/src/descriptor/directory.dart
|
| @@ -0,0 +1,93 @@
|
| +// 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:pathos/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 Iterable<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('Directory', parent, name);
|
| + return Future.wait(
|
| + contents.map((entry) => entry.validate(fullPath)).toList());
|
| + }, 'validating directory:\n${describe()}');
|
| +
|
| + Stream<List<int>> load(String pathToLoad) {
|
| + return futureStream(new Future.immediate(null).then((_) {
|
| + if (path.isAbsolute(pathToLoad)) {
|
| + throw "Can't load absolute path '$pathToLoad'.";
|
| + }
|
| +
|
| + var split = path.split(path.normalize(pathToLoad));
|
| + if (split.isEmpty || split.first == '.' || split.first == '..') {
|
| + throw "Can't load '$pathToLoad' from within $nameDescription.";
|
| + }
|
| +
|
| + var matchingEntries = contents.where((entry) =>
|
| + entry.stringName == split.first).toList();
|
| +
|
| + 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.read();
|
| + } else {
|
| + return matchingEntries.first.load(path.joinAll(remainingPath));
|
| + }
|
| + }
|
| + }));
|
| + }
|
| +
|
| + Stream<List<int>> read() => errorStream("Can't read the contents of "
|
| + "$nameDescription: is a directory.");
|
| +
|
| + String describe() {
|
| + var description = name;
|
| + if (name is! String) description = 'directory matching $nameDescription';
|
| + if (contents.isEmpty) return description;
|
| +
|
| + var buffer = new StringBuffer();
|
| + buffer.writeln(description);
|
| + for (var entry in contents.take(contents.length - 1)) {
|
| + var entryString = prefixLines(entry.describe(), prefix: '| ')
|
| + .replaceFirst('| ', '|-- ');
|
| + buffer.writeln(entryString);
|
| + }
|
| +
|
| + var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ')
|
| + .replaceFirst(' ', "'-- ");
|
| + buffer.add(lastEntryString);
|
| + return buffer.toString();
|
| + }
|
| +}
|
|
|