1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 12:06:04 +00:00

Use pytest-docker-pexpect for func tests

This commit is contained in:
nvbn 2015-09-06 00:56:18 +03:00
parent 8bf4182f86
commit 8cc19daaaa
11 changed files with 155 additions and 216 deletions

View File

@ -15,7 +15,7 @@ addons:
- pandoc - pandoc
- git - git
env: env:
- FUNCTIONAL=true BARE=true - FUNCTIONAL=true
install: install:
- pip install coveralls - pip install coveralls
- pip install -r requirements.txt - pip install -r requirements.txt
@ -23,5 +23,5 @@ install:
- rm -rf build - rm -rf build
script: script:
- export COVERAGE_PYTHON_VERSION=python-${TRAVIS_PYTHON_VERSION:0:1} - export COVERAGE_PYTHON_VERSION=python-${TRAVIS_PYTHON_VERSION:0:1}
- coverage run --source=thefuck,tests -m py.test -v --capture=sys - coverage run --source=thefuck,tests -m py.test -v --capture=sys --run-without-docker
after_success: coveralls after_success: coveralls

View File

@ -6,3 +6,4 @@ setuptools>=17.1
pexpect pexpect
pypandoc pypandoc
pytest-benchmark pytest-benchmark
pytest-docker-pexpect

View File

@ -1,6 +1,3 @@
from pexpect import TIMEOUT
def _set_confirmation(proc, require): def _set_confirmation(proc, require):
proc.sendline(u'mkdir -p ~/.thefuck') proc.sendline(u'mkdir -p ~/.thefuck')
proc.sendline( proc.sendline(
@ -8,7 +5,7 @@ def _set_confirmation(proc, require):
require)) require))
def with_confirmation(proc): def with_confirmation(proc, TIMEOUT):
"""Ensures that command can be fixed when confirmation enabled.""" """Ensures that command can be fixed when confirmation enabled."""
_set_confirmation(proc, True) _set_confirmation(proc, True)
@ -23,19 +20,19 @@ def with_confirmation(proc):
assert proc.expect([TIMEOUT, u'test']) assert proc.expect([TIMEOUT, u'test'])
def history_changed(proc, to): def history_changed(proc, TIMEOUT, to):
"""Ensures that history changed.""" """Ensures that history changed."""
proc.send('\033[A') proc.send('\033[A')
assert proc.expect([TIMEOUT, to]) assert proc.expect([TIMEOUT, to])
def history_not_changed(proc): def history_not_changed(proc, TIMEOUT):
"""Ensures that history not changed.""" """Ensures that history not changed."""
proc.send('\033[A') proc.send('\033[A')
assert proc.expect([TIMEOUT, u'fuck']) assert proc.expect([TIMEOUT, u'fuck'])
def select_command_with_arrows(proc): def select_command_with_arrows(proc, TIMEOUT):
"""Ensures that command can be selected with arrow keys.""" """Ensures that command can be selected with arrow keys."""
_set_confirmation(proc, True) _set_confirmation(proc, True)
@ -55,7 +52,7 @@ def select_command_with_arrows(proc):
assert proc.expect([TIMEOUT, u'Not a git repository']) assert proc.expect([TIMEOUT, u'Not a git repository'])
def refuse_with_confirmation(proc): def refuse_with_confirmation(proc, TIMEOUT):
"""Ensures that fix can be refused when confirmation enabled.""" """Ensures that fix can be refused when confirmation enabled."""
_set_confirmation(proc, True) _set_confirmation(proc, True)
@ -70,7 +67,7 @@ def refuse_with_confirmation(proc):
assert proc.expect([TIMEOUT, u'Aborted']) assert proc.expect([TIMEOUT, u'Aborted'])
def without_confirmation(proc): def without_confirmation(proc, TIMEOUT):
"""Ensures that command can be fixed when confirmation disabled.""" """Ensures that command can be fixed when confirmation disabled."""
_set_confirmation(proc, False) _set_confirmation(proc, False)

View File

