diff --git a/tests/functional/test_bash.py b/tests/functional/test_bash.py index ef4b5d42..ca320780 100644 --- a/tests/functional/test_bash.py +++ b/tests/functional/test_bash.py @@ -1,6 +1,5 @@ import pytest -from tests.functional.utils import build_container, spawn, run, read_until, \ - root, functional +from tests.functional.utils import spawn, functional containers = [('thefuck/ubuntu-python3-bash', ''' FROM ubuntu:latest @@ -8,7 +7,6 @@ RUN apt-get update RUN apt-get install -yy python3 python3-pip python3-dev RUN pip3 install -U setuptools RUN ln -s /usr/bin/pip3 /usr/bin/pip -RUN echo "PS1='$ '" > /root/.bashrc CMD ["/bin/bash"] '''), ('thefuck/ubuntu-python2-bash', ''' @@ -16,7 +14,6 @@ FROM ubuntu:latest RUN apt-get update RUN apt-get install -yy python python-pip python-dev RUN pip2 install -U pip setuptools -RUN echo "PS1='$ '" > /root/.bashrc CMD ["/bin/bash"] ''')] @@ -24,26 +21,31 @@ CMD ["/bin/bash"] @functional @pytest.mark.parametrize('tag, dockerfile', containers) def test_with_confirmation(tag, dockerfile): - build_container(tag, dockerfile) - with spawn(tag, '{}:/src'.format(root), - ['cd /src', 'pip install .', 'eval $(thefuck-alias)']) as proc: - run(proc, 'ehco test') + with spawn(tag, dockerfile) as proc: + proc.sendline('eval $(thefuck-alias)') + + proc.sendline('ehco test') + proc.expect('command not found') + proc.sendline('fuck') - read_until(proc, '[') + proc.expect('echo test') + proc.expect('enter') + proc.expect_exact('ctrl+c') proc.send('\n') - out = read_until(proc) - assert out.split('\n')[-2] == 'test\r\r' + + proc.expect('test') @functional @pytest.mark.parametrize('tag, dockerfile', containers) def test_without_confirmation(tag, dockerfile): - build_container(tag, dockerfile) - with spawn(tag, '{}:/src'.format(root), - ['cd /src', 'pip install .', - 'export THEFUCK_REQUIRE_CONFIRMATION=false', - 'eval $(thefuck-alias)']) as proc: - run(proc, 'ehco test') - run(proc, 'fuck') - out = read_until(proc) - assert out.split('\n')[-2] == 'test\r\r' + with spawn(tag, dockerfile) as proc: + proc.sendline('export THEFUCK_REQUIRE_CONFIRMATION=false') + proc.sendline('eval $(thefuck-alias)') + + proc.sendline('ehco test') + proc.expect('command not found') + + proc.sendline('fuck') + proc.expect('echo test') + proc.expect('test') diff --git a/tests/functional/test_zsh.py b/tests/functional/test_zsh.py index bd311b27..ccbdac64 100644 --- a/tests/functional/test_zsh.py +++ b/tests/functional/test_zsh.py @@ -1,6 +1,5 @@ import pytest -from tests.functional.utils import build_container, spawn, run, read_until, \ - root, functional +from tests.functional.utils import spawn, functional containers = [('thefuck/ubuntu-python3-zsh', ''' FROM ubuntu:latest @@ -8,7 +7,6 @@ RUN apt-get update RUN apt-get install -yy python3 python3-pip python3-dev zsh RUN pip3 install -U setuptools RUN ln -s /usr/bin/pip3 /usr/bin/pip -RUN echo "PS1='\\n$ '" > /root/.zshrc CMD ["/bin/zsh"] '''), ('thefuck/ubuntu-python2-zsh', ''' @@ -16,7 +14,6 @@ FROM ubuntu:latest RUN apt-get update RUN apt-get install -yy python python-pip python-dev zsh RUN pip2 install -U pip setuptools -RUN echo "PS1='\\n$ '" > /root/.zshrc CMD ["/bin/zsh"] ''')] @@ -24,27 +21,31 @@ CMD ["/bin/zsh"] @functional @pytest.mark.parametrize('tag, dockerfile', containers) def test_with_confirmation(tag, dockerfile): - build_container(tag, dockerfile) - with spawn(tag, '{}:/src'.format(root), - ['cd /src', 'pip install .', 'eval $(thefuck-alias)']) as proc: - run(proc, 'ehco "\ntest"') + with spawn(tag, dockerfile) as proc: + proc.sendline('eval $(thefuck-alias)') + + proc.sendline('ehco test') + proc.expect('command not found') + proc.sendline('fuck') - read_until(proc, '[') + proc.expect('echo test') + proc.expect('enter') + proc.expect_exact('ctrl+c') proc.send('\n') - read_until(proc) - out = read_until(proc) - assert out.split('\n')[-3] == 'test\r\r' + + proc.expect('test') @functional @pytest.mark.parametrize('tag, dockerfile', containers) def test_without_confirmation(tag, dockerfile): - build_container(tag, dockerfile) - with spawn(tag, '{}:/src'.format(root), - ['cd /src', 'pip install .', - 'export THEFUCK_REQUIRE_CONFIRMATION=false', - 'eval $(thefuck-alias)']) as proc: - run(proc, 'ehco "\ntest"') - run(proc, 'fuck') - out = read_until(proc) - assert out.split('\n')[-3] == 'test\r\r' + with spawn(tag, dockerfile) as proc: + proc.sendline('export THEFUCK_REQUIRE_CONFIRMATION=false') + proc.sendline('eval $(thefuck-alias)') + + proc.sendline('ehco test') + proc.expect('command not found') + + proc.sendline('fuck') + proc.expect('echo test') + proc.expect('test') diff --git a/tests/functional/utils.py b/tests/functional/utils.py index 684dc93b..ee930502 100644 --- a/tests/functional/utils.py +++ b/tests/functional/utils.py @@ -1,4 +1,3 @@ -import sys import os import pytest from contextlib import contextmanager @@ -15,38 +14,20 @@ def build_container(tag, dockerfile): tmpdir = mkdtemp() with Path(tmpdir).joinpath('Dockerfile').open('w') as file: file.write(dockerfile) - if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir]) != 0: + if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir], + cwd=root) != 0: raise Exception("Can't build container") shutil.rmtree(tmpdir) -def read_until(proc, string='\n$ '): - text = '' - while True: - text += proc.read(1) - sys.stdout.write(text[-1]) - sys.stdout.flush() - if text.endswith(string): - return text - - -def run(proc, cmd): - proc.sendline(cmd) - return read_until(proc) - - @contextmanager -def spawn(tag, volume, prepare=None): - if prepare is None: - prepare = [] - +def spawn(tag, dockerfile): + build_container(tag, dockerfile) proc = pexpect.spawnu( - 'docker run --volume {} --tty=true --interactive=true {}'.format( - volume, tag)) + 'docker run --volume {}:/src --tty=true --interactive=true {}'.format(root, tag)) + proc.sendline('pip install /src') try: - for line in prepare: - run(proc, line) yield proc finally: proc.terminate()