-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[xdg_directories] Migrate to null safety #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
7823389
7abc119
e9e7977
c734d91
b7d28d2
35ced39
dc06918
a1be179
0279a53
bb2b723
c1b4be7
a0277df
4e4f35d
eee3ac8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,15 +13,15 @@ import 'package:process/process.dart'; | |
|
|
||
| /// An override function used by the tests to override the environment variable | ||
| /// lookups using [xdgEnvironmentOverride]. | ||
| typedef EnvironmentAccessor = String Function(String envVar); | ||
| typedef EnvironmentAccessor = String? Function(String envVar); | ||
|
|
||
| /// A testing setter that replaces the real environment lookups with an override. | ||
| /// | ||
| /// Set to null to stop overriding. | ||
| /// | ||
| /// Only available to tests. | ||
| @visibleForTesting | ||
| set xdgEnvironmentOverride(EnvironmentAccessor override) { | ||
| set xdgEnvironmentOverride(EnvironmentAccessor? override) { | ||
| _xdgEnvironmentOverride = override; | ||
| _getenv = _xdgEnvironmentOverride ?? _productionGetEnv; | ||
| } | ||
|
|
@@ -31,8 +31,8 @@ set xdgEnvironmentOverride(EnvironmentAccessor override) { | |
| /// | ||
| /// Only available to tests. | ||
| @visibleForTesting | ||
| EnvironmentAccessor get xdgEnvironmentOverride => _xdgEnvironmentOverride; | ||
| EnvironmentAccessor _xdgEnvironmentOverride; | ||
| EnvironmentAccessor? get xdgEnvironmentOverride => _xdgEnvironmentOverride; | ||
| EnvironmentAccessor? _xdgEnvironmentOverride; | ||
| EnvironmentAccessor _productionGetEnv = | ||
| (String value) => Platform.environment[value]; | ||
| EnvironmentAccessor _getenv = _productionGetEnv; | ||
|
|
@@ -50,9 +50,9 @@ ProcessManager _processManager = const LocalProcessManager(); | |
|
|
||
| List<Directory> _directoryListFromEnvironment( | ||
| String envVar, List<Directory> fallback) { | ||
| assert(envVar != null); | ||
| assert(fallback != null); | ||
| final String value = _getenv(envVar); | ||
| ArgumentError.checkNotNull(envVar); | ||
| ArgumentError.checkNotNull(fallback); | ||
| final String? value = _getenv(envVar); | ||
| if (value == null || value.isEmpty) { | ||
| return fallback; | ||
| } | ||
|
|
@@ -63,9 +63,9 @@ List<Directory> _directoryListFromEnvironment( | |
| }).toList(); | ||
| } | ||
|
|
||
| Directory _directoryFromEnvironment(String envVar, String fallback) { | ||
| assert(envVar != null); | ||
| final String value = _getenv(envVar); | ||
| Directory? _directoryFromEnvironment(String envVar, String? fallback) { | ||
| ArgumentError.checkNotNull(envVar); | ||
| final String? value = _getenv(envVar); | ||
| if (value == null || value.isEmpty) { | ||
| if (fallback == null) { | ||
| return null; | ||
|
|
@@ -77,9 +77,9 @@ Directory _directoryFromEnvironment(String envVar, String fallback) { | |
|
|
||
| // Creates a Directory from a fallback path. | ||
| Directory _getDirectory(String subdir) { | ||
| assert(subdir != null); | ||
| ArgumentError.checkNotNull(subdir); | ||
| assert(subdir.isNotEmpty); | ||
| final String homeDir = _getenv('HOME'); | ||
| final String? homeDir = _getenv('HOME'); | ||
| if (homeDir == null || homeDir.isEmpty) { | ||
| throw StateError( | ||
| 'The "HOME" environment variable is not set. This package (and POSIX) ' | ||
|
|
@@ -93,7 +93,7 @@ Directory _getDirectory(String subdir) { | |
| /// `$XDG_CACHE_HOME`). | ||
| /// | ||
| /// Throws [StateError] if the HOME environment variable is not set. | ||
| Directory get cacheHome => | ||
| Directory? get cacheHome => | ||
| _directoryFromEnvironment('XDG_CACHE_HOME', '.cache'); | ||
|
|
||
| /// The list of preference-ordered base directories relative to | ||
|
|
@@ -112,7 +112,7 @@ 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 => | ||
| Directory? get configHome => | ||
| _directoryFromEnvironment('XDG_CONFIG_HOME', '.config'); | ||
|
|
||
| /// The list of preference-ordered base directories relative to | ||
|
|
@@ -130,15 +130,15 @@ List<Directory> get dataDirs { | |
| /// written. (Corresponds to `$XDG_DATA_HOME`). | ||
| /// | ||
| /// Throws [StateError] if the HOME environment variable is not set. | ||
| Directory get dataHome => | ||
| Directory? get dataHome => | ||
| _directoryFromEnvironment('XDG_DATA_HOME', '.local/share'); | ||
|
|
||
| /// The base directory relative to which user-specific runtime | ||
| /// files and other file objects should be placed. (Corresponds to | ||
| /// `$XDG_RUNTIME_DIR`). | ||
| /// | ||
| /// Throws [StateError] if the HOME environment variable is not set. | ||
| Directory get runtimeDir => _directoryFromEnvironment('XDG_RUNTIME_DIR', null); | ||
| Directory? get runtimeDir => _directoryFromEnvironment('XDG_RUNTIME_DIR', null); | ||
|
|
||
| /// Gets the xdg user directory named by `dirName`. | ||
| /// | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on a bug that came in recently, we should make |
||
|
|
@@ -147,7 +147,7 @@ Directory getUserDirectory(String dirName) { | |
| final ProcessResult result = _processManager.runSync( | ||
| <String>['xdg-user-dir', dirName], | ||
| includeParentEnvironment: true, | ||
| stdoutEncoding: Encoding.getByName('utf8'), | ||
| stdoutEncoding: Encoding.getByName('utf8') ?? systemEncoding, | ||
| ); | ||
| final String path = utf8.decode(result.stdout).split('\n')[0]; | ||
| return Directory(path); | ||
|
|
@@ -161,7 +161,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')); | ||
|
yash1200 marked this conversation as resolved.
Outdated
|
||
| List<String> contents; | ||
| try { | ||
| contents = configFile.readAsLinesSync(); | ||
|
|
@@ -172,11 +172,11 @@ Set<String> getUserDirectoryNames() { | |
| final RegExp dirRegExp = | ||
| RegExp(r'^\s*XDG_(?<dirname>[^=]*)_DIR\s*=\s*(?<dir>.*)\s*$'); | ||
| for (String line in contents) { | ||
| final RegExpMatch match = dirRegExp.firstMatch(line); | ||
| final RegExpMatch? match = dirRegExp.firstMatch(line); | ||
| if (match == null) { | ||
| continue; | ||
| } | ||
| result.add(match.namedGroup('dirname')); | ||
| result.add(match.namedGroup('dirname')!); | ||
| } | ||
| return result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| name: xdg_directories | ||
| description: A Dart package for reading XDG directory configuration information on Linux. | ||
| version: 0.1.2 | ||
| version: 1.0.0-nullsafety.0 | ||
|
yash1200 marked this conversation as resolved.
Outdated
|
||
| homepage: https://github.com/flutter/packages/tree/master/packages/xdg_directories | ||
|
|
||
| environment: | ||
| sdk: ">=2.3.0 <3.0.0" | ||
| sdk: ">=2.12.0-0 <3.0.0" | ||
|
|
||
| dependencies: | ||
| meta: ">=1.2.2 <2.0.0" | ||
| path: ">=1.6.4 <2.0.0" | ||
| process: ">=3.0.12 <5.0.0" | ||
| meta: ^1.3.0-nullsafety.6 | ||
| path: ^1.8.0-nullsafety.3 | ||
| process: ^4.0.0-nullsafety.4 | ||
|
|
||
| dev_dependencies: | ||
| mockito: ^4.1.1 | ||
| test: ^1.15.3 | ||
| mockito: ^5.0.0-nullsafety.1 | ||
| test: ^1.16.0-nullsafety.13 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,26 +14,26 @@ import 'package:xdg_directories/xdg_directories.dart' as xdg; | |
|
|
||
| void main() { | ||
| final Map<String, String> fakeEnv = <String, String>{}; | ||
| Directory tmpDir; | ||
| Directory? tmpDir; | ||
|
yash1200 marked this conversation as resolved.
Outdated
|
||
|
|
||
| String testPath(String subdir) => path.join(tmpDir.path, subdir); | ||
| String testPath(String subdir) => path.join(tmpDir!.path, subdir); | ||
|
|
||
| setUp(() { | ||
| tmpDir = Directory.systemTemp.createTempSync('xdg_test'); | ||
| fakeEnv.clear(); | ||
| fakeEnv['HOME'] = tmpDir.path; | ||
| fakeEnv['HOME'] = tmpDir!.path; | ||
| fakeEnv['XDG_CACHE_HOME'] = testPath('.test_cache'); | ||
| fakeEnv['XDG_CONFIG_DIRS'] = testPath('etc/test_xdg'); | ||
| fakeEnv['XDG_CONFIG_HOME'] = testPath('.test_config'); | ||
| fakeEnv['XDG_DATA_DIRS'] = | ||
| '${testPath('usr/local/test_share')}:${testPath('usr/test_share')}'; | ||
| fakeEnv['XDG_DATA_HOME'] = testPath('.local/test_share'); | ||
| fakeEnv['XDG_RUNTIME_DIR'] = testPath('.local/test_runtime'); | ||
| Directory(fakeEnv['XDG_CONFIG_HOME']).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_CACHE_HOME']).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_DATA_HOME']).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_RUNTIME_DIR']).createSync(recursive: true); | ||
| File(path.join(fakeEnv['XDG_CONFIG_HOME'], 'user-dirs.dirs')) | ||
| Directory(fakeEnv['XDG_CONFIG_HOME']!).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_CACHE_HOME']!).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_DATA_HOME']!).createSync(recursive: true); | ||
| Directory(fakeEnv['XDG_RUNTIME_DIR']!).createSync(recursive: true); | ||
| File(path.join(fakeEnv['XDG_CONFIG_HOME']!, 'user-dirs.dirs')) | ||
| .writeAsStringSync(r''' | ||
| XDG_DESKTOP_DIR="$HOME/Desktop" | ||
| XDG_DOCUMENTS_DIR="$HOME/Documents" | ||
|
|
@@ -44,12 +44,12 @@ 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]; | ||
|
yash1200 marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| tearDown(() { | ||
| if (tmpDir != null) { | ||
| tmpDir.deleteSync(recursive: true); | ||
| tmpDir!.deleteSync(recursive: true); | ||
| } | ||
| // Stop overriding the environment accessor. | ||
| xdg.xdgEnvironmentOverride = null; | ||
|
|
@@ -62,21 +62,21 @@ 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'))); | ||
| 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'))); | ||
|
yash1200 marked this conversation as resolved.
Outdated
|
||
| 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.runtimeDir.path, equals(testPath('.local/test_runtime'))); | ||
| 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'))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
|
||
| expectDirList(xdg.configDirs, <String>[testPath('etc/test_xdg')]); | ||
| expectDirList(xdg.dataDirs, <String>[ | ||
|
|
@@ -122,13 +122,13 @@ class FakeProcessManager extends Fake implements ProcessManager { | |
| @override | ||
| ProcessResult runSync( | ||
| List<dynamic> command, { | ||
| String workingDirectory, | ||
| Map<String, String> environment, | ||
| String? workingDirectory, | ||
| Map<String, String>? environment, | ||
| bool includeParentEnvironment = true, | ||
| bool runInShell = false, | ||
| Encoding stdoutEncoding = systemEncoding, | ||
| Encoding stderrEncoding = systemEncoding, | ||
| }) { | ||
| return ProcessResult(0, 0, expected[command[1]].codeUnits, <int>[]); | ||
| return ProcessResult(0, 0, expected[command[1]]!.codeUnits, <int>[]); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.