From d5b9347c59afe003b888cd1843111b42cf109a80 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Fri, 26 May 2023 12:37:07 -0400 Subject: [PATCH] RF: Use data loaders in place of __file__ --- .../bidsschematools/tests/test_schema.py | 9 ++--- .../bidsschematools/tests/test_validator.py | 34 ++++++------------- tools/schemacode/bidsschematools/utils.py | 5 +-- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/tools/schemacode/bidsschematools/tests/test_schema.py b/tools/schemacode/bidsschematools/tests/test_schema.py index c5a1652d01..73041b0b3b 100644 --- a/tools/schemacode/bidsschematools/tests/test_schema.py +++ b/tools/schemacode/bidsschematools/tests/test_schema.py @@ -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__ diff --git a/tools/schemacode/bidsschematools/tests/test_validator.py b/tools/schemacode/bidsschematools/tests/test_validator.py index a28ed2d80e..fd808fb709 100644 --- a/tools/schemacode/bidsschematools/tests/test_validator.py +++ b/tools/schemacode/bidsschematools/tests/test_validator.py @@ -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 = [ @@ -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( @@ -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 @@ -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. diff --git a/tools/schemacode/bidsschematools/utils.py b/tools/schemacode/bidsschematools/utils.py index 76b6034efa..308fd981f6 100644 --- a/tools/schemacode/bidsschematools/utils.py +++ b/tools/schemacode/bidsschematools/utils.py @@ -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(): @@ -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):