Skip to content

Commit f053d93

Browse files
authored
By default avoiding generating files in temp directory (#329)
* initial commit * testing num files * fix conftest
1 parent f1c4b5e commit f053d93

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

tests/sparsezoo/conftest.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import tempfile
17+
from typing import List
18+
19+
import pytest
20+
21+
22+
def _get_files(directory: str) -> List[str]:
23+
list_filepaths = []
24+
for root, dirs, files in os.walk(directory):
25+
for file in files:
26+
list_filepaths.append(os.path.join(os.path.abspath(root), file))
27+
return list_filepaths
28+
29+
30+
@pytest.fixture(scope="session", autouse=True)
31+
def check_for_created_files():
32+
start_files_root = _get_files(directory=r".")
33+
start_files_temp = _get_files(directory=tempfile.gettempdir())
34+
yield
35+
end_files_root = _get_files(directory=r".")
36+
end_files_temp = _get_files(directory=tempfile.gettempdir())
37+
38+
assert len(start_files_root) >= len(end_files_root), (
39+
f"{len(end_files_root) - len(start_files_root)} "
40+
f"files created in current working "
41+
f"directory during pytest run. "
42+
f"Created files: {set(end_files_root) - set(start_files_root)}"
43+
)
44+
max_allowed_sized_temp_files_megabytes = 1
45+
size_of_temp_files_bytes = sum(
46+
os.path.getsize(path) for path in set(end_files_temp) - set(start_files_temp)
47+
)
48+
size_of_temp_files_megabytes = size_of_temp_files_bytes / 1024 / 1024
49+
50+
assert max_allowed_sized_temp_files_megabytes >= size_of_temp_files_megabytes, (
51+
f"{size_of_temp_files_megabytes} "
52+
f"megabytes of temp files created in temp directory during pytest run. "
53+
f"Created files: {set(end_files_temp) - set(start_files_temp)}"
54+
)

tests/sparsezoo/model/test_model.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,14 @@
9797
)
9898
class TestSetupModel:
9999
@pytest.fixture()
100-
def setup(self, stub, args, should_pass):
101-
temp_dir = tempfile.TemporaryDirectory(dir="/tmp")
102-
103-
yield stub, args, should_pass
100+
def setup(self, stub, args, should_pass, tmpdir):
101+
yield stub, args, should_pass, tmpdir
104102

105-
shutil.rmtree(temp_dir.name)
106-
107-
def test_model_from_stub(self, stub, args, should_pass):
108-
temp_dir = tempfile.TemporaryDirectory(dir="/tmp")
103+
def test_model_from_stub(self, setup):
104+
stub, args, should_pass, tmpdir = setup
109105
path = stub + "?" + args[0] + "=" + args[1]
110106
if should_pass:
111-
model = Model(path, temp_dir.name)
107+
model = Model(path, tmpdir)
112108
self._assert_correct_files_downloaded(model, args)
113109
self._assert_validation_results_exist(model)
114110
assert model.compressed_size

0 commit comments

Comments
 (0)