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

Commit

Permalink
Handle throwing fs.currentDirectory (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderso authored May 15, 2020
1 parent 873a6d6 commit d0a8c5a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 3.0.13

* Handle `currentDirectory` throwing an exception in `getExecutablePath()`.

#### 3.0.12

* Updated version constraint on intl.
Expand Down
9 changes: 8 additions & 1 deletion lib/src/interface/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ String getExecutablePath(
}) {
assert(_osToPathStyle[platform.operatingSystem] == fs.path.style.name);

workingDirectory ??= fs.currentDirectory.path;
try {
workingDirectory ??= fs.currentDirectory.path;
} on FileSystemException {
// The `currentDirectory` getter can throw a FileSystemException for example
// when the process doesn't have read/list permissions in each component of
// the cwd path. In this case, fall back on '.'.
workingDirectory ??= '.';
}
Context context =
new Context(style: fs.path.style, current: workingDirectory);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: process
version: 3.0.12
version: 3.0.13
authors:
- Todd Volkert <tvolkert@google.com>
- Michael Goderbauer <goderbauer@google.com>
Expand Down
37 changes: 37 additions & 0 deletions test/src/interface/common_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,34 @@ void main() {
platform: platform),
'C:\\\"Program Files\"\\bla.exe');
});

test('with absolute path when currentDirectory getter throws', () {
FileSystem fsNoCwd = MemoryFileSystemNoCwd(fs);
String command = fs.path.join(dir3.path, 'bla.exe');
String expectedPath = command;
fs.file(command).createSync();

String executablePath = getExecutablePath(
command,
null,
platform: platform,
fs: fsNoCwd,
);
_expectSamePath(executablePath, expectedPath);
});

test('with relative path when currentDirectory getter throws', () {
FileSystem fsNoCwd = MemoryFileSystemNoCwd(fs);
String command = fs.path.join('.', 'bla.exe');

String executablePath = getExecutablePath(
command,
null,
platform: platform,
fs: fsNoCwd,
);
expect(executablePath, isNull);
});
});

group('on Linux', () {
Expand Down Expand Up @@ -284,3 +312,12 @@ void _expectSamePath(String actual, String expected) {
expect(actual, isNotNull);
expect(actual.toLowerCase(), expected.toLowerCase());
}

class MemoryFileSystemNoCwd extends ForwardingFileSystem {
MemoryFileSystemNoCwd(FileSystem delegate) : super(delegate);

@override
Directory get currentDirectory {
throw FileSystemException('Access denied');
}
}

0 comments on commit d0a8c5a

Please sign in to comment.