File: test_directorychango.py

package info (click to toggle)
python-sphinx-chango 0.6.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,840 kB
  • sloc: python: 4,909; javascript: 74; makefile: 23
file content (311 lines) | stat: -rw-r--r-- 11,588 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#  SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
#  SPDX-License-Identifier: MIT

import datetime as dtm

import pytest
import shortuuid

from chango import Version
from chango.action import ChanGoActionData, ParentPullRequest
from chango.concrete import (
    CommentChangeNote,
    CommentVersionNote,
    DirectoryChanGo,
    DirectoryVersionScanner,
    HeaderVersionHistory,
)
from chango.concrete.sections import (
    GitHubSectionChangeNote,
    PullRequest,
    Section,
    SectionVersionNote,
)
from chango.error import ChanGoError
from chango.helpers import ensure_uid
from tests.auxil.files import data_path


@pytest.fixture(scope="module")
def scanner() -> DirectoryVersionScanner:
    return DirectoryVersionScanner(TestDirectoryChango.DATA_ROOT, "unreleased")


@pytest.fixture(scope="module")
def chango(scanner) -> DirectoryChanGo:
    return DirectoryChanGo(
        change_note_type=CommentChangeNote,
        version_note_type=CommentVersionNote,
        version_history_type=HeaderVersionHistory,
        scanner=scanner,
    )


DummySectionChangeNote = GitHubSectionChangeNote.with_sections(
    [
        Section(uid="req_0", title="req_0", is_required=True),
        Section(uid="opt_0", title="opt_0", is_required=False),
        Section(uid="opt_1", title="opt_1", is_required=False),
    ]
)


@pytest.fixture(scope="module")
def section_scanner() -> DirectoryVersionScanner:
    return DirectoryVersionScanner(TestDirectoryChango.SECTION_DATA_ROOT, "unreleased")


@pytest.fixture(scope="module")
def section_chango(section_scanner) -> DirectoryChanGo:
    return DirectoryChanGo(
        change_note_type=DummySectionChangeNote,
        version_note_type=SectionVersionNote,
        version_history_type=HeaderVersionHistory,
        scanner=section_scanner,
    )


