| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library descriptor.directory; | 5 library descriptor.directory; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:stack_trace/stack_trace.dart'; |
| 11 | 12 |
| 12 import '../../descriptor.dart'; | 13 import '../../descriptor.dart'; |
| 13 import '../../scheduled_test.dart'; | 14 import '../../scheduled_test.dart'; |
| 14 import '../utils.dart'; | 15 import '../utils.dart'; |
| 15 | 16 |
| 16 /// A descriptor describing a directory containing multiple files. | 17 /// A descriptor describing a directory containing multiple files. |
| 17 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor { | 18 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor { |
| 18 /// The entries contained within this directory. This is intentionally | 19 /// The entries contained within this directory. This is intentionally |
| 19 /// mutable. | 20 /// mutable. |
| 20 final List<Descriptor> contents; | 21 final List<Descriptor> contents; |
| 21 | 22 |
| 22 DirectoryDescriptor(String name, Iterable<Descriptor> contents) | 23 DirectoryDescriptor(String name, Iterable<Descriptor> contents) |
| 23 : super(name), | 24 : super(name), |
| 24 contents = contents.toList(); | 25 contents = contents.toList(); |
| 25 | 26 |
| 26 Future create([String parent]) => schedule(() { | 27 Future create([String parent]) => schedule(() { |
| 27 if (parent == null) parent = defaultRoot; | 28 if (parent == null) parent = defaultRoot; |
| 28 var fullPath = path.join(parent, name); | 29 var fullPath = path.join(parent, name); |
| 29 return new Directory(fullPath).create(recursive: true).then((_) { | 30 return Chain.track(new Directory(fullPath).create(recursive: true)) |
| 31 .then((_) { |
| 30 return Future.wait( | 32 return Future.wait( |
| 31 contents.map((entry) => entry.create(fullPath)).toList()); | 33 contents.map((entry) => entry.create(fullPath)).toList()); |
| 32 }); | 34 }); |
| 33 }, 'creating directory:\n${describe()}'); | 35 }, 'creating directory:\n${describe()}'); |
| 34 | 36 |
| 35 Future validate([String parent]) => schedule(() => validateNow(parent), | 37 Future validate([String parent]) => schedule(() => validateNow(parent), |
| 36 'validating directory:\n${describe()}'); | 38 'validating directory:\n${describe()}'); |
| 37 | 39 |
| 38 Future validateNow([String parent]) { | 40 Future validateNow([String parent]) { |
| 39 if (parent == null) parent = defaultRoot; | 41 if (parent == null) parent = defaultRoot; |
| 40 var fullPath = path.join(parent, name); | 42 var fullPath = path.join(parent, name); |
| 41 if (!new Directory(fullPath).existsSync()) { | 43 if (!new Directory(fullPath).existsSync()) { |
| 42 fail("Directory not found: '$fullPath'."); | 44 fail("Directory not found: '$fullPath'."); |
| 43 } | 45 } |
| 44 | 46 |
| 45 return Future.wait(contents.map((entry) { | 47 return Future.wait(contents.map((entry) { |
| 46 return new Future.sync(() => entry.validateNow(fullPath)) | 48 return syncFuture(() => entry.validateNow(fullPath)) |
| 47 .then((_) => null) | 49 .then((_) => null) |
| 48 .catchError((e) => e); | 50 .catchError((e) => e); |
| 49 })).then((results) { | 51 })).then((results) { |
| 50 var errors = results.where((e) => e != null); | 52 var errors = results.where((e) => e != null); |
| 51 if (errors.isEmpty) return; | 53 if (errors.isEmpty) return; |
| 52 throw _DirectoryValidationError.merge(errors); | 54 throw _DirectoryValidationError.merge(errors); |
| 53 }); | 55 }); |
| 54 } | 56 } |
| 55 | 57 |
| 56 Stream<List<int>> load(String pathToLoad) { | 58 Stream<List<int>> load(String pathToLoad) { |
| 57 return futureStream(new Future.value().then((_) { | 59 return futureStream(syncFuture(() { |
| 58 if (path.posix.isAbsolute(pathToLoad)) { | 60 if (path.posix.isAbsolute(pathToLoad)) { |
| 59 throw new ArgumentError("Can't load absolute path '$pathToLoad'."); | 61 throw new ArgumentError("Can't load absolute path '$pathToLoad'."); |
| 60 } | 62 } |
| 61 | 63 |
| 62 var split = path.posix.split(path.posix.normalize(pathToLoad)); | 64 var split = path.posix.split(path.posix.normalize(pathToLoad)); |
| 63 if (split.isEmpty || split.first == '.' || split.first == '..') { | 65 if (split.isEmpty || split.first == '.' || split.first == '..') { |
| 64 throw new ArgumentError("Can't load '$pathToLoad' from within " | 66 throw new ArgumentError("Can't load '$pathToLoad' from within " |
| 65 "'$name'."); | 67 "'$name'."); |
| 66 } | 68 } |
| 67 | 69 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 126 |
| 125 _DirectoryValidationError(Iterable errors) | 127 _DirectoryValidationError(Iterable errors) |
| 126 : errors = errors.map((e) => e.toString()).toList(); | 128 : errors = errors.map((e) => e.toString()).toList(); |
| 127 | 129 |
| 128 String toString() { | 130 String toString() { |
| 129 if (errors.length == 1) return errors.single; | 131 if (errors.length == 1) return errors.single; |
| 130 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) | 132 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) |
| 131 .join('\n'); | 133 .join('\n'); |
| 132 } | 134 } |
| 133 } | 135 } |
| OLD | NEW |