-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add an exists method to FileSystem #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brianquinlan
wants to merge
6
commits into
dart-lang:main
Choose a base branch
from
brianquinlan:add-exists-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad4e4a2
feat: Add an exists method to FileSystem
google-labs-jules[bot] dcd46cd
Merge remote-tracking branch 'upstream/main' into add-exists-method
brianquinlan d0ccc25
Fixes
brianquinlan 970a480
Add missing
brianquinlan ec22a07
Update file_system.dart
brianquinlan b3f0e5d
Update README.md
brianquinlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
@TestOn('vm') | ||
library; | ||
|
||
import 'dart:io' as io; | ||
|
||
import 'package:io_file/io_file.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:test/test.dart'; | ||
|
||
import 'file_system_file_utils.dart' hide fileUtils; | ||
import 'test_utils.dart'; | ||
|
||
void tests(FileUtils utils, FileSystem fs) { | ||
late String tmp; | ||
late String cwd; | ||
|
||
setUp(() { | ||
tmp = utils.createTestDirectory('exists'); | ||
cwd = fs.currentDirectory; | ||
fs.currentDirectory = tmp; | ||
}); | ||
|
||
tearDown(() { | ||
fs.currentDirectory = cwd; | ||
utils.deleteDirectoryTree(tmp); | ||
}); | ||
|
||
test('file exists', () { | ||
final path = p.join(tmp, 'file'); | ||
utils.createTextFile(path, 'Hello World!'); | ||
|
||
expect(fs.exists(path), isTrue); | ||
}); | ||
|
||
test('directory exists', () { | ||
final path = p.join(tmp, 'dir'); | ||
utils.createDirectory(path); | ||
|
||
expect(fs.exists(path), isTrue); | ||
}); | ||
|
||
test('link to file exists', () { | ||
final filePath = p.join(tmp, 'file'); | ||
final linkPath = p.join(tmp, 'link'); | ||
utils.createTextFile(filePath, 'Hello World!'); | ||
io.Link(linkPath).createSync(filePath); | ||
|
||
expect(fs.exists(linkPath), isTrue); | ||
}); | ||
|
||
test('absolute path, long name', () { | ||
final path = p.join(tmp, 'f' * 255); | ||
utils.createTextFile(path, 'Hello World'); | ||
|
||
expect(fs.exists(path), isTrue); | ||
}); | ||
|
||
test('relative path, long name', () { | ||
final path = 'f' * 255; | ||
utils.createTextFile(path, 'Hello World'); | ||
|
||
expect(fs.exists(path), isTrue); | ||
}); | ||
|
||
test('link to directory exists', () { | ||
final dirPath = p.join(tmp, 'dir'); | ||
final linkPath = p.join(tmp, 'link'); | ||
utils.createDirectory(dirPath); | ||
io.Link(linkPath).createSync(dirPath); | ||
|
||
expect(fs.exists(linkPath), isTrue); | ||
}); | ||
|
||
test('broken link exists', () { | ||
final linkPath = p.join(tmp, 'link'); | ||
io.Link(linkPath).createSync('non-existent'); | ||
|
||
expect(fs.exists(linkPath), isFalse); | ||
}); | ||
|
||
test('path does not exist', () { | ||
final path = p.join(tmp, 'file'); | ||
|
||
expect(fs.exists(path), isFalse); | ||
}); | ||
} | ||
|
||
void main() { | ||
group('exists', () { | ||
group('dart:io verification', () => tests(fileUtils(), fileSystem)); | ||
group( | ||
'self verification', | ||
() => tests(FileSystemFileUtils(fileSystem), fileSystem), | ||
); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds unexpected: it is surprising that
exists
can cause change to any state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exists
callCreateFile
, which opens the file.dart:io
(sometimes) has the same behavior (and so does Python, Rust, etc.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about making
exists
not follow the symlink so that it doesn't need to open the file. But that does against the current behavior ofdart:io
(and Python and Rust...)