Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Added convenience methods in Directory... (#46)
Browse files Browse the repository at this point in the history
... for creating child entities.
  • Loading branch information
tvolkert authored Feb 27, 2017
1 parent de508ca commit 8f5f853
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#### 2.3.0

* Added the following convenience methods in `Directory`:
* `Directory.childDirectory(String basename)`
* `Directory.childFile(String basename)`
* `Directory.childLink(String basename)`

#### 2.2.0

* Added `ErrorCodes` class, which holds errno values.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/backends/chroot/chroot_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
part of file.src.backends.chroot;

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

factory _ChrootDirectory.wrapped(
Expand Down
1 change: 1 addition & 0 deletions lib/src/backends/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library file.src.backends.local;

import 'dart:async';

import 'package:file/src/common.dart' as common;
import 'package:file/src/forwarding.dart';
import 'package:file/src/io.dart' as io;
import 'package:file/file.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/backends/local/local_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ part of file.src.backends.local;

class _LocalDirectory
extends _LocalFileSystemEntity<_LocalDirectory, io.Directory>
with ForwardingDirectory {
with ForwardingDirectory, common.DirectoryAddOnsMixin {
_LocalDirectory(FileSystem fs, io.Directory delegate) : super(fs, delegate);

@override
Expand Down
4 changes: 3 additions & 1 deletion lib/src/backends/memory/memory_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

part of file.src.backends.memory;

class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
class _MemoryDirectory extends _MemoryFileSystemEntity
with common.DirectoryAddOnsMixin
implements Directory {
static int _tempCounter = 0;

_MemoryDirectory(MemoryFileSystem fileSystem, String path)
Expand Down
10 changes: 10 additions & 0 deletions lib/src/backends/record_replay/recording_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class RecordingDirectory extends RecordingFileSystemEntity<Directory>
#createTempSync: _createTempSync,
#list: _list,
#listSync: _listSync,
#childDirectory: _childDirectory,
#childFile: _childFile,
#childLink: _childLink,
});
}

Expand Down Expand Up @@ -63,4 +66,11 @@ class RecordingDirectory extends RecordingFileSystemEntity<Directory>
}
throw new FileSystemException('Unsupported type: $entity', entity.path);
}

Directory _childDirectory(String basename) =>
wrapDirectory(delegate.childDirectory(basename));

File _childFile(String basename) => wrapFile(delegate.childFile(basename));

Link _childLink(String basename) => wrapLink(delegate.childLink(basename));
}
3 changes: 3 additions & 0 deletions lib/src/backends/record_replay/replay_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ReplayDirectory extends ReplayFileSystemEntity implements Directory {
#createTempSync: reviveDirectory,
#list: reviveEntities.fuse(const ToStream<FileSystemEntity>()),
#listSync: reviveEntities,
#childDirectory: reviveDirectory,
#childFile: new ReviveFile(fileSystem),
#childLink: new ReviveLink(fileSystem),
});

properties.addAll(<Symbol, Converter<dynamic, dynamic>>{
Expand Down
19 changes: 19 additions & 0 deletions lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,22 @@ FileSystemException badFileDescriptor(String path) {
FileSystemException _fsException(String path, String msg, int errorCode) {
return new FileSystemException(msg, path, new OSError(msg, errorCode));
}

/// Mixin containing implementations of [Directory] methods that are common
/// to all implementations.
abstract class DirectoryAddOnsMixin implements Directory {
@override
Directory childDirectory(String basename) {
return fileSystem.directory(fileSystem.path.join(path, basename));
}

@override
File childFile(String basename) {
return fileSystem.file(fileSystem.path.join(path, basename));
}

@override
Link childLink(String basename) {
return fileSystem.link(fileSystem.path.join(path, basename));
}
}
14 changes: 14 additions & 0 deletions lib/src/interface/directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import 'dart:async';

import 'file.dart';
import 'file_system_entity.dart';
import 'link.dart';
import '../io.dart' as io;

/// A reference to a directory on the file system.
Expand Down Expand Up @@ -35,4 +37,16 @@ abstract class Directory implements FileSystemEntity, io.Directory {
@override
List<FileSystemEntity> listSync(
{bool recursive: false, bool followLinks: true});

/// Returns a reference to a [Directory] that exists as a child of this
/// directory and has the specified [basename].
Directory childDirectory(String basename);

/// Returns a reference to a [File] that exists as a child of this directory
/// and has the specified [basename].
File childFile(String basename);

/// Returns a reference to a [Link] that exists as a child of this directory
/// and has the specified [basename].
Link childLink(String basename);
}
4 changes: 2 additions & 2 deletions lib/src/interface/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:async';

import 'package:path/path.dart' as path;
import 'package:path/path.dart' as p;

import 'directory.dart';
import 'file.dart';
Expand Down Expand Up @@ -38,7 +38,7 @@ abstract class FileSystem {
Link link(dynamic path);

/// An object for manipulating paths in this file system.
path.Context get path;
p.Context get path;

/// Gets the system temp directory.
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: file
version: 2.2.0
version: 2.3.0
authors:
- Matan Lurey <matanl@google.com>
- Yegor Jbanov <yjbanov@google.com>
Expand Down
10 changes: 10 additions & 0 deletions test/common_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,16 @@ void runCommonTests(
}
});
});

test('childEntities', () {
Directory dir = fs.directory(ns('/foo'))..createSync();
dir.childDirectory('bar').createSync();
dir.childFile('baz').createSync();
dir.childLink('qux').createSync('bar');
expect(fs.directory(ns('/foo/bar')), exists);
expect(fs.file(ns('/foo/baz')), exists);
expect(fs.link(ns('/foo/qux')), exists);
});
});

group('File', () {
Expand Down

0 comments on commit 8f5f853

Please sign in to comment.