1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

59 lines
1.5 KiB
Python
Raw Normal View History

2015-08-26 12:12:52 +03:00
import pytest
2015-07-24 03:56:21 +03:00
import os
import subprocess
import shutil
from tempfile import mkdtemp
from pathlib import Path
2015-07-24 08:04:49 +03:00
import sys
import pexpect
2015-08-26 12:12:52 +03:00
from tests.utils import root
2015-07-24 03:56:21 +03:00
2015-08-26 14:34:39 +03:00
bare = os.environ.get('BARE')
2015-07-25 03:33:30 +03:00
enabled = os.environ.get('FUNCTIONAL')
2015-07-24 03:56:21 +03:00
2015-08-26 14:34:39 +03:00
def build_container(tag, dockerfile, copy_src=False):
2015-07-24 03:56:21 +03:00
tmpdir = mkdtemp()
try:
2015-08-26 14:34:39 +03:00
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)
2015-08-26 14:34:39 +03:00
if subprocess.call(['docker', 'build', '--tag={}'.format(tag), tmpdir]) != 0:
raise Exception("Can't build a container")
finally:
shutil.rmtree(tmpdir)
2015-07-24 03:56:21 +03:00
2015-08-26 14:34:39 +03:00
def spawn(request, tag, dockerfile, cmd, install=True, copy_src=False):
if bare:
proc = pexpect.spawnu(cmd)
else:
tag = 'thefuck/{}'.format(tag)
2015-08-26 14:34:39 +03:00
build_container(tag, dockerfile, copy_src)
proc = pexpect.spawnu('docker run --volume {}:/src --tty=true '
'--interactive=true {} {}'.format(root, tag, cmd))
if install:
proc.sendline('pip install /src')
2015-07-30 18:39:41 +03:00
proc.sendline('cd /')
2015-07-24 08:04:49 +03:00
proc.logfile = sys.stdout
2015-07-24 03:56:21 +03:00
2015-08-26 14:34:39 +03:00
request.addfinalizer(lambda: proc.terminate(True))
2015-07-29 16:30:32 +03:00
return proc
def images(*items):
if bare:
return [items[0]]
else:
return items
2015-07-24 03:56:21 +03:00
functional = pytest.mark.skipif(
2015-07-25 03:33:30 +03:00
not enabled,
2015-07-24 03:56:21 +03:00
reason='Functional tests are disabled by default.')