From 38f89e955faaea992086b70bd5bd474b28e61b1a Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Mon, 24 Jul 2017 22:38:48 +0100 Subject: [PATCH] Added test for environment and a script for local running --- .travis.yml | 4 +-- appveyor.yml | 2 +- bin/dev_run_test | 6 ++++ bin/run_test.py | 45 ++++++++++++++++++++++++++ run_tests.py => bin/run_tests.py | 4 ++- test/05_environment/environment.json | 3 ++ test/05_environment/setup.py | 18 +++++++++++ test/05_environment/spam.c | 48 ++++++++++++++++++++++++++++ test/05_environment/version.txt | 2 ++ 9 files changed, 128 insertions(+), 4 deletions(-) create mode 100755 bin/dev_run_test create mode 100755 bin/run_test.py rename run_tests.py => bin/run_tests.py (93%) mode change 100644 => 100755 create mode 100644 test/05_environment/environment.json create mode 100644 test/05_environment/setup.py create mode 100644 test/05_environment/spam.c create mode 100644 test/05_environment/version.txt diff --git a/.travis.yml b/.travis.yml index 9ac8583b..3e5ea3f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,9 +9,9 @@ script: - | if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then pip install . - python ./run_tests.py + python ./bin/run_tests.py else # linux test requires root to clean up the wheelhouse (docker runs as root) sudo pip install . - sudo python ./run_tests.py + sudo python ./bin/run_tests.py fi diff --git a/appveyor.yml b/appveyor.yml index 2c7debeb..93560bed 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,3 +1,3 @@ build_script: - pip install . - - python ./run_tests.py + - python ./bin/run_tests.py diff --git a/bin/dev_run_test b/bin/dev_run_test new file mode 100755 index 00000000..86500be2 --- /dev/null +++ b/bin/dev_run_test @@ -0,0 +1,6 @@ +#!/bin/bash + +cd "$(dirname "$0")" +cd .. + +CIBW_PLATFORM=linux ./bin/run_test.py $1 diff --git a/bin/run_test.py b/bin/run_test.py new file mode 100755 index 00000000..2fa1cbc5 --- /dev/null +++ b/bin/run_test.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +from __future__ import print_function +import os, sys, subprocess, shutil, json, argparse +from glob import glob + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("test_project_dir") + args = parser.parse_args() + + test_project = os.path.abspath(args.test_project_dir) + + # move cwd to the project root + os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + + if not os.path.exists(test_project): + print('No test project not found.', file=sys.stderr) + exit(2) + + # load project settings into environment + env_file = os.path.join(test_project, 'environment.json') + project_env = {} + if os.path.exists(env_file): + with open(env_file) as f: + project_env = json.load(f) + + # run the build + env = os.environ.copy() + project_env = {str(k): str(v) for k, v in project_env.items()} # unicode not allowed in env + env.update(project_env) + print('Building %s with environment %s' % (test_project, project_env)) + subprocess.check_call(['cibuildwheel', test_project], env=env) + wheels = glob('wheelhouse/*.whl') + print('%s built successfully. %i wheels built.' % (test_project, len(wheels))) + + # check some wheels were actually built + assert len(wheels) >= 4 + + # clean up + shutil.rmtree('wheelhouse') + + print('Project built successfully.') + +main() diff --git a/run_tests.py b/bin/run_tests.py old mode 100644 new mode 100755 similarity index 93% rename from run_tests.py rename to bin/run_tests.py index cc86cf48..748591bf --- a/run_tests.py +++ b/bin/run_tests.py @@ -1,9 +1,11 @@ +#!/usr/bin/python + from __future__ import print_function import os, sys, subprocess, shutil, json from glob import glob # move cwd to the project root -os.chdir(os.path.dirname(os.path.abspath(__file__))) +os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) test_projects = glob('test/??_*') diff --git a/test/05_environment/environment.json b/test/05_environment/environment.json new file mode 100644 index 00000000..582a1340 --- /dev/null +++ b/test/05_environment/environment.json @@ -0,0 +1,3 @@ +{ + "CIBW_ENVIRONMENT": "CIBW_TEST_VAR=\"a b c\" CIBW_TEST_VAR_2=1" +} diff --git a/test/05_environment/setup.py b/test/05_environment/setup.py new file mode 100644 index 00000000..21f71264 --- /dev/null +++ b/test/05_environment/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup, Extension +import sys, os + +if sys.argv[-1] != '--name': + # explode if environment isn't correct, as set in CIBW_ENVIRONMENT + CIBW_TEST_VAR = os.environ.get('CIBW_TEST_VAR') + CIBW_TEST_VAR_2 = os.environ.get('CIBW_TEST_VAR_2') + + if CIBW_TEST_VAR != 'a b c': + raise Exception('CIBW_TEST_VAR should equal "a b c". It was "%s"' % CIBW_TEST_VAR) + if CIBW_TEST_VAR_2 != '1': + raise Exception('CIBW_TEST_VAR_2 should equal "1". It was "%s"' % CIBW_TEST_VAR_2) + +setup( + name="spam", + ext_modules=[Extension('spam', sources=['spam.c'])], + version="0.1.0", +) diff --git a/test/05_environment/spam.c b/test/05_environment/spam.c new file mode 100644 index 00000000..d1ab0f22 --- /dev/null +++ b/test/05_environment/spam.c @@ -0,0 +1,48 @@ +#include + +static PyObject * +spam_system(PyObject *self, PyObject *args) +{ + const char *command; + int sts; + + if (!PyArg_ParseTuple(args, "s", &command)) + return NULL; + sts = system(command); + return PyLong_FromLong(sts); +} + +/* Module initialization */ + +#if PY_MAJOR_VERSION >= 3 + #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void) + #define MOD_DEF(m, name, doc, methods, module_state_size) \ + static struct PyModuleDef moduledef = { \ + PyModuleDef_HEAD_INIT, name, doc, module_state_size, methods, }; \ + m = PyModule_Create(&moduledef); + #define MOD_RETURN(m) return m; +#else + #define MOD_INIT(name) PyMODINIT_FUNC init##name(void) + #define MOD_DEF(m, name, doc, methods, module_state_size) \ + m = Py_InitModule3(name, methods, doc); + #define MOD_RETURN(m) return; +#endif + +static PyMethodDef module_methods[] = { + {"system", (PyCFunction)spam_system, METH_VARARGS, + "Execute a shell command."}, + {NULL} /* Sentinel */ +}; + +MOD_INIT(spam) +{ + PyObject* m; + + MOD_DEF(m, + "spam", + "Example module", + module_methods, + -1) + + MOD_RETURN(m) +} diff --git a/test/05_environment/version.txt b/test/05_environment/version.txt new file mode 100644 index 00000000..3e4354cb --- /dev/null +++ b/test/05_environment/version.txt @@ -0,0 +1,2 @@ +3.6.0 (default, Feb 7 2017, 23:55:32) +[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] \ No newline at end of file