Skip to content

Commit

Permalink
fix: tmp path in tests is working (#164)
Browse files Browse the repository at this point in the history
### Description

- Resolved the issue with temporary paths in tests by ensuring the
`CRAWLEE_LOCAL_STORAGE_DIR` environment variable is set before the
`Config` instantiation.
- Added `ipdb` debugger as a development dependency.

### Related issues

- Closes: #159

### Testing

- Try to run tests locally.

### Checklist

- [x] Changes are described in the `CHANGELOG.md`
- [x] CI passed
  • Loading branch information
vdusek authored May 31, 2024
1 parent 29f6c96 commit 382b6f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ tldextract = "^5.1.2"
[tool.poetry.group.dev.dependencies]
build = "~1.2.0"
filelock = "~3.14.0"
ipdb = "^0.13.13"
mypy = "~1.10.0"
pre-commit = "~3.7.0"
pydoc-markdown = "~4.8.2"
Expand Down
50 changes: 25 additions & 25 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable
import os
from typing import TYPE_CHECKING

import pytest

Expand All @@ -13,34 +14,33 @@
from pathlib import Path


@pytest.fixture()
def reset_default_instances(monkeypatch: pytest.MonkeyPatch) -> Callable[[], None]:
def reset() -> None:
StorageClientManager._local_client = MemoryStorageClient()
StorageClientManager._cloud_client = None
@pytest.fixture(autouse=True)
def _isolate_test_environment(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""Isolate tests by resetting the storage clients, clearing caches, and setting the environment variables.
The fixture is applied automatically to all test cases.
monkeypatch.setattr(_creation_management, '_cache_dataset_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_dataset_by_name', {})
monkeypatch.setattr(_creation_management, '_cache_kvs_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_kvs_by_name', {})
monkeypatch.setattr(_creation_management, '_cache_rq_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_rq_by_name', {})
Args:
monkeypatch: Test utility provided by pytest.
tmp_path: A unique temporary directory path provided by pytest for test isolation.
"""
# Set the environment variable for the local storage directory to the temporary path
monkeypatch.setenv('CRAWLEE_LOCAL_STORAGE_DIR', str(tmp_path))

return reset
# Reset the local and cloud clients in StorageClientManager
StorageClientManager._local_client = MemoryStorageClient()
StorageClientManager._cloud_client = None

# Clear creation-related caches to ensure no state is carried over between tests
monkeypatch.setattr(_creation_management, '_cache_dataset_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_dataset_by_name', {})
monkeypatch.setattr(_creation_management, '_cache_kvs_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_kvs_by_name', {})
monkeypatch.setattr(_creation_management, '_cache_rq_by_id', {})
monkeypatch.setattr(_creation_management, '_cache_rq_by_name', {})

# To isolate the tests, we need to reset the used singletons before each test case
# We also set the MemoryStorageClient to use a temp path
@pytest.fixture(autouse=True)
def _reset_and_patch_default_instances(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
reset_default_instances: Callable[[], None],
) -> None:
reset_default_instances()

# This forces the MemoryStorageClient to use tmp_path for its storage dir
monkeypatch.setenv('CRAWLEE_LOCAL_STORAGE_DIR', str(tmp_path))
# Verify that the environment variable is set correctly
assert os.environ.get('CRAWLEE_LOCAL_STORAGE_DIR') == str(tmp_path)


@pytest.fixture()
Expand Down

0 comments on commit 382b6f4

Please sign in to comment.