Skip to content

Commit

Permalink
Merge pull request #14 from sbidoul/more-tests
Browse files Browse the repository at this point in the history
Add tests for options
  • Loading branch information
sbidoul authored Oct 22, 2023
2 parents a9c9b0d + 743260f commit 419a727
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def addon1(tmp_path: Path) -> Path:
addon_dir.joinpath("__init__.py").touch()
subprocess.check_call(["git", "add", "."], cwd=addon_dir)
subprocess.check_call(["git", "commit", "-am", "initial commit"], cwd=addon_dir)
addon_dir.joinpath("hook.py").touch()
subprocess.check_call(["git", "add", "."], cwd=addon_dir)
subprocess.check_call(["git", "commit", "-m", "second commit"], cwd=addon_dir)
return addon_dir


Expand All @@ -33,4 +36,7 @@ def addon1_with_pyproject(addon1: Path) -> Path:
"{'name': 'addon1', 'version': '15.0.1.1.0'}"
)
subprocess.check_call(["git", "commit", "-am", "add pyproject.toml"], cwd=addon1)
addon1.joinpath("hook2.py").touch()
subprocess.check_call(["git", "add", "."], cwd=addon1)
subprocess.check_call(["git", "commit", "-m", "one more commit"], cwd=addon1)
return addon1
16 changes: 8 additions & 8 deletions tests/test_build_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@
def test_build_sdist(addon1: Path, tmp_path: Path) -> None:
with dir_changer(addon1):
sdist_name = build_sdist(os.fspath(tmp_path))
assert sdist_name == "odoo-addon-addon1-15.0.1.0.0.tar.gz"
assert sdist_name == "odoo-addon-addon1-15.0.1.0.0.1.tar.gz"
assert (tmp_path / sdist_name).exists()


