Skip to content

Commit

Permalink
test: fix permission error on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenShuo2021 committed Dec 7, 2024
1 parent 2c6d81c commit c035473
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def real_base_config(tmp_path) -> BaseConfig:

@pytest.fixture
def real_args():
expected_file_count = 12
return SimpleNamespace(
url="https://www.v2ph.com/album/Weekly-Young-Jump-2012-No29",
input_file="",
Expand All @@ -40,14 +41,15 @@ def real_args():
use_default_chrome_profile=False,
directory=None,
language="ja",
)
), expected_file_count


@pytest.fixture
def real_runtime_config(real_args, real_base_config, mock_logger):
args, _ = real_args
def _create_runtime_config(service_type, log_level):
return create_runtime_config(
args=real_args,
args=args,
base_config=real_base_config,
logger=mock_logger,
log_level=log_level,
Expand Down
25 changes: 12 additions & 13 deletions tests/test_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
@pytest.fixture
def setup_test_env(tmp_path, real_base_config, real_runtime_config):
def setup_env(service_type, log_level):
expected_file_count = 12
runtime_config = real_runtime_config(service_type, log_level)
web_bot = get_bot(runtime_config, real_base_config)
scraper = ScrapeHandler(runtime_config, real_base_config, web_bot)

return scraper, real_base_config, runtime_config, expected_file_count
return scraper, real_base_config, runtime_config

try:
yield setup_env
Expand Down Expand Up @@ -77,16 +76,15 @@ def real_scrape_handler(mock_runtime_config, mock_base_config, mock_web_bot):


@pytest.mark.skipif(os.getenv("GITHUB_ACTIONS") == "true", reason="No GUI on Github")
def test_download(setup_test_env):
def test_download(setup_test_env, real_args):
scraper: ScrapeHandler
base_config: BaseConfig
runtime_config: RuntimeConfig
valid_extensions = (".jpg", ".jpeg", ".png", ".JPG", ".JPEG", ".PNG")

setup_env = setup_test_env
scraper, base_config, runtime_config, expected_file_count = setup_env(
ServiceType.ASYNC, logging.DEBUG
)
scraper, base_config, runtime_config = setup_env(ServiceType.ASYNC, logging.DEBUG)
_, expected_file_count = real_args
test_download_dir = Path(base_config.download.download_dir)

scraper.scrape(runtime_config.url)
Expand All @@ -101,17 +99,17 @@ def test_download(setup_test_env):
# Check number of files
image_files = sorted(download_subdir.glob("*"), key=lambda x: x.name)
image_files = [f for f in image_files if f.suffix.lower() in valid_extensions]
assert len(image_files) == expected_file_count, (
f"Expected {expected_file_count} images, found {len(image_files)}"
)
assert (
len(image_files) == expected_file_count
), f"Expected {expected_file_count} images, found {len(image_files)}"

# Check file names match 001, 002, 003...
for idx, image_file in enumerate(image_files, start=1):
expected_filename = f"{idx:03d}"
actual_filename = image_file.stem
assert expected_filename == actual_filename, (
f"Expected file name {expected_filename}, found {actual_filename}"
)
assert (
expected_filename == actual_filename
), f"Expected file name {expected_filename}, found {actual_filename}"

# Verify image file size
for image_file in image_files:
Expand All @@ -135,7 +133,8 @@ def test_load_urls(mock_runtime_config, real_scrape_manager, tmp_path):
urls = scrape_manager._load_urls()
assert urls == [TEST_ALBUM_URL]

shutil.rmtree(tmp_path)
if os.name != "nt":
shutil.rmtree(tmp_path)


def test_start_scraping(real_scrape_manager):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import atexit
import base64
import shutil
Expand Down Expand Up @@ -89,7 +90,8 @@ def account_manager(encryption_config, logger, tmp_path):
yield account_manager_instance

atexit.unregister(account_manager_instance._save_yaml)
shutil.rmtree(tmp_path)
if os.name != "nt":
shutil.rmtree(tmp_path)


def test_create_account(account_manager: AccountManager):
Expand Down
15 changes: 6 additions & 9 deletions tests/test_util_multitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from v2dl.utils import AsyncService, Task, ThreadingService

WAIT_TIME = 0.01
WAIT_TIME = 0.10


def simple_add(a, b):
Expand All @@ -17,7 +17,7 @@ def failing_task():


async def async_simple_add(a, b):
await asyncio.sleep(0.001)
await asyncio.sleep(0.000001)
return a + b


Expand Down Expand Up @@ -155,7 +155,7 @@ async def test_async_add_task_starts_service(async_service):
assert not async_service.is_running
task = Task(task_id="task_start", func=async_simple_add, args=(2, 2))
async_service.add_task(task)
await asyncio.sleep(0.000001)
await asyncio.sleep(0.0000001) # DO NOT CHANGE
assert async_service.is_running


Expand All @@ -164,7 +164,7 @@ async def test_async_add_tasks_starts_service(async_service):
assert not async_service.is_running
tasks = [Task(task_id=f"task{i}", func=async_simple_add, args=(i, i)) for i in range(3)]
async_service.add_tasks(tasks)
await asyncio.sleep(0.000001)
await asyncio.sleep(0.0000001) # DO NOT CHANGE
assert async_service.is_running


Expand All @@ -173,7 +173,7 @@ async def test_async_get_results_with_max_results(async_service):
tasks = [Task(task_id=f"task{i}", func=async_simple_add, args=(i, i)) for i in range(5)]
async_service.start()
async_service.add_tasks(tasks)
await asyncio.sleep(WAIT_TIME)
await asyncio.sleep(WAIT_TIME*5)

results = async_service.get_results(max_results=3)
assert len(results) == 3
Expand All @@ -185,14 +185,11 @@ async def test_async_get_results_with_max_results(async_service):

@pytest.mark.asyncio
async def test_async_service_stop(async_service):
# 模擬 `AsyncService` 啟動
async_service.start()
assert async_service.is_running # 確認服務已經啟動
assert async_service.is_running
assert async_service.thread is not None and async_service.thread.is_alive()

# 停止服務
async_service.stop()

# 驗證服務已停止
assert not async_service.is_running
assert async_service.thread is None

0 comments on commit c035473

Please sign in to comment.