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

Commit fb5bce5

Browse files
authored
Fix Uri handling in FileSystem methods (#57)
1 parent 5beb596 commit fb5bce5

13 files changed

+67
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#### 2.3.2
2+
3+
* Fixed `FileSystem.directory(Uri)`, `FileSystem.file(Uri)`, and
4+
`FileSystem.link(Uri)` to consult the file system's path context when
5+
converting the URI to a file path rather than using `Uri.toFilePath()`.
6+
17
#### 2.3.1
28

39
* Fixed `MemoryFileSystem` to make `File.writeAs...()` update the last modified

lib/src/backends/chroot/chroot_directory.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,7 @@ class _ChrootDirectory extends _ChrootFileSystemEntity<Directory, io.Directory>
170170
}
171171
throw new FileSystemException('Unsupported type: $entity', entity.path);
172172
}
173+
174+
@override
175+
String toString() => "ChrootDirectory: '$path'";
173176
}

lib/src/backends/chroot/chroot_file.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
333333
encoding: encoding,
334334
flush: flush,
335335
);
336+
337+
@override
338+
String toString() => "ChrootFile: '$path'";
336339
}

lib/src/backends/chroot/chroot_file_system.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class ChrootFileSystem extends FileSystem {
6161

6262
@override
6363
Directory directory(dynamic path) =>
64-
new _ChrootDirectory(this, common.getPath(path));
64+
new _ChrootDirectory(this, getPath(path));
6565

6666
@override
67-
File file(dynamic path) => new _ChrootFile(this, common.getPath(path));
67+
File file(dynamic path) => new _ChrootFile(this, getPath(path));
6868

6969
@override
70-
Link link(dynamic path) => new _ChrootLink(this, common.getPath(path));
70+
Link link(dynamic path) => new _ChrootLink(this, getPath(path));
7171

7272
@override
7373
p.Context get path =>

lib/src/backends/chroot/chroot_link.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ class _ChrootLink extends _ChrootFileSystemEntity<Link, io.Link>
4141

4242
@override
4343
Link get absolute => new _ChrootLink(fileSystem, _absolutePath);
44+
45+
@override
46+
String toString() => "ChrootLink: '$path'";
4447
}