@ -2,27 +2,28 @@ import pytest
from tests.functional.plots import with_confirmation, without_confirmation, \ from tests.functional.plots import with_confirmation, without_confirmation, \
refuse_with_confirmation, history_changed, history_not_changed, \ refuse_with_confirmation, history_changed, history_not_changed, \
select_command_with_arrows select_command_with_arrows
from tests.functional.utils import spawn, functional, images from tests.functional.utils import functional
containers = images(('ubuntu-python3-bash', u''' containers = ((u'thefuck/ubuntu-python3-bash',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python3 python3-pip python3-dev git RUN apt-get install -yy python3 python3-pip python3-dev git
RUN pip3 install -U setuptools RUN pip3 install -U setuptools
RUN ln -s /usr/bin/pip3 /usr/bin/pip RUN ln -s /usr/bin/pip3 /usr/bin/pip''',
'''), u'bash'),
('ubuntu-python2-bash', u''' (u'thefuck/ubuntu-python2-bash',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python python-pip python-dev git RUN apt-get install -yy python python-pip python-dev git
RUN pip2 install -U pip setuptools RUN pip2 install -U pip setuptools''',
''')) u'bash'))
@pytest.fixture(params=containers) @pytest.fixture(params=containers)
def proc(request): def proc(request, spawnu, run_without_docker):
tag, dockerfile = request.param proc = spawnu(*request.param)
proc = spawn(request, tag, dockerfile, u'bash') if not run_without_docker:
proc.sendline(u"pip install /src")
proc.sendline(u"export PS1='$ '") proc.sendline(u"export PS1='$ '")
proc.sendline(u'eval $(thefuck --alias)') proc.sendline(u'eval $(thefuck --alias)')
proc.sendline(u'echo > $HISTFILE') proc.sendline(u'echo > $HISTFILE')
@ -30,24 +31,24 @@ def proc(request):
@functional @functional
def test_with_confirmation(proc): def test_with_confirmation(proc, TIMEOUT):
with_confirmation(proc) with_confirmation(proc, TIMEOUT)
history_changed(proc, u'echo test') history_changed(proc, TIMEOUT, u'echo test')
@functional @functional
def test_select_command_with_arrows(proc): def test_select_command_with_arrows(proc, TIMEOUT):
select_command_with_arrows(proc) select_command_with_arrows(proc, TIMEOUT)
history_changed(proc, u'git push') history_changed(proc, TIMEOUT, u'git push')
@functional @functional
def test_refuse_with_confirmation(proc): def test_refuse_with_confirmation(proc, TIMEOUT):
refuse_with_confirmation(proc) refuse_with_confirmation(proc, TIMEOUT)
history_not_changed(proc) history_not_changed(proc, TIMEOUT)
@functional @functional
def test_without_confirmation(proc): def test_without_confirmation(proc, TIMEOUT):
without_confirmation(proc) without_confirmation(proc, TIMEOUT)
history_changed(proc, u'echo test') history_changed(proc, TIMEOUT, u'echo test')

View File

@ -1,59 +1,55 @@
import pytest import pytest
from tests.functional.plots import with_confirmation, without_confirmation, \ from tests.functional.plots import with_confirmation, without_confirmation, \
refuse_with_confirmation, select_command_with_arrows refuse_with_confirmation, select_command_with_arrows
from tests.functional.utils import spawn, functional, images, bare from tests.functional.utils import functional
containers = images(('ubuntu-python3-fish', u''' containers = (('thefuck/ubuntu-python3-fish',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python3 python3-pip python3-dev fish git RUN apt-get install -yy python3 python3-pip python3-dev fish git
RUN pip3 install -U setuptools RUN pip3 install -U setuptools
RUN ln -s /usr/bin/pip3 /usr/bin/pip RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN apt-get install -yy fish RUN apt-get install -yy fish''',
'''), u'fish'),
('ubuntu-python2-fish', u''' ('thefuck/ubuntu-python2-fish',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python python-pip python-dev git RUN apt-get install -yy python python-pip python-dev git
RUN pip2 install -U pip setuptools RUN pip2 install -U pip setuptools
RUN apt-get install -yy fish RUN apt-get install -yy fish''',
''')) u'fish'))
@pytest.fixture(params=containers) @pytest.fixture(params=containers)
def proc(request): def proc(request, spawnu):
tag, dockerfile = request.param proc = spawnu(*request.param)
proc = spawn(request, tag, dockerfile, u'fish') proc.sendline(u"pip install /src")
proc.sendline(u'thefuck --alias > ~/.config/fish/config.fish') proc.sendline(u'thefuck --alias > ~/.config/fish/config.fish')
proc.sendline(u'fish') proc.sendline(u'fish')
return proc return proc
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') def test_with_confirmation(proc, TIMEOUT):
def test_with_confirmation(proc): with_confirmation(proc, TIMEOUT)
with_confirmation(proc)
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') def test_select_command_with_arrows(proc, TIMEOUT):
def test_select_command_with_arrows(proc): select_command_with_arrows(proc, TIMEOUT)
select_command_with_arrows(proc)
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') def test_refuse_with_confirmation(proc, TIMEOUT):
def test_refuse_with_confirmation(proc): refuse_with_confirmation(proc, TIMEOUT)
refuse_with_confirmation(proc)
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason='https://github.com/travis-ci/apt-source-whitelist/issues/71') def test_without_confirmation(proc, TIMEOUT):
def test_without_confirmation(proc): without_confirmation(proc, TIMEOUT)
without_confirmation(proc)
# TODO: ensure that history changes. # TODO: ensure that history changes.

