Add helpers to make it easier to do stuff in the sandbox (#13)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81c5461..cbdb5cd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 1.1.0
+
+* Add a `path()` function that returns the a path within the sandbox directory.
+
+* Add `io` getters to `FileDescriptor` and `DirectoryDescriptor` that returns
+ `dart:io` `File` and `Directory` objects, respectively, within the sandbox
+ directory.
+
## 1.0.4
* Support test `1.x.x'.
diff --git a/lib/src/directory_descriptor.dart b/lib/src/directory_descriptor.dart
index 32b2e07..0b9f36f 100644
--- a/lib/src/directory_descriptor.dart
+++ b/lib/src/directory_descriptor.dart
@@ -27,6 +27,10 @@
/// This may be modified.
final List<Descriptor> contents;
+ /// Returns a `dart:io` [Directory] object that refers to this file within
+ /// [sandbox].
+ Directory get io => new Directory(p.join(sandbox, name));
+
DirectoryDescriptor(String name, Iterable<Descriptor> contents)
: contents = contents.toList(),
super(name);
diff --git a/lib/src/file_descriptor.dart b/lib/src/file_descriptor.dart
index 1d7a109..ef030f0 100644
--- a/lib/src/file_descriptor.dart
+++ b/lib/src/file_descriptor.dart
@@ -44,6 +44,10 @@
return new _MatcherFileDescriptor(name, contents);
}
+ /// Returns a `dart:io` [File] object that refers to this file within
+ /// [sandbox].
+ File get io => new File(p.join(sandbox, name));
+
/// Creates a new binary [FileDescriptor] with [name] that matches its binary
/// contents against [matcher].
///
diff --git a/lib/test_descriptor.dart b/lib/test_descriptor.dart
index 54649ab..fe27b92 100644
--- a/lib/test_descriptor.dart
+++ b/lib/test_descriptor.dart
@@ -2,6 +2,7 @@
// 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.
+import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'src/descriptor.dart';
@@ -9,6 +10,7 @@
import 'src/file_descriptor.dart';
import 'src/nothing_descriptor.dart';
import 'src/pattern_descriptor.dart';
+import 'src/sandbox.dart';
export 'src/descriptor.dart';
export 'src/directory_descriptor.dart';
@@ -70,3 +72,6 @@
/// constructs a [DirectoryDescriptor] descriptor.
PatternDescriptor dirPattern(Pattern name, [Iterable<Descriptor> contents]) =>
pattern(name, (realName) => dir(realName, contents));
+
+/// Returns [path] within the [sandbox] directory.
+String path(String path) => p.join(sandbox, path);
diff --git a/pubspec.yaml b/pubspec.yaml
index c304171..af77fab 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: test_descriptor
-version: 1.0.4
+version: 1.1.0
description: An API for defining and verifying directory structures.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/test_descriptor
diff --git a/test/directory_test.dart b/test/directory_test.dart
index 5507ea4..97dcacb 100644
--- a/test/directory_test.dart
+++ b/test/directory_test.dart
@@ -300,4 +300,8 @@
]).validate();
});
});
+
+ test("io refers to the directory within the sandbox", () {
+ expect(d.file('dir').io.path, equals(p.join(d.sandbox, 'dir')));
+ });
}
diff --git a/test/file_test.dart b/test/file_test.dart
index b3be840..70093c1 100644
--- a/test/file_test.dart
+++ b/test/file_test.dart
@@ -154,4 +154,8 @@
throwsUnsupportedError);
});
});
+
+ test("io refers to the file within the sandbox", () {
+ expect(d.file('name.txt').io.path, equals(p.join(d.sandbox, 'name.txt')));
+ });
}
diff --git a/test/sandbox_test.dart b/test/sandbox_test.dart
new file mode 100644
index 0000000..34c90ac
--- /dev/null
+++ b/test/sandbox_test.dart
@@ -0,0 +1,35 @@
+// Copyright (c) 2018, 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.
+
+@TestOn('vm')
+
+import 'dart:async';
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:path/path.dart' as p;
+import 'package:test/test.dart';
+
+import 'package:test_descriptor/test_descriptor.dart' as d;
+
+import 'utils.dart';
+
+void main() {
+ test("accessing the getter creates the directory", () {
+ expect(new Directory(d.sandbox).existsSync(), isTrue);
+ });
+
+ test("the directory is deleted after the test", () {
+ String sandbox;
+ addTearDown(() {
+ expect(new Directory(sandbox).existsSync(), isFalse);
+ });
+
+ sandbox = d.sandbox;
+ });
+
+ test("path() returns a path in the sandbox", () {
+ expect(d.path("foo"), equals(p.join(d.sandbox, "foo")));
+ });
+}