This commit is contained in:
nbe 2023-07-28 22:09:30 +03:00
parent 31d999f082
commit 63a4790e78
2 changed files with 11 additions and 18 deletions

View File

@ -1,6 +1,5 @@
import pytest
from thefuck.rules.nix_shell import get_nixpkgs_name, get_new_command
from thefuck.rules import nix_shell
from thefuck.types import Command
from unittest.mock import patch
@ -15,9 +14,8 @@ mocked_nixpkgs = {
"command, output", [("lsof", "lsof"), ("xev", "xorg.xev"), ("foo", "")]
)
def test_get_nixpkgs_name(command, output):
""" Check that `get_nixpkgs_name` returns the correct name """
"""Check that `get_nixpkgs_name` returns the correct name"""
nix_shell.nixpkgs_name = ""
with patch("subprocess.run") as mocked_run:
instance = mocked_run.return_value
instance.stderr = mocked_nixpkgs[command]
@ -25,15 +23,18 @@ def test_get_nixpkgs_name(command, output):
# check that flags and params are preserved for the new command
@pytest.mark.parametrize('command_script, new_command', [
('lsof -i :3000', 'nix-shell -p lsof --run "lsof -i :3000"'),
('xev', 'nix-shell -p xorg.xev --run "xev"')])
@pytest.mark.parametrize(
"command_script, new_command",
[
("lsof -i :3000", 'nix-shell -p lsof --run "lsof -i :3000"'),
("xev", 'nix-shell -p xorg.xev --run "xev"'),
],
)
def test_get_new_command(command_script, new_command):
""" Check that flags and params are preserved in the new command """
"""Check that flags and params are preserved in the new command"""
nix_shell.nixpkgs_name = ""
command = Command(command_script, '')
with patch('subprocess.run') as mocked_run:
command = Command(command_script, "")
with patch("subprocess.run") as mocked_run:
instance = mocked_run.return_value
instance.stderr = mocked_nixpkgs[command.script_parts[0]]
assert get_new_command(command) == new_command

View File

@ -8,20 +8,12 @@ enabled_by_default = nix_available
priority = 999
nixpkgs_name = ""
def get_nixpkgs_name(bin):
"""
Returns the name of the Nix package that provides the given binary. It uses the
`command-not-found` binary to do so, which is how nix-shell generates it's own suggestions.
"""
# Avoid getting the nixpkgs_name twice
global nixpkgs_name
if nixpkgs_name:
return nixpkgs_name
result = subprocess.run(
["command-not-found", bin], stderr=subprocess.PIPE, universal_newlines=True
)