View File

@ -1,25 +1,26 @@
import pytest import pytest
from pexpect import TIMEOUT from thefuck.main import _get_current_version
from tests.functional.utils import spawn, functional, bare from tests.functional.utils import functional
envs = ((u'bash', 'ubuntu-bash', u'''
envs = ((u'bash', 'thefuck/ubuntu-bash', u'''
FROM ubuntu:latest FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy bash RUN apt-get install -yy bash
'''), (u'bash', 'generic-bash', u''' '''), (u'bash', 'thefuck/generic-bash', u'''
FROM fedora:latest FROM fedora:latest
RUN dnf install -yy python-devel sudo wget gcc RUN dnf install -yy python-devel sudo wget gcc
''')) '''))
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason="Can't be tested in bare run")
@pytest.mark.parametrize('shell, tag, dockerfile', envs) @pytest.mark.parametrize('shell, tag, dockerfile', envs)
def test_installation(request, shell, tag, dockerfile): def test_installation(spawnu, shell, TIMEOUT, tag, dockerfile):
proc = spawn(request, tag, dockerfile, shell, install=False) proc = spawnu(tag, dockerfile, shell)
proc.sendline(u'cat /src/install.sh | sh - && $0') proc.sendline(u'cat /src/install.sh | sh - && $0')
proc.sendline(u'thefuck --version') proc.sendline(u'thefuck --version')
assert proc.expect([TIMEOUT, u'The Fuck'], timeout=600) assert proc.expect([TIMEOUT, u'thefuck {}'.format(_get_current_version())],
timeout=600)
proc.sendline(u'fuck') proc.sendline(u'fuck')
assert proc.expect([TIMEOUT, u'No fucks given']) assert proc.expect([TIMEOUT, u'No fucks given'])

View File

@ -1,7 +1,6 @@
from pexpect import TIMEOUT
import pytest import pytest
import time import time
from tests.functional.utils import spawn, functional, bare from tests.functional.utils import functional
dockerfile = u''' dockerfile = u'''
FROM ubuntu:latest FROM ubuntu:latest
@ -11,24 +10,17 @@ RUN pip3 install -U setuptools
RUN ln -s /usr/bin/pip3 /usr/bin/pip RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN adduser --disabled-password --gecos '' test RUN adduser --disabled-password --gecos '' test
ENV SEED "{seed}" ENV SEED "{seed}"
COPY thefuck /src
WORKDIR /src WORKDIR /src
RUN pip install .
USER test USER test
RUN echo 'eval $(thefuck --alias)' > /home/test/.bashrc RUN echo 'eval $(thefuck --alias)' > /home/test/.bashrc
RUN echo > /home/test/.bash_history RUN echo > /home/test/.bash_history
RUN git config --global user.email "you@example.com" RUN git config --global user.email "you@example.com"
RUN git config --global user.name "Your Name" RUN git config --global user.name "Your Name"
USER root
'''.format(seed=time.time()) '''.format(seed=time.time())
@pytest.fixture def plot(proc, TIMEOUT):
def proc(request):
return spawn(request, 'ubuntu-python3-bash-performance',
dockerfile, u'bash', install=False, copy_src=True)
def plot(proc):
proc.sendline(u'cd /home/test/') proc.sendline(u'cd /home/test/')
proc.sendline(u'fuck') proc.sendline(u'fuck')
assert proc.expect([TIMEOUT, u'No fucks given']) assert proc.expect([TIMEOUT, u'No fucks given'])
@ -49,8 +41,11 @@ def plot(proc):
@functional @functional
@pytest.mark.skipif( @pytest.mark.skip_without_docker
bool(bare), reason='Would lie on a bare run')
@pytest.mark.benchmark(min_rounds=10) @pytest.mark.benchmark(min_rounds=10)
def test_performance(proc, benchmark): def test_performance(spawnu, TIMEOUT, benchmark):
assert benchmark(plot, proc) is None proc = spawnu(u'thefuck/ubuntu-python3-bash-performance',
dockerfile, u'bash')
proc.sendline(u'pip install /src')
proc.sendline(u'su test')
assert benchmark(plot, proc, TIMEOUT) is None

