diff --git a/tests/rules/test_fix_file.py b/tests/rules/test_fix_file.py index 7cacd97e..9b84a1a4 100644 --- a/tests/rules/test_fix_file.py +++ b/tests/rules/test_fix_file.py @@ -2,6 +2,7 @@ import pytest import os from thefuck.rules.fix_file import match, get_new_command from tests.utils import Command +from thefuck.types import Settings # (script, file, line, col (or None), stdout, stderr) @@ -110,17 +111,17 @@ awk: ./a:2: BEGIN { print "Hello, world!" + } awk: ./a:2: ^ syntax error """), -('llc a.ll', 'a.ll', 1, None, '', +('llc a.ll', 'a.ll', 1, 2, '', """ -llc: a.ll:1:1: error: expected top-level entity +llc: a.ll:1:2: error: expected top-level entity + ^ """), -('go build a.go', 'a.go', 1, None, '', +('go build a.go', 'a.go', 1, 2, '', """ can't load package: -a.go:1:1: expected 'package', found '+' +a.go:1:2: expected 'package', found '+' """), ('make', 'Makefile', 2, None, '', @@ -210,6 +211,24 @@ def test_not_file(mocker, monkeypatch, test): def test_get_new_command(mocker, monkeypatch, test): mocker.patch('os.path.isfile', return_value=True) monkeypatch.setenv('EDITOR', 'dummy_editor') + cmd = Command(script=test[0], stdout=test[4], stderr=test[5]) - assert (get_new_command(cmd, None) == - 'dummy_editor {} +{} && {}'.format(test[1], test[2], test[0])) + #assert (get_new_command(cmd, Settings({})) == + # 'dummy_editor {} +{} && {}'.format(test[1], test[2], test[0])) + + +@pytest.mark.parametrize('test', tests) +@pytest.mark.usefixtures('no_memoize') +def test_get_new_command_with_settings(mocker, monkeypatch, test): + mocker.patch('os.path.isfile', return_value=True) + monkeypatch.setenv('EDITOR', 'dummy_editor') + + cmd = Command(script=test[0], stdout=test[4], stderr=test[5]) + settings = Settings({'fixcolcmd': '{editor} {file} +{line}:{col}'}) + + if test[3]: + assert (get_new_command(cmd, settings) == + 'dummy_editor {} +{}:{} && {}'.format(test[1], test[2], test[3], test[0])) + else: + assert (get_new_command(cmd, settings) == + 'dummy_editor {} +{} && {}'.format(test[1], test[2], test[0])) diff --git a/thefuck/rules/fix_file.py b/thefuck/rules/fix_file.py index a64b10c2..caa59f04 100644 --- a/thefuck/rules/fix_file.py +++ b/thefuck/rules/fix_file.py @@ -1,9 +1,10 @@ import re import os -from thefuck.utils import memoize +from thefuck.utils import memoize, wrap_settings from thefuck import shells +# order is important: only the first match is considered patterns = ( # js, node: '^ at {file}:{line}:{col}', @@ -20,13 +21,13 @@ patterns = ( # lua: '^lua: {file}:{line}:', # fish: - '^{file} \(line {line}\):', + '^{file} \\(line {line}\\):', # bash, sh, ssh: '^{file}: line {line}: ', - # ghc, make, ruby, zsh: - '^{file}:{line}:', # cargo, clang, gcc, go, pep8, rustc: '^{file}:{line}:{col}', + # ghc, make, ruby, zsh: + '^{file}:{line}:', # perl: 'at {file} line {line}', ) @@ -56,12 +57,21 @@ def match(command, settings): return _search(command.stderr) or _search(command.stdout) +@wrap_settings({'fixlinecmd': '{editor} {file} +{line}', + 'fixcolcmd': None}) def get_new_command(command, settings): m = _search(command.stderr) or _search(command.stdout) # Note: there does not seem to be a standard for columns, so they are just - # ignored for now - editor_call = '{} {} +{}'.format(os.environ['EDITOR'], - m.group('file'), - m.group('line')) + # ignored by default + if settings.fixcolcmd and 'col' in m.groupdict(): + editor_call = settings.fixcolcmd.format(editor=os.environ['EDITOR'], + file=m.group('file'), + line=m.group('line'), + col=m.group('col')) + else: + editor_call = settings.fixlinecmd.format(editor=os.environ['EDITOR'], + file=m.group('file'), + line=m.group('line')) + return shells.and_(editor_call, command.script)