Skip to content

Using FileSystem with IOOverrides from dart:io #2334

@jonasfj

Description

@jonasfj

package:file provides a nice abstract implementation of the file-system interfaces from dart:io. But it doesn't supply a means of running existing code (that uses dart:io) with an IOOverrides that delegates to the FileSystem.

I think an implementation might be as simple as:

import 'dart:io';

import 'package:file/file.dart' as f;

/// Creates an [IOOverrides] that uses [fs] for all operations.
IOOverrides createFileSystemIOOverrides(f.FileSystem fs) =>
    _FileSystemIOOverrides(fs);

/// An [IOOverrides] that uses a [f.FileSystem] for all operations.
final class _FileSystemIOOverrides extends IOOverrides {
  final f.FileSystem _fs;

  _FileSystemIOOverrides(this._fs);

  @override
  File createFile(String path) => _fs.file(path);

  @override
  Directory createDirectory(String path) => _fs.directory(path);

  @override
  Link createLink(String path) => _fs.link(path);

  @override
  Future<FileStat> stat(String path) => _fs.stat(path);

  @override
  FileStat statSync(String path) => _fs.statSync(path);

  @override
  Future<bool> fseIdentical(String path1, String path2) =>
      _fs.identical(path1, path2);

  @override
  bool fseIdenticalSync(String path1, String path2) =>
      _fs.identicalSync(path1, path2);

  @override
  Future<FileSystemEntityType> fseGetType(String path, bool followLinks) =>
      _fs.type(path, followLinks: followLinks);

  @override
  FileSystemEntityType fseGetTypeSync(String path, bool followLinks) =>
      _fs.typeSync(path, followLinks: followLinks);

  @override
  Directory getCurrentDirectory() => _fs.currentDirectory;

  @override
  void setCurrentDirectory(String path) {
    _fs.currentDirectory = path;
  }

  @override
  Directory getSystemTempDirectory() => _fs.systemTempDirectory;
}

Maybe, we could add a static FileSystem.runWithIOOverrides(FileSystem fs, ...) that wraps fs in as illustrated above and calls IOOverrides.runWithIOOverrides

We could also just do: abstract base class FileSystem extends IOOverrides, it's a bit of a breaking change. But entirely possible to fix the rest of the package around this.

It's also possible that we shouldn't put this logic into package:file. Because, this packages is about passing in a FileSystem instead of relying on IOOverrides.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions