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

Make applicationConfigHome failures catchable #68

Merged
merged 3 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/cli_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
/// [XDG Base Directory Specification][1] on Linux and [File System Basics][2]
/// on Mac OS.
///
/// Throws if `%APPDATA%` or `$HOME` is undefined.
/// Throws an [EnvironmentNotFoundException] if `%APPDATA%` or `$HOME` is needed
/// but undefined.
///
/// [1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
/// [2]: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1
Expand All @@ -91,7 +92,8 @@ String get _configHome {
if (Platform.isWindows) {
final appdata = Platform.environment['APPDATA'];
if (appdata == null) {
throw StateError('Environment variable %APPDATA% is not defined!');
throw EnvironmentNotFoundException(
'Environment variable %APPDATA% is not defined!');
}
return appdata;
}
Expand All @@ -118,7 +120,17 @@ String get _configHome {
String get _home {
final home = Platform.environment['HOME'];
if (home == null) {
throw StateError('Environment variable \$HOME is not defined!');
throw EnvironmentNotFoundException(
'Environment variable \$HOME is not defined!');
}
return home;
}

class EnvironmentNotFoundException implements Exception {
final String message;
EnvironmentNotFoundException(this.message);
@override
String toString() {
return message;
}
}
16 changes: 16 additions & 0 deletions test/cli_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,21 @@ void defineTests() {
// just a dummy check that some part of the path exists.
expect(Directory(p.joinAll(path.take(2))).existsSync(), isTrue);
});

test('Throws IOException when run with empty environment', () {
final scriptPath = p.join('test', 'print_config_home.dart');
final result = Process.runSync(
Platform.resolvedExecutable,
[scriptPath],
environment: {},
includeParentEnvironment: false,
);
final varName = Platform.isWindows ? '%APPDATA%' : r'$HOME';
expect(
(result.stdout as String).trim(),
'Caught: Environment variable $varName is not defined!',
);
expect(result.exitCode, 0);
});
});
}
9 changes: 9 additions & 0 deletions test/print_config_home.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:cli_util/cli_util.dart';

void main() {
try {
print(applicationConfigHome('dart'));
} on EnvironmentNotFoundException catch (e) {
print('Caught: $e');
}
}