dart-lang / glob

Bash-style filename globbing for Dart.
https://pub.dev/packages/glob
BSD 3-Clause "New" or "Revised" License
56 stars 18 forks source link

Glob ignores Directory.current set on IOOverrides #52

Open shyndman opened 3 years ago

shyndman commented 3 years ago

Hi there,

Because Glob depends on the current working directory as determined by the path library, and path bypasses IOOverrides to determine this value (https://github.com/dart-lang/sdk/issues/39796), libraries using glob become more difficult to test when using mocked filesystems.

Giuspepe commented 1 year ago

I ran into the same problem, here is a minimum reproducible example:

import 'dart:io';

import 'package:glob/glob.dart';
import 'package:glob/list_local_fs.dart';
import 'package:path/path.dart';
import 'package:test/test.dart';

void main() {
  test('IOOverrides should be respected', () {
    final tempDir = Directory.systemTemp.createTempSync();
    addTearDown(() => tempDir.deleteSync(recursive: true));

    final files = [
      'foo.txt',
      'foo/bar/baz.txt',
    ].map((p) => File(join(tempDir.path, p)));
    final filePaths = files.map((e) => e.path);
    for (final file in files) {
      file.createSync(recursive: true);
    }

    IOOverrides.runZoned(
      () {
        final fileList =
            Directory.current.listSync(recursive: true).map((e) => e.path);
        expect(fileList, containsAll(filePaths));

        final glob = Glob('**');
        final globFileList = glob.listSync().map((e) => e.path);
        expect(globFileList, fileList);
      },
      getCurrentDirectory: () => tempDir,
    );
  });
}

If you replace glob.listSync() with glob.listSync(root: Directory.current.path) it works as expected.