|
| 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 | + ) |
0 commit comments