Skip to content

Testing build pluginsビルドプラグインのテスト


For testing Hatchling plugins, you'll usually want to generate a project to execute builds as a real user would. For example, as a minimal pytest fixture:テスト用のHatchlingプラグインでは、通常、実際のユーザーがビルドを実行するようにプロジェクトを生成したいと思います。例えば、最小限のpytestフィクスチャとして:

from pathlib import Path

import pytest


@pytest.fixture
def new_project(tmp_path):
    project_dir = tmp_path / 'my-app'
    project_dir.mkdir()

    project_file = project_dir / 'pyproject.toml'
    project_file.write_text(
        f"""\
[build-system]
requires = ["hatchling", "hatch-plugin-name @ {Path.cwd().as_uri()}"]
build-backend = "hatchling.build"

[project]
name = "my-app"
version = "0.1.0"
""",
        encoding='utf-8',
    )
    ...

The issue with this is that after the first test session, the project will be forever cached by pip based on the file path. Therefore, subsequent tests runs will never use updated code.これに関する問題は、最初のテストセッションの後、プロジェクトがファイルパスに基づいてpipによって永遠にキャッシュされることです。したがって、以降のテスト実行では更新されたコードが使用されることはありません。

To invalidate the cache, copy your code to a new path for every test session:キャッシュを無効にするには、各テストセッションごとにコードを新しいパスにコピーしてください:

import shutil
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest


@pytest.fixture(scope='session')
def plugin_dir():
    with TemporaryDirectory() as d:
        directory = Path(d, 'plugin')
        shutil.copytree(
            Path.cwd(), directory, ignore=shutil.ignore_patterns('.git')
        )

        yield directory.resolve()


@pytest.fixture
def new_project(tmp_path, plugin_dir):
    project_dir = tmp_path / 'my-app'
    project_dir.mkdir()

    project_file = project_dir / 'pyproject.toml'
    project_file.write_text(
        f"""\
[build-system]
requires = ["hatchling", "hatch-plugin-name @ {plugin_dir.as_uri()}"]
build-backend = "hatchling.build"

[project]
name = "my-app"
version = "0.1.0"
""",
        encoding='utf-8',
    )
    ...

Note注意

This example chooses to ignore copying .git for performance reasons. You may want to ignore more patterns, or copy only specific paths like this plugin does.この例では、パフォーマンス上の理由から.gitのコピーを無視することを選択しています。より多くのパターンを無視したり、このプラグインのように特定のパスのみをコピーしたりすることを検討しても良いでしょう。