Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 8f5f853

Browse files
authored
Added convenience methods in Directory... (#46)
... for creating child entities.
1 parent de508ca commit 8f5f853

12 files changed

+72
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#### 2.3.0
2+
3+
* Added the following convenience methods in `Directory`:
4+
* `Directory.childDirectory(String basename)`
5+
* `Directory.childFile(String basename)`
6+
* `Directory.childLink(String basename)`
7+
18
#### 2.2.0
29

310
* Added `ErrorCodes` class, which holds errno values.

lib/src/backends/chroot/chroot_directory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
part of file.src.backends.chroot;
66

77
class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
8-
with ForwardingDirectory {
8+
with ForwardingDirectory, common.DirectoryAddOnsMixin {
99
_ChrootDirectory(ChrootFileSystem fs, String path) : super(fs, path);
1010

1111
factory _ChrootDirectory.wrapped(

lib/src/backends/local.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library file.src.backends.local;
66

77
import 'dart:async';
88

9+
import 'package:file/src/common.dart' as common;
910
import 'package:file/src/forwarding.dart';
1011
import 'package:file/src/io.dart' as io;
1112
import 'package:file/file.dart';

lib/src/backends/local/local_directory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ part of file.src.backends.local;
66

77
class _LocalDirectory
88
extends _LocalFileSystemEntity<_LocalDirectory, io.Directory>
9-
with ForwardingDirectory {
9+
with ForwardingDirectory, common.DirectoryAddOnsMixin {
1010
_LocalDirectory(FileSystem fs, io.Directory delegate) : super(fs, delegate);
1111

1212
@override

lib/src/backends/memory/memory_directory.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
part of file.src.backends.memory;
66

7-
class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
7+
class _MemoryDirectory extends _MemoryFileSystemEntity
8+
with common.DirectoryAddOnsMixin
9+
implements Directory {
810
static int _tempCounter = 0;
911

1012
_MemoryDirectory(MemoryFileSystem fileSystem, String path)

lib/src/backends/record_replay/recording_directory.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class RecordingDirectory extends RecordingFileSystemEntity<Directory>
2424
#createTempSync: _createTempSync,
2525
#list: _list,
2626
#listSync: _listSync,
27+
#childDirectory: _childDirectory,
28+
#childFile: _childFile,
29+
#childLink: _childLink,
2730
});
2831
}
2932

@@ -63,4 +66,11 @@ class RecordingDirectory extends RecordingFileSystemEntity<Directory>
6366
}
6467
throw new FileSystemException('Unsupported type: $entity', entity.path);
6568
}
69+
70+
Directory _childDirectory(String basename) =>
71+
wrapDirectory(delegate.childDirectory(basename));
72+
73+
File _childFile(String basename) => wrapFile(delegate.childFile(basename));
74+
75+
Link _childLink(String basename) => wrapLink(delegate.childLink(basename));
6676
}

lib/src/backends/record_replay/replay_directory.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class ReplayDirectory extends ReplayFileSystemEntity implements Directory {
3636
#createTempSync: reviveDirectory,
3737
#list: reviveEntities.fuse(const ToStream<FileSystemEntity>()),
3838
#listSync: reviveEntities,
39+
#childDirectory: reviveDirectory,
40+
#childFile: new ReviveFile(fileSystem),
41+
#childLink: new ReviveLink(fileSystem),
3942
});
4043

4144
properties.addAll(<Symbol, Converter<dynamic, dynamic>>{

lib/src/common.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,22 @@ FileSystemException badFileDescriptor(String path) {
6363
FileSystemException _fsException(String path, String msg, int errorCode) {
6464
return new FileSystemException(msg, path, new OSError(msg, errorCode));
6565
}
66+
67+
/// Mixin containing implementations of [Directory] methods that are common
68+
/// to all implementations.
69+
abstract class DirectoryAddOnsMixin implements Directory {
70+
@override
71+
Directory childDirectory(String basename) {
72+
return fileSystem.directory(fileSystem.path.join(path, basename));
73+
}
74+
75+
@override
76+
File childFile(String basename) {
77+
return fileSystem.file(fileSystem.path.join(path, basename));
78+
}
79+
80+
@override
81+
Link childLink(String basename) {
82+
return fileSystem.link(fileSystem.path.join(path, basename));
83+
}
84+
}

lib/src/interface/directory.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import 'dart:async';
66

7+
import 'file.dart';
78
import 'file_system_entity.dart';
9+
import 'link.dart';
810
import '../io.dart' as io;
911

1012
/// A reference to a directory on the file system.
@@ -35,4 +37,16 @@ abstract class Directory implements FileSystemEntity, io.Directory {
3537
@override
3638
List<FileSystemEntity> listSync(
3739
{bool recursive: false, bool followLinks: true});
40+
41+
/// Returns a reference to a [Directory] that exists as a child of this
42+
/// directory and has the specified [basename].
43+
Directory childDirectory(String basename);
44+
45+
/// Returns a reference to a [File] that exists as a child of this directory
46+
/// and has the specified [basename].
47+
File childFile(String basename);
48+
49+
/// Returns a reference to a [Link] that exists as a child of this directory
50+
/// and has the specified [basename].
51+
Link childLink(String basename);
3852
}

lib/src/interface/file_system.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:async';
66

7-
import 'package:path/path.dart' as path;
7+
import 'package:path/path.dart' as p;
88

99
import 'directory.dart';
1010
import 'file.dart';
@@ -38,7 +38,7 @@ abstract class FileSystem {
3838
Link link(dynamic path);
3939

4040
/// An object for manipulating paths in this file system.
41-
path.Context get path;
41+
p.Context get path;
4242

4343
/// Gets the system temp directory.
4444
///

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: file
2-
version: 2.2.0
2+
version: 2.3.0
33
authors:
44
- Matan Lurey <[email protected]>
55
- Yegor Jbanov <[email protected]>

test/common_tests.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,16 @@ void runCommonTests(
11011101
}
11021102
});
11031103
});
1104+
1105+
test('childEntities', () {
1106+
Directory dir = fs.directory(ns('/foo'))..createSync();
1107+
dir.childDirectory('bar').createSync();
1108+
dir.childFile('baz').createSync();
1109+
dir.childLink('qux').createSync('bar');
1110+
expect(fs.directory(ns('/foo/bar')), exists);
1111+
expect(fs.file(ns('/foo/baz')), exists);
1112+
expect(fs.link(ns('/foo/qux')), exists);
1113+
});
11041114
});
11051115

11061116
group('File', () {

0 commit comments

Comments
 (0)