-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat: new rule for nix-shell
#1393
Open
thenbe
wants to merge
6
commits into
nvbn:master
Choose a base branch
from
thenbe:nix-shell
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−0
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
31d999f
feat: new rule for `nix-shell`
thenbe 63a4790
simplify
thenbe d0ce1b3
annotate match conditions
thenbe 872c8c7
remove redundant comment
thenbe f17fec9
better unit tests
thenbe 6202d32
handle multiple suggestions
thenbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest | ||
from thefuck.rules.nix_shell import get_nixpkgs_name, get_new_command | ||
from thefuck.types import Command | ||
from unittest.mock import patch | ||
|
||
mocked_nixpkgs = { | ||
"lsof": "The program 'lsof' is not in your PATH. It is provided by several packages.\nYou can make it available in an ephemeral shell by typing one of the following:\n nix-shell -p busybox\n nix-shell -p lsof", | ||
"xev": "The program 'xev' is not in your PATH. You can make it available in an ephemeral shell by typing:\n nix-shell -p xorg.xev", | ||
"foo": "foo: command not found", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"command, output", [("lsof", "lsof"), ("xev", "xorg.xev"), ("foo", "")] | ||
) | ||
def test_get_nixpkgs_name(command, output): | ||
"""Check that `get_nixpkgs_name` returns the correct name""" | ||
|
||
with patch("subprocess.run") as mocked_run: | ||
instance = mocked_run.return_value | ||
instance.stderr = mocked_nixpkgs[command] | ||
assert 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"'), | ||
], | ||
) | ||
def test_get_new_command(command_script, new_command): | ||
"""Check that flags and params are preserved in the new command""" | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from thefuck.specific.nix import nix_available | ||
import subprocess | ||
|
||
enabled_by_default = nix_available | ||
|
||
# Set the priority just ahead of `fix_file` rule, which can generate low quality matches due | ||
# to the sheer amount of paths in the nix store. | ||
priority = 999 | ||
|
||
|
||
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. | ||
""" | ||
|
||
result = subprocess.run( | ||
["command-not-found", bin], stderr=subprocess.PIPE, universal_newlines=True | ||
) | ||
|
||
# return early if package is not available through nix | ||
if "nix-shell" not in result.stderr: | ||
return "" | ||
|
||
nixpkgs_name = result.stderr.split()[-1] if result.stderr.split() else "" | ||
return nixpkgs_name | ||
|
||
|
||
def match(command): | ||
bin = command.script_parts[0] | ||
return ( | ||
"nix-shell" not in command.script | ||
and "command not found" in command.output | ||
and get_nixpkgs_name(bin) | ||
) | ||
|
||
|
||
def get_new_command(command): | ||
bin = command.script_parts[0] | ||
return 'nix-shell -p {0} --run "{1}"'.format(get_nixpkgs_name(bin), command.script) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your example:
command not found
is not part of the output. Didn't you mean:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For commands that could be made available through nix:
command
to file, we do see "command not found":I'm not sure exactly why they're different, but it works 🤷🏼♂️.
Admittedly, it could be simpler if we could do:
But I couldn't find a way to access the numeric exit code (127) from within the
match
function. Only the human error message (command not found) is available it seems.Finally, I've annotated the match conditions to make it clear what my intentions are for each.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. That's because the command is executed through the shell —
/bin/sh
. Would you please run thefuck in debug mode and look for the line that saysDEBUG: Received output:
? e.g.:Among the debug messages there will be one with the output generated by the shell. Could you then please update the output values in
mocked_nixpkgs
?Also, how about writing tests for
match
? 😊There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I've cleaned up the PR a fair bit. As for the mock output, I've indicated which command I've used to get them so their purpose should be more clear now. If you notice that I've missed anything let me know. Thank you.