Skip to content

Commit

Permalink
⚠️ Remove settings param from rules match, get_new_command and …
Browse files Browse the repository at this point in the history
…`side_effect`
  • Loading branch information
nvbn committed Sep 7, 2015
1 parent 382eb8b commit df4d2cc
Show file tree
Hide file tree
Showing 154 changed files with 535 additions and 465 deletions.
12 changes: 7 additions & 5 deletions README.md
Expand Up @@ -226,20 +226,21 @@ For adding your own rule you should create `your-rule-name.py`
in `~/.thefuck/rules`. The rule should contain two functions:

```python
match(command: Command, settings: Settings) -> bool
get_new_command(command: Command, settings: Settings) -> str | list[str]
match(command: Command) -> bool
get_new_command(command: Command) -> str | list[str]
```

Also the rule can contain an optional function

```python
side_effect(old_command: Command, fixed_command: str, settings: Settings) -> None
side_effect(old_command: Command, fixed_command: str) -> None
```
and optional `enabled_by_default`, `requires_output` and `priority` variables.

`Command` has three attributes: `script`, `stdout` and `stderr`.

`Settings` is a special object filled with `~/.thefuck/settings.py` and values from env ([see more below](#settings)).
*Rules api changed in 3.0:* For accessing settings in rule you need to import it with `from thefuck.conf import settings`.
`settings` is a special object filled with `~/.thefuck/settings.py` and values from env ([see more below](#settings)).

Simple example of the rule for running script with `sudo`:

Expand All @@ -264,7 +265,8 @@ requires_output = True
```

[More examples of rules](https://github.com/nvbn/thefuck/tree/master/thefuck/rules),
[utility functions for rules](https://github.com/nvbn/thefuck/tree/master/thefuck/utils.py).
[utility functions for rules](https://github.com/nvbn/thefuck/tree/master/thefuck/utils.py),
[app/os-specific helpers](https://github.com/nvbn/thefuck/tree/master/thefuck/specific/).

## Settings

Expand Down
10 changes: 5 additions & 5 deletions tests/rules/test_apt_get.py
Expand Up @@ -11,7 +11,7 @@
@pytest.mark.parametrize('command', [
Command(script='vim', stderr='vim: command not found')])
def test_match(command):
assert match(command, None)
assert match(command)


@pytest.mark.parametrize('command, return_value', [
Expand All @@ -24,15 +24,15 @@ def test_match(command):
def test_match_mocked(cmdnf_mock, command, return_value):
get_packages = Mock(return_value=return_value)
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
assert match(command, None)
assert match(command)
assert cmdnf_mock.CommandNotFound.called
assert get_packages.called


@pytest.mark.parametrize('command', [
Command(script='vim', stderr=''), Command()])
def test_not_match(command):
assert not match(command, None)
assert not match(command)


# python-commandnotfound is available in ubuntu 14.04+
Expand All @@ -44,7 +44,7 @@ def test_not_match(command):
(Command('sudo vim'), 'sudo apt-get install vim && sudo vim'),
(Command('sudo convert'), 'sudo apt-get install imagemagick && sudo convert')])
def test_get_new_command(command, new_command):
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command


@pytest.mark.parametrize('command, new_command, return_value', [
Expand All @@ -63,4 +63,4 @@ def test_get_new_command(command, new_command):
def test_get_new_command_mocked(cmdnf_mock, command, new_command, return_value):
get_packages = Mock(return_value=return_value)
cmdnf_mock.CommandNotFound.return_value = Mock(getPackages=get_packages)
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command
6 changes: 3 additions & 3 deletions tests/rules/test_apt_get_search.py
Expand Up @@ -4,7 +4,7 @@


def test_match():
assert match(Command('apt-get search foo'), None)
assert match(Command('apt-get search foo'))


@pytest.mark.parametrize('command', [
Expand All @@ -18,8 +18,8 @@ def test_match():
Command('apt-get update')
])
def test_not_match(command):
assert not match(command, None)
assert not match(command)


def test_get_new_command():
assert get_new_command(Command('apt-get search foo'), None) == 'apt-cache search foo'
assert get_new_command(Command('apt-get search foo')) == 'apt-cache search foo'
6 changes: 3 additions & 3 deletions tests/rules/test_brew_install.py
Expand Up @@ -28,9 +28,9 @@ def _is_not_okay_to_test():
def test_match(brew_no_available_formula, brew_already_installed,
brew_install_no_argument):
assert match(Command('brew install elsticsearch',
stderr=brew_no_available_formula), None)
stderr=brew_no_available_formula))
assert not match(Command('brew install git',
stderr=brew_already_installed), None)
stderr=brew_already_installed))
assert not match(Command('brew install', stderr=brew_install_no_argument),
None)

Expand All @@ -39,7 +39,7 @@ def test_match(brew_no_available_formula, brew_already_installed,
reason='No need to run if there\'s no formula')
def test_get_new_command(brew_no_available_formula):
assert get_new_command(Command('brew install elsticsearch',
stderr=brew_no_available_formula), None)\
stderr=brew_no_available_formula))\
== 'brew install elasticsearch'

assert get_new_command(Command('brew install aa',
Expand Down
10 changes: 5 additions & 5 deletions tests/rules/test_brew_unknown_command.py
Expand Up @@ -15,15 +15,15 @@ def brew_unknown_cmd2():


def test_match(brew_unknown_cmd):
assert match(Command('brew inst', stderr=brew_unknown_cmd), None)
assert match(Command('brew inst', stderr=brew_unknown_cmd))
for command in _brew_commands():
assert not match(Command('brew ' + command), None)
assert not match(Command('brew ' + command))


def test_get_new_command(brew_unknown_cmd, brew_unknown_cmd2):
assert get_new_command(Command('brew inst', stderr=brew_unknown_cmd),
None) == ['brew list', 'brew install', 'brew uninstall']
assert get_new_command(Command('brew inst', stderr=brew_unknown_cmd)) \
== ['brew list', 'brew install', 'brew uninstall']

cmds = get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2), None)
cmds = get_new_command(Command('brew instaa', stderr=brew_unknown_cmd2))
assert 'brew install' in cmds
assert 'brew uninstall' in cmds
4 changes: 2 additions & 2 deletions tests/rules/test_brew_upgrade.py
Expand Up @@ -6,10 +6,10 @@
@pytest.mark.parametrize('command', [
Command(script='brew upgrade')])
def test_match(command):
assert match(command, None)
assert match(command)


@pytest.mark.parametrize('command, new_command', [
(Command('brew upgrade'), 'brew upgrade --all')])
def test_get_new_command(command, new_command):
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command
4 changes: 2 additions & 2 deletions tests/rules/test_cargo_no_command.py
Expand Up @@ -12,10 +12,10 @@
@pytest.mark.parametrize('command', [
Command(script='cargo buid', stderr=no_such_subcommand)])
def test_match(command):
assert match(command, None)
assert match(command)


@pytest.mark.parametrize('command, new_command', [
(Command('cargo buid', stderr=no_such_subcommand), 'cargo build')])
def test_get_new_command(command, new_command):
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command
6 changes: 3 additions & 3 deletions tests/rules/test_cd_mkdir.py
Expand Up @@ -9,17 +9,17 @@
stderr='cd: foo: No such file or directory'),
Command(script='cd foo/bar/baz', stderr='cd: can\'t cd to foo/bar/baz')])
def test_match(command):
assert match(command, None)
assert match(command)


@pytest.mark.parametrize('command', [
Command(script='cd foo', stderr=''), Command()])
def test_not_match(command):
assert not match(command, None)
assert not match(command)


@pytest.mark.parametrize('command, new_command', [
(Command('cd foo'), 'mkdir -p foo && cd foo'),
(Command('cd foo/bar/baz'), 'mkdir -p foo/bar/baz && cd foo/bar/baz')])
def test_get_new_command(command, new_command):
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command
6 changes: 3 additions & 3 deletions tests/rules/test_cd_parent.py
Expand Up @@ -3,10 +3,10 @@


def test_match():
assert match(Command('cd..', stderr='cd..: command not found'), None)
assert not match(Command(), None)
assert match(Command('cd..', stderr='cd..: command not found'))
assert not match(Command())


def test_get_new_command():
assert get_new_command(
Command('cd..'), None) == 'cd ..'
Command('cd..')) == 'cd ..'
13 changes: 6 additions & 7 deletions tests/rules/test_composer_not_command.py
Expand Up @@ -41,17 +41,16 @@ def composer_not_command_one_of_this():

def test_match(composer_not_command, composer_not_command_one_of_this):
assert match(Command('composer udpate',
stderr=composer_not_command), None)
stderr=composer_not_command))
assert match(Command('composer pdate',
stderr=composer_not_command_one_of_this), None)
assert not match(Command('ls update', stderr=composer_not_command),
None)
stderr=composer_not_command_one_of_this))
assert not match(Command('ls update', stderr=composer_not_command))


def test_get_new_command(composer_not_command, composer_not_command_one_of_this):
assert get_new_command(Command('composer udpate',
stderr=composer_not_command), None) \
stderr=composer_not_command)) \
== 'composer update'
assert get_new_command(
Command('composer pdate', stderr=composer_not_command_one_of_this),
None) == 'composer selfupdate'
Command('composer pdate', stderr=composer_not_command_one_of_this)) \
== 'composer selfupdate'
6 changes: 3 additions & 3 deletions tests/rules/test_cp_omitting_directory.py
Expand Up @@ -7,16 +7,16 @@
('cp dir', 'cp: dor: is a directory'),
('cp dir', "cp: omitting directory 'dir'")])
def test_match(script, stderr):
assert match(Command(script, stderr=stderr), None)
assert match(Command(script, stderr=stderr))


@pytest.mark.parametrize('script, stderr', [
('some dir', 'cp: dor: is a directory'),
('some dir', "cp: omitting directory 'dir'"),
('cp dir', '')])
def test_not_match(script, stderr):
assert not match(Command(script, stderr=stderr), None)
assert not match(Command(script, stderr=stderr))


def test_get_new_command():
assert get_new_command(Command(script='cp dir'), None) == 'cp -a dir'
assert get_new_command(Command(script='cp dir')) == 'cp -a dir'
6 changes: 3 additions & 3 deletions tests/rules/test_dirty_untar.py
Expand Up @@ -45,19 +45,19 @@ def reset(path):
@parametrize_script
def test_match(tar_error, filename, script, fixed):
tar_error(filename)
assert match(Command(script=script.format(filename)), None)
assert match(Command(script=script.format(filename)))


@parametrize_filename
@parametrize_script
def test_side_effect(tar_error, filename, script, fixed):
tar_error(filename)
side_effect(Command(script=script.format(filename)), None, None)
side_effect(Command(script=script.format(filename)), None)
assert(os.listdir('.') == [filename])


@parametrize_filename
@parametrize_script
def test_get_new_command(tar_error, filename, script, fixed):
tar_error(filename)
assert get_new_command(Command(script=script.format(filename)), None) == fixed.format(filename)
assert get_new_command(Command(script=script.format(filename))) == fixed.format(filename)
6 changes: 3 additions & 3 deletions tests/rules/test_dirty_unzip.py
Expand Up @@ -27,19 +27,19 @@ def reset(path):
'unzip foo',
'unzip foo.zip'])
def test_match(zip_error, script):
assert match(Command(script=script), None)
assert match(Command(script=script))


@pytest.mark.parametrize('script', [
'unzip foo',
'unzip foo.zip'])
def test_side_effect(zip_error, script):
side_effect(Command(script=script), None, None)
side_effect(Command(script=script), None)
assert(os.listdir('.') == ['foo.zip'])


@pytest.mark.parametrize('script,fixed', [
('unzip foo', 'unzip foo -d foo'),
('unzip foo.zip', 'unzip foo.zip -d foo')])
def test_get_new_command(zip_error, script, fixed):
assert get_new_command(Command(script=script), None) == fixed
assert get_new_command(Command(script=script)) == fixed
12 changes: 6 additions & 6 deletions tests/rules/test_django_south_ghost.py
Expand Up @@ -41,13 +41,13 @@ def stderr():


def test_match(stderr):
assert match(Command('./manage.py migrate', stderr=stderr), None)
assert match(Command('python manage.py migrate', stderr=stderr), None)
assert not match(Command('./manage.py migrate'), None)
assert not match(Command('app migrate', stderr=stderr), None)
assert not match(Command('./manage.py test', stderr=stderr), None)
assert match(Command('./manage.py migrate', stderr=stderr))
assert match(Command('python manage.py migrate', stderr=stderr))
assert not match(Command('./manage.py migrate'))
assert not match(Command('app migrate', stderr=stderr))
assert not match(Command('./manage.py test', stderr=stderr))


def test_get_new_command():
assert get_new_command(Command('./manage.py migrate auth'), None)\
assert get_new_command(Command('./manage.py migrate auth'))\
== './manage.py migrate auth --delete-ghost-migrations'
12 changes: 6 additions & 6 deletions tests/rules/test_django_south_merge.py
Expand Up @@ -31,13 +31,13 @@ def stderr():


def test_match(stderr):
assert match(Command('./manage.py migrate', stderr=stderr), None)
assert match(Command('python manage.py migrate', stderr=stderr), None)
assert not match(Command('./manage.py migrate'), None)
assert not match(Command('app migrate', stderr=stderr), None)
assert not match(Command('./manage.py test', stderr=stderr), None)
assert match(Command('./manage.py migrate', stderr=stderr))
assert match(Command('python manage.py migrate', stderr=stderr))
assert not match(Command('./manage.py migrate'))
assert not match(Command('app migrate', stderr=stderr))
assert not match(Command('./manage.py test', stderr=stderr))


def test_get_new_command():
assert get_new_command(Command('./manage.py migrate auth'), None) \
assert get_new_command(Command('./manage.py migrate auth')) \
== './manage.py migrate auth --merge'
6 changes: 3 additions & 3 deletions tests/rules/test_docker_not_command.py
Expand Up @@ -110,14 +110,14 @@ def stderr(cmd):


def test_match():
assert match(Command('docker pes', stderr=stderr('pes')), None)
assert match(Command('docker pes', stderr=stderr('pes')))


@pytest.mark.parametrize('script, stderr', [
('docker ps', ''),
('cat pes', stderr('pes'))])
def test_not_match(script, stderr):
assert not match(Command(script, stderr=stderr), None)
assert not match(Command(script, stderr=stderr))


@pytest.mark.usefixtures('docker_help')
Expand All @@ -126,4 +126,4 @@ def test_not_match(script, stderr):
('tags', ['tag', 'stats', 'images'])])
def test_get_new_command(wrong, fixed):
command = Command('docker {}'.format(wrong), stderr=stderr(wrong))
assert get_new_command(command, None) == ['docker {}'.format(x) for x in fixed]
assert get_new_command(command) == ['docker {}'.format(x) for x in fixed]
4 changes: 2 additions & 2 deletions tests/rules/test_dry.py
Expand Up @@ -7,11 +7,11 @@
Command(script='cd cd foo'),
Command(script='git git push origin/master')])
def test_match(command):
assert match(command, None)
assert match(command)


@pytest.mark.parametrize('command, new_command', [
(Command('cd cd foo'), 'cd foo'),
(Command('git git push origin/master'), 'git push origin/master')])
def test_get_new_command(command, new_command):
assert get_new_command(command, None) == new_command
assert get_new_command(command) == new_command
8 changes: 4 additions & 4 deletions tests/rules/test_fix_alt_space.py
Expand Up @@ -11,12 +11,12 @@ def test_match():
"""
assert match(Command(u'ps -ef | grep foo',
stderr=u'-bash:  grep: command not found'), None)
assert not match(Command('ps -ef | grep foo'), None)
assert not match(Command(), None)
stderr=u'-bash:  grep: command not found'))
assert not match(Command('ps -ef | grep foo'))
assert not match(Command())


def test_get_new_command():
""" Replace the Alt+Space character by a simple space """
assert get_new_command(Command(u'ps -ef | grep foo'), None)\
assert get_new_command(Command(u'ps -ef | grep foo'))\
== 'ps -ef | grep foo'

0 comments on commit df4d2cc

Please sign in to comment.