File: test_plugin.py

package info (click to toggle)
hatch-jupyter-builder 0.8.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 632 kB
  • sloc: python: 1,506; javascript: 135; makefile: 23
file content (123 lines) | stat: -rw-r--r-- 3,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import platform
import subprocess
import sys
import venv
import warnings
from pathlib import Path

import pytest
from hatchling.metadata.core import ProjectMetadata
from hatchling.plugin.manager import PluginManager

from hatch_jupyter_builder.plugin import JupyterBuildHook


def test_build_hook(tmp_path):
    manager = PluginManager()
    meta = ProjectMetadata(".", manager, {})

    if "SKIP_JUPYTER_BUILD" in os.environ:
        del os.environ["SKIP_JUPYTER_BUILD"]

    config = {
        "build-function": "test.foo",
        "ensured-targets": ["test.py"],
        "build-kwargs": {"foo-bar": "1", "fizz_buzz": "2"},
        "install-pre-commit-hook": True,
    }
    os.chdir(tmp_path)
    test = Path("test.py")
    text = """
def foo(target_name, version, foo_bar=None, fizz_buzz=None):
    return(target_name)
"""
    test.write_text(text, encoding="utf-8")
    os.makedirs(".git/hooks")

    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "wheel")
    assert hook.initialize("standard", {})
    assert hook.initialize("editable", {})

    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "sdist")
    assert hook.initialize("standard", {})

    hook = JupyterBuildHook(tmp_path, {}, {}, meta, tmp_path, "wheel")
    assert hook.initialize("standard", {})
    assert hook.initialize("editable", {})

    config["skip-if-exists"] = ["foo", "bar"]
    assert hook.initialize("standard", {})
    del config["skip-if-exists"]

    config["editable-build-kwargs"] = {"foo-bar": "2", "fizz_buzz": "3"}
    assert hook.initialize("editable", {})

    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "foo")
    assert not hook.initialize("standard", {})

    text = """
def foo(target_name, version, foo_bar=None, fizz_buzz=None):
    raise RuntimeError('trigger error')
"""
    test.write_text(text, encoding="utf-8")
    # Force a re-import
    del sys.modules["test"]

    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "wheel")
    with pytest.raises(RuntimeError):
        hook.initialize("editable", {})

    os.environ["SKIP_JUPYTER_BUILDER"] = "1"
    assert not hook.initialize("standard", {})
    del os.environ["SKIP_JUPYTER_BUILDER"]

    config["optional-editable-build"] = "true"
    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "wheel")
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        assert hook.initialize("editable", {})

    config["optional-editable-build"] = True
    hook = JupyterBuildHook(tmp_path, config, {}, meta, tmp_path, "wheel")
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        assert hook.initialize("editable", {})

    del sys.modules["test"]


HERE = Path(__file__).parent
REPO_ROOT = str(HERE.parent).replace(os.sep, "/")
TOML_CONTENT = f"""
[build-system]
requires = ["hatchling>=1.0"]
build-backend = "hatchling.build"

[project]
name = "test"
version = "0.6.0"

[tool.hatch.build.hooks.jupyter-builder]
dependencies = ["hatch-jupyter-builder@file://{REPO_ROOT}"]
"""


@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="Does not work on PyPy")
def test_hatch_build(tmp_path):
    venv.create(tmp_path, with_pip=True)
    if os.name == "nt":
        python = Path(tmp_path) / "Scripts/python.exe"
    else:
        python = Path(tmp_path) / "bin/python"
    pyproject = Path(tmp_path) / "pyproject.toml"
    pyproject.write_text(TOML_CONTENT, "utf-8")
    test = Path(tmp_path) / "test.py"
    test.write_text("print('hello')", "utf-8")
    env = os.environ.copy()
    # Handle running min version test.
    if "PIP_CONSTRAINT" in env:
        del env["PIP_CONSTRAINT"]
    subprocess.check_call([python, "-m", "pip", "install", "build"], cwd=tmp_path, env=env)

    subprocess.check_call([python, "-m", "build", "--sdist", "."], cwd=tmp_path, env=env)