Skip to content

Commit

Permalink
Allow tests to use the data files in the source tree
Browse files Browse the repository at this point in the history
We don't ship these in the package,
but do want to run the tests that use them

tests_path() is removed completely because it is unclear whether it
should point to the tests code or the directory above the test data

Author: Rebecca N. Palmer <rebecca_palmer@zoho.com>
Forwarded: pandas-dev/pandas#54907


Gbp-Pq: Name find_test_data.patch
  • Loading branch information
Debian Science Team authored and rebecca-palmer committed Apr 21, 2024
1 parent 11ae3b8 commit 67bc1ae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
20 changes: 11 additions & 9 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TYPE_CHECKING,
Callable,
)
import argparse

from dateutil.tz import (
tzlocal,
Expand Down Expand Up @@ -107,6 +108,7 @@ def pytest_addoption(parser) -> None:
action="store_false",
help="Don't fail if a test is skipped for missing data file.",
)
parser.addoption("--deb-data-root-dir", action="store", help=argparse.SUPPRESS) # for internal use of the Debian CI infrastructure, may change without warning. Security note: test_pickle can run arbitrary code from this directory


def ignore_doctest_warning(item: pytest.Item, path: str, message: str) -> None:
Expand Down Expand Up @@ -1169,17 +1171,15 @@ def strict_data_files(pytestconfig):


@pytest.fixture
def tests_path() -> Path:
return Path(__file__).parent / "tests"
def tests_io_data_path(pytestconfig) -> Path:
BASE_PATH = pytestconfig.getoption("--deb-data-root-dir", default=None)
if BASE_PATH is None:
BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")
return Path(BASE_PATH) / "io" / "data"


@pytest.fixture
def tests_io_data_path(tests_path) -> Path:
return tests_path / "io" / "data"


@pytest.fixture
def datapath(strict_data_files: str) -> Callable[..., str]:
def datapath(strict_data_files: str, pytestconfig) -> Callable[..., str]:
"""
Get the path to a data file.
Expand All @@ -1197,7 +1197,9 @@ def datapath(strict_data_files: str) -> Callable[..., str]:
ValueError
If the path doesn't exist and the --no-strict-data-files option is not set.
"""
BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")
BASE_PATH = pytestconfig.getoption("--deb-data-root-dir", default=None)
if BASE_PATH is None:
BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")

def deco(*args):
path = os.path.join(BASE_PATH, *args)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def tpl_table(env):
return env.get_template("html_table.tpl")


def test_html_template_extends_options():
def test_html_template_extends_options(datapath):
# make sure if templates are edited tests are updated as are setup fixtures
# to understand the dependency
with open("pandas/io/formats/templates/html.tpl", encoding="utf-8") as file:
with open(datapath("../io/formats/templates/html.tpl"), encoding="utf-8") as file:
result = file.read()
assert "{% include html_style_tpl %}" in result
assert "{% include html_table_tpl %}" in result
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_pickles(datapath):
pytest.skip("known failure on non-little endian")

# For loop for compat with --strict-data-files
for legacy_pickle in Path(__file__).parent.glob("data/legacy_pickle/*/*.p*kl*"):
for legacy_pickle in Path(datapath("io", "data", "legacy_pickle")).glob("*/*.p*kl*"):
legacy_pickle = datapath(legacy_pickle)

data = pd.read_pickle(legacy_pickle)
Expand Down Expand Up @@ -574,7 +574,7 @@ def test_pickle_big_dataframe_compression(protocol, compression):
def test_pickle_frame_v124_unpickle_130(datapath):
# GH#42345 DataFrame created in 1.2.x, unpickle in 1.3.x
path = datapath(
Path(__file__).parent,
"io",
"data",
"legacy_pickle",
"1.2.4",
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,14 @@ def test_empty_string_etree(val):
read_xml(BytesIO(val), parser="etree")


def test_wrong_file_path(parser):
@pytest.mark.xfail(reason="broken by etree changes", strict=False)
def test_wrong_file_path(parser, datapath):
msg = (
"Passing literal xml to 'read_xml' is deprecated and "
"will be removed in a future version. To read from a "
"literal string, wrap it in a 'StringIO' object."
)
filename = os.path.join("data", "html", "books.xml")
filename = os.path.join(datapath("io", "data", "html"), "books.xml")

with pytest.raises(
FutureWarning,
Expand Down Expand Up @@ -1357,17 +1358,16 @@ def test_stylesheet_with_etree(kml_cta_rail_lines, xsl_flatten_doc):


@pytest.mark.parametrize("val", ["", b""])
def test_empty_stylesheet(val):
def test_empty_stylesheet(val, datapath):
pytest.importorskip("lxml")
msg = (
"Passing literal xml to 'read_xml' is deprecated and "
"will be removed in a future version. To read from a "
"literal string, wrap it in a 'StringIO' object."
)
kml = os.path.join("data", "xml", "cta_rail_lines.kml")
kml = datapath("io", "data", "xml", "cta_rail_lines.kml")

with pytest.raises(FutureWarning, match=msg):
read_xml(kml, stylesheet=val)
read_xml(kml, stylesheet=val)


# ITERPARSE
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/util/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_datapath_missing(datapath):
datapath("not_a_file")


@pytest.mark.xfail(reason="--deb-data-root-dir intentionally breaks this", strict=False)
def test_datapath(datapath):
args = ("io", "data", "csv", "iris.csv")

Expand Down

0 comments on commit 67bc1ae

Please sign in to comment.