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

Commit 71ba36e

Browse files
authored
Make applicationConfigHome failures catchable (#68)
It is not always possible to resolve any environment variables - but we still might want to query for the directory if it exists.
1 parent 6872f39 commit 71ba36e

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

lib/cli_util.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
8080
/// [XDG Base Directory Specification][1] on Linux and [File System Basics][2]
8181
/// on Mac OS.
8282
///
83-
/// Throws if `%APPDATA%` or `$HOME` is undefined.
83+
/// Throws an [EnvironmentNotFoundException] if `%APPDATA%` or `$HOME` is needed
84+
/// but undefined.
8485
///
8586
/// [1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
8687
/// [2]: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1
@@ -91,7 +92,8 @@ String get _configHome {
9192
if (Platform.isWindows) {
9293
final appdata = Platform.environment['APPDATA'];
9394
if (appdata == null) {
94-
throw StateError('Environment variable %APPDATA% is not defined!');
95+
throw EnvironmentNotFoundException(
96+
'Environment variable %APPDATA% is not defined!');
9597
}
9698
return appdata;
9799
}
@@ -118,7 +120,17 @@ String get _configHome {
118120
String get _home {
119121
final home = Platform.environment['HOME'];
120122
if (home == null) {
121-
throw StateError('Environment variable \$HOME is not defined!');
123+
throw EnvironmentNotFoundException(
124+
'Environment variable \$HOME is not defined!');
122125
}
123126
return home;
124127
}
128+
129+
class EnvironmentNotFoundException implements Exception {
130+
final String message;
131+
EnvironmentNotFoundException(this.message);
132+
@override
133+
String toString() {
134+
return message;
135+
}
136+
}

test/cli_util_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,21 @@ void defineTests() {
4949
// just a dummy check that some part of the path exists.
5050
expect(Directory(p.joinAll(path.take(2))).existsSync(), isTrue);
5151
});
52+
53+
test('Throws IOException when run with empty environment', () {
54+
final scriptPath = p.join('test', 'print_config_home.dart');
55+
final result = Process.runSync(
56+
Platform.resolvedExecutable,
57+
[scriptPath],
58+
environment: {},
59+
includeParentEnvironment: false,
60+
);
61+
final varName = Platform.isWindows ? '%APPDATA%' : r'$HOME';
62+
expect(
63+
(result.stdout as String).trim(),
64+
'Caught: Environment variable $varName is not defined!',
65+
);
66+
expect(result.exitCode, 0);
67+
});
5268
});
5369
}

test/print_config_home.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:cli_util/cli_util.dart';
2+
3+
void main() {
4+
try {
5+
print(applicationConfigHome('dart'));
6+
} on EnvironmentNotFoundException catch (e) {
7+
print('Caught: $e');
8+
}
9+
}

0 commit comments

Comments
 (0)