Skip to content

Commit 71af3dd

Browse files
Neeratyoymfeurer
authored andcommitted
Cleaning files after unit test from local (#721)
* Collecting and cleaning unit test dump * Adding session level fixture with yield to delay deletion of files * Adding PEP8 ignore F401 * Changelog update + pytest argument fix * Leaner implementation without additional imports * Removing TODO * Making changes suggested * Editing CI script to check for files after unit testing * Adding comments to shell script change
1 parent 692af97 commit 71af3dd

File tree

7 files changed

+74
-7
lines changed

7 files changed

+74
-7
lines changed

ci_scripts/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
set -e
22

3+
# check status and branch before running the unit tests
4+
before="`git status --porcelain -b`"
5+
before="$before"
6+
37
run_tests() {
48
# Get into a temp directory to run test from the installed scikit learn and
59
# check if we do not leave artifacts
@@ -32,3 +36,11 @@ fi
3236
if [[ "$SKIP_TESTS" != "true" ]]; then
3337
run_tests
3438
fi
39+
40+
# check status and branch after running the unit tests
41+
# compares with $before to check for remaining files
42+
after="`git status --porcelain -b`"
43+
if [[ "$before" != "$after" ]]; then
44+
echo "All generated files have not been deleted!"
45+
exit 1
46+
fi

doc/progress.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Changelog
1515
* DOC #639: More descriptive documention for function to convert array format.
1616
* ADD #687: Adds a function to retrieve the list of evaluation measures available.
1717
* ADD #695: A function to retrieve all the data quality measures available.
18+
* FIX #447: All files created by unit tests are deleted after the completion of all unit tests.
1819

1920
0.9.0
2021
~~~~~

openml/testing.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import openml
1818
from openml.tasks import TaskTypeEnum
1919

20+
import pytest
21+
2022

2123
class TestBase(unittest.TestCase):
2224
"""Base class for tests
@@ -44,7 +46,6 @@ def setUp(self, n_levels: int = 1):
4446
Number of nested directories the test is in. Necessary to resolve the path to the
4547
``files`` directory, which is located directly under the ``tests`` directory.
4648
"""
47-
4849
# This cache directory is checked in to git to simulate a populated
4950
# cache
5051
self.maxDiff = None
@@ -103,6 +104,48 @@ def tearDown(self):
103104
openml.config.server = self.production_server
104105
openml.config.connection_n_retries = self.connection_n_retries
105106

107+
@pytest.fixture(scope="session", autouse=True)
108+
def _cleanup_fixture(self):
109+
"""Cleans up files generated by Unit tests
110+
111+
This function is called at the beginning of the invocation of
112+
TestBase (defined below), by each of class that inherits TestBase.
113+
The 'yield' creates a checkpoint and breaks away to continue running
114+
the unit tests of the sub class. When all the tests end, execution
115+
resumes from the checkpoint.
116+
"""
117+
118+
abspath_this_file = os.path.abspath(inspect.getfile(self.__class__))
119+
static_cache_dir = os.path.dirname(abspath_this_file)
120+
# Could be a risky while condition, however, going up a directory
121+
# n-times will eventually end at main directory
122+
while True:
123+
if 'openml' in os.listdir(static_cache_dir):
124+
break
125+
else:
126+
static_cache_dir = os.path.join(static_cache_dir, '../')
127+
directory = os.path.join(static_cache_dir, 'tests/files/')
128+
# directory = "{}/tests/files/".format(static_cache_dir)
129+
files = os.walk(directory)
130+
old_file_list = []
131+
for root, _, filenames in files:
132+
for filename in filenames:
133+
old_file_list.append(os.path.join(root, filename))
134+
# context switches to other remaining tests
135+
# pauses the code execution here till all tests in the 'session' is over
136+
yield
137+
# resumes from here after all collected tests are completed
138+
files = os.walk(directory)
139+
new_file_list = []
140+
for root, _, filenames in files:
141+
for filename in filenames:
142+
new_file_list.append(os.path.join(root, filename))
143+
# filtering the files generated during this run
144+
new_file_list = list(set(new_file_list) - set(old_file_list))
145+
print("Files to delete in local: {}".format(new_file_list))
146+
for file in new_file_list:
147+
os.remove(file)
148+
106149
def _get_sentinel(self, sentinel=None):
107150
if sentinel is None:
108151
# Create a unique prefix for the flow. Necessary because the flow

tests/test_datasets/test_dataset_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def tearDown(self):
4343
super(TestOpenMLDataset, self).tearDown()
4444

4545
def _remove_pickle_files(self):
46-
cache_dir = self.static_cache_dir
46+
self.lock_path = os.path.join(openml.config.get_cache_directory(), 'locks')
4747
for did in ['-1', '2']:
4848
with lockutils.external_lock(
4949
name='datasets.functions.get_dataset:%s' % did,
50-
lock_path=os.path.join(openml.config.get_cache_directory(), 'locks'),
50+
lock_path=self.lock_path,
5151
):
52-
pickle_path = os.path.join(cache_dir, 'datasets', did,
53-
'dataset.pkl')
52+
pickle_path = os.path.join(openml.config.get_cache_directory(), 'datasets',
53+
did, 'dataset.pkl.py3')
5454
try:
5555
os.remove(pickle_path)
5656
except (OSError, FileNotFoundError):

tests/test_tasks/test_split.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def setUp(self):
1919
self.directory, "..", "files", "org", "openml", "test",
2020
"tasks", "1882", "datasplits.arff"
2121
)
22-
# TODO Needs to be adapted regarding the python version
23-
self.pd_filename = self.arff_filename.replace(".arff", ".pkl")
22+
self.pd_filename = self.arff_filename.replace(".arff", ".pkl.py3")
2423

2524
def tearDown(self):
2625
try:

tests/test_tasks/test_task_functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
class TestTask(TestBase):
1313
_multiprocess_can_split_ = True
1414

15+
def setUp(self):
16+
super(TestTask, self).setUp()
17+
18+
def tearDown(self):
19+
super(TestTask, self).tearDown()
20+
1521
def test__get_cached_tasks(self):
1622
openml.config.cache_directory = self.static_cache_dir
1723
tasks = openml.tasks.functions._get_cached_tasks()

tests/test_tasks/test_task_methods.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
# Common methods between tasks
88
class OpenMLTaskMethodsTest(TestBase):
99

10+
def setUp(self):
11+
super(OpenMLTaskMethodsTest, self).setUp()
12+
13+
def tearDown(self):
14+
super(OpenMLTaskMethodsTest, self).tearDown()
15+
1016
def test_tagging(self):
1117
task = openml.tasks.get_task(1)
1218
tag = "testing_tag_{}_{}".format(self.id(), time())

0 commit comments

Comments
 (0)