From 4a0d71c1c4422419c9539a2ecd9ccbeb674c08ef Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Mon, 9 Jan 2017 18:13:37 +0100 Subject: [PATCH] #N/A: Add ifconfig_device_not_found rule --- README.md | 1 + tests/rules/test_ifconfig_device_not_found.py | 53 +++++++++++++++++++ thefuck/rules/ifconfig_device_not_found.py | 25 +++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/rules/test_ifconfig_device_not_found.py create mode 100644 thefuck/rules/ifconfig_device_not_found.py diff --git a/README.md b/README.md index e0c42e94..db5a6f88 100644 --- a/README.md +++ b/README.md @@ -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`; diff --git a/tests/rules/test_ifconfig_device_not_found.py b/tests/rules/test_ifconfig_device_not_found.py new file mode 100644 index 00000000..e09daffc --- /dev/null +++ b/tests/rules/test_ifconfig_device_not_found.py @@ -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 diff --git a/thefuck/rules/ifconfig_device_not_found.py b/thefuck/rules/ifconfig_device_not_found.py new file mode 100644 index 00000000..f8692236 --- /dev/null +++ b/thefuck/rules/ifconfig_device_not_found.py @@ -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) + +