This repository was archived by the owner on Nov 1, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -80,7 +80,8 @@ String getSdkPath() => path.dirname(path.dirname(Platform.resolvedExecutable));
80
80
/// [XDG Base Directory Specification][1] on Linux and [File System Basics][2]
81
81
/// on Mac OS.
82
82
///
83
- /// Throws if `%APPDATA%` or `$HOME` is undefined.
83
+ /// Throws an [EnvironmentNotFoundException] if `%APPDATA%` or `$HOME` is needed
84
+ /// but undefined.
84
85
///
85
86
/// [1] : https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
86
87
/// [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 {
91
92
if (Platform .isWindows) {
92
93
final appdata = Platform .environment['APPDATA' ];
93
94
if (appdata == null ) {
94
- throw StateError ('Environment variable %APPDATA% is not defined!' );
95
+ throw EnvironmentNotFoundException (
96
+ 'Environment variable %APPDATA% is not defined!' );
95
97
}
96
98
return appdata;
97
99
}
@@ -118,7 +120,17 @@ String get _configHome {
118
120
String get _home {
119
121
final home = Platform .environment['HOME' ];
120
122
if (home == null ) {
121
- throw StateError ('Environment variable \$ HOME is not defined!' );
123
+ throw EnvironmentNotFoundException (
124
+ 'Environment variable \$ HOME is not defined!' );
122
125
}
123
126
return home;
124
127
}
128
+
129
+ class EnvironmentNotFoundException implements Exception {
130
+ final String message;
131
+ EnvironmentNotFoundException (this .message);
132
+ @override
133
+ String toString () {
134
+ return message;
135
+ }
136
+ }
Original file line number Diff line number Diff line change @@ -49,5 +49,21 @@ void defineTests() {
49
49
// just a dummy check that some part of the path exists.
50
50
expect (Directory (p.joinAll (path.take (2 ))).existsSync (), isTrue);
51
51
});
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
+ });
52
68
});
53
69
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments