Skip to content

Commit ff4be2b

Browse files
Debian Science Teamrebecca-palmer
Debian Science Team
authored andcommitted
Allow tests to use the data files in the source tree
We don't ship these in the package, but do want to run the tests that use them Author: Rebecca N. Palmer <[email protected]> Forwarded: pandas-dev/pandas#54907 Gbp-Pq: Name find_test_data.patch
1 parent ef9c96a commit ff4be2b

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

pandas/conftest.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
TYPE_CHECKING,
3535
Callable,
3636
)
37+
import argparse
3738

3839
from dateutil.tz import (
3940
tzlocal,
@@ -114,6 +115,7 @@ def pytest_addoption(parser) -> None:
114115
action="store_false",
115116
help="Don't fail if a test is skipped for missing data file.",
116117
)
118+
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
117119

118120

119121
def ignore_doctest_warning(item: pytest.Item, path: str, message: str) -> None:
@@ -1098,7 +1100,7 @@ def strict_data_files(pytestconfig):
10981100

10991101

11001102
@pytest.fixture
1101-
def datapath(strict_data_files: str) -> Callable[..., str]:
1103+
def datapath(strict_data_files: str, pytestconfig) -> Callable[..., str]:
11021104
"""
11031105
Get the path to a data file.
11041106
@@ -1116,7 +1118,9 @@ def datapath(strict_data_files: str) -> Callable[..., str]:
11161118
ValueError
11171119
If the path doesn't exist and the --no-strict-data-files option is not set.
11181120
"""
1119-
BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")
1121+
BASE_PATH = pytestconfig.getoption("--deb-data-root-dir", default=None)
1122+
if BASE_PATH is None:
1123+
BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")
11201124

11211125
def deco(*args):
11221126
path = os.path.join(BASE_PATH, *args)

pandas/tests/io/formats/style/test_html.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def tpl_table(env):
4444
return env.get_template("html_table.tpl")
4545

4646

47-
def test_html_template_extends_options():
47+
def test_html_template_extends_options(datapath):
4848
# make sure if templates are edited tests are updated as are setup fixtures
4949
# to understand the dependency
50-
with open("pandas/io/formats/templates/html.tpl", encoding="utf-8") as file:
50+
with open(datapath("../io/formats/templates/html.tpl"), encoding="utf-8") as file:
5151
result = file.read()
5252
assert "{% include html_style_tpl %}" in result
5353
assert "{% include html_table_tpl %}" in result

pandas/tests/io/test_pickle.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_pickles(datapath):
115115
pytest.skip("known failure on non-little endian")
116116

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

121121
data = pd.read_pickle(legacy_pickle)
@@ -627,7 +627,7 @@ def test_pickle_big_dataframe_compression(protocol, compression):
627627
def test_pickle_frame_v124_unpickle_130(datapath):
628628
# GH#42345 DataFrame created in 1.2.x, unpickle in 1.3.x
629629
path = datapath(
630-
Path(__file__).parent,
630+
"io",
631631
"data",
632632
"legacy_pickle",
633633
"1.2.4",

pandas/tests/io/xml/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55

66
@pytest.fixture
7-
def xml_data_path():
8-
return Path(__file__).parent.parent / "data" / "xml"
7+
def xml_data_path(datapath):
8+
return Path(datapath("io", "data", "xml"))
99

1010

1111
@pytest.fixture

pandas/tests/io/xml/test_xml.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,13 @@ def test_empty_string_etree(val):
487487
read_xml(BytesIO(val), parser="etree")
488488

489489

490-
def test_wrong_file_path(parser):
490+
def test_wrong_file_path(parser, datapath):
491491
msg = (
492492
"Passing literal xml to 'read_xml' is deprecated and "
493493
"will be removed in a future version. To read from a "
494494
"literal string, wrap it in a 'StringIO' object."
495495
)
496-
filename = os.path.join("data", "html", "books.xml")
496+
filename = os.path.join(datapath("io", "data", "html"), "books.xml")
497497

498498
with pytest.raises(
499499
FutureWarning,
@@ -1358,17 +1358,16 @@ def test_stylesheet_with_etree(kml_cta_rail_lines, xsl_flatten_doc):
13581358

13591359

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

1370-
with pytest.raises(FutureWarning, match=msg):
1371-
read_xml(kml, stylesheet=val)
1370+
read_xml(kml, stylesheet=val)
13721371

13731372

13741373
# ITERPARSE

pandas/tests/util/test_util.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_datapath_missing(datapath):
3535
datapath("not_a_file")
3636

3737

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

0 commit comments

Comments
 (0)