From d2d74597fc144961c784f4285de5f68122942e05 Mon Sep 17 00:00:00 2001 From: mia-recki <93606650+mia-recki@users.noreply.github.com> Date: Fri, 10 Feb 2023 21:10:03 +0100 Subject: [PATCH] feat: store completions in `$XDG_CONFIG_HOME` instead of `$HOME` (#50) --- doc/README.md | 2 +- .../installer/completion_installation.dart | 12 +++-- .../completion_installation_test.dart | 45 +++++++++++++------ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/doc/README.md b/doc/README.md index 6de6420..8cd350b 100644 --- a/doc/README.md +++ b/doc/README.md @@ -10,7 +10,7 @@ It works on bash and zsh on Linux, macOS, and Windows. There are several ways to achieve shell completions for CLI commands. Most of them depend on which shell you are using and, in some cases, the terminal. -The approach used by `cli_completion` is to create shell script files in a directory under the user's home ( `~/.dart-cli-completion` for Linux/macOS users) and then source these scripts in the shell initialization files (`~/.zshrc`, for example). For Windows users, completion information is stored in local app data (typically something like `C:\Users\You\AppData\Local`). Currently, completion will only work from a bash shell on Windows. +The approach used by `cli_completion` is to create shell script files in a directory under the user's home ( `$XDG_CONFIG_HOME/.dart-cli-completion` or `~/.dart-cli-completion` for Linux/macOS users) and then source these scripts in the shell initialization files (`~/.zshrc`, for example). For Windows users, completion information is stored in local app data (typically something like `C:\Users\You\AppData\Local`). Currently, completion will only work from a bash shell on Windows. We call this process [installation](#the-installation-process). diff --git a/lib/src/installer/completion_installation.dart b/lib/src/installer/completion_installation.dart index 86b2115..5f6064f 100644 --- a/lib/src/installer/completion_installation.dart +++ b/lib/src/installer/completion_installation.dart @@ -72,7 +72,7 @@ class CompletionInstallation { /// %LOCALAPPDATA%/DartCLICompletion /// /// If [isWindows] is false, it will return the directory defined by - /// $HOME/.dart_cli_completion + /// $XDG_CONFIG_HOME/.dart_cli_completion or $HOME/.dart_cli_completion @visibleForTesting Directory get completionConfigDir { if (isWindows) { @@ -80,9 +80,13 @@ class CompletionInstallation { final localAppData = environment['LOCALAPPDATA']!; return Directory(path.join(localAppData, 'DartCLICompletion')); } else { - // Use home on posix systems - final home = environment['HOME']!; - return Directory(path.join(home, '.dart-cli-completion')); + // Try using XDG config folder + var dirPath = environment['XDG_CONFIG_HOME']; + // Fallback to $HOME if not following XDG specification + if (dirPath == null || dirPath.isEmpty) { + dirPath = environment['HOME']; + } + return Directory(path.join(dirPath!, '.dart-cli-completion')); } } diff --git a/test/src/installer/completion_installation_test.dart b/test/src/installer/completion_installation_test.dart index e5c8527..c57307c 100644 --- a/test/src/installer/completion_installation_test.dart +++ b/test/src/installer/completion_installation_test.dart @@ -73,20 +73,39 @@ void main() { ); }); - test('gets config dir location on posix', () { - final installation = CompletionInstallation( - configuration: zshConfiguration, - logger: logger, - isWindows: false, - environment: { - 'HOME': tempDir.path, - }, - ); + group('gets config dir location on posix', () { + test('respects XDG home', () { + final installation = CompletionInstallation( + configuration: zshConfiguration, + logger: logger, + isWindows: false, + environment: { + 'XDG_CONFIG_HOME': tempDir.path, + 'HOME': 'ooohnoooo', + }, + ); - expect( - installation.completionConfigDir.path, - path.join(tempDir.path, '.dart-cli-completion'), - ); + expect( + installation.completionConfigDir.path, + path.join(tempDir.path, '.dart-cli-completion'), + ); + }); + + test('defaults to home', () { + final installation = CompletionInstallation( + configuration: zshConfiguration, + logger: logger, + isWindows: false, + environment: { + 'HOME': tempDir.path, + }, + ); + + expect( + installation.completionConfigDir.path, + path.join(tempDir.path, '.dart-cli-completion'), + ); + }); }); });