Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop using datumaro.util.test_utils #8259

Merged
merged 9 commits into from
Oct 23, 2024
2 changes: 1 addition & 1 deletion cvat/apps/dataset_manager/tests/test_rest_api_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
from attr import define, field
from datumaro.components.dataset import Dataset
from datumaro.components.operations import ExactComparator
from datumaro.util.test_utils import TestDir
from django.contrib.auth.models import Group, User
from PIL import Image
from rest_framework import status

import cvat.apps.dataset_manager as dm
from cvat.apps.dataset_manager.bindings import CvatTaskOrJobDataExtractor, TaskData
from cvat.apps.dataset_manager.task import TaskAnnotation
from cvat.apps.dataset_manager.tests.utils import TestDir
from cvat.apps.dataset_manager.util import get_export_cache_lock
from cvat.apps.dataset_manager.views import clear_export_cache, export, parse_export_file_path
from cvat.apps.engine.models import Task
Expand Down
78 changes: 78 additions & 0 deletions cvat/apps/dataset_manager/tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (C) 2024 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

import os
import tempfile
import unittest
from types import TracebackType
from typing import Optional, Type

from datumaro.util.os_util import rmfile, rmtree

from cvat.apps.dataset_manager.util import current_function_name


class FileRemover:
def __init__(self, path: str, is_dir: bool = False):
self.path = path
self.is_dir = is_dir

def __enter__(self) -> str:
return self.path

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
if self.is_dir:
try:
rmtree(self.path)
except unittest.SkipTest:
# Suppress skip test errors from git.util.rmtree
if not exc_type:
raise
else:
rmfile(self.path)


class TestDir(FileRemover):
"""
Creates a temporary directory for a test. Uses the name of
the test function to name the directory.

Usage:

.. code-block::

with TestDir() as test_dir:
...
"""

def __init__(self, path: Optional[str] = None, frame_id: int = 2):
if not path:
prefix = f"temp_{current_function_name(frame_id)}-"
else:
prefix = None
self._prefix = prefix

super().__init__(path, is_dir=True)

def __enter__(self) -> str:
"""
Creates a test directory.

Returns: path to the directory
"""

path = self.path

if path is None:
path = tempfile.mkdtemp(dir=os.getcwd(), prefix=self._prefix)
self.path = path
else:
os.makedirs(path, exist_ok=False)

return path
3 changes: 2 additions & 1 deletion cvat/apps/engine/tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from rest_framework import status
from rest_framework.test import APIClient

from datumaro.util.test_utils import current_function_name, TestDir
from cvat.apps.dataset_manager.tests.utils import TestDir
from cvat.apps.dataset_manager.util import current_function_name
from cvat.apps.engine.models import (AttributeSpec, AttributeType, Data, Job,
Project, Segment, StageChoice, StatusChoice, Task, Label, StorageMethodChoice,
StorageChoice, DimensionType, SortingMethod)
Expand Down
2 changes: 1 addition & 1 deletion cvat/apps/engine/tests/test_rest_api_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from django.contrib.auth.models import Group, User
from rest_framework import status

from cvat.apps.dataset_manager.tests.utils import TestDir
from cvat.apps.engine.media_extractors import ValidateDimension
from cvat.apps.dataset_manager.task import TaskAnnotation
from datumaro.util.test_utils import TestDir

from cvat.apps.engine.tests.utils import get_paginated_collection, ApiTestBase, ForceLogin

Expand Down
1 change: 1 addition & 0 deletions dev/format_python_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ for paths in \
"cvat/apps/engine/default_settings.py" \
"cvat/apps/engine/field_validation.py" \
"cvat/apps/engine/model_utils.py" \
"cvat/apps/dataset_manager/tests/utils.py" \
; do
${BLACK} -- ${paths}
${ISORT} -- ${paths}
Expand Down
Loading