From 004c0d06ebc875685115d74371fc9cba0ba6df4b Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 19 Aug 2015 17:12:54 +0100 Subject: [PATCH 1/4] starts up vagrant if not already running --- README.md | 1 + tests/rules/test_vagrant_up.py | 25 +++++++++++++++++++++++++ thefuck/rules/vagrant_up.py | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/rules/test_vagrant_up.py create mode 100644 thefuck/rules/vagrant_up.py diff --git a/README.md b/README.md index 806b6dcc..11378a71 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `tsuru_not_command` – fixes wrong `tsuru` commands like `tsuru shell`; * `tmux` – fixes `tmux` commands; * `unknown_command` – fixes hadoop hdfs-style "unknown command" for example adds missing '-' to the command on `hdfs dfs ls`; +* `vagrant_up` – starts up the vagrant instance; * `whois` – fixes `whois` command. Enabled by default only on specific platforms: diff --git a/tests/rules/test_vagrant_up.py b/tests/rules/test_vagrant_up.py new file mode 100644 index 00000000..f3b8e4b3 --- /dev/null +++ b/tests/rules/test_vagrant_up.py @@ -0,0 +1,25 @@ +import pytest +from thefuck.rules.vagrant_up import match, get_new_command +from tests.utils import Command + +@pytest.mark.parametrize('command', [ + Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), + Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), + Command(script='vagrant rdp', + stderr='VM must be created before running this command. Run `vagrant up` first.'), + Command(script='vagrant rdp devbox', + stderr='VM must be created before running this command. Run `vagrant up` first.')]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up && vagrant ssh'), + (Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up devbox && vagrant ssh devbox'), + (Command(script='vagrant rdp', + stderr='VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up && vagrant rdp'), + (Command(script='vagrant rdp devbox', + stderr='VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up devbox && vagrant rdp devbox')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command + diff --git a/thefuck/rules/vagrant_up.py b/thefuck/rules/vagrant_up.py new file mode 100644 index 00000000..44954a47 --- /dev/null +++ b/thefuck/rules/vagrant_up.py @@ -0,0 +1,12 @@ +from thefuck import shells + + +def match(command, settings): + return command.script.startswith('vagrant ') and 'run `vagrant up`' in command.stderr.lower() + +def get_new_command(command, settings): + cmds = command.script.split(' ') + machine = "" + if len(cmds) >= 3: + machine = cmds[2] + return "vagrant up " + machine + " && " + command.script From 7cb0388ed0845545e878b29783bbf8e901a02745 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 20 Aug 2015 09:41:01 +0100 Subject: [PATCH 2/4] Not matched unit tests, code style. --- tests/rules/test_vagrant_up.py | 9 +++++++++ thefuck/rules/vagrant_up.py | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/rules/test_vagrant_up.py b/tests/rules/test_vagrant_up.py index f3b8e4b3..0ede9712 100644 --- a/tests/rules/test_vagrant_up.py +++ b/tests/rules/test_vagrant_up.py @@ -13,6 +13,15 @@ def test_match(command): assert match(command, None) +@pytest.mark.parametrize('command', [ + Command(script='vagrant ssh', stderr=''), + Command(script='vagrant ssh jeff', stderr='The machine with the name \'jeff\' was not found configured for this Vagrant environment.'), + Command(script='vagrant ssh', stderr='A Vagrant environment or target machine is required to run this command. Run `vagrant init` to create a new Vagrant environment. Or, get an ID of a target machine from `vagrant global-status` to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.'), + Command()]) +def test_not_match(command): + assert not match(command, None) + + @pytest.mark.parametrize('command, new_command', [ (Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up && vagrant ssh'), (Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up devbox && vagrant ssh devbox'), diff --git a/thefuck/rules/vagrant_up.py b/thefuck/rules/vagrant_up.py index 44954a47..1c27db48 100644 --- a/thefuck/rules/vagrant_up.py +++ b/thefuck/rules/vagrant_up.py @@ -4,9 +4,10 @@ from thefuck import shells def match(command, settings): return command.script.startswith('vagrant ') and 'run `vagrant up`' in command.stderr.lower() + def get_new_command(command, settings): cmds = command.script.split(' ') machine = "" if len(cmds) >= 3: machine = cmds[2] - return "vagrant up " + machine + " && " + command.script + return shells.and_("vagrant up " + machine, command.script) From feb3eee2a08f0cba4552373d728509bc90b561ab Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 20 Aug 2015 10:06:41 +0100 Subject: [PATCH 3/4] Support for either starting only the machine requested, or starting all machines --- tests/rules/test_vagrant_up.py | 8 ++++---- thefuck/rules/vagrant_up.py | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/rules/test_vagrant_up.py b/tests/rules/test_vagrant_up.py index 0ede9712..b65e9e40 100644 --- a/tests/rules/test_vagrant_up.py +++ b/tests/rules/test_vagrant_up.py @@ -23,12 +23,12 @@ def test_not_match(command): @pytest.mark.parametrize('command, new_command', [ - (Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up && vagrant ssh'), - (Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up devbox && vagrant ssh devbox'), + (Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), 'vagrant up && vagrant ssh'), + (Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), ['vagrant up devbox && vagrant ssh devbox', 'vagrant up && vagrant ssh devbox']), (Command(script='vagrant rdp', - stderr='VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up && vagrant rdp'), + stderr='VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up && vagrant rdp'), (Command(script='vagrant rdp devbox', - stderr='VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up devbox && vagrant rdp devbox')]) + stderr='VM must be created before running this command. Run `vagrant up` first.'), ['vagrant up devbox && vagrant rdp devbox', 'vagrant up && vagrant rdp devbox'])]) def test_get_new_command(command, new_command): assert get_new_command(command, None) == new_command diff --git a/thefuck/rules/vagrant_up.py b/thefuck/rules/vagrant_up.py index 1c27db48..9c0a1e40 100644 --- a/thefuck/rules/vagrant_up.py +++ b/thefuck/rules/vagrant_up.py @@ -7,7 +7,12 @@ def match(command, settings): def get_new_command(command, settings): cmds = command.script.split(' ') - machine = "" + machine = None if len(cmds) >= 3: machine = cmds[2] - return shells.and_("vagrant up " + machine, command.script) + + startAllInstances = shells.and_("vagrant up", command.script) + if machine is None: + return startAllInstances + else: + return [ shells.and_("vagrant up " + machine, command.script), startAllInstances] From 336d8b7b4bc9dee67d2712672ced58e098660ec0 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 20 Aug 2015 11:37:49 +0100 Subject: [PATCH 4/4] Style change --- tests/rules/test_vagrant_up.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/rules/test_vagrant_up.py b/tests/rules/test_vagrant_up.py index b65e9e40..1a7c9022 100644 --- a/tests/rules/test_vagrant_up.py +++ b/tests/rules/test_vagrant_up.py @@ -2,6 +2,7 @@ import pytest from thefuck.rules.vagrant_up import match, get_new_command from tests.utils import Command + @pytest.mark.parametrize('command', [ Command(script='vagrant ssh', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'), Command(script='vagrant ssh devbox', stderr='VM must be running to open SSH connection. Run `vagrant up`\nto start the virtual machine.'),