mirror of
https://github.com/flutter/packages.git
synced 2025-07-03 09:08:54 +08:00
[xdg_directories] Return null from getUserDirectory
instead of throwing exception (#3033)
* fix: xdg.getUserDirectory to return null on missing executable * Update changelog * Fix review comments
This commit is contained in:
@ -1,3 +1,7 @@
|
|||||||
|
## 0.2.0+3
|
||||||
|
|
||||||
|
* Returns null instead of throwing exception from getUserDirectory when xdg-user-dir executable is missing.
|
||||||
|
|
||||||
## 0.2.0+2
|
## 0.2.0+2
|
||||||
|
|
||||||
* Fixes unit tests on Windows.
|
* Fixes unit tests on Windows.
|
||||||
|
@ -149,7 +149,12 @@ Directory? get runtimeDir => _directoryFromEnvironment('XDG_RUNTIME_DIR');
|
|||||||
/// Gets the xdg user directory named by `dirName`.
|
/// Gets the xdg user directory named by `dirName`.
|
||||||
///
|
///
|
||||||
/// Use [getUserDirectoryNames] to find out the list of available names.
|
/// Use [getUserDirectoryNames] to find out the list of available names.
|
||||||
|
///
|
||||||
|
/// If the `xdg-user-dir` executable is not present this returns null.
|
||||||
Directory? getUserDirectory(String dirName) {
|
Directory? getUserDirectory(String dirName) {
|
||||||
|
if (!_processManager.canRun('xdg-user-dir')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
final ProcessResult result = _processManager.runSync(
|
final ProcessResult result = _processManager.runSync(
|
||||||
<String>['xdg-user-dir', dirName],
|
<String>['xdg-user-dir', dirName],
|
||||||
stdoutEncoding: utf8,
|
stdoutEncoding: utf8,
|
||||||
|
@ -2,7 +2,7 @@ name: xdg_directories
|
|||||||
description: A Dart package for reading XDG directory configuration information on Linux.
|
description: A Dart package for reading XDG directory configuration information on Linux.
|
||||||
repository: https://github.com/flutter/packages/tree/main/packages/xdg_directories
|
repository: https://github.com/flutter/packages/tree/main/packages/xdg_directories
|
||||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+xdg_directories%22
|
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+xdg_directories%22
|
||||||
version: 0.2.0+2
|
version: 0.2.0+3
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.12.0 <3.0.0"
|
sdk: ">=2.12.0 <3.0.0"
|
||||||
|
@ -113,6 +113,16 @@ XDG_VIDEOS_DIR="$HOME/Videos"
|
|||||||
xdg.xdgProcessManager = const LocalProcessManager();
|
xdg.xdgProcessManager = const LocalProcessManager();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Returns null when xdg-user-dir executable is not present', () {
|
||||||
|
xdg.xdgProcessManager = FakeProcessManager(
|
||||||
|
<String, String>{},
|
||||||
|
canRunExecutable: false,
|
||||||
|
);
|
||||||
|
expect(xdg.getUserDirectory('DESKTOP'), isNull,
|
||||||
|
reason: 'Found xdg user directory without access to xdg-user-dir');
|
||||||
|
xdg.xdgProcessManager = const LocalProcessManager();
|
||||||
|
});
|
||||||
|
|
||||||
test('Throws StateError when HOME not set', () {
|
test('Throws StateError when HOME not set', () {
|
||||||
fakeEnv.clear();
|
fakeEnv.clear();
|
||||||
expect(() {
|
expect(() {
|
||||||
@ -122,9 +132,10 @@ XDG_VIDEOS_DIR="$HOME/Videos"
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FakeProcessManager extends Fake implements ProcessManager {
|
class FakeProcessManager extends Fake implements ProcessManager {
|
||||||
FakeProcessManager(this.expected);
|
FakeProcessManager(this.expected, {this.canRunExecutable = true});
|
||||||
|
|
||||||
Map<String, String> expected;
|
Map<String, String> expected;
|
||||||
|
final bool canRunExecutable;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ProcessResult runSync(
|
ProcessResult runSync(
|
||||||
@ -138,4 +149,9 @@ class FakeProcessManager extends Fake implements ProcessManager {
|
|||||||
}) {
|
}) {
|
||||||
return ProcessResult(0, 0, expected[command[1]], '');
|
return ProcessResult(0, 0, expected[command[1]], '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool canRun(dynamic executable, {String? workingDirectory}) {
|
||||||
|
return canRunExecutable;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user