From 48e1e4217fd112b89c409135466ea73e38071280 Mon Sep 17 00:00:00 2001 From: Tycho Grouwstra Date: Wed, 26 Jun 2019 20:01:02 +0200 Subject: [PATCH] support nixos command-not-found, closes #912 (#922) --- README.md | 1 + tests/rules/test_nixos_cmd_not_found.py | 25 +++++++++++++++++++++++++ thefuck/rules/nixos_cmd_not_found.py | 15 +++++++++++++++ thefuck/specific/nix.py | 3 +++ 4 files changed, 44 insertions(+) create mode 100644 tests/rules/test_nixos_cmd_not_found.py create mode 100644 thefuck/rules/nixos_cmd_not_found.py create mode 100644 thefuck/specific/nix.py diff --git a/README.md b/README.md index a6788565..26cf0d0f 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ The following rules are enabled by default on specific platforms only: * `brew_unknown_command` – fixes wrong brew commands, for example `brew docto/brew doctor`; * `brew_update_formula` – turns `brew update ` into `brew upgrade `; * `dnf_no_such_command` – fixes mistyped DNF commands; +* `nixos_cmd_not_found` – installs apps on NixOS; * `pacman` – installs app with `pacman` if it is not installed (uses `yay` or `yaourt` if available); * `pacman_not_found` – fixes package name with `pacman`, `yay` or `yaourt`. diff --git a/tests/rules/test_nixos_cmd_not_found.py b/tests/rules/test_nixos_cmd_not_found.py new file mode 100644 index 00000000..43703345 --- /dev/null +++ b/tests/rules/test_nixos_cmd_not_found.py @@ -0,0 +1,25 @@ +import pytest +from thefuck.rules.nixos_cmd_not_found import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('command', [ + Command('vim', 'nix-env -iA nixos.vim')]) +def test_match(mocker, command): + mocker.patch('thefuck.rules.nixos_cmd_not_found', return_value=None) + assert match(command) + + +@pytest.mark.parametrize('command', [ + Command('vim', ''), + Command('', '')]) +def test_not_match(mocker, command): + mocker.patch('thefuck.rules.nixos_cmd_not_found', return_value=None) + assert not match(command) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('vim', 'nix-env -iA nixos.vim'), 'nix-env -iA nixos.vim && vim'), + (Command('pacman', 'nix-env -iA nixos.pacman'), 'nix-env -iA nixos.pacman && pacman')]) +def test_get_new_command(mocker, command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/nixos_cmd_not_found.py b/thefuck/rules/nixos_cmd_not_found.py new file mode 100644 index 00000000..40d9d550 --- /dev/null +++ b/thefuck/rules/nixos_cmd_not_found.py @@ -0,0 +1,15 @@ +import re +from thefuck.specific.nix import nix_available +from thefuck.shells import shell + +regex = re.compile(r'nix-env -iA ([^\s]*)') +enabled_by_default = nix_available + + +def match(command): + return regex.findall(command.output) + + +def get_new_command(command): + name = regex.findall(command.output)[0] + return shell.and_('nix-env -iA {}'.format(name), command.script) diff --git a/thefuck/specific/nix.py b/thefuck/specific/nix.py new file mode 100644 index 00000000..87b1f1d7 --- /dev/null +++ b/thefuck/specific/nix.py @@ -0,0 +1,3 @@ +from thefuck.utils import which + +nix_available = bool(which('nix'))