Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
opadron committed Jul 20, 2016
1 parent 9b1e202 commit e551643
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 218 deletions.
1 change: 0 additions & 1 deletion skbuild/__init__.py
Expand Up @@ -5,4 +5,3 @@
__author__ = 'The scikit-build team'
__email__ = 'scikit-build@googlegroups.com'
__version__ = '0.2.0'

9 changes: 6 additions & 3 deletions skbuild/cmaker.py
Expand Up @@ -20,6 +20,7 @@
RE_FILE_INSTALL = re.compile(
r"""[ \t]*file\(INSTALL DESTINATION "([^"]+)".*"([^"]+)"\).*""")


def pop_arg(arg, a, default=None):
"""Pops an arg(ument) from an argument list a and returns the new list
and the value of the argument if present and a default otherwise.
Expand Down Expand Up @@ -49,6 +50,7 @@ def _remove_cwd_prefix(path):

return result


def _touch_init(folder):
init = os.path.join(folder, "__init__.py")
if not os.path.exists(init):
Expand Down Expand Up @@ -169,7 +171,9 @@ def get_python_version():

return python_version

@staticmethod
# NOTE(opadron): The try-excepts raise the cyclomatic complexity, but we
# need them for this function.
@staticmethod # noqa: C901: Complexity
def get_python_include_dir(python_version):
# determine python include dir
python_include_dir = sysconfig.get_config_var('INCLUDEPY')
Expand Down Expand Up @@ -254,7 +258,7 @@ def get_python_library(python_version):

# if static (or nonexistent), try to find a suitable dynamic libpython
if (python_library is None or
os.path.splitext(python_library)[1][-2:] == '.a'):
os.path.splitext(python_library)[1][-2:] == '.a'):

candidate_lib_prefixes = ['', 'lib']

Expand Down Expand Up @@ -383,4 +387,3 @@ def _parse_manifest(self):
return [_remove_cwd_prefix(path) for path in manifest]

return []

6 changes: 4 additions & 2 deletions skbuild/command/bdist.py
Expand Up @@ -4,12 +4,14 @@
except ImportError:
from distutils.command.bdist import bdist as _bdist

from ..cmaker import SETUPTOOLS_INSTALL_DIR


class bdist(_bdist):
def finalize_options(self):
try:
if not self.build_base or self.build_base == 'build':
self.build_base = cmaker.SETUPTOOLS_INSTALL_DIR
self.build_base = SETUPTOOLS_INSTALL_DIR
except AttributeError:
pass
_bdist.finalize_options(self)

6 changes: 4 additions & 2 deletions skbuild/command/bdist_wheel.py
@@ -1,12 +1,14 @@

from wheel.bdist_wheel import bdist_wheel as _bdist_wheel

from ..cmaker import SETUPTOOLS_INSTALL_DIR


class bdist_wheel(_bdist_wheel):
def finalize_options(self):
try:
if not self.build_base or self.build_base == 'build':
self.build_base = cmaker.SETUPTOOLS_INSTALL_DIR
self.build_base = SETUPTOOLS_INSTALL_DIR
except AttributeError:
pass
_bdist_wheel.finalize_options(self)

2 changes: 1 addition & 1 deletion skbuild/setuptools_wrap.py
Expand Up @@ -16,6 +16,7 @@
except ImportError:
from distutils.core import setup as upstream_setup


def move_arg(arg, a, b, newarg=None, f=lambda x: x, concatenate_value=False):
"""Moves an argument from a list to b list, possibly giving it a new name
and/or performing a transformation on the value. Returns a and b. The arg
Expand Down Expand Up @@ -237,4 +238,3 @@ def setup(*args, **kw):
kw['cmdclass'] = cmdclass

return upstream_setup(*args, **kw)

103 changes: 103 additions & 0 deletions tests/__init__.py
@@ -1 +1,104 @@
# -*- coding: utf-8 -*-

import inspect
import os
import os.path
import shutil
import sys

from contextlib import contextmanager
from types import FunctionType

from skbuild.cmaker import SKBUILD_DIR

PYTHON2 = (sys.version_info < (3, 0))

# NOTE(opadron): we need to use separate files for the Python 2 and 3 cases
# because there's no way to write code that executes a file that parses
# successfully for both versions.
if PYTHON2:
from .exec_2 import execute
else:
from .exec_3 import execute


@contextmanager
def push_dir(dir):
old_cwd = os.getcwd()
os.chdir(dir)
yield
os.chdir(old_cwd)


@contextmanager
def push_argv(argv):
old_argv = sys.argv
sys.argv = argv
yield
sys.argv = old_argv


def _noop():
pass


class project_test():
def __init__(self, project):
self.project = project

def __call__(self, func=_noop):
def result(*args, **kwargs):
dir = list(self.project)
dir.insert(0, os.path.dirname(os.path.abspath(__file__)))
dir = os.path.join(*dir)

with push_dir(dir):
result2 = func(*args, **kwargs)

return result2

return FunctionType(
result.__code__,
result.__globals__,
func.__name__,
result.__defaults__,
result.__closure__
)


class project_setup_py_test():
def __init__(self, project, setup_args, clear_cache=False):
self.project = project
self.setup_args = setup_args
self.clear_cache = clear_cache

