Make `FileSystem.isLink` actually work (#214)
* Make `FileSystem.isLink` actually work
`FileSystem.isLink` called `FileSystem.type` with the default
argument of `followLinks: true`. This is obviously wrong since it
would mean that `isLink` would never return true (except maybe for
broken symbolic links).
Make `FileSystem.isLink` use `followLinks: false`. This is what
`dart:io`'s `FileSystemEntity.isLink` is documented to do.
Partially addresses https://github.com/google/file.dart/issues/8.
* Reorganize isFile/isDirectory/isLink tests
Reorganize the isFile/isDirectory/isLink tests so that the test names
are more appropriate.
diff --git a/packages/file/CHANGELOG.md b/packages/file/CHANGELOG.md
index e067af9..dd09a11 100644
--- a/packages/file/CHANGELOG.md
+++ b/packages/file/CHANGELOG.md
@@ -1,6 +1,7 @@
#### 7.0.0-wip
* Dart 3 fixes for class modifiers.
+* Fix `FileSystem.isLink`/`FileSystem.isLinkSync` to not follow symbolic links.
#### 6.1.4
diff --git a/packages/file/lib/src/interface/file_system.dart b/packages/file/lib/src/interface/file_system.dart
index da898a3..4fd528d 100644
--- a/packages/file/lib/src/interface/file_system.dart
+++ b/packages/file/lib/src/interface/file_system.dart
@@ -138,12 +138,12 @@
/// Checks if [`type(path)`](type) returns [io.FileSystemEntityType.LINK].
Future<bool> isLink(String path) async =>
- await type(path) == io.FileSystemEntityType.link;
+ await type(path, followLinks: false) == io.FileSystemEntityType.link;
/// Synchronously checks if [`type(path)`](type) returns
/// [io.FileSystemEntityType.LINK].
bool isLinkSync(String path) =>
- typeSync(path) == io.FileSystemEntityType.link;
+ typeSync(path, followLinks: false) == io.FileSystemEntityType.link;
/// Gets the string path represented by the specified generic [path].
///
diff --git a/packages/file/test/common_tests.dart b/packages/file/test/common_tests.dart
index 14010bd..679a90a 100644
--- a/packages/file/test/common_tests.dart
+++ b/packages/file/test/common_tests.dart
@@ -498,6 +498,46 @@
expect(type, FileSystemEntityType.notFound);
});
});
+
+ group('isFile/isDirectory/isLink', () {
+ late String filePath;
+ late String directoryPath;
+ late String fileLinkPath;
+ late String directoryLinkPath;
+
+ setUp(() {
+ filePath = ns('/file');
+ directoryPath = ns('/directory');
+ fileLinkPath = ns('/file-link');
+ directoryLinkPath = ns('/directory-link');
+
+ fs.file(filePath).createSync();
+ fs.directory(directoryPath).createSync();
+ fs.link(fileLinkPath).createSync(filePath);
+ fs.link(directoryLinkPath).createSync(directoryPath);
+ });
+
+ test('isFile', () {
+ expect(fs.isFileSync(filePath), true);
+ expect(fs.isFileSync(directoryPath), false);
+ expect(fs.isFileSync(fileLinkPath), true);
+ expect(fs.isFileSync(directoryLinkPath), false);
+ });
+
+ test('isDirectory', () {
+ expect(fs.isDirectorySync(filePath), false);
+ expect(fs.isDirectorySync(directoryPath), true);
+ expect(fs.isDirectorySync(fileLinkPath), false);
+ expect(fs.isDirectorySync(directoryLinkPath), true);
+ });
+
+ test('isLink', () {
+ expect(fs.isLinkSync(filePath), false);
+ expect(fs.isLinkSync(directoryPath), false);
+ expect(fs.isLinkSync(fileLinkPath), true);
+ expect(fs.isLinkSync(directoryLinkPath), true);
+ });
+ });
});
group('Directory', () {