| 1 | """Runs tests from ./vspec in vim-vspec.""" |
| 2 | import os |
| 3 | import subprocess |
| 4 | try: |
| 5 | from urllib.request import urlretrieve |
| 6 | except ImportError: |
| 7 | from urllib import urlretrieve |
| 8 | import zipfile |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | vspec_version = '1.9.0' |
| 13 | |
| 14 | VSPEC_URL = 'https://github.com/kana/vim-vspec/archive/%s.zip' % vspec_version |
| 15 | root = os.path.dirname(os.path.dirname(__file__)) |
| 16 | CACHE_FOLDER = os.path.join(root, 'build') |
| 17 | VSPEC_FOLDER = os.path.join(CACHE_FOLDER, 'vim-vspec-%s' % vspec_version) |
| 18 | VSPEC_RUNNER = os.path.join(VSPEC_FOLDER, 'bin/vspec') |
| 19 | TEST_DIR = os.path.join(root, 'test', 'vspec') |
| 20 | |
| 21 | |
| 22 | @pytest.fixture(scope='session') |
| 23 | def install_vspec(): |
| 24 | if not os.path.isdir(CACHE_FOLDER): |
| 25 | os.mkdir(CACHE_FOLDER) |
| 26 | |
| 27 | if not os.path.exists(VSPEC_FOLDER): |
| 28 | name, hdrs = urlretrieve(VSPEC_URL) |
| 29 | z = zipfile.ZipFile(name) |
| 30 | for n in z.namelist(): |
| 31 | dest = os.path.join(CACHE_FOLDER, n) |
| 32 | destdir = os.path.dirname(dest) |
| 33 | if not os.path.isdir(destdir): |
| 34 | os.makedirs(destdir) |
| 35 | data = z.read(n) |
| 36 | if not os.path.isdir(dest): |
| 37 | with open(dest, 'wb') as f: |
| 38 | f.write(data) |
| 39 | z.close() |
| 40 | os.chmod(VSPEC_RUNNER, 0o777) |
| 41 | |
| 42 | |
| 43 | def get_vspec_tests(): |
| 44 | for f in os.listdir(TEST_DIR): |
| 45 | yield os.path.relpath(os.path.join(TEST_DIR, f)) |
| 46 | |
| 47 | |
| 48 | @pytest.mark.parametrize('path', get_vspec_tests()) |
| 49 | def test_integration(install_vspec, path): |
| 50 | output = subprocess.check_output( |
| 51 | [VSPEC_RUNNER, '.', VSPEC_FOLDER, os.path.relpath(path, root)], |
| 52 | cwd=root, |
| 53 | ) |
| 54 | had_ok = False |
| 55 | for line in output.splitlines(): |
| 56 | if (line.startswith(b'not ok') or |
| 57 | line.startswith(b'Error') or |
| 58 | line.startswith(b'Bail out!')): |
| 59 | pytest.fail(u"{0} failed:\n{1}".format( |
| 60 | path, output.decode('utf-8')), pytrace=False) |
| 61 | if not had_ok and line.startswith(b'ok'): |
| 62 | had_ok = True |
| 63 | if not had_ok: |
| 64 | pytest.fail(u"{0} failed: no 'ok' found:\n{1}".format( |
| 65 | path, output.decode('utf-8')), pytrace=False) |