File: test_entry_computed_values.py

package info (click to toggle)
python-changelogd 0.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 448 kB
  • sloc: python: 1,921; makefile: 21
file content (308 lines) | stat: -rw-r--r-- 9,174 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
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
import builtins
import functools
import getpass
from pathlib import Path
from types import SimpleNamespace

import pytest
from pytest_subprocess import FakeProcess
from ruamel.yaml import YAML

from changelogd import changelogd
from changelogd.config import Config

yaml = YAML()


def fake_yaml_dump(data, _, namespace):
    namespace.data = data


def test_missing_type(monkeypatch, fp: FakeProcess, fs):
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- name: test\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)

    with pytest.raises(
        SystemExit,
        match="Missing `type` for computed value: {'name': 'test'}",
    ):
        changelogd.entry(config, None, {})


def test_invalid_type(monkeypatch, fp: FakeProcess, fs):
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: invalid\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)

    with pytest.raises(
        SystemExit,
        match=(
            "Unavailable type: 'invalid'. Available types: "
            "local_branch_name, remote_branch_name, branch_name, last_commit_message"
        ),
    ):
        changelogd.entry(config, None, {})


def test_basic_data(monkeypatch, fp: FakeProcess, fs):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: branch_name\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout="local_branch_name"
    )
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
        stdout="remote_branch_name",
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "branch_name": "local_branch_name - remote_branch_name",
    }


@pytest.mark.parametrize(
    "branches",
    [
        ("fixing task JIRA-1234", "remote_branch_name"),
        ("local_branch_name", "fixing task JIRA-1234"),
    ],
)
def test_matching_regex(monkeypatch, fp: FakeProcess, fs, branches):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: branch_name\n"
            "  regex: '(?P<value>JIRA-\\d+)'\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    local_branch_name, remote_branch_name = branches
    fp.register(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=local_branch_name)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
        stdout=remote_branch_name,
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "branch_name": "JIRA-1234",
    }


def test_not_matching_regex(monkeypatch, fp: FakeProcess, fs, caplog):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: local_branch_name\n"
            "  regex: '(?P<value>JIRA-\\d+)'\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout="local_branch_name"
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "local_branch_name": None,
    }
    assert (
        caplog.messages[0]
        == "The regex '(?P<value>JIRA-\\d+)' didn't match 'local_branch_name'."
    )


def test_subprocess_failure(monkeypatch, fp: FakeProcess, fs, caplog):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: branch_name\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    fp.register(["git", "rev-parse", "--abbrev-ref", "HEAD"], returncode=128)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
        returncode=128,
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "branch_name": None,
    }
    assert (
        caplog.messages[0]
        == "Failed to run 'git rev-parse --abbrev-ref HEAD' to get local branch name"
    )
    assert caplog.messages[2] == (
        "Failed to run 'git rev-parse --abbrev-ref --symbolic-full-name @{u}' "
        "to get remote branch name"
    )


def test_default_value(monkeypatch, fp: FakeProcess, fs, caplog):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: branch_name\n"
            "  default: default_name\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    fp.register(["git", "rev-parse", "--abbrev-ref", "HEAD"], returncode=128)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
        returncode=128,
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "branch_name": "default_name",
    }


def test_default_not_matching_regex(monkeypatch, fp: FakeProcess, fs, caplog):
    namespace = SimpleNamespace()
    config_path = Path("/fake/path/to/changelog.d")
    fs.create_file(
        config_path / "config.yaml",
        contents=(
            "message_types:\n"
            "- name: feature\n"
            "  title: Features\n"
            "computed_values:\n"
            "- type: local_branch_name\n"
            "  default: default_name\n"
            "  regex: '(?P<value>JIRA-\\d+)'\n"
            "user_data: null\n"
        ),
    )
    config = Config(config_path)
    fp.register(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout="local_branch_name"
    )
    fp.register(["git", "add", fp.any()])
    fp.keep_last_process(True)

    monkeypatch.setattr(
        YAML, "dump", functools.partial(fake_yaml_dump, namespace=namespace)
    )
    monkeypatch.setattr(builtins, "input", lambda _: "1")

    changelogd.entry(config, None, {})
    assert namespace.data.pop("timestamp")
    assert namespace.data == {
        "type": "feature",
        "local_branch_name": "default_name",
    }
    assert (
        caplog.messages[0]
        == "The regex '(?P<value>JIRA-\\d+)' didn't match 'local_branch_name'."
    )