Skip to content

Commit

Permalink
chore: Remove excess test data from bidsschematools installation dire…
Browse files Browse the repository at this point in the history
…ctories (#1985)

* chore: Move bidsschematools to src-layout

* chore: Update locations in .pre-commit-config.yaml

* chore: Ignore tests/data/bids*-examples

* test: Save test data into tests/data

* chore: Update package data discovery

* chore(ci): Make sure schemacode tests are triggered on maintenance branches

* chore(ci): Update pytest invocation

* chore: Ensure coverage is collected from tests and installed modules
  • Loading branch information
effigies authored Nov 14, 2024
1 parent bb139e9 commit 9540a82
Show file tree
Hide file tree
Showing 39 changed files with 86 additions and 78 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/schemacode_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ on:
push:
branches:
- "master"
- "maint/*"
tags:
- "schema-*"
pull_request:
branches:
- "master"
- "maint/*"
- "*"

concurrency:
Expand Down Expand Up @@ -87,14 +90,15 @@ jobs:
- name: "Run tests"
run: |
python -m pytest -vs --pyargs bidsschematools -m "not validate_schema" \
--cov-append --cov-report=xml --cov=bidsschematools --doctest-modules
python -m pytest -vs --doctest-modules -m "not validate_schema" \
--cov-append --cov-report=xml --cov-report=term --cov=src/bidsschematools
working-directory: tools/schemacode

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: unit_${{ matrix.os }}_${{ matrix.python-version }}
path: coverage.xml
path: tools/schemacode/coverage.xml
if: success()

publish:
Expand Down Expand Up @@ -143,13 +147,16 @@ jobs:
python -m pip install -e ./tools/schemacode[all]
- name: Run schema validation tests
run: python -m pytest --pyargs bidsschematools -m "validate_schema" --cov-append --cov-report=xml --cov=bidsschematools
run: |
python -m pytest -vs --doctest-modules -m "not validate_schema" \
--cov-append --cov-report=xml --cov-report=term --cov=src/bidsschematools
working-directory: tools/schemacode

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: schema_validation
path: coverage.xml
path: tools/schemacode/coverage.xml
if: success()

upload_to_codecov:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
ci:
skip: [shellcheck]
exclude: 'tools/schemacode/bidsschematools/tests/data/broken_dataset_description.json'
exclude: 'tools/schemacode/src/bidsschematools/tests/data/broken_dataset_description.json'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand Down Expand Up @@ -83,7 +83,7 @@ repos:
- types-jsonschema
- jsonschema
- httpx
args: ["tools/schemacode/bidsschematools"]
args: ["tools/schemacode/src"]
pass_filenames: false
- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
Expand Down
1 change: 1 addition & 0 deletions tools/schemacode/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include tests *
1 change: 0 additions & 1 deletion tools/schemacode/bidsschematools/data/metaschema.json

This file was deleted.

1 change: 0 additions & 1 deletion tools/schemacode/bidsschematools/data/schema

This file was deleted.

48 changes: 0 additions & 48 deletions tools/schemacode/bidsschematools/tests/test_make_testdata.py

This file was deleted.

10 changes: 6 additions & 4 deletions tools/schemacode/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ markers = [
"validate_schema: tests that validate the schema itself",
]

[tool.coverage.paths]
source = [
"src/bidsschematools",
"**/site-packages/bidsschematools",
]

[tool.coverage.run]
parallel = true
omit = [
"*/*/tests/*",
"**/tests/*"
]
7 changes: 2 additions & 5 deletions tools/schemacode/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = bidsschematools
version = file:bidsschematools/data/schema/SCHEMA_VERSION
version = file:src/bidsschematools/data/schema/SCHEMA_VERSION
url = https://github.com/bids-standard/bids-specification
author = bids-standard developers
author_email = bids.maintenance@gmail.com
Expand All @@ -25,8 +25,6 @@ install_requires =
pyyaml
importlib_resources; python_version < "3.9"
jsonschema
packages = find:
include_package_data = false
zip_safe = false

[options.extras_require]
Expand Down Expand Up @@ -59,8 +57,7 @@ bidsschematools =
data/schema/BIDS_VERSION
data/schema/SCHEMA_VERSION
data/schema/**/*.yaml
tests/data/**/*
tests/data/**/.bidsignore
tests/data/*

[options.entry_points]
console_scripts =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import logging
import tempfile
from pathlib import Path
from subprocess import run

try:
from importlib.resources import as_file, files
except ImportError: # PY<3.9
from importlib_resources import as_file, files

import pytest

lgr = logging.getLogger()
Expand Down Expand Up @@ -35,23 +31,37 @@
]


@pytest.fixture(scope="session")
def tests_data_dir():
try:
this_file = Path(__file__)
except NameError:
return None

data_dir = this_file.parent.parent.parent / "tests" / "data"

if data_dir.exists():
return data_dir


def get_gitrepo_fixture(url, whitelist):
@pytest.fixture(scope="session")
def fixture():
def fixture(tests_data_dir):
if tests_data_dir is None:
pytest.skip("No test data directory found; probably in an installed package")
archive_name = url.rsplit("/", 1)[-1]
testdata_dir = files("bidsschematools.tests.data") / archive_name
if testdata_dir.is_dir():
archive_dir = tests_data_dir / archive_name
if archive_dir.is_dir():
lgr.info(
f"Found static testdata archive under `{testdata_dir}`. "
f"Found static testdata archive under `{archive_dir}`. "
"Not downloading latest data from version control."
)
with as_file(testdata_dir) as path:
yield path
yield archive_dir
else:
lgr.info(
"No static testdata available under `%s`. "
"Attempting to fetch live data from version control.",
testdata_dir,
archive_dir,
)
with tempfile.TemporaryDirectory() as path:
lgr.debug("Cloning %r into %r", url, path)
Expand Down
1 change: 1 addition & 0 deletions tools/schemacode/src/bidsschematools/data/metaschema.json
1 change: 1 addition & 0 deletions tools/schemacode/src/bidsschematools/data/schema
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions tools/schemacode/src/bidsschematools/tests/test_make_testdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import shutil

import pytest


def require_env(var):
env = os.environ.get(var)
return pytest.mark.skipif(
not env, reason=f"To activate this test/feature `export {var}=1` and re-run."
)


@require_env("BIDSSCHEMATOOLS_RELEASE")
def test_make_archive(tests_data_dir, bids_examples, bids_error_examples):
"""
ATTENTION! This is not a test!
Create static testdata archive containing the bidsschematools data reference whitelist.
Notes
-----
Due to intricacies arising from:
(1) fixtures not working outside of pytest
(2) implicit teardown leveraging tempdata removal (while held open by yield)
(3) wrappers evaluating the yield statement
(4) the desire to not download testdata twice for archive creation
testdata archive creation is now inconspicuously posing as a test.
"""

ignore_git = shutil.ignore_patterns(".git*")
target_examples = tests_data_dir / "bids-examples"
target_error_examples = tests_data_dir / "bids-error-examples"
if bids_examples != target_examples:
shutil.copytree(bids_examples, target_examples, ignore=ignore_git)
if bids_error_examples != target_error_examples:
shutil.copytree(bids_error_examples, target_error_examples, ignore=ignore_git)
File renamed without changes.
1 change: 1 addition & 0 deletions tools/schemacode/tests/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bids*-examples

0 comments on commit 9540a82

Please sign in to comment.