diff --git a/tests/functional/plots.py b/tests/functional/plots.py index 3a007051..46eaf005 100644 --- a/tests/functional/plots.py +++ b/tests/functional/plots.py @@ -1,10 +1,16 @@ from pexpect import TIMEOUT +def _set_confirmation(proc, require): + proc.sendline(u'mkdir -p ~/.thefuck') + proc.sendline( + u'echo "require_confirmation = {}" > ~/.thefuck/settings.py'.format( + require)) + + def with_confirmation(proc): """Ensures that command can be fixed when confirmation enabled.""" - proc.sendline(u'mkdir -p ~/.thefuck') - proc.sendline(u'echo "require_confirmation = True" > ~/.thefuck/settings.py') + _set_confirmation(proc, True) proc.sendline(u'ehco test') @@ -17,10 +23,10 @@ def with_confirmation(proc): assert proc.expect([TIMEOUT, u'test']) -def history_changed(proc): +def history_changed(proc, to=u'echo test'): """Ensures that history changed.""" proc.send('\033[A') - assert proc.expect([TIMEOUT, u'echo test']) + assert proc.expect([TIMEOUT, to]) def history_not_changed(proc): @@ -31,8 +37,7 @@ def history_not_changed(proc): def refuse_with_confirmation(proc): """Ensures that fix can be refused when confirmation enabled.""" - proc.sendline(u'mkdir -p ~/.thefuck') - proc.sendline(u'echo "require_confirmation = True" > ~/.thefuck/settings.py') + _set_confirmation(proc, True) proc.sendline(u'ehco test') @@ -47,8 +52,7 @@ def refuse_with_confirmation(proc): def without_confirmation(proc): """Ensures that command can be fixed when confirmation disabled.""" - proc.sendline(u'mkdir -p ~/.thefuck') - proc.sendline(u'echo "require_confirmation = False" > ~/.thefuck/settings.py') + _set_confirmation(proc, False) proc.sendline(u'ehco test') diff --git a/tests/functional/test_bash.py b/tests/functional/test_bash.py index d36e9bb9..a40361cd 100644 --- a/tests/functional/test_bash.py +++ b/tests/functional/test_bash.py @@ -18,34 +18,29 @@ RUN pip2 install -U pip setuptools ''')) -@functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'bash') as proc: - proc.sendline(u"export PS1='$ '") - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'touch $HISTFILE') - with_confirmation(proc) - history_changed(proc) +@pytest.fixture(params=containers) +def proc(request): + tag, dockerfile = request.param + proc = spawn(request, tag, dockerfile, u'bash') + proc.sendline(u"export PS1='$ '") + proc.sendline(u'eval $(thefuck-alias)') + proc.sendline(u'touch $HISTFILE') + return proc @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_refuse_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'bash') as proc: - proc.sendline(u"export PS1='$ '") - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'touch $HISTFILE') - refuse_with_confirmation(proc) - history_not_changed(proc) +def test_with_confirmation(proc): + with_confirmation(proc) + history_changed(proc) @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_without_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'bash') as proc: - proc.sendline(u"export PS1='$ '") - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'touch $HISTFILE') - without_confirmation(proc) - history_changed(proc) +def test_refuse_with_confirmation(proc): + refuse_with_confirmation(proc) + history_not_changed(proc) + + +@functional +def test_without_confirmation(proc): + without_confirmation(proc) + history_changed(proc) diff --git a/tests/functional/test_fish.py b/tests/functional/test_fish.py index 89ad11ac..d465faf3 100644 --- a/tests/functional/test_fish.py +++ b/tests/functional/test_fish.py @@ -18,36 +18,33 @@ RUN pip2 install -U pip setuptools ''')) -@functional -@pytest.mark.skipif( - bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'fish') as proc: - proc.sendline(u'thefuck-alias > ~/.config/fish/config.fish') - proc.sendline(u'fish') - with_confirmation(proc) +@pytest.fixture(params=containers) +def proc(request): + tag, dockerfile = request.param + proc = spawn(request, tag, dockerfile, u'fish') + proc.sendline(u'thefuck-alias > ~/.config/fish/config.fish') + proc.sendline(u'fish') + return proc @functional @pytest.mark.skipif( bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_refuse_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'fish') as proc: - proc.sendline(u'thefuck-alias > ~/.config/fish/config.fish') - proc.sendline(u'fish') - refuse_with_confirmation(proc) +def test_with_confirmation(proc): + with_confirmation(proc) @functional @pytest.mark.skipif( bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_without_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'fish') as proc: - proc.sendline(u'thefuck-alias > ~/.config/fish/config.fish') - proc.sendline(u'fish') - without_confirmation(proc) +def test_refuse_with_confirmation(proc): + refuse_with_confirmation(proc) + + +@functional +@pytest.mark.skipif( + bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') +def test_without_confirmation(proc): + without_confirmation(proc) # TODO: ensure that history changes. diff --git a/tests/functional/test_tcsh.py b/tests/functional/test_tcsh.py index 674c106b..6dd926cc 100644 --- a/tests/functional/test_tcsh.py +++ b/tests/functional/test_tcsh.py @@ -18,30 +18,27 @@ RUN pip2 install -U pip setuptools ''')) -@functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'tcsh') as proc: - proc.sendline(u'tcsh') - proc.sendline(u'eval `thefuck-alias`') - with_confirmation(proc) +@pytest.fixture(params=containers) +def proc(request): + tag, dockerfile = request.param + proc = spawn(request, tag, dockerfile, u'tcsh') + proc.sendline(u'tcsh') + proc.sendline(u'eval `thefuck-alias`') + return proc @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_refuse_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'tcsh') as proc: - proc.sendline(u'tcsh') - proc.sendline(u'eval `thefuck-alias`') - refuse_with_confirmation(proc) +def test_with_confirmation(proc): + with_confirmation(proc) @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_without_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'tcsh') as proc: - proc.sendline(u'tcsh') - proc.sendline(u'eval `thefuck-alias`') - without_confirmation(proc) +def test_refuse_with_confirmation(proc): + refuse_with_confirmation(proc) + + +@functional +def test_without_confirmation(proc): + without_confirmation(proc) # TODO: ensure that history changes. diff --git a/tests/functional/test_zsh.py b/tests/functional/test_zsh.py index da46d94a..0201fa36 100644 --- a/tests/functional/test_zsh.py +++ b/tests/functional/test_zsh.py @@ -18,34 +18,29 @@ RUN pip2 install -U pip setuptools ''')) -@functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'zsh') as proc: - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'export HISTFILE=~/.zsh_history') - proc.sendline(u'touch $HISTFILE') - with_confirmation(proc) - history_changed(proc) +@pytest.fixture(params=containers) +def proc(request): + tag, dockerfile = request.param + proc = spawn(request, tag, dockerfile, u'zsh') + proc.sendline(u'eval $(thefuck-alias)') + proc.sendline(u'export HISTFILE=~/.zsh_history') + proc.sendline(u'touch $HISTFILE') + return proc @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_refuse_with_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'zsh') as proc: - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'export HISTFILE=~/.zsh_history') - proc.sendline(u'touch $HISTFILE') - refuse_with_confirmation(proc) - history_not_changed(proc) +def test_with_confirmation(proc): + with_confirmation(proc) + history_changed(proc) @functional -@pytest.mark.parametrize('tag, dockerfile', containers) -def test_without_confirmation(tag, dockerfile): - with spawn(tag, dockerfile, u'zsh') as proc: - proc.sendline(u'eval $(thefuck-alias)') - proc.sendline(u'export HISTFILE=~/.zsh_history') - proc.sendline(u'touch $HISTFILE') - without_confirmation(proc) - history_changed(proc) +def test_refuse_with_confirmation(proc): + refuse_with_confirmation(proc) + history_not_changed(proc) + + +@functional +def test_without_confirmation(proc): + without_confirmation(proc) + history_changed(proc) diff --git a/tests/functional/utils.py b/tests/functional/utils.py index 1650ab0b..69edb4b8 100644 --- a/tests/functional/utils.py +++ b/tests/functional/utils.py @@ -23,8 +23,7 @@ def build_container(tag, dockerfile): shutil.rmtree(tmpdir) -@contextmanager -def spawn(tag, dockerfile, cmd): +def spawn(request, tag, dockerfile, cmd): if bare: proc = pexpect.spawnu(cmd) else: @@ -36,10 +35,8 @@ def spawn(tag, dockerfile, cmd): proc.logfile = sys.stdout - try: - yield proc - finally: - proc.terminate(force=bare) + request.addfinalizer(proc.terminate) + return proc def images(*items):