Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: pkg/scheduled_test/lib/src/descriptor/file.dart

Issue 12330062: Add a filesystem descriptor library to scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/scheduled_test/lib/src/descriptor/file.dart
diff --git a/pkg/scheduled_test/lib/src/descriptor/file.dart b/pkg/scheduled_test/lib/src/descriptor/file.dart
new file mode 100644
index 0000000000000000000000000000000000000000..98a002b1486dac758741029b7a8f52758bc70811
--- /dev/null
+++ b/pkg/scheduled_test/lib/src/descriptor/file.dart
@@ -0,0 +1,78 @@
+// 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 'dart:utf';
+
+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 single file.
+class File extends descriptor.Entry {
+ /// Whether this descriptor describes a binary file. This is only used when
+ /// displaying error messages.
+ final bool binary;
+
+ /// The contents of the file, in bytes.
+ final List<int> contents;
+
+ /// The contents of the file as a String. Assumes UTF-8 encoding.
+ String get textContents => new String.fromCharCodes(contents);
+
+ File.bytes(Pattern name, List<int> contents)
Bob Nystrom 2013/02/22 17:58:21 bytes -> binary The rest of the package seems to
nweiz 2013/02/22 20:57:20 Done.
+ : this._(name, contents, true);
+
+ File(Pattern name, String contents)
+ : this._(name, encodeUtf8(contents), false);
+
+ File._(Pattern name, this.contents, this.binary)
+ : super(name);
+
+ Future create([String parent]) => schedule(() {
+ if (parent == null) parent = descriptor.defaultRoot;
+ return new io.File(path.join(parent, stringName)).writeAsBytes(contents);
+ }, 'creating file $nameDescription');
+
+ Future validate([String parent]) => schedule(() {
+ if (parent == null) parent = descriptor.defaultRoot;
+ var fullPath = entryMatchingPattern(parent, name);
+ return new io.File(fullPath).readAsBytes()
+ .then((actualContents) {
+ if (orderedIterableEquals(contents, actualContents)) return;
+ if (binary) {
+ throw "File $nameDescription didn't contain the expected binary "
+ "data.";
Bob Nystrom 2013/02/22 17:58:21 If the data is pretty small, it might be nice to s
nweiz 2013/02/22 20:57:20 I'll add a TODO.
+ }
+ var description = nameDescription;
+ if (name is! String) {
+ description = "'${path.basename(fullPath)}' (matching $description)";
+ }
+ throw "File $description should contain:\n"
+ "${prefixLines(textContents)}\n"
+ "but actually contained:\n"
+ "${prefixLines(new String.fromCharCodes(actualContents))}";
Bob Nystrom 2013/02/22 17:58:21 Consider stealing the comparison code in test_pub
nweiz 2013/02/22 20:57:20 Done.
+ });
+ }, 'validating file $nameDescription');
+
+ Stream<List<int>> load([String pathToLoad]) {
+ if (pathToLoad != null) {
+ return errorStream("Can't load '$pathToLoad' from within "
+ "$nameDescription: not a directory.");
+ }
+
+ return new Future.immediate(contents).asStream();
+ }
+
+ String describe() {
+ if (name is String) return name;
+ return 'file matching $nameDescription';
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698