Skip to content

Commit

Permalink
RF: Use data loaders in place of __file__
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jun 5, 2023
1 parent 6fefcf9 commit d5b9347
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
9 changes: 3 additions & 6 deletions tools/schemacode/bidsschematools/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

from bidsschematools import __bids_version__, schema, types

from ..data import load_resource


def test__get_bids_version(tmp_path):
# Is the version being read in correctly?
schema_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
os.pardir,
"data",
"schema",
)
schema_path = str(load_resource("schema"))
bids_version = schema._get_bids_version(schema_path)
assert bids_version == __bids_version__

Expand Down
34 changes: 10 additions & 24 deletions tools/schemacode/bidsschematools/tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from bidsschematools.conftest import BIDS_ERROR_SELECTION, BIDS_SELECTION
from bidsschematools.validator import select_schema_path, validate_bids

from ..data import load_resource
from .data import load_test_data


def test_inheritance_examples():
correct_inheritance = [
Expand Down Expand Up @@ -68,20 +71,11 @@ def test_write_report(tmp_path):
"rawdata/sub-EXC022/anat/sub-EXC022_ses-MRI_flip-1_VFA.nii.gz"
]

report_path = os.path.join(
tmp_path,
"output_bids_validator_xs_write.log",
)
expected_report_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"data/expected_bids_validator_xs_write.log",
)
write_report(validation_result, report_path=report_path)
with open(report_path, "r") as f:
report_text = f.read()
with open(expected_report_path, "r") as f:
expected_report_text = f.read()
assert report_text == expected_report_text
report_path = tmp_path / "output_bids_validator_xs_write.log"
write_report(validation_result, report_path=str(report_path))

expected_report_path = load_test_data("expected_bids_validator_xs_write.log")
assert report_path.read_text() == expected_report_path.read_text()


@pytest.mark.skipif(
Expand Down Expand Up @@ -125,12 +119,7 @@ def test_validate_bids(bids_examples, tmp_path):
assert len(result["path_tracking"]) == 0

# Is the schema version recorded correctly?
schema_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
os.pardir,
"data",
"schema",
)
schema_path = load_resource("schema")
with open(os.path.join(schema_path, "BIDS_VERSION")) as f:
expected_version = f.readline().rstrip()
assert result["bids_version"] == expected_version
Expand All @@ -148,10 +137,7 @@ def test_broken_json_dataset(bids_examples, tmp_path):
dataset_path = os.path.join(bids_examples, dataset)
dataset_json = os.path.join(dataset_path, "dataset_description.json")

broken_json = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"data/broken_dataset_description.json",
)
broken_json = load_test_data("broken_dataset_description.json")
shutil.copyfile(broken_json, dataset_json)

# No assert, will simply raise JSON reader error if not catching it properly.
Expand Down
5 changes: 3 additions & 2 deletions tools/schemacode/bidsschematools/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utility functions for the bids-specification schema."""
import logging
import os.path as op

from . import data


def get_bundled_schema_path():
Expand All @@ -11,7 +12,7 @@ def get_bundled_schema_path():
str
Absolute path to the directory containing schema-related files.
"""
return op.abspath(op.join(op.dirname(__file__), "data", "schema"))
return str(data.load_resource("schema"))


def get_logger(name=None):
Expand Down

0 comments on commit d5b9347

Please sign in to comment.