Visitar URL original
[xdg_directories] Migrate to null safety by yash1200 · Pull Request #250 · flutter/packages · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Made required changes
  • Loading branch information
yash1200 committed Jan 17, 2021
commit a1be179b35f7d53e2216c37a39b9130948f83e29
23 changes: 16 additions & 7 deletions packages/xdg_directories/lib/xdg_directories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ Directory? _directoryFromEnvironment(String envVar, String? fallback) {
return Directory(value);
}

Directory _directoryFromEnv(String envVar, String fallback) {
ArgumentError.checkNotNull(envVar);
final String? value = _getenv(envVar);
if (value == null || value.isEmpty) {
return _getDirectory(fallback);
}
return Directory(value);
}

// Creates a Directory from a fallback path.
Directory _getDirectory(String subdir) {
ArgumentError.checkNotNull(subdir);
Expand All @@ -93,8 +102,8 @@ Directory _getDirectory(String subdir) {
/// `$XDG_CACHE_HOME`).
///
/// Throws [StateError] if the HOME environment variable is not set.
Directory? get cacheHome =>
_directoryFromEnvironment('XDG_CACHE_HOME', '.cache');
Directory get cacheHome =>
_directoryFromEnv('XDG_CACHE_HOME', '.cache');

/// The list of preference-ordered base directories relative to
/// which configuration files should be searched. (Corresponds to
Expand All @@ -112,8 +121,8 @@ List<Directory> get configDirs {
/// configuration files should be written. (Corresponds to `$XDG_CONFIG_HOME`).
///
/// Throws [StateError] if the HOME environment variable is not set.
Directory? get configHome =>
_directoryFromEnvironment('XDG_CONFIG_HOME', '.config');
Directory get configHome =>
_directoryFromEnv('XDG_CONFIG_HOME', '.config');

/// The list of preference-ordered base directories relative to
/// which data files should be searched. (Corresponds to `$XDG_DATA_DIRS`).
Expand All @@ -130,8 +139,8 @@ List<Directory> get dataDirs {
/// written. (Corresponds to `$XDG_DATA_HOME`).
///
/// Throws [StateError] if the HOME environment variable is not set.
Directory? get dataHome =>
_directoryFromEnvironment('XDG_DATA_HOME', '.local/share');
Directory get dataHome =>
_directoryFromEnv('XDG_DATA_HOME', '.local/share');

/// The base directory relative to which user-specific runtime
/// files and other file objects should be placed. (Corresponds to
Expand Down Expand Up @@ -161,7 +170,7 @@ Directory getUserDirectory(String dirName) {
/// These are the names of the variables in "[configHome]/user-dirs.dirs", with
/// the `XDG_` prefix removed and the `_DIR` suffix removed.
Set<String> getUserDirectoryNames() {
final File configFile = File(path.join(configHome!.path, 'user-dirs.dirs'));
final File configFile = File(path.join(configHome.path, 'user-dirs.dirs'));
List<String> contents;
try {
contents = configFile.readAsLinesSync();
Expand Down
14 changes: 7 additions & 7 deletions packages/xdg_directories/test/xdg_directories_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ XDG_PUBLICSHARE_DIR="$HOME/Public"
XDG_TEMPLATES_DIR="$HOME/Templates"
XDG_VIDEOS_DIR="$HOME/Videos"
''');
xdg.xdgEnvironmentOverride = (String? key) => fakeEnv[key];
xdg.xdgEnvironmentOverride = (String key) => fakeEnv[key];
});

tearDown(() {
Expand All @@ -63,19 +63,19 @@ XDG_VIDEOS_DIR="$HOME/Videos"
test('Default fallback values work', () {
fakeEnv.clear();
fakeEnv['HOME'] = tmpDir!.path;
expect(xdg.cacheHome!.path, equals(testPath('.cache')));
expect(xdg.configHome!.path, equals(testPath('.config')));
expect(xdg.dataHome!.path, equals(testPath('.local/share')));
expect(xdg.cacheHome.path, equals(testPath('.cache')));
expect(xdg.configHome.path, equals(testPath('.config')));
expect(xdg.dataHome.path, equals(testPath('.local/share')));
expect(xdg.runtimeDir, isNull);

expectDirList(xdg.configDirs, <String>['/etc/xdg']);
expectDirList(xdg.dataDirs, <String>['/usr/local/share', '/usr/share']);
});

test('Values pull from environment', () {
expect(xdg.cacheHome!.path, equals(testPath('.test_cache')));
expect(xdg.configHome!.path, equals(testPath('.test_config')));
expect(xdg.dataHome!.path, equals(testPath('.local/test_share')));
expect(xdg.cacheHome.path, equals(testPath('.test_cache')));
expect(xdg.configHome.path, equals(testPath('.test_config')));
expect(xdg.dataHome.path, equals(testPath('.local/test_share')));
expect(xdg.runtimeDir!.path, equals(testPath('.local/test_runtime')));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ! is not correct; the API can return a null. You need to validate both that it's not null, and that it matches if it's not.


expectDirList(xdg.configDirs, <String>[testPath('etc/test_xdg')]);
Expand Down