class TestDirectoryChango:
    DATA_ROOT = data_path("directoryversionscanner")
    SECTION_DATA_ROOT = data_path("directoryversionscanner-sections")

    def test_init_basic(self, chango, scanner):
        assert chango.directory_format == "{uid}_{date}"
        assert chango.change_note_type == CommentChangeNote
        assert chango.version_note_type == CommentVersionNote
        assert chango.version_history_type == HeaderVersionHistory
        assert chango.scanner is scanner

    def test_init_custom_format(self, scanner):
        chango = DirectoryChanGo(
            change_note_type=CommentChangeNote,
            version_note_type=CommentVersionNote,
            version_history_type=HeaderVersionHistory,
            scanner=scanner,
            directory_format="{uid} custom {date}",
        )
        assert chango.directory_format == "{uid} custom {date}"

    @pytest.mark.parametrize("uid", [None, "uid"])
    def test_build_template_change_note(self, chango, uid):
        note = chango.build_template_change_note("slug", uid)
        assert isinstance(note, CommentChangeNote)
        assert note.slug == "slug"
        if uid is not None:
            assert note.uid == uid
        else:
            assert isinstance(note.uid, str)
            assert len(note.uid) == len(shortuuid.ShortUUID().uuid())

    class TestBuildGitHubEeventChangeNote:
        def test_basic(self, chango):
            event = {
                "pull_request": {
                    "html_url": "https://example.com/pull/42",
                    "number": 42,
                    "title": "example title",
                }
            }
            note = chango.build_github_event_change_note(event)
            assert isinstance(note, CommentChangeNote)
            assert isinstance(note.uid, str)
            assert len(note.uid) == len(shortuuid.ShortUUID().uuid())

        @pytest.mark.parametrize(
            "data",
            [
                pytest.param(None, id="None"),
                pytest.param({"some": "dict"}, id="dict"),
                pytest.param(
                    ChanGoActionData(parent_pull_request=None, linked_issues=None),
                    id="ChanGoActionData without parent PR",
                ),
            ],
        )
        def test_section_change_note_early_exit(self, chango, data):
            event = {
                "pull_request": {
                    "html_url": "https://example.com/pull/42",
                    "number": 42,
                    "title": "example title",
                }
            }
            note = chango.build_github_event_change_note(event, data)
            assert isinstance(note, CommentChangeNote)
            assert isinstance(note.uid, str)
            assert len(note.uid) == len(shortuuid.ShortUUID().uuid())

        def test_section_change_note_no_existing(self, section_chango):
            event = {
                "pull_request": {
                    "html_url": "https://example.com/pull/42",
                    "number": 33,
                    "title": "example title",
                    "user": {"login": "author"},
                }
            }
            data = ChanGoActionData(
                parent_pull_request=ParentPullRequest(
                    url="https://example.com/pull/43",
                    number=50,
                    title="example title",
                    state="OPEN",
                    author_login="author",
                ),
                linked_issues=None,
            )
            note = section_chango.build_github_event_change_note(event, data)
            assert isinstance(note, DummySectionChangeNote)
            assert note.slug == "0033"
            assert note.pull_requests == (
                PullRequest(uid="33", author_uids=("author",), closes_threads=()),
            )
            assert note.req_0 == "example title"

        @pytest.mark.parametrize("modifies", [True, False])
        def test_section_change_note_with_existing(self, section_chango, modifies):
            event = {
                "pull_request": {
                    "html_url": "https://example.com/pull/42",
                    "number": 101,
                    "title": "example title" if modifies else "req_0",
                    "user": {"login": "author"},
                }
            }
            note = section_chango.build_github_event_change_note(event, None)
            if not modifies:
                assert note is None
                return

            assert isinstance(note, DummySectionChangeNote)
            assert note.slug == "0101"
            assert note.uid == "abcuc4HVWKycYccXAcM3xc"
            assert note.pull_requests == (
                PullRequest(uid="101", author_uids=("author",), closes_threads=()),
            )
            assert note.req_0 == "example title"

        def test_section_change_note_with_existing_parent(self, section_chango, monkeypatch):
            def get_sections(*args, **kwargs):  # noqa: ARG001
                return {"req_0", "opt_0"}

            monkeypatch.setattr(DummySectionChangeNote, "get_sections", get_sections)

            event = {
                "pull_request": {
                    "html_url": "https://example.com/pull/42",
                    "number": 45,
                    "title": "example title",
                    "user": {"login": "author"},
                }
            }
            data = ChanGoActionData(
                parent_pull_request=ParentPullRequest(
                    url="https://example.com/pull/43",
                    number=43,
                    title="example title",
                    state="OPEN",
                    author_login="author",
                ),
                linked_issues=None,
            )
            note = section_chango.build_github_event_change_note(event, data)

            assert isinstance(note, DummySectionChangeNote)
            assert note.slug == "0043"
            assert note.pull_requests == (
                PullRequest(
                    uid="43",
                    author_uids=("parent_author",),
                    closes_threads=("existing_thread1", "existing_thread2"),
                ),
                PullRequest(uid="45", author_uids=("author",), closes_threads=()),
            )
            assert note.req_0 == "existing_req_0\nexample title"
            assert note.opt_0 == "example title"
            assert note.opt_1 == "existing_opt_1"

    def test_build_version_note_version(self, chango):
        version = Version("uid", dtm.date.today())
        note = chango.build_version_note(version)
        assert isinstance(note, CommentVersionNote)
        assert note.version == version

    def test_build_version_note_none(self, chango):
        note = chango.build_version_note(None)
        assert isinstance(note, CommentVersionNote)
        assert note.version is None

    def test_build_version_note_sections(self, section_chango):
        note = section_chango.build_version_note(None)
        assert isinstance(note, SectionVersionNote)
        assert note.version is None

    def test_build_version_history(self, chango):
        history = chango.build_version_history()
        assert isinstance(history, HeaderVersionHistory)

    @pytest.mark.parametrize("idx", [1, 2, 3])
    def test_load_change_note(self, chango, idx):
        uid = f"uid_1-{idx}_0"
        change_note = chango.load_change_note(uid)
        assert isinstance(change_note, CommentChangeNote)
        assert change_note.uid == uid
        assert change_note.slug == "comment-change-note"
        path = (
            self.DATA_ROOT / f"1.{idx}_2024-01-0{idx}" / f"comment-change-note.uid_1-{idx}_0.txt"
        )
        assert change_note.comment == path.read_text()

    @pytest.mark.parametrize(
        "version",
        [
            None,
            "1.1",
            Version("1.1", dtm.date(2024, 1, 1)),
            "1.2",
            Version("1.2", dtm.date(2024, 1, 2)),
        ],
    )
    @pytest.mark.parametrize(
        "change_note", ["str_change_note", CommentChangeNote.build_template("slug", "uid")]
    )
    def test_get_write_directory(self, chango, version, change_note):
        if version is None:
            expected_path = chango.scanner.unreleased_directory
        else:
            version_uid = ensure_uid(version)
            day = int(version_uid.split(".")[-1])
            expected_path = chango.scanner.base_directory / f"{version_uid}_2024-01-0{day}"

        path_existed = expected_path.is_dir()

        try:
            path = chango.get_write_directory(change_note, version)
            assert path.is_dir()
        finally:
            # Clean up
            if (expected_path is not None) and (not path_existed):
                expected_path.rmdir()

    @pytest.mark.parametrize(
        "change_note", ["str_change_note", CommentChangeNote.build_template("slug", "uid")]
    )
    def test_get_write_directory_new_version(self, chango, change_note):
        version = Version("new-version", dtm.date(2024, 1, 17))
        expected_path = chango.scanner.base_directory / "new-version_2024-01-17"
        try:
            path = chango.get_write_directory(change_note, version)
            assert path.is_dir()
            assert path == expected_path
        finally:
            # Clean up
            expected_path.rmdir()

    @pytest.mark.parametrize(
        "change_note", ["str_change_note", CommentChangeNote.build_template("slug", "uid")]
    )
    def test_get_write_directory_new_str_version(self, chango, change_note):
        with pytest.raises(ChanGoError, match=r"'new-version' not available."):
            chango.get_write_directory(change_note, "new-version")