Skip to content

Commit

Permalink
Refactor testing to use an example package, instead of devpy itself (#25
Browse files Browse the repository at this point in the history
)

This also allows us to test the building of binaries

Closes #10
  • Loading branch information
stefanv authored Nov 23, 2022
2 parents 748ccc6 + ce2464c commit f1eb8ce
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ jobs:
pip install -e .
pip install pytest meson-python ninja
- name: Test
shell: 'script -q -e -c "bash --noprofile --norc -eo pipefail {0}"'
env:
TERM: xterm-256color
run: |
python -m devpy test --site-path=$PWD
cd example_pkg
./dev.py build
./dev.py test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ commands = ['devpy.build', 'devpy.test']
python -m devpy
```

On Unix-like systems, you can also copy the `dev.py` script to the root of your project directory, and launch it as:
On Unix-like systems, you can also copy the [`dev.py` script](https://github.com/scientific-python/devpy/blob/main/example_pkg/dev.py) to the root of your project directory, and launch it as:

```
./dev.py
Expand Down
10 changes: 3 additions & 7 deletions devpy/cmds/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
@click.option(
"--build-dir", default="build", help="Build directory; default is `$PWD/build`"
)
@click.option(
"--site-path", help="For devpy testing only; installed package path", hidden=True
)
@click.argument("pytest_args", nargs=-1)
def test(build_dir, site_path, pytest_args):
def test(build_dir, pytest_args):
"""🔧 Run tests
PYTEST_ARGS are passed through directly to pytest, e.g.:
Expand All @@ -30,9 +27,8 @@ def test(build_dir, site_path, pytest_args):
)
sys.exit(1)

if not site_path:
site_path = get_site_packages(build_dir)
set_pythonpath(build_dir)
site_path = get_site_packages(build_dir)
set_pythonpath(build_dir)

print(f'$ export PYTHONPATH="{site_path}"')
run(["pytest", f"--rootdir={site_path}"] + list(pytest_args), cwd=site_path)
4 changes: 2 additions & 2 deletions devpy/cmds/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

def run(cmd, cwd=None, replace=False, sys_exit=True, output=True, *args, **kwargs):
if cwd:
click.secho(f"$ cd {cwd}", dim=True)
click.secho(f"$ cd {cwd}", bold=True, fg="bright_blue")
os.chdir(cwd)
click.secho(f"$ {shlex.join(cmd)}", dim=True)
click.secho(f"$ {shlex.join(cmd)}", bold=True, fg="bright_blue")

if output is False:
output_kwargs = {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT}
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions example_pkg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
build-install
File renamed without changes.
1 change: 1 addition & 0 deletions example_pkg/example_pkg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._core import echo
44 changes: 44 additions & 0 deletions example_pkg/example_pkg/coremodule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *
core_echo(PyObject *self, PyObject *args)
{
const char *str;
PyObject *ret;

if (!PyArg_ParseTuple(args, "s", &str))
return NULL;

printf("%s\n", str);

ret = PyLong_FromLong(42);
Py_INCREF(ret);
return ret;
}

static PyMethodDef CoreMethods[] = {
{"echo", core_echo, METH_VARARGS, "Echo a string and return 42"},
{NULL, NULL, 0, NULL} /* Sentinel */
};

static struct PyModuleDef coremodule = {
PyModuleDef_HEAD_INIT,
"core", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
CoreMethods
};

PyMODINIT_FUNC
PyInit__core(void)
{
PyObject *m;

m = PyModule_Create(&coremodule);
if (m == NULL)
return NULL;

return m;
}
17 changes: 17 additions & 0 deletions example_pkg/example_pkg/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
py.extension_module(
'_core',
'coremodule.c',
install: true,
subdir: 'example_pkg'
)

python_sources = [
'__init__.py'
]

py.install_sources(
python_sources,
subdir: 'example_pkg'
)

install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/tests')
6 changes: 6 additions & 0 deletions example_pkg/example_pkg/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from example_pkg import echo


def test_core():
ans = echo("hello world")
assert ans == 42
20 changes: 20 additions & 0 deletions example_pkg/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
project(
'devpy-example-pkg',
'c',
version: '0.0.dev0',
license: 'BSD-3',
meson_version: '>= 0.64.0',
default_options: [
'buildtype=debugoptimized',
'c_std=c99',
'cpp_std=c++14',
],
)

cc = meson.get_compiler('c')

py_mod = import('python')
py = py_mod.find_installation(pure: false)
py_dep = py.dependency()

subdir('example_pkg')
17 changes: 17 additions & 0 deletions example_pkg/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[project]
name = "devpy-example"
version = "0.0dev0"
requires-python = ">=3.7"
description = "DevPy Example Package"

[tool.devpy]
package = 'example_pkg'

[tool.devpy.commands]
# If you don't need sections, you can also provide a list of commands under [tool.devpy]:
#
# commands = ["devpy.build", "devpy.test", "devpy.shell", "devpy.ipython", "devpy.python", "custom/cmds.py:example"]

"Build" = ["devpy.build", "devpy.test"]
"Environments" = ["devpy.shell", "devpy.ipython", "devpy.python"]
"Extensions" = [".devpy/cmds.py:example"]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ package = 'devpy'

"Build" = ["devpy.build", "devpy.test"]
"Environments" = ["devpy.shell", "devpy.ipython", "devpy.python"]
"Extensions" = ["custom/cmds.py:example"]
"Extensions" = ["example_pkg/.devpy/cmds.py:example"]

0 comments on commit f1eb8ce

Please sign in to comment.