File: test_build_api.py

package info (click to toggle)
jupyter-packaging 0.12.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 260 kB
  • sloc: python: 1,533; sh: 15; makefile: 14
file content (133 lines) | stat: -rw-r--r-- 4,466 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
import os
import sys
from subprocess import check_call

import pytest

from jupyter_packaging.build_api import build_sdist, build_wheel

TOML_CONTENT = """
[tool.jupyter-packaging.builder]
factory = "foo.main"

[tool.jupyter-packaging.build-args]
fizz = "buzz"
"""

FOO_CONTENT = r"""
from pathlib import Path
def main(fizz=None):
    Path('foo.txt').write_text(f'fizz={fizz}', encoding='utf-8')
"""

BAD_CONTENT = """
[tool.jupyter-packaging.builder]
bar = "foo.main"
"""

ENSURED_CONTENT = """
[tool.jupyter-packaging.options]
ensured-targets = ["foo.txt"]
"""

SKIP_IF_EXISTS = """
[tool.jupyter-packaging.options]
skip-if-exists = ["foo.txt"]
"""


def test_build_wheel(tmp_path, mocker):
    os.chdir(tmp_path)
    tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
    tmp_path.joinpath("pyproject.toml").write_text(
        TOML_CONTENT + ENSURED_CONTENT, encoding="utf-8"
    )
    orig_wheel = mocker.patch("jupyter_packaging.build_api.orig_build_wheel")
    build_wheel(tmp_path)
    orig_wheel.assert_called_with(
        tmp_path, config_settings=None, metadata_directory=None
    )
    data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
    assert data == "fizz=buzz"

    content = TOML_CONTENT.replace("buzz", "fizz") + SKIP_IF_EXISTS
    tmp_path.joinpath("pyproject.toml").write_text(content, encoding="utf-8")
    build_wheel(tmp_path)
    data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
    assert data == "fizz=buzz"


def test_build_wheel_bad_toml(tmp_path, mocker):
    os.chdir(tmp_path)
    tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
    tmp_path.joinpath("pyproject.toml").write_text(BAD_CONTENT, encoding="utf-8")
    orig_wheel = mocker.patch("jupyter_packaging.build_api.orig_build_wheel")
    with pytest.raises(ValueError):
        build_wheel(tmp_path)
    orig_wheel.assert_not_called()


def test_build_wheel_no_toml(tmp_path, mocker):
    os.chdir(tmp_path)
    orig_wheel = mocker.patch("jupyter_packaging.build_api.orig_build_wheel")
    build_wheel(tmp_path)
    orig_wheel.assert_called_with(
        tmp_path, config_settings=None, metadata_directory=None
    )


def test_build_sdist(tmp_path, mocker):
    os.chdir(tmp_path)
    tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
    tmp_path.joinpath("pyproject.toml").write_text(
        TOML_CONTENT + ENSURED_CONTENT, encoding="utf-8"
    )
    orig_sdist = mocker.patch("jupyter_packaging.build_api.orig_build_sdist")
    build_sdist(tmp_path)
    orig_sdist.assert_called_with(tmp_path, config_settings=None)
    data = tmp_path.joinpath("foo.txt").read_text(encoding="utf-8")
    assert data == "fizz=buzz"


def test_build_sdist_bad_toml(tmp_path, mocker):
    os.chdir(tmp_path)
    tmp_path.joinpath("foo.py").write_text(FOO_CONTENT)
    tmp_path.joinpath("pyproject.toml").write_text(BAD_CONTENT, encoding="utf-8")
    orig_sdist = mocker.patch("jupyter_packaging.build_api.orig_build_sdist")
    with pytest.raises(ValueError):
        build_sdist(tmp_path)
    orig_sdist.assert_not_called()


def test_build_sdist_no_toml(tmp_path, mocker):
    os.chdir(tmp_path)
    orig_sdist = mocker.patch("jupyter_packaging.build_api.orig_build_sdist")
    build_sdist(tmp_path)
    orig_sdist.assert_called_with(tmp_path, config_settings=None)


def test_build_package(make_package):
    package_dir = make_package()
    pyproject = package_dir / "pyproject.toml"
    text = pyproject.read_text(encoding="utf-8")
    text = text.replace("setuptools.build_meta", "jupyter_packaging.build_api")
    text += TOML_CONTENT
    pyproject.write_text(text, encoding="utf-8")
    package_dir.joinpath("foo.py").write_text(FOO_CONTENT, encoding="utf-8")
    check_call([sys.executable, "-m", "build"], cwd=package_dir)
    data = package_dir.joinpath("foo.txt").read_text(encoding="utf-8")
    assert data == "fizz=buzz"


def test_deprecated_metadata(make_package):
    package_dir = make_package()
    pyproject = package_dir / "pyproject.toml"
    text = pyproject.read_text(encoding="utf-8")
    text = text.replace("setuptools.build_meta", "jupyter_packaging.build_api")
    text += TOML_CONTENT
    text = text.replace("factory =", "func =")
    pyproject.write_text(text, encoding="utf-8")
    package_dir.joinpath("foo.py").write_text(FOO_CONTENT, encoding="utf-8")
    check_call([sys.executable, "-m", "build"], cwd=package_dir)
    data = package_dir.joinpath("foo.txt").read_text(encoding="utf-8")
    assert data == "fizz=buzz"