thrnciar / tests / python

Forked from tests/python 4 years ago
Clone

Blame smoke/venv.sh

42e0692
#!/bin/sh -eux
42e0692
41eef2c
# set python version
41eef2c
VERSION=${VERSION:-3.7}
42e0692
PYTHON=python$VERSION
aa5f2fe
METHOD=${METHOD:-venv}
42e0692
42e0692
# clean from possible older runs
42e0692
rm -rf venv .tox __pycache__ .pytest* test_*.py *.pyx *.c *.so || :
42e0692
42e0692
# check the we can create the venv
aa5f2fe
if [ "$METHOD" == "venv" ]; then
aa5f2fe
  $PYTHON -m venv venv
aa5f2fe
elif [ "$METHOD" == "virtualenv" ]; then
aa5f2fe
  virtualenv --python=$PYTHON venv
aa5f2fe
else
aa5f2fe
  echo 'Unsupported $METHOD' $METHOD >&2
aa5f2fe
exit 1
aa5f2fe
fi
aa5f2fe
42e0692
42e0692
# and activate it
a6067a8
# unset variables on 3.5, it's known
a6067a8
set +u
42e0692
source venv/bin/activate
a6067a8
set -u
42e0692
42e0692
# run python in it
42e0692
python -c 'import sys; print(sys.version)' | head -n1 | grep $VERSION
42e0692
42e0692
# install packages with pip
42e0692
python -m pip install pytest
42e0692
42e0692
# run tests
42e0692
cat > test_foo.py << EOF
42e0692
def test_foo():
42e0692
    assert True
42e0692
EOF
42e0692
42e0692
python -m pytest -v test_foo.py
42e0692
42e0692
# check that we can do extension modules
42e0692
cat > module.pyx << EOF
42e0692
cdef int add(int a, int b):
42e0692
    return a + b
42e0692
42e0692
def two():
42e0692
    cdef int a = 1
42e0692
    cdef int b = 1
42e0692
    return add(a, b)
42e0692
EOF
42e0692
42e0692
cat > setup.py << EOF
42e0692
from setuptools import setup
42e0692
from Cython.Build import cythonize
42e0692
42e0692
setup(ext_modules = cythonize('module.pyx'))
42e0692
EOF
42e0692
42e0692
python -m pip install Cython
42e0692
python setup.py build_ext --inplace
42e0692
42e0692
python -c 'import module; print(module.two())' | grep '^2$'
42e0692
42e0692
# deactivate has unset variable, it's known
42e0692
set +u
42e0692
deactivate
42e0692
set -u
42e0692
42e0692
# use it with tox
42e0692
cat > tox.ini << EOF
42e0692
[tox]
42e0692
skipsdist = True
42e0692
[testenv]
42e0692
deps = pytest
42e0692
commands = python -m pytest -v test_foo.py
42e0692
EOF
42e0692
42e0692
tox -e py${VERSION/./}