View File

@ -1,51 +1,52 @@
import pytest import pytest
from tests.functional.utils import spawn, functional, images from tests.functional.utils import functional
from tests.functional.plots import with_confirmation, without_confirmation, \ from tests.functional.plots import with_confirmation, without_confirmation, \
refuse_with_confirmation, select_command_with_arrows refuse_with_confirmation, select_command_with_arrows
containers = images(('ubuntu-python3-tcsh', u''' containers = (('thefuck/ubuntu-python3-tcsh',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python3 python3-pip python3-dev git RUN apt-get install -yy python3 python3-pip python3-dev git
RUN pip3 install -U setuptools RUN pip3 install -U setuptools
RUN ln -s /usr/bin/pip3 /usr/bin/pip RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN apt-get install -yy tcsh RUN apt-get install -yy tcsh''',
'''), u'tcsh'),
('ubuntu-python2-tcsh', u''' ('thefuck/ubuntu-python2-tcsh',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python python-pip python-dev git RUN apt-get install -yy python python-pip python-dev git
RUN pip2 install -U pip setuptools RUN pip2 install -U pip setuptools
RUN apt-get install -yy tcsh RUN apt-get install -yy tcsh''',
''')) u'tcsh'))
@pytest.fixture(params=containers) @pytest.fixture(params=containers)
def proc(request): def proc(request, spawnu, run_without_docker):
tag, dockerfile = request.param proc = spawnu(*request.param)
proc = spawn(request, tag, dockerfile, u'tcsh') if not run_without_docker:
proc.sendline(u'pip install /src')
proc.sendline(u'tcsh') proc.sendline(u'tcsh')
proc.sendline(u'eval `thefuck --alias`') proc.sendline(u'eval `thefuck --alias`')
return proc return proc
@functional @functional
def test_with_confirmation(proc): def test_with_confirmation(proc, TIMEOUT):
with_confirmation(proc) with_confirmation(proc, TIMEOUT)
@functional @functional
def test_select_command_with_arrows(proc): def test_select_command_with_arrows(proc, TIMEOUT):
select_command_with_arrows(proc) select_command_with_arrows(proc, TIMEOUT)
@functional @functional
def test_refuse_with_confirmation(proc): def test_refuse_with_confirmation(proc, TIMEOUT):
refuse_with_confirmation(proc) refuse_with_confirmation(proc, TIMEOUT)
@functional @functional
def test_without_confirmation(proc): def test_without_confirmation(proc, TIMEOUT):
without_confirmation(proc) without_confirmation(proc, TIMEOUT)
# TODO: ensure that history changes. # TODO: ensure that history changes.

View File

