From 272c8777d7c134bbf6bebdb4ce3b39b0315487b2 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Thu, 7 Oct 2021 13:43:06 +0000 Subject: [PATCH 1/3] Make applicationConfigHome failures catchable --- lib/cli_util.dart | 17 ++++++++++++++--- test/cli_util_test.dart | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index 0fdd211..b7c93ec 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -80,7 +80,7 @@ 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 IOException 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 @@ -91,7 +91,8 @@ String get _configHome { if (Platform.isWindows) { final appdata = Platform.environment['APPDATA']; if (appdata == null) { - throw StateError('Environment variable %APPDATA% is not defined!'); + throw _NoEnvironmentFoundException( + 'Environment variable %APPDATA% is not defined!'); } return appdata; } @@ -118,7 +119,17 @@ String get _configHome { String get _home { final home = Platform.environment['HOME']; if (home == null) { - throw StateError('Environment variable \$HOME is not defined!'); + throw _NoEnvironmentFoundException( + 'Environment variable \$HOME is not defined!'); } return home; } + +class _NoEnvironmentFoundException implements IOException { + final String message; + _NoEnvironmentFoundException(this.message); + @override + String toString() { + return message; + } +} diff --git a/test/cli_util_test.dart b/test/cli_util_test.dart index 32dcdfc..510e12d 100644 --- a/test/cli_util_test.dart +++ b/test/cli_util_test.dart @@ -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); + }); }); } From 6d4d6ce6528674892b7ffd1be5705aec7a9d49ce Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Thu, 7 Oct 2021 13:47:30 +0000 Subject: [PATCH 2/3] Add missing file --- test/print_config_home.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/print_config_home.dart diff --git a/test/print_config_home.dart b/test/print_config_home.dart new file mode 100644 index 0000000..901bc73 --- /dev/null +++ b/test/print_config_home.dart @@ -0,0 +1,11 @@ +import 'dart:io'; + +import 'package:cli_util/cli_util.dart'; + +void main() { + try { + print(applicationConfigHome('dart')); + } on IOException catch (e) { + print('Caught: $e'); + } +} From d47d5d124a77c0c357e29fa65e2769821e90e26a Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Mon, 11 Oct 2021 09:57:44 +0000 Subject: [PATCH 3/3] Don't use IOException --- lib/cli_util.dart | 11 ++++++----- test/print_config_home.dart | 4 +--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/cli_util.dart b/lib/cli_util.dart index b7c93ec..430c294 100644 --- a/lib/cli_util.dart +++ b/lib/cli_util.dart @@ -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 an IOException if `%APPDATA%` or `$HOME` is needed but 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 @@ -91,7 +92,7 @@ String get _configHome { if (Platform.isWindows) { final appdata = Platform.environment['APPDATA']; if (appdata == null) { - throw _NoEnvironmentFoundException( + throw EnvironmentNotFoundException( 'Environment variable %APPDATA% is not defined!'); } return appdata; @@ -119,15 +120,15 @@ String get _configHome { String get _home { final home = Platform.environment['HOME']; if (home == null) { - throw _NoEnvironmentFoundException( + throw EnvironmentNotFoundException( 'Environment variable \$HOME is not defined!'); } return home; } -class _NoEnvironmentFoundException implements IOException { +class EnvironmentNotFoundException implements Exception { final String message; - _NoEnvironmentFoundException(this.message); + EnvironmentNotFoundException(this.message); @override String toString() { return message; diff --git a/test/print_config_home.dart b/test/print_config_home.dart index 901bc73..7667ff7 100644 --- a/test/print_config_home.dart +++ b/test/print_config_home.dart @@ -1,11 +1,9 @@ -import 'dart:io'; - import 'package:cli_util/cli_util.dart'; void main() { try { print(applicationConfigHome('dart')); - } on IOException catch (e) { + } on EnvironmentNotFoundException catch (e) { print('Caught: $e'); } }