From 3d369aff8a6f041e871edd31091354a79c06e946 Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 9 Feb 2018 15:50:33 +0100 Subject: [PATCH] run tests in memory instead of subprocesses greatly improves error/failure reporting --- tests/conftest.py | 78 ++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9a9a1899..462e95fb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,11 +7,15 @@ and then ./verify is run inside the built container. It should return a non-zero exit code for the test to be considered a success. """ -import pytest -import subprocess -import yaml + +import os import shlex +import pytest +import yaml + +from repo2docker.app import Repo2Docker + def pytest_collect_file(parent, path): if path.basename == 'verify': @@ -20,22 +24,36 @@ def pytest_collect_file(parent, path): return RemoteRepoList(path, parent) +def make_test_func(args): + """Generate a test function that runs repo2docker""" + def test(): + app = Repo2Docker() + app.initialize(args) + app.start() + return test + + +class Repo2DockerTest(pytest.Function): + """A pytest.Item for running repo2docker""" + def __init__(self, name, parent, args): + f = parent.obj = make_test_func(args) + super().__init__(name, parent, callobj=f) + self.save_cwd = os.getcwd() + + def reportinfo(self): + return self.parent.fspath, None, "" + + def teardown(self): + super().teardown() + os.chdir(self.save_cwd) + + class LocalRepo(pytest.File): def collect(self): - yield LocalRepoTest(self.fspath.basename, self, self.fspath) - - -class LocalRepoTest(pytest.Item): - def __init__(self, name, parent, path): - super().__init__(name, parent) - self.path = path - - def runtest(self): - subprocess.check_call([ - 'jupyter-repo2docker', - str(self.path.dirname), - './verify' - ]) + yield Repo2DockerTest( + self.fspath.basename, self, + args=[self.fspath.dirname, './verify'], + ) class RemoteRepoList(pytest.File): @@ -43,21 +61,11 @@ class RemoteRepoList(pytest.File): with self.fspath.open() as f: repos = yaml.safe_load(f) for repo in repos: - yield RemoteRepoTest(repo['name'], self, repo['url'], - repo['ref'], repo['verify']) - - -class RemoteRepoTest(pytest.Item): - def __init__(self, name, parent, url, ref, verify): - super().__init__(name, parent) - self.url = url - self.ref = ref - self.verify = verify - - def runtest(self): - subprocess.check_call([ - 'jupyter-repo2docker', - '--ref', self.ref, - self.url, - '--', - ] + shlex.split(self.verify)) + yield Repo2DockerTest( + repo['name'], self, + args=[ + '--ref', repo['ref'], + repo['url'], + '--', + ] + shlex.split(repo['verify']), + )