@ -1,29 +1,30 @@
import pytest import pytest
from tests.functional.utils import spawn, functional, images from tests.functional.utils import functional
from tests.functional.plots import with_confirmation, without_confirmation, \ from tests.functional.plots import with_confirmation, without_confirmation, \
refuse_with_confirmation, history_changed, history_not_changed, select_command_with_arrows refuse_with_confirmation, history_changed, history_not_changed, select_command_with_arrows
containers = images(('ubuntu-python3-zsh', u''' containers = (('ubuntu-python3-zsh',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python3 python3-pip python3-dev git RUN apt-get install -yy python3 python3-pip python3-dev git
RUN pip3 install -U setuptools RUN pip3 install -U setuptools
RUN ln -s /usr/bin/pip3 /usr/bin/pip RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN apt-get install -yy zsh RUN apt-get install -yy zsh''',
'''), u'zsh'),
('ubuntu-python2-zsh', u''' ('ubuntu-python2-zsh',
FROM ubuntu:latest u'''FROM ubuntu:latest
RUN apt-get update RUN apt-get update
RUN apt-get install -yy python python-pip python-dev git RUN apt-get install -yy python python-pip python-dev git
RUN pip2 install -U pip setuptools RUN pip2 install -U pip setuptools
RUN apt-get install -yy zsh RUN apt-get install -yy zsh''',
''')) u'zsh'))
@pytest.fixture(params=containers) @pytest.fixture(params=containers)
def proc(request): def proc(request, spawnu, run_without_docker):
tag, dockerfile = request.param proc = spawnu(*request.param)
proc = spawn(request, tag, dockerfile, u'zsh') if not run_without_docker:
proc.sendline(u'pip install /src')
proc.sendline(u'eval $(thefuck --alias)') proc.sendline(u'eval $(thefuck --alias)')
proc.sendline(u'export HISTFILE=~/.zsh_history') proc.sendline(u'export HISTFILE=~/.zsh_history')
proc.sendline(u'echo > $HISTFILE') proc.sendline(u'echo > $HISTFILE')
@ -34,24 +35,24 @@ def proc(request):
@functional @functional
def test_with_confirmation(proc): def test_with_confirmation(proc, TIMEOUT):
with_confirmation(proc) with_confirmation(proc, TIMEOUT)
history_changed(proc, u'echo test') history_changed(proc, TIMEOUT, u'echo test')
@functional @functional
def test_select_command_with_arrows(proc): def test_select_command_with_arrows(proc, TIMEOUT):
select_command_with_arrows(proc) select_command_with_arrows(proc, TIMEOUT)
history_changed(proc, u'git push') history_changed(proc, TIMEOUT, u'git push')
@functional @functional
def test_refuse_with_confirmation(proc): def test_refuse_with_confirmation(proc, TIMEOUT):
refuse_with_confirmation(proc) refuse_with_confirmation(proc, TIMEOUT)
history_not_changed(proc) history_not_changed(proc, TIMEOUT)
@functional @functional
def test_without_confirmation(proc): def test_without_confirmation(proc, TIMEOUT):
without_confirmation(proc) without_confirmation(proc, TIMEOUT)
history_changed(proc, u'echo test') history_changed(proc, TIMEOUT, u'echo test')

View File

@ -1,65 +1,8 @@
import pytest import pytest
import os import os
import subprocess
import shutil
from tempfile import mkdtemp
from pathlib import Path
import sys
import pexpect
from tests.utils import root
bare = os.environ.get('BARE')
enabled = os.environ.get('FUNCTIONAL') enabled = os.environ.get('FUNCTIONAL')
def build_container(tag, dockerfile, copy_src=False):
tmpdir = mkdtemp()
try:
if copy_src:
subprocess.call(['cp', '-a', str(root), tmpdir])
dockerfile_path = Path(tmpdir).joinpath('Dockerfile')
with dockerfile_path.open('w') as file:
file.write(dockerfile)
if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir]) != 0:
raise Exception("Can't build a container")
finally:
shutil.rmtree(tmpdir)
def spawn(request, tag, dockerfile, cmd, install=True, copy_src=False):
if bare:
proc = pexpect.spawnu(cmd)
else:
tag = 'thefuck/{}'.format(tag)
build_container(tag, dockerfile, copy_src)
proc = pexpect.spawnu('docker run --rm=true --volume {}:/src --tty=true '
'--interactive=true {} {}'.format(root, tag, cmd))
if install:
proc.sendline('pip install /src')
proc.sendline('cd /')
proc.logfile = sys.stdout
def _finalizer():
proc.terminate()
if not bare:
container_id = subprocess.check_output(['docker', 'ps']) \
.decode('utf-8').split('\n')[-2].split()[0]
subprocess.check_call(['docker', 'kill', container_id])
request.addfinalizer(_finalizer)
return proc
def images(*items):
if bare:
return [items[0]]
else:
return items
functional = pytest.mark.skipif( functional = pytest.mark.skipif(
not enabled, not enabled,
reason='Functional tests are disabled by default.') reason='Functional tests are disabled by default.')

View File

@ -103,6 +103,10 @@ def fix_command():
run_command(command, selected_command, settings) run_command(command, selected_command, settings)
def _get_current_version():
return pkg_resources.require('thefuck')[0].version
def print_alias(entry_point=True): def print_alias(entry_point=True):
if entry_point: if entry_point:
warn('`thefuck-alias` is deprecated, use `thefuck --alias` instead.') warn('`thefuck-alias` is deprecated, use `thefuck --alias` instead.')
@ -120,8 +124,7 @@ def main():
parser = ArgumentParser(prog='thefuck') parser = ArgumentParser(prog='thefuck')
parser.add_argument('-v', '--version', parser.add_argument('-v', '--version',
action='version', action='version',
version='%(prog)s {}'.format( version='%(prog)s {}'.format(_get_current_version()))
pkg_resources.require('thefuck')[0].version))
parser.add_argument('-a', '--alias', parser.add_argument('-a', '--alias',
action='store_true', action='store_true',
help='[custom-alias-name] prints alias for current shell') help='[custom-alias-name] prints alias for current shell')