f = inspect.currentframe().f_back
self.locals = f.f_locals
self.globals = f.f_globals

def __call__(self, func=_noop):
@project_test(self.project)
def result(*args, **kwargs):
argv = ["setup.py"] + self.setup_args
with push_argv(argv):
if self.clear_cache and os.path.exists(SKBUILD_DIR):
shutil.rmtree(SKBUILD_DIR)

setup_code = None
with open("setup.py", "r") as fp:
setup_code = compile(fp.read(), "setup.py", mode="exec")

if setup_code is not None:
execute(setup_code, self.globals, self.locals)

result2 = func(*args, **kwargs)

return result2

return FunctionType(
result.__code__,
result.__globals__,
func.__name__,
result.__defaults__,
result.__closure__
)
3 changes: 3 additions & 0 deletions tests/exec_2.py
@@ -0,0 +1,3 @@

def execute(code, _globals, _locals):
exec code in _globals, _locals # flake8: noqa: E901: SyntaxError
3 changes: 3 additions & 0 deletions tests/exec_3.py
@@ -0,0 +1,3 @@

def execute(code, _globals, _locals):
exec(code, _globals, _locals)
49 changes: 13 additions & 36 deletions tests/test_hello.py
Expand Up @@ -7,42 +7,19 @@
Tries to build and test the `hello` sample project.
"""

import os
import os.path
import shutil
import subprocess
import sys

from skbuild.cmaker import SKBUILD_DIR, CMAKE_BUILD_DIR
from . import project_setup_py_test


@project_setup_py_test(("samples", "hello"), ["build"], clear_cache=True)
def test_hello_builds():
old_argv = sys.argv
old_cwd = os.getcwd()

sys.argv = ["setup.py", "build"]
cur_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(cur_dir, "samples", "hello"))

if os.path.exists(SKBUILD_DIR):
shutil.rmtree(SKBUILD_DIR)

try:
with open("setup.py", "r") as fp:
exec(fp.read())
finally:
os.chdir(old_cwd)
sys.argv = old_argv


def test_hello_works():
old_cwd = os.getcwd()
cur_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(cur_dir, "samples", "hello", CMAKE_BUILD_DIR))
try:
subprocess.check_call(
["ctest", "--build-config",
os.environ.get("SKBUILD_CMAKE_CONFIG", "Debug"),
"--output-on-failure"])
finally:
os.chdir(old_cwd)
pass


# @project_setup_py_test(("samples", "hello"), ["test"])
# def test_hello_works():
# pass


@project_setup_py_test(("samples", "hello"), ["bdist_wheel"])
def test_hello_wheel():
pass
49 changes: 13 additions & 36 deletions tests/test_hello_cython.py
Expand Up @@ -7,42 +7,19 @@
Tries to build and test the `hello-cython` sample project.
"""

import os
import os.path
import shutil
import subprocess
import sys

from skbuild.cmaker import SKBUILD_DIR, CMAKE_BUILD_DIR
from . import project_setup_py_test


@project_setup_py_test(("samples", "hello-cython"), ["build"], clear_cache=True)
def test_hello_cython_builds():
old_argv = sys.argv
old_cwd = os.getcwd()

sys.argv = ["setup.py", "build"]
cur_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(cur_dir, "samples", "hello-cython"))

if os.path.exists(SKBUILD_DIR):
shutil.rmtree(SKBUILD_DIR)

try:
with open("setup.py", "r") as fp:
exec(fp.read())
finally:
os.chdir(old_cwd)
sys.argv = old_argv


def test_hello_cython_works():
old_cwd = os.getcwd()
cur_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(cur_dir, "samples", "hello-cython", CMAKE_BUILD_DIR))
try:
subprocess.check_call(
["ctest", "--build-config",
os.environ.get("SKBUILD_CMAKE_CONFIG", "Debug"),
"--output-on-failure"])
finally:
os.chdir(old_cwd)
pass


# @project_setup_py_test(("samples", "hello-cython"), ["test"])
# def test_hello_cython_works():
# pass


@project_setup_py_test(("samples", "hello-cython"), ["bdist_wheel"])
def test_hello_cython_wheel():
pass
28 changes: 8 additions & 20 deletions tests/test_outside_project_root.py
Expand Up @@ -8,34 +8,22 @@
attempt fails with an SKBuildError exception.
"""

import os
import os.path
import shutil
import sys

from skbuild.exceptions import SKBuildError
from skbuild.cmaker import SKBUILD_DIR


def test_outside_project_root_installs():
old_argv = sys.argv
old_cwd = os.getcwd()
from . import project_setup_py_test

sys.argv = ["setup.py", "install"]
cur_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(cur_dir, "samples", "fail-outside-project-root"))

if os.path.exists(SKBUILD_DIR):
shutil.rmtree(SKBUILD_DIR)
def test_outside_project_root_fails():
@project_setup_py_test(("samples", "fail-outside-project-root"),
["install"],
clear_cache=True)
def should_fail():
pass

exception_thrown = False
try:
with open("setup.py", "r") as fp:
exec(fp.read())
should_fail()
except SKBuildError:
exception_thrown = True
finally:
os.chdir(old_cwd)
sys.argv = old_argv

assert exception_thrown

0 comments on commit e551643

Please sign in to comment.