File: test_package_utils.py

package info (click to toggle)
python-azure 20251202%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 786,344 kB
  • sloc: python: 6,510,493; ansic: 804; javascript: 287; sh: 204; makefile: 198; xml: 109
file content (71 lines) | stat: -rw-r--r-- 2,558 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
from pathlib import Path
import os

try:
    import tomllib as toml
except Exception:  # pragma: no cover
    import tomli as toml  # type: ignore

import packaging_tools.package_utils as pu


def _create_basic_package(tmp_path: Path, package_name: str, version_line: str):
    package_dir = tmp_path / package_name
    package_dir.mkdir()

    # nested package init path per implementation logic
    nested_init_path = package_dir / package_name.replace("-", "/")
    nested_init_path.mkdir(parents=True, exist_ok=True)
    (nested_init_path / "__init__.py").write_text("__all__ = ['FooClient']\n")

    # changelog with version line
    (package_dir / "CHANGELOG.md").write_text(f"{version_line}\n")

    # minimal pyproject (can be empty, but create one to assert modifications)
    (package_dir / "pyproject.toml").write_text("[project]\nname='example'\n")

    # readme containing placeholder to be replaced
    (package_dir / "README.md").write_text("This is MyService client library.\n")

    return package_dir


def test_check_file_populates_pyproject_stable(tmp_path, monkeypatch):
    package_name = "azure-ai-foo"
    package_dir = _create_basic_package(tmp_path, package_name, "## 1.2.3 (2025-01-01)")

    # stub out build_packaging to avoid external side effects
    monkeypatch.setattr(pu, "build_packaging", lambda **kwargs: None)

    # run the function under test
    pu.check_file(package_dir)

    # validate pyproject.toml modifications
    with open(package_dir / "pyproject.toml", "rb") as fd:
        data = toml.load(fd)

    assert data["packaging"]["title"] == "FooClient"
    assert data["packaging"]["is_stable"] is True
    assert data["tool"]["azure-sdk-build"]["breaking"] is False
    assert data["tool"]["azure-sdk-build"]["pyright"] is False
    assert data["tool"]["azure-sdk-build"]["mypy"] is False

    # README placeholder replaced with pprint name ("Foo")
    readme_content = (package_dir / "README.md").read_text()
    assert "Foo" in readme_content and "MyService" not in readme_content


def test_check_file_sets_is_stable_false_for_beta(tmp_path, monkeypatch):
    package_name = "azure-ai-bar"
    package_dir = _create_basic_package(tmp_path, package_name, "## 2.0.0b1 (2025-01-01)")

    monkeypatch.setattr(pu, "build_packaging", lambda **kwargs: None)

    pu.check_file(package_dir)

    with open(package_dir / "pyproject.toml", "rb") as fd:
        data = toml.load(fd)

    assert data["packaging"]["is_stable"] is False
    # title still populated
    assert data["packaging"]["title"] == "FooClient"