lib/src/backends/local/local_file_system.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class LocalFileSystem extends FileSystem {
1414

1515
@override
1616
Directory directory(dynamic path) =>
17-
new _LocalDirectory(this, shim.newDirectory(path));
17+
new _LocalDirectory(this, shim.newDirectory(getPath(path)));
1818

1919
@override
20-
File file(dynamic path) => new _LocalFile(this, shim.newFile(path));
20+
File file(dynamic path) => new _LocalFile(this, shim.newFile(getPath(path)));
2121

2222
@override
23-
Link link(dynamic path) => new _LocalLink(this, shim.newLink(path));
23+
Link link(dynamic path) => new _LocalLink(this, shim.newLink(getPath(path)));
2424

2525
@override
2626
p.Context get path => new p.Context();

lib/src/backends/memory/memory_file_system.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class MemoryFileSystem extends FileSystem {
6161

6262
@override
6363
Directory directory(dynamic path) =>
64-
new _MemoryDirectory(this, common.getPath(path));
64+
new _MemoryDirectory(this, getPath(path));
6565

6666
@override
67-
File file(dynamic path) => new _MemoryFile(this, common.getPath(path));
67+
File file(dynamic path) => new _MemoryFile(this, getPath(path));
6868

6969
@override
70-
Link link(dynamic path) => new _MemoryLink(this, common.getPath(path));
70+
Link link(dynamic path) => new _MemoryLink(this, getPath(path));
7171

7272
@override
7373
p.Context get path => new p.Context(style: p.Style.posix, current: _cwd);

lib/src/common.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'interface.dart';
6-
import 'io.dart' as io;
7-
8-
/// Gets the string path represented by the specified generic [path].
9-
String getPath(dynamic path) {
10-
if (path is io.FileSystemEntity) {
11-
return path.path;
12-
} else if (path is String) {
13-
return path;
14-
} else if (path is Uri) {
15-
return path.toFilePath();
16-
} else {
17-
throw new ArgumentError('Invalid type for "path": ${path?.runtimeType}');
18-
}
19-
}
206

217
/// Returns a 'No such file or directory' [FileSystemException].
228
FileSystemException noSuchFileOrDirectory(String path) {

lib/src/interface/file_system.dart

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

55
import 'dart:async';
66

7+
import 'package:meta/meta.dart';
78
import 'package:path/path.dart' as p;
89

910
import 'directory.dart';
@@ -144,4 +145,20 @@ abstract class FileSystem {
144145
/// [io.FileSystemEntityType.LINK].
145146
bool isLinkSync(String path) =>
146147
typeSync(path) == io.FileSystemEntityType.LINK;
148+
149+
/// Gets the string path represented by the specified generic [path].
150+
///
151+
/// [path] may be a [io.FileSystemEntity], a [String], or a [Uri].
152+
@protected
153+
String getPath(dynamic path) {
154+
if (path is io.FileSystemEntity) {
155+
return path.path;
156+
} else if (path is String) {
157+
return path;
158+
} else if (path is Uri) {
159+
return this.path.fromUri(path);
160+
} else {
161+
throw new ArgumentError('Invalid type for "path": ${path?.runtimeType}');
162+
}
163+
}
147164
}

lib/src/io/shim_dart_io.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
76
import 'dart:io' as io;
87

9-
import 'package:file/src/common.dart' as common;
10-
118
/// Creates a new [io.Directory] with the specified [path].
12-
io.Directory newDirectory(dynamic path) =>
13-
new io.Directory(common.getPath(path));
9+
io.Directory newDirectory(String path) => new io.Directory(path);
1410

1511
/// Creates a new [io.File] with the specified [path].
16-
io.File newFile(dynamic path) => new io.File(common.getPath(path));
12+
io.File newFile(String path) => new io.File(path);
1713

1814
/// Creates a new [io.Link] with the specified [path].
19-
io.Link newLink(dynamic path) => new io.Link(common.getPath(path));
15+
io.Link newLink(String path) => new io.Link(path);
2016

2117
/// Wraps [io.Directory.systemTemp].
2218
io.Directory systemTemp() => io.Directory.systemTemp;

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.3.1
2+
version: 2.3.2
33
authors:
44
- Matan Lurey <[email protected]>
55
- Yegor Jbanov <[email protected]>

test/common_tests.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ void runCommonTests(
158158
expect(fs.directory(Uri.parse('file:///')), isDirectory);
159159
});
160160

161+
test('succeedsWithUriArgument', () {
162+
fs.directory(ns('/foo')).createSync();
163+
Uri uri = Uri.parse('file://${ns('/foo')}${fs.path.separator}');
164+
expect(fs.directory(uri), exists);
165+
});
166+
161167
test('allowsDirectoryArgument', () {
162168
expect(fs.directory(new io.Directory(ns('/foo'))), isDirectory);
163169
});
@@ -176,6 +182,12 @@ void runCommonTests(
176182
expect(fs.file(Uri.parse('file:///')), isFile);
177183
});
178184

185+
test('succeedsWithUriArgument', () {
186+
fs.file(ns('/foo')).createSync();
187+
Uri uri = Uri.parse('file://${ns('/foo')}');
188+
expect(fs.file(uri), exists);
189+
});
190+
179191
test('allowsDirectoryArgument', () {
180192
expect(fs.file(new io.File(ns('/foo'))), isFile);
181193
});
@@ -194,6 +206,13 @@ void runCommonTests(
194206
expect(fs.link(Uri.parse('file:///')), isLink);
195207
});
196208

209+
test('succeedsWithUriArgument', () {
210+
fs.file(ns('/foo')).createSync();
211+
fs.link(ns('/bar')).createSync(ns('/foo'));
212+
Uri uri = Uri.parse('file://${ns('/bar')}');
213+
expect(fs.link(uri), exists);
214+
});
215+
197216
test('allowsDirectoryArgument', () {
198217
expect(fs.link(new io.File(ns('/foo'))), isLink);
199218
});

test/local_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ void main() {
4444

4545
Map<String, List<String>> skipOnPlatform = <String, List<String>>{
4646
'windows': <String>[
47+
'FileSystem > directory > succeedsWithUriArgument',
48+
'FileSystem > file > succeedsWithUriArgument',
49+
'FileSystem > link > succeedsWithUriArgument',
4750
'FileSystem > currentDirectory > throwsIfHasNonExistentPathInComplexChain',
4851
'FileSystem > currentDirectory > staysAtRootIfSetToParentOfRoot',
4952
'FileSystem > currentDirectory > resolvesLinksIfEncountered',

0 commit comments

Comments
 (0)