Skip to content

Commit 77ffb39

Browse files
fineguyThe TensorFlow Datasets Authors
authored andcommitted
Reuse pytest fixture for setting TFDS_DATA_DIR.
PiperOrigin-RevId: 792731989
1 parent 502c65f commit 77ffb39

File tree

5 files changed

+93
-98
lines changed

5 files changed

+93
-98
lines changed

tensorflow_datasets/conftest.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@
2020
2121
See: https://docs.pytest.org/en/latest/writing_plugins.html
2222
"""
23+
2324
from __future__ import annotations
2425

2526
import builtins
2627
import importlib
28+
import os
29+
import pathlib
2730
import sys
2831
import typing
2932
from typing import Iterator, Type
3033

34+
from etils import epath
3135
import pytest
3236
from tensorflow_datasets import setup_teardown
37+
from tensorflow_datasets.core import constants
3338

3439
if typing.TYPE_CHECKING:
3540
from tensorflow_datasets import testing
@@ -52,11 +57,9 @@ def disable_community_datasets():
5257
# visibility isn't automatically set.
5358
from tensorflow_datasets.core import visibility # pylint: disable=g-import-not-at-top
5459

55-
visibility.set_availables(
56-
[
57-
visibility.DatasetType.TFDS_PUBLIC,
58-
]
59-
)
60+
visibility.set_availables([
61+
visibility.DatasetType.TFDS_PUBLIC,
62+
])
6063

6164

6265
@pytest.fixture(scope='session', autouse=True)
@@ -144,3 +147,13 @@ def dummy_dataset(
144147
return _make_dataset(tmp_path_factory, testing.DummyDataset)
145148

146149

150+
@pytest.fixture(name='default_data_dir')
151+
def mock_default_data_dir(
152+
monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path
153+
) -> epath.Path:
154+
"""Sets the default data dir to a temp dir."""
155+
default_data_dir = epath.Path(tmp_path) / 'default_data_dir'
156+
monkeypatch.setattr(constants, 'DATA_DIR', os.fspath(default_data_dir))
157+
return default_data_dir
158+
159+

tensorflow_datasets/core/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# IMPORTANT: when changing values here, update docstrings.
1919

2020
import os
21+
from typing import Final
2122

2223
# Directory in which datasets are declared within TFDS sources.
2324
DATASETS_TFDS_SRC_DIR = 'datasets'
@@ -27,7 +28,7 @@
2728

2829
# Directory where to store processed datasets.
2930
# If modifying this, should also update `scripts/cli/build.py` `--data_dir`
30-
DATA_DIR = os.environ.get(
31+
DATA_DIR: Final[str] = os.environ.get(
3132
'TFDS_DATA_DIR',
3233
os.path.join(os.path.expanduser('~'), 'tensorflow_datasets'),
3334
)

tensorflow_datasets/core/utils/file_utils_test.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,8 @@ def _assert_data_dir(
6262
assert data_dir == expected_data_dir
6363

6464

65-
@pytest.fixture(name='default_data_dir')
66-
def mock_default_data_dir(monkeypatch, tmp_path):
67-
"""Sets the default data dir to a temp dir."""
68-
default_data_dir = tmp_path / 'default_data_dir'
69-
monkeypatch.setattr(constants, 'DATA_DIR', default_data_dir)
70-
return default_data_dir
71-
72-
7365
@pytest.fixture(name='other_data_dir')
74-
def mock_other_data_dir(default_data_dir):
66+
def mock_other_data_dir(default_data_dir: epath.Path):
7567
"""Adds another data dir to the registered data dirs."""
7668
other_data_dir = default_data_dir.parent / 'other_data_dir'
7769
file_utils.add_data_dir(other_data_dir)

0 commit comments

Comments
 (0)