Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict files written by tests #1061

Merged
merged 22 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trigger:
variables:
PIP_CACHE_DIR: $(Pipeline.Workspace)/.pip
RUN_COVERAGE: no
PYTEST_ADDOPTS: --color=yes --junitxml=junit/test-results.xml
PYTEST_ADDOPTS: --color=yes --nunit-xml=test-data/test-results.xml
PRERELEASE_DEPENDENCIES: no

jobs:
Expand Down Expand Up @@ -37,15 +37,13 @@ jobs:
displayName: Cache pip packages

- script: |
python -m pip install --upgrade pip
pip install pytest-cov wheel
python -m pip install --upgrade pip wheel
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
pip install .[dev,test]
displayName: "Install dependencies"
condition: eq(variables['PRERELEASE_DEPENDENCIES'], 'no')

- script: |
python -m pip install --pre --upgrade pip
pip install --pre pytest-cov wheel
python -m pip install --pre --upgrade pip wheel
pip install --pre .[dev,test]
displayName: "Install dependencies release candidates"
condition: eq(variables['PRERELEASE_DEPENDENCIES'], 'yes')
Expand All @@ -67,14 +65,14 @@ jobs:
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: "$(System.DefaultWorkingDirectory)/**/coverage.xml"
reportDirectory: "$(System.DefaultWorkingDirectory)/**/htmlcov"
summaryFileLocation: "test-data/coverage.xml"
condition: eq(variables['RUN_COVERAGE'], 'yes')

- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: "junit/test-*.xml"
testResultsFiles: "test-data/test-results.xml"
testResultsFormat: NUnit
testRunTitle: "Publish test results for Python $(python.version)"

- script: bash <(curl -s https://codecov.io/bash)
Expand Down
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
__pycache__/

# Distribution / packaging
/build/
/dist/
/anndata/_version.py
/*.egg-info/
/requirements*.lock
/.python-version
/hatch.toml

# Tests and coverage
/*cache/
/test-data/
/data/
/tmp.zarr/
test.h5ad
test.loom
test.zarr
.coverage

# jupyter
.ipynb_checkpoints
Expand Down
36 changes: 35 additions & 1 deletion anndata/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from __future__ import annotations

from contextlib import AbstractContextManager
from dataclasses import dataclass, field

from functools import singledispatch, wraps
from codecs import decode
from inspect import signature, Parameter
import os
from pathlib import Path
from typing import Any, Tuple, Union, Mapping, Optional
from warnings import warn

Expand All @@ -24,7 +29,31 @@ class Empty:
H5Array = h5py.Dataset


# try importing zarr, dask, and zappy
#############################
# stdlib
#############################


try:
from contextlib import chdir
except ImportError: # Python < 3.11

@dataclass
class chdir(AbstractContextManager):
path: Path
_old_cwd: list[Path] = field(default_factory=list)

def __enter__(self) -> None:
self._old_cwd.append(Path())
os.chdir(self.path)

def __exit__(self, *_exc_info) -> None:
os.chdir(self._old_cwd.pop())


flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
#############################
# Optional deps
#############################

try:
from zarr.core import Array as ZarrArray
Expand Down Expand Up @@ -105,6 +134,11 @@ def __repr__():
return "mock cupy.ndarray"


#############################
# IO helpers
#############################


@singledispatch
def _read_attr(attrs: Mapping, name: str, default: Optional[Any] = Empty):
if default is Empty:
Expand Down
34 changes: 28 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
# This file exists just to allow ignoring warnings without test collection failing on CI
# TODO: Fix that
# This file exists
# 1. to allow ignoring warnings without test collection failing on CI
# 2. as a pytest plugin/config that applies to doctests as well
# TODO: Fix that, e.g. with the `pytest -p anndata.testing._pytest` pattern.

from pathlib import Path

import pytest

from anndata.compat import chdir


doctest_marker = pytest.mark.usefixtures("doctest_env")


@pytest.fixture
def doctest_env(cache: pytest.Cache, tmp_path: Path) -> None:
from scanpy import settings

old_dd, settings.datasetdir = settings.datasetdir, cache.mkdir("scanpy-data")
with chdir(tmp_path):
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
yield
settings.datasetdir = old_dd


def pytest_itemcollected(item):
"""Defining behavior of pytest.mark.gpu"""
"""Define behavior of pytest.mark.gpu and doctests."""
from importlib.util import find_spec

gpu = len([mark for mark in item.iter_markers(name="gpu")]) > 0

if gpu:
is_gpu = len([mark for mark in item.iter_markers(name="gpu")]) > 0
if is_gpu:
item.add_marker(
pytest.mark.skipif(not find_spec("cupy"), reason="Cupy not installed.")
)

if isinstance(item, pytest.DoctestItem):
item.add_marker(doctest_marker)
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ test = [
"loompy>=3.0.5",
"pytest>=6.0",
"pytest-cov>=2.10",
"pytest-nunit",
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
"zarr",
"matplotlib",
"scikit-learn",
Expand All @@ -107,13 +108,16 @@ source = "vcs"
version-file = "anndata/_version.py"

[tool.coverage.run]
data_file = "test-data/coverage"
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
source = ["anndata"]
omit = [
"setup.py",
"versioneer.py",
"anndata/_version.py",
"**/test_*.py",
]
[tool.coverage.xml]
output = "test-data/coverage.xml"
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved

[tool.pytest.ini_options]
addopts = "--doctest-modules"
Expand Down
Loading