Skip to content

Commit f623990

Browse files
authored
Refactor tests into scripts (#832)
* Complete testing script. * Test scripts in CI and update `contributing.md` to use script.
1 parent ada7871 commit f623990

16 files changed

+110
-34
lines changed

.github/workflows/ci.yml

+8-28
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ jobs:
3030
env:
3131
PYTHON_VERSION: ${{ matrix.python }}
3232
NUMBA_BOUNDSCHECK: ${{ matrix.numba_boundscheck }}
33-
PYTHONFAULTHANDLER: '${{ github.workspace }}/faulthandler.log'
3433
steps:
3534
- name: Checkout Repo
3635
uses: actions/checkout@v4
@@ -49,15 +48,7 @@ jobs:
4948
run: |
5049
pip install -e '.[tests]'
5150
- name: Run tests
52-
run: |
53-
if [ $(python -c 'import numpy as np; print(np.lib.NumpyVersion(np.__version__) >= "2.0.0a1")') = 'True' ]; then
54-
pytest --pyargs sparse --doctest-modules --cov-report=xml:coverage_Numba.xml -n auto -vvv
55-
else
56-
pytest --pyargs sparse --cov-report=xml:coverage_Numba.xml -n auto -vvv
57-
fi
58-
python -c 'import finch'
59-
SPARSE_BACKEND=Finch pytest --pyargs sparse/tests --cov-report=xml:coverage_Finch.xml -n auto -vvv
60-
SPARSE_BACKEND=MLIR pytest --pyargs sparse/mlir_backend --cov-report=xml:coverage_MLIR.xml -n auto -vvv
51+
run: ci/test_backends.sh
6152
- uses: codecov/codecov-action@v5
6253
if: always()
6354
with:
@@ -79,8 +70,7 @@ jobs:
7970
pip install -U setuptools wheel
8071
pip install '.[finch]' scipy
8172
- name: Run examples
82-
run: |
83-
source ci/test_examples.sh
73+
run: ci/test_examples.sh
8474

8575
notebooks:
8676
runs-on: ubuntu-latest
@@ -96,46 +86,36 @@ jobs:
9686
run: |
9787
pip install -e '.[notebooks]'
9888
- name: Run notebooks
99-
run: |
100-
source ci/test_notebooks.sh
89+
run: ci/test_notebooks.sh
10190

10291
array_api_tests:
10392
strategy:
10493
matrix:
10594
backend: ['Numba', 'Finch']
10695
fail-fast: false
96+
env:
97+
ARRAY_API_TESTS_DIR: ${{ github.workspace }}/array-api-tests
10798
runs-on: ubuntu-latest
10899
steps:
109100
- name: Checkout Repo
110101
uses: actions/checkout@v4
111102
- name: Checkout array-api-tests
112-
uses: actions/checkout@v4
113-
with:
114-
repository: data-apis/array-api-tests
115-
ref: '33f2d2ea2f3dd2b3ceeeb4519d55e08096184149' # Latest commit as of 2024-05-29
116-
submodules: 'true'
117-
path: 'array-api-tests'
103+
run: ci/clone_array_api_tests.sh
118104
- name: Set up Python
119105
uses: actions/setup-python@v5
120106
with:
121107
python-version: '3.11'
122108
cache: 'pip'
123109
- name: Install build and test dependencies from PyPI
124110
run: |
125-
pip install pytest-xdist -r array-api-tests/requirements.txt
111+
pip install pytest-xdist -r "$ARRAY_API_TESTS_DIR/requirements.txt"
126112
- name: Build and install Sparse
127113
run: |
128114
pip install '.[finch]'
129115
- name: Run the test suite
130116
env:
131-
ARRAY_API_TESTS_MODULE: sparse
132117
SPARSE_BACKEND: ${{ matrix.backend }}
133-
run: |
134-
cd ${GITHUB_WORKSPACE}/array-api-tests
135-
if [ "${SPARSE_BACKEND}" = "Finch" ]; then
136-
python -c 'import finch'
137-
fi
138-
pytest array_api_tests -v -c pytest.ini -n auto --max-examples=2 --derandomize --disable-deadline -o xfail_strict=True --xfails-file ${GITHUB_WORKSPACE}/ci/${{ matrix.backend }}-array-api-xfails.txt --skips-file ${GITHUB_WORKSPACE}/ci/${{ matrix.backend }}-array-api-skips.txt
118+
run: ci/test_array_api.sh
139119

140120
on:
141121
# Trigger the workflow on push or pull request,

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ results/
8484
# Notebooks converted to scripts.
8585
docs/examples_ipynb/
8686

87-
# Pixi envs
87+
# Envs
8888
.pixi/
8989
pixi.lock
90+
.venv/

ci/array-api-tests-rev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
33f2d2ea2f3dd2b3ceeeb4519d55e08096184149

ci/clone_array_api_tests.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
ARRAY_API_TESTS_DIR="${ARRAY_API_TESTS_DIR:-"../array-api-tests"}"
5+
if [ ! -d "$ARRAY_API_TESTS_DIR" ]; then
6+
git clone --recursive https://github.com/data-apis/array-api-tests.git "$ARRAY_API_TESTS_DIR"
7+
fi
8+
9+
git --git-dir="$ARRAY_API_TESTS_DIR/.git" --work-tree "$ARRAY_API_TESTS_DIR" clean -xddf
10+
git --git-dir="$ARRAY_API_TESTS_DIR/.git" --work-tree "$ARRAY_API_TESTS_DIR" fetch
11+
git --git-dir="$ARRAY_API_TESTS_DIR/.git" --work-tree "$ARRAY_API_TESTS_DIR" reset --hard $(cat "ci/array-api-tests-rev.txt")

ci/setup_env.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
if [ ! -d ".venv" ]; then
5+
python -m venv .venv
6+
source .venv/bin/activate
7+
pip install -e .[all]
8+
source ci/clone_array_api_tests.sh
9+
pip install -r ../array-api-tests/requirements.txt
10+
pip uninstall -y matrepr
11+
fi

ci/test_Finch.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
python -c 'import finch'
5+
PYTHONFAULTHANDLER="${HOME}/faulthandler.log" SPARSE_BACKEND=Finch pytest --pyargs sparse/tests --cov-report=xml:coverage_Finch.xml -n auto -vvv

ci/test_MLIR.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
SPARSE_BACKEND=MLIR pytest --pyargs sparse/mlir_backend --cov-report=xml:coverage_MLIR.xml -n auto -vvv

ci/test_Numba.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
if [ $(python -c 'import numpy as np; print(np.lib.NumpyVersion(np.__version__) >= "2.0.0a1")') = 'True' ]; then
5+
pytest --pyargs sparse --doctest-modules --cov-report=xml:coverage_Numba.xml -n auto -vvv
6+
else
7+
pytest --pyargs sparse --cov-report=xml:coverage_Numba.xml -n auto -vvv
8+
fi

ci/test_all.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
ACTIVATE_VENV="${ACTIVATE_VENV:-0}"
5+
6+
if [ $ACTIVATE_VENV = "1" ]; then
7+
source .venv/bin/activate
8+
fi
9+
10+
source ci/test_backends.sh
11+
source ci/test_examples.sh
12+
source ci/test_notebooks.sh
13+
SPARSE_BACKEND="Numba" source ci/test_array_api.sh
14+
SPARSE_BACKEND="Finch" PYTHONFAULTHANDLER="${HOME}/faulthandler.log" source ci/test_array_api.sh

ci/test_array_api.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
source ci/clone_array_api_tests.sh
5+
6+
if [ "${SPARSE_BACKEND}" = "Finch" ]; then
7+
python -c 'import finch'
8+
fi
9+
ARRAY_API_TESTS_MODULE="sparse" pytest "$ARRAY_API_TESTS_DIR/array_api_tests/" -v -c "$ARRAY_API_TESTS_DIR/pytest.ini" --ci --max-examples=2 --derandomize --disable-deadline -o xfail_strict=True -n auto --xfails-file ../sparse/ci/${SPARSE_BACKEND}-array-api-xfails.txt --skips-file ../sparse/ci/${SPARSE_BACKEND}-array-api-skips.txt

ci/test_backends.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
source ci/test_Numba.sh
5+
source ci/test_Finch.sh
6+
source ci/test_MLIR.sh

ci/test_examples.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
14
for example in $(find ./docs/examples/ -iname *.py); do
25
CI_MODE=1 python $example
36
done

ci/test_notebooks.sh

100644100755
+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
14
CI_MODE=1 pytest -n 4 --nbmake --nbmake-timeout=600 ./examples/*.ipynb

docs/contributing.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ check code style. You don't need to configure these extensions yourself. Once yo
6969
configured your environment, you can just `cd` to the root of your repository and run
7070

7171
```bash
72-
7372
pytest --pyargs sparse
7473
```
7574

@@ -78,6 +77,16 @@ even though it doesn't fail on low coverage.
7877

7978
Unit tests are automatically run on Travis CI for pull requests.
8079

80+
### Advanced
81+
82+
To run the complete set of unit tests run in CI for your platform, run the following
83+
in the repository root:
84+
85+
```bash
86+
ci/setup_env.sh
87+
ACTIVATE_VENV=1 ci/test_all.sh
88+
```
89+
8190
## Coverage
8291

8392
The `pytest` script automatically reports coverage, both on the terminal for

pixi.toml

+13-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ mkdocs-section-index = "*"
2626
mkdocs-jupyter = "*"
2727

2828
[feature.test.tasks]
29-
test = "pytest -n auto --doctest-modules"
30-
test-mlir = "pytest --pyargs sparse.mlir_backend -v"
31-
test-finch = { cmd = "pytest --pyargs sparse/tests -n auto -v", depends-on = ["precompile"] }
29+
test = "ci/test_Numba.sh"
30+
test-mlir = "ci/test_MLIR.sh"
31+
test-finch = "ci/test_Finch.sh"
3232

3333
[feature.test.pypi-dependencies]
3434
pytest = ">=3.5"
@@ -71,6 +71,15 @@ scipy = ">=0.19"
7171
finch-mlir = ">=0.0.2"
7272
"PyYAML" = "*"
7373

74+
[feature.barebones.dependencies]
75+
python = ">=3.10,<3.13"
76+
pip = ">=24"
77+
78+
[feature.barebones.tasks]
79+
setup-env = {cmd = "ci/setup_env.sh" }
80+
test-all = { cmd = "ci/test_all.sh", env = { ACTIVATE_VENV = "1" }, depends-on = ["setup-env"] }
81+
test-finch = "ci/test_Finch.sh"
82+
7483
[feature.mlir.activation.env]
7584
SPARSE_BACKEND = "MLIR"
7685

@@ -80,3 +89,4 @@ doc = ["doc", "extra"]
8089
mlir-dev = {features = ["test", "mlir"], no-default-feature = true}
8190
finch-dev = {features = ["test", "finch"], no-default-feature = true}
8291
notebooks = ["extra", "mlir", "finch", "notebooks"]
92+
barebones = {features = ["barebones"], no-default-feature = true}

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ tests = [
5353
]
5454
tox = ["sparse[tests]", "tox"]
5555
notebooks = ["sparse[tests]", "nbmake", "matplotlib"]
56-
all = ["sparse[docs,tox,notebooks]", "matrepr"]
56+
all = ["sparse[docs,tox,notebooks,mlir]", "matrepr"]
5757
finch = ["finch-tensor>=0.2.3"]
58+
mlir = ["finch-mlir>=0.0.2"]
5859

5960
[project.urls]
6061
Documentation = "https://sparse.pydata.org/"

0 commit comments

Comments
 (0)