Skip to content

Commit

Permalink
Restrict files written by tests (#1061)
Browse files Browse the repository at this point in the history
Co-authored-by: Isaac Virshup <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 5, 2023
1 parent fea7561 commit 2c8759d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
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 --junitxml=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
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: JUnit
testRunTitle: "Publish test results for Python $(python.version)"

- script: bash <(curl -s https://codecov.io/bash)
Expand Down
17 changes: 6 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
.DS_Store
*~

# Compiled files
# Caches for compiled and downloaded files
__pycache__/
/*cache/
/data/

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

# Tests and coverage
/*cache/
/data/
/tmp.zarr/
test.h5ad
test.loom
test.zarr
.coverage
# Test results (nunit/junit) and coverage
/test-data/
/*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())


#############################
# 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):
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)

0 comments on commit 2c8759d

Please sign in to comment.