Skip to content

Commit 972384f

Browse files
Ryan Sepassicopybara-github
authored andcommitted
Enable temporary GCS access in tests
PiperOrigin-RevId: 234192557
1 parent f0a8605 commit 972384f

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

oss_scripts/oss_tests.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,26 @@ fi
3232
TF2_IGNORE=$(for test in $TF2_IGNORE_TESTS; do echo "--ignore=$test "; done)
3333

3434
# Run Tests
35-
pytest $TF2_IGNORE --ignore="tensorflow_datasets/testing/test_utils.py"
35+
# Ignores:
36+
# * Some TF2 tests if running against TF2 (see above)
37+
# * test_utils.py is not a test file
38+
# * eager_not_enabled_by_default_test needs to be run separately because the
39+
# enable_eager_execution calls set global state and pytest runs all the tests
40+
# in the same process.
41+
pytest \
42+
--disable-warnings \
43+
$TF2_IGNORE \
44+
--ignore="tensorflow_datasets/testing/test_utils.py" \
45+
--ignore="tensorflow_datasets/eager_not_enabled_by_default_test.py"
3646
set_status
47+
# If not running with TF2, ensure Eager is not enabled by default
48+
if [[ "$TF_VERSION" != "tf2" ]]
49+
then
50+
pytest \
51+
--disable-warnings \
52+
tensorflow_datasets/eager_not_enabled_by_default_test.py
53+
set_status
54+
fi
3755

3856
# Test notebooks in isolated environments
3957
NOTEBOOKS="
@@ -54,8 +72,9 @@ function test_notebook() {
5472
set_status
5573
}
5674

57-
# Re-enable as TF 2.0 gets closer to stable release
75+
# TODO(tfds): Re-enable as TF 2.0 gets closer to stable release
5876
if [[ "$PY_BIN" = "python2.7" && "$TF_VERSION" = "tf2" ]]
77+
then
5978
echo "Skipping notebook tests"
6079
else
6180
for notebook in $NOTEBOOKS

tensorflow_datasets/core/dataset_info_test.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,17 @@ def test_restore_after_modification(self):
197197
})
198198

199199
def test_reading_from_gcs_bucket(self):
200-
mnist_builder = mnist.MNIST(
201-
data_dir=tempfile.mkdtemp(dir=self.get_temp_dir()))
202-
info = dataset_info.DatasetInfo(builder=mnist_builder)
203-
info = mnist_builder.info
204-
205-
# A nominal check to see if we read it.
206-
self.assertTrue(info.initialized)
207-
self.assertEqual(10000, info.splits["test"].num_examples)
200+
# The base TestCase prevents GCS access, so we explicitly ask it to restore
201+
# access here.
202+
with self.gcs_access():
203+
mnist_builder = mnist.MNIST(
204+
data_dir=tempfile.mkdtemp(dir=self.get_temp_dir()))
205+
info = dataset_info.DatasetInfo(builder=mnist_builder)
206+
info = mnist_builder.info
207+
208+
# A nominal check to see if we read it.
209+
self.assertTrue(info.initialized)
210+
self.assertEqual(10000, info.splits["test"].num_examples)
208211

209212
def test_str_smoke(self):
210213
info = mnist.MNIST(data_dir="/tmp/some_dummy_dir").info

tensorflow_datasets/testing/test_case.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from __future__ import division
2020
from __future__ import print_function
2121

22+
import contextlib
2223
import os
2324
import tempfile
2425

@@ -29,6 +30,12 @@
2930

3031

3132

33+
GCS_ACCESS_FNS = {
34+
"original": dataset_info.gcs_dataset_files,
35+
"dummy": lambda _: []
36+
}
37+
38+
3239
class TestCase(tf.test.TestCase):
3340
"""Base TestCase to be used for all tests.
3441
@@ -41,7 +48,15 @@ def setUpClass(cls):
4148
super(TestCase, cls).setUpClass()
4249
cls.test_data = os.path.join(os.path.dirname(__file__), "test_data")
4350
# Test must not communicate with GCS.
44-
dataset_info.gcs_dataset_files = lambda _: []
51+
dataset_info.gcs_dataset_files = GCS_ACCESS_FNS["dummy"]
52+
53+
@contextlib.contextmanager
54+
def gcs_access(self):
55+
# Restore GCS access
56+
dataset_info.gcs_dataset_files = GCS_ACCESS_FNS["original"]
57+
yield
58+
# Revert access
59+
dataset_info.gcs_dataset_files = GCS_ACCESS_FNS["dummy"]
4560

4661
def setUp(self):
4762
super(TestCase, self).setUp()

0 commit comments

Comments
 (0)