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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
"""Tests for the `diff` module."""
from __future__ import annotations
import pytest
from griffe import Breakage, BreakageKind, find_breaking_changes, temporary_visited_module, temporary_visited_package
@pytest.mark.parametrize(
("old_code", "new_code", "expected_breakages"),
[
(
"a = True",
"a = False",
[BreakageKind.ATTRIBUTE_CHANGED_VALUE],
),
(
"class a(int, str): ...",
"class a(int): ...",
[BreakageKind.CLASS_REMOVED_BASE],
),
(
"a = 0",
"class a: ...",
[BreakageKind.OBJECT_CHANGED_KIND],
),
(
"a = True",
"",
[BreakageKind.OBJECT_REMOVED],
),
(
"def a(): ...",
"def a(x): ...",
[BreakageKind.PARAMETER_ADDED_REQUIRED],
),
(
"def a(x=0): ...",
"def a(x=1): ...",
[BreakageKind.PARAMETER_CHANGED_DEFAULT],
),
(
# Positional-only to keyword-only.
"def a(x, /): ...",
"def a(*, x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Keyword-only to positional-only.
"def a(*, x): ...",
"def a(x, /): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Positional or keyword to positional-only.
"def a(x): ...",
"def a(x, /): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Positional or keyword to keyword-only.
"def a(x): ...",
"def a(*, x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
# To variadic positional.
(
# Positional-only to variadic positional.
"def a(x, /): ...",
"def a(*x): ...",
[],
),
(
# Positional or keyword to variadic positional.
"def a(x): ...",
"def a(*x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Keyword-only to variadic positional.
"def a(*, x): ...",
"def a(*x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Variadic keyword to variadic positional.
"def a(**x): ...",
"def a(*x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Positional or keyword to variadic positional, with variadic keyword.
"def a(x): ...",
"def a(*x, **y): ...",
[],
),
(
# Keyword-only to variadic positional, with variadic keyword.
"def a(*, x): ...",
"def a(*x, **y): ...",
[],
),
# To variadic keyword.
(
# Positional-only to variadic keyword.
"def a(x, /): ...",
"def a(**x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Positional or keyword to variadic keyword.
"def a(x): ...",
"def a(**x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Keyword-only to variadic keyword.
"def a(*, x): ...",
"def a(**x): ...",
[],
),
(
# Variadic positional to variadic keyword.
"def a(*x): ...",
"def a(**x): ...",
[BreakageKind.PARAMETER_CHANGED_KIND],
),
(
# Positional-only to variadic keyword, with variadic positional.
"def a(x, /): ...",
"def a(*y, **x): ...",
[],
),
(
# Positional or keyword to variadic keyword, with variadic positional.
"def a(x): ...",
"def a(*y, **x): ...",
[],
),
(
"def a(x=1): ...",
"def a(x): ...",
[BreakageKind.PARAMETER_CHANGED_REQUIRED],
),
(
"def a(x, y): ...",
"def a(y, x): ...",
[BreakageKind.PARAMETER_MOVED, BreakageKind.PARAMETER_MOVED],
),
(
"def a(x, y): ...",
"def a(x): ...",
[BreakageKind.PARAMETER_REMOVED],
),
(
"class a:\n\tb: int | None = None",
"class a:\n\tb: int",
[BreakageKind.ATTRIBUTE_CHANGED_VALUE],
),
(
"def a() -> int: ...",
"def a() -> str: ...",
[], # Not supported yet: `BreakageKind.RETURN_CHANGED_TYPE`.
),
],
)
def test_diff_griffe(old_code: str, new_code: str, expected_breakages: list[Breakage]) -> None:
"""Test the different incompatibility finders.
Parameters:
old_code: Parametrized code of the old module version.
new_code: Parametrized code of the new module version.
expected_breakages: A list of breakage kinds to expect.
"""
# Check without any alias.
with temporary_visited_module(old_code) as old_package, temporary_visited_module(new_code) as new_package:
breaking = list(find_breaking_changes(old_package, new_package))
assert len(breaking) == len(expected_breakages)
for breakage, expected_kind in zip(breaking, expected_breakages):
assert breakage.kind is expected_kind
# Check with aliases.
import_a = "from ._mod_a import a\n__all__ = ['a']"
old_modules = {"__init__.py": import_a, "_mod_a.py": old_code}
new_modules = {"__init__.py": new_code and import_a, "_mod_a.py": new_code}
with temporary_visited_package("package_old", old_modules) as old_package: # noqa: SIM117
with temporary_visited_package("package_new", new_modules) as new_package:
breaking = list(find_breaking_changes(old_package, new_package))
assert len(breaking) == len(expected_breakages)
for breakage, expected_kind in zip(breaking, expected_breakages):
assert breakage.kind is expected_kind
def test_moving_members_in_parent_classes() -> None:
"""Test that moving an object from a base class to a parent class doesn't trigger a breakage."""
old_code = """
class Parent:
...
class Base(Parent):
def method(self):
...
"""
new_code = """
class Parent:
def method(self):
...
class Base(Parent):
...
"""
with temporary_visited_module(old_code) as old_package, temporary_visited_module(new_code) as new_package:
assert not list(find_breaking_changes(old_package, new_package))
|