mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-18 20:11:17 +00:00
#N/A: Add ifconfig_device_not_found rule
This commit is contained in:
parent
a6f63c0568
commit
4a0d71c1c4
@ -199,6 +199,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
|
||||
* `has_exists_script` – prepends `./` when script/binary exists;
|
||||
* `heroku_not_command` – fixes wrong `heroku` commands like `heroku log`;
|
||||
* `history` – tries to replace command with most similar command from history;
|
||||
* `ifconfig_device_not_found` – fixes wrong device names like `wlan0` to `wlp2s0`;
|
||||
* `java` – removes `.java` extension when running Java programs;
|
||||
* `javac` – appends missing `.java` when compiling Java files;
|
||||
* `lein_not_task` – fixes wrong `lein` tasks like `lein rpl`;
|
||||
|
53
tests/rules/test_ifconfig_device_not_found.py
Normal file
53
tests/rules/test_ifconfig_device_not_found.py
Normal file
@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
from six import BytesIO
|
||||
from thefuck.rules.ifconfig_device_not_found import match, get_new_command
|
||||
from tests.utils import Command
|
||||
|
||||
|
||||
stderr = '{}: error fetching interface information: Device not found'
|
||||
|
||||
stdout = b'''
|
||||
wlp2s0 Link encap:Ethernet HWaddr 5c:51:4f:7c:58:5d
|
||||
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
|
||||
inet6 addr: fe80::be23:69b9:96d2:6d39/64 Scope:Link
|
||||
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
|
||||
RX packets:23581604 errors:0 dropped:0 overruns:0 frame:0
|
||||
TX packets:17017655 errors:0 dropped:0 overruns:0 carrier:0
|
||||
collisions:0 txqueuelen:1000
|
||||
RX bytes:16148429061 (16.1 GB) TX bytes:7067533695 (7.0 GB)
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def ifconfig(mocker):
|
||||
mock = mocker.patch(
|
||||
'thefuck.rules.ifconfig_device_not_found.subprocess.Popen')
|
||||
mock.return_value.stdout = BytesIO(stdout)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script, stderr', [
|
||||
('ifconfig wlan0', stderr.format('wlan0')),
|
||||
('ifconfig -s eth0', stderr.format('eth0')),
|
||||
])
|
||||
def test_match(script, stderr):
|
||||
assert match(Command(script, stderr=stderr))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script, stderr', [
|
||||
('config wlan0',
|
||||
'wlan0: error fetching interface information: Device not found'),
|
||||
('ifconfig eth0', ''),
|
||||
])
|
||||
def test_not_match(script, stderr):
|
||||
assert not match(Command(script, stderr=stderr))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script, result', [
|
||||
('ifconfig wlan0', ['ifconfig wlp2s0']),
|
||||
('ifconfig -s wlan0', ['ifconfig -s wlp2s0']),
|
||||
])
|
||||
def test_get_new_comman(script, result):
|
||||
new_command = get_new_command(
|
||||
Command(script, stderr=stderr.format('wlan0')))
|
||||
assert new_command == result
|
25
thefuck/rules/ifconfig_device_not_found.py
Normal file
25
thefuck/rules/ifconfig_device_not_found.py
Normal file
@ -0,0 +1,25 @@
|
||||
import subprocess
|
||||
from thefuck.utils import for_app, replace_command, eager
|
||||
import sys
|
||||
|
||||
@for_app('ifconfig')
|
||||
def match(command):
|
||||
return 'error fetching interface information: Device not found' \
|
||||
in command.stderr
|
||||
|
||||
|
||||
@eager
|
||||
def _get_possible_interfaces():
|
||||
proc = subprocess.Popen(['ifconfig', '-a'], stdout=subprocess.PIPE)
|
||||
for line in proc.stdout.readlines():
|
||||
line = line.decode()
|
||||
if line and line != '\n' and not line.startswith(' '):
|
||||
yield line.split(' ')[0]
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
interface = command.stderr.split(' ')[0][:-1]
|
||||
possible_interfaces = _get_possible_interfaces()
|
||||
return replace_command(command, interface, possible_interfaces)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user