| 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.directory; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import 'package:path/path.dart' as p; | |
| 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 descriptor describing a directory containing multiple files. | |
| 18 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor { | |
| 19 /// The entries contained within this directory. This is intentionally | |
| 20 /// mutable. | |
| 21 final List<Descriptor> contents; | |
| 22 | |
| 23 DirectoryDescriptor(String name, Iterable<Descriptor> contents) | |
| 24 : super(name), | |
| 25 contents = contents.toList(); | |
| 26 | |
| 27 /// Creates a directory descriptor named [name] based on a directory on the | |
| 28 /// physical directory at [path]. | |
| 29 /// | |
| 30 /// Note that reading from the filesystem isn't scheduled; it occurs as soon | |
| 31 /// as this descriptor is constructed. | |
| 32 DirectoryDescriptor.fromFilesystem(String name, String path) | |
| 33 : this(name, new Directory(path).listSync().map((entity) { | |
| 34 // Ignore hidden files. | |
| 35 if (p.basename(entity.path).startsWith(".")) return null; | |
| 36 | |
| 37 if (entity is Directory) { | |
| 38 return new DirectoryDescriptor.fromFilesystem( | |
| 39 p.basename(entity.path), entity.path); | |
| 40 } else if (entity is File) { | |
| 41 return new FileDescriptor.binary( | |
| 42 p.basename(entity.path), entity.readAsBytesSync()); | |
| 43 } | |
| 44 // Ignore broken symlinks. | |
| 45 }).where((path) => path != null)); | |
| 46 | |
| 47 Future create([String parent]) => schedule(() { | |
| 48 if (parent == null) parent = defaultRoot; | |
| 49 var fullPath = p.join(parent, name); | |
| 50 return Chain.track(new Directory(fullPath).create(recursive: true)) | |
| 51 .then((_) { | |
| 52 return Future.wait( | |
| 53 contents.map((entry) => entry.create(fullPath)).toList()); | |
| 54 }); | |
| 55 }, 'creating directory:\n${describe()}'); | |
| 56 | |
| 57 Future validate([String parent]) => schedule(() => validateNow(parent), | |
| 58 'validating directory:\n${describe()}'); | |
| 59 | |
| 60 Future validateNow([String parent]) { | |
| 61 if (parent == null) parent = defaultRoot; | |
| 62 var fullPath = p.join(parent, name); | |
| 63 if (!new Directory(fullPath).existsSync()) { | |
| 64 fail("Directory not found: '$fullPath'."); | |
| 65 } | |
| 66 | |
| 67 return Future.wait(contents.map((entry) { | |
| 68 return syncFuture(() => entry.validateNow(fullPath)) | |
| 69 .then((_) => null) | |
| 70 .catchError((e) => e); | |
| 71 })).then((results) { | |
| 72 var errors = results.where((e) => e != null); | |
| 73 if (errors.isEmpty) return; | |
| 74 throw _DirectoryValidationError.merge(errors); | |
| 75 }); | |
| 76 } | |
| 77 | |
| 78 Stream<List<int>> load(String pathToLoad) { | |
| 79 return futureStream(syncFuture(() { | |
| 80 if (p.posix.isAbsolute(pathToLoad)) { | |
| 81 throw new ArgumentError("Can't load absolute path '$pathToLoad'."); | |
| 82 } | |
| 83 | |
| 84 var split = p.posix.split(p.posix.normalize(pathToLoad)); | |
| 85 if (split.isEmpty || split.first == '.' || split.first == '..') { | |
| 86 throw new ArgumentError("Can't load '$pathToLoad' from within " | |
| 87 "'$name'."); | |
| 88 } | |
| 89 | |
| 90 var requiresReadable = split.length == 1; | |
| 91 var matchingEntries = contents.where((entry) { | |
| 92 return entry.name == split.first && (requiresReadable ? | |
| 93 entry is ReadableDescriptor : | |
| 94 entry is LoadableDescriptor); | |
| 95 }).toList(); | |
| 96 | |
| 97 var adjective = requiresReadable ? 'readable' : 'loadable'; | |
| 98 if (matchingEntries.length == 0) { | |
| 99 fail("Couldn't find a $adjective entry named '${split.first}' within " | |
| 100 "'$name'."); | |
| 101 } else if (matchingEntries.length > 1) { | |
| 102 fail("Found multiple $adjective entries named '${split.first}' within " | |
| 103 "'$name'."); | |
| 104 } else { | |
| 105 var remainingPath = split.sublist(1); | |
| 106 if (remainingPath.isEmpty) { | |
| 107 return (matchingEntries.first as ReadableDescriptor).read(); | |
| 108 } else { | |
| 109 return (matchingEntries.first as LoadableDescriptor) | |
| 110 .load(p.posix.joinAll(remainingPath)); | |
| 111 } | |
| 112 } | |
| 113 })); | |
| 114 } | |
| 115 | |
| 116 String describe() { | |
| 117 if (contents.isEmpty) return name; | |
| 118 | |
| 119 var buffer = new StringBuffer(); | |
| 120 buffer.writeln(name); | |
| 121 for (var entry in contents.take(contents.length - 1)) { | |
| 122 var entryString = prefixLines(entry.describe(), prefix: '| ') | |
| 123 .replaceFirst('| ', '|-- '); | |
| 124 buffer.writeln(entryString); | |
| 125 } | |
| 126 | |
| 127 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') | |
| 128 .replaceFirst(' ', "'-- "); | |
| 129 buffer.write(lastEntryString); | |
| 130 return buffer.toString(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 /// A class for formatting errors thrown by [DirectoryDescriptor]. | |
| 135 class _DirectoryValidationError { | |
| 136 final Iterable<String> errors; | |
| 137 | |
| 138 /// Flatten nested [_DirectoryValidationError]s in [errors] to create a single | |
| 139 /// list of errors. | |
| 140 static _DirectoryValidationError merge(Iterable errors) { | |
| 141 return new _DirectoryValidationError(errors.expand((error) { | |
| 142 if (error is _DirectoryValidationError) return error.errors; | |
| 143 return [error]; | |
| 144 })); | |
| 145 } | |
| 146 | |
| 147 _DirectoryValidationError(Iterable errors) | |
| 148 : errors = errors.map((e) => e.toString()).toList(); | |
| 149 | |
| 150 String toString() { | |
| 151 if (errors.length == 1) return errors.single; | |
| 152 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) | |
| 153 .join('\n'); | |
| 154 } | |
| 155 } | |
| OLD | NEW |