def test_build_sdist_from_sdist(addon1_with_pyproject: Path, tmp_path: Path) -> None:
sdist_name = _build_sdist(addon1_with_pyproject, tmp_path)
assert sdist_name == "odoo-addon-addon1-15.0.1.1.0.tar.gz"
assert sdist_name == "odoo-addon-addon1-15.0.1.1.0.1.tar.gz"
# extract sdist and test that the root directory has the correct name
tmp_path2 = tmp_path / "2"
tmp_path2.mkdir()
with TarFile.open(tmp_path / sdist_name, mode="r:gz") as tf1:
tf1_names = sorted(tf1.getnames())
tf1.extractall(tmp_path2)
assert "odoo-addon-addon1-15.0.1.1.0/PKG-INFO" in tf1_names
assert "odoo-addon-addon1-15.0.1.1.0/pyproject.toml" in tf1_names
assert "odoo-addon-addon1-15.0.1.1.0.1/PKG-INFO" in tf1_names
assert "odoo-addon-addon1-15.0.1.1.0.1/pyproject.toml" in tf1_names
# build sdist from sdist
tmp_path3 = tmp_path / "3"
tmp_path3.mkdir()
sdist_name = _build_sdist(tmp_path2 / "odoo-addon-addon1-15.0.1.1.0", tmp_path3)
assert sdist_name == "odoo-addon-addon1-15.0.1.1.0.tar.gz"
sdist_name = _build_sdist(tmp_path2 / "odoo-addon-addon1-15.0.1.1.0.1", tmp_path3)
assert sdist_name == "odoo-addon-addon1-15.0.1.1.0.1.tar.gz"
# extract 2nd sdist and test that the root directory has the correct name
with TarFile.open(tmp_path3 / sdist_name, mode="r:gz") as tf2:
tf2_names = sorted(tf2.getnames())
tf2.extractall(tmp_path3)
# content of both sdists must be identical
assert tf1_names == tf2_names
# PKG-INFO in both sdists must be identical
assert (tmp_path2 / "odoo-addon-addon1-15.0.1.1.0" / "PKG-INFO").read_bytes() == (
tmp_path3 / "odoo-addon-addon1-15.0.1.1.0" / "PKG-INFO"
assert (tmp_path2 / "odoo-addon-addon1-15.0.1.1.0.1" / "PKG-INFO").read_bytes() == (
tmp_path3 / "odoo-addon-addon1-15.0.1.1.0.1" / "PKG-INFO"
).read_bytes()
2 changes: 1 addition & 1 deletion tests/test_build_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def test_build_wheel(addon1_with_pyproject: Path, tmp_path: Path) -> None:
with dir_changer(addon1_with_pyproject):
wheel_name = build_wheel(os.fspath(tmp_path))
assert wheel_name == "odoo_addon_addon1-15.0.1.1.0-py3-none-any.whl"
assert wheel_name == "odoo_addon_addon1-15.0.1.1.0.1-py3-none-any.whl"
assert (tmp_path / wheel_name).exists()
with ZipFile(tmp_path / wheel_name) as zf:
names = zf.namelist()
Expand Down
48 changes: 45 additions & 3 deletions tests/test_prepare_metadata.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import os
import textwrap
from pathlib import Path

from whool.buildapi import (
prepare_metadata_for_build_editable,
prepare_metadata_for_build_wheel,
)

from .utils import dir_changer
from .utils import dir_changer, env_changer


def test_prepare_metadata_for_build_wheel(addon1: Path, tmp_path: Path) -> None:
with dir_changer(addon1):
dist_info_dir = prepare_metadata_for_build_wheel(os.fspath(tmp_path))
assert dist_info_dir == "odoo_addon_addon1-15.0.1.0.0.dist-info"
assert dist_info_dir == "odoo_addon_addon1-15.0.1.0.0.1.dist-info"
dist_info_path = tmp_path / dist_info_dir
assert dist_info_path.is_dir()
metadata_path = dist_info_path / "METADATA"
Expand All @@ -22,8 +23,49 @@ def test_prepare_metadata_for_build_wheel(addon1: Path, tmp_path: Path) -> None:
def test_prepare_metadata_for_build_editable(addon1: Path, tmp_path: Path) -> None:
with dir_changer(addon1):
dist_info_dir = prepare_metadata_for_build_editable(os.fspath(tmp_path))
assert dist_info_dir == "odoo_addon_addon1-15.0.1.0.0.dist-info"
assert dist_info_dir == "odoo_addon_addon1-15.0.1.0.0.1.dist-info"
dist_info_path = tmp_path / dist_info_dir
assert dist_info_path.is_dir()
metadata_path = dist_info_path / "METADATA"
assert metadata_path.is_file()


def test_post_version_strategy_override_in_pyproject(
addon1_with_pyproject: Path, tmp_path: Path
) -> None:
# This also tests that options are properly passed to the manifestoo-core
# metadata_from_addon_dir() function, since the whool tool.whool dict is passed as
# is.
with addon1_with_pyproject.joinpath("pyproject.toml").open("a") as f:
f.write(
textwrap.dedent(
"""
[tool.whool]
post_version_strategy_override = "none"
"""
)
)
with dir_changer(addon1_with_pyproject):
dist_info_dir = prepare_metadata_for_build_editable(os.fspath(tmp_path))
assert dist_info_dir == "odoo_addon_addon1-15.0.1.1.0.dist-info"


def test_post_version_strategy_override_in_envvar(
addon1_with_pyproject: Path, tmp_path: Path
) -> None:
# Test that the WHOOL_POST_VERSION_STRATEGY_OVERRIDE is properly passed, and has
# priority over pyproject.toml.
with addon1_with_pyproject.joinpath("pyproject.toml").open("a") as f:
f.write(
textwrap.dedent(
"""
[tool.whool]
post_version_strategy_override = "none"
"""
)
)
with dir_changer(addon1_with_pyproject), env_changer(
{"WHOOL_POST_VERSION_STRATEGY_OVERRIDE": "+1.devN"}
):
dist_info_dir = prepare_metadata_for_build_editable(os.fspath(tmp_path))
assert dist_info_dir == "odoo_addon_addon1-15.0.1.1.1.dev2.dist-info"
14 changes: 13 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator
from typing import Dict, Iterator


@contextmanager
Expand All @@ -13,3 +13,15 @@ def dir_changer(path: Path) -> Iterator[None]:
yield
finally:
os.chdir(old_cwd)


@contextmanager
def env_changer(env: Dict[str, str]) -> Iterator[None]:
"""A context manager that changes the current working directory"""
old_env = os.environ.copy()
os.environ.update(env)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_env)

0 comments on commit 419a727

Please sign in to comment.