forked from callowayproject/bump-my-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_show.py
170 lines (145 loc) · 5.29 KB
/
test_show.py
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
import shutil
from dataclasses import dataclass
from pathlib import Path
from tests.conftest import get_config_data, inside_dir
import pytest
from pytest import param
from bumpversion import show
from bumpversion import config
mapping = {"key1": "value1", "dict-key": {"dict-key-key1": "value2-1"}}
FIXTURES_PATH = Path(__file__).parent.joinpath("fixtures")
@dataclass
class SimpleObj:
"""An object for testing."""
dict_attr: dict
list_attr: list
@property
def exc(self):
return self._notavailable
test_obj = SimpleObj(dict_attr=mapping, list_attr=["a", "b", "c", "d"])
@pytest.mark.parametrize(
["name", "data", "expected"],
[
param("key1", mapping, "value1", id="simple mapping"),
param("dict-key.dict-key-key1", mapping, "value2-1", id="nested mapping"),
param("dict_attr", test_obj, mapping, id="attribute lookup"),
param("list_attr.2", test_obj, "c", id="list lookup"),
],
)
def test_resolve_name(name: str, data, expected):
"""Test the resolve_name method gets the correct values."""
assert show.resolve_name(data, name) == expected
def test_resolve_name_default():
"""Test a default value."""
assert show.resolve_name(mapping, "key3", default="default") == "default"
def test_resolve_name_property_error():
"""An error in a property returns default."""
assert show.resolve_name(test_obj, "exc", default="default") == "default"
DEFAULT_OUTPUT = FIXTURES_PATH.joinpath("basic_cfg_expected.txt").read_text().strip()
YAML_OUTPUT = FIXTURES_PATH.joinpath("basic_cfg_expected.yaml").read_text().strip()
JSON_OUTPUT = FIXTURES_PATH.joinpath("basic_cfg_expected_full.json").read_text().strip()
FOUR_ARGS_DEFAULT_OUTPUT = "{'allow_dirty': False,\n 'current_version': '1.0.0',\n 'sign_tags': False,\n 'tag': True}"
FOUR_ARGS_YAML_OUTPUT = 'allow_dirty: false\ncurrent_version: "1.0.0"\nsign_tags: false\ntag: true'
FOUR_ARGS_JSON_OUTPUT = '{"allow_dirty": false, "current_version": "1.0.0", "sign_tags": false, "tag": true}'
DOTTED_DEFAULT_OUTPUT = (
"{'current_version': '1.0.0',\n 'files.1.filename': 'bumpversion/__init__.py',\n"
" 'parts.release.values': ['dev', 'gamma']}"
)
DOTTED_YAML_OUTPUT = (
'current_version: "1.0.0"\n'
'files.1.filename: "bumpversion/__init__.py"\n'
"parts.release.values:\n"
' - "dev"\n'
' - "gamma"'
)
DOTTED_JSON_OUTPUT = (
'{\n "current_version": "1.0.0",'
'\n "files.1.filename": "bumpversion/__init__.py",\n "parts.release.values": [\n "dev",\n "gamma"\n ]\n}'
)
@pytest.mark.parametrize(
["req_args", "format_", "expected"],
(
param(
[],
None,
DEFAULT_OUTPUT,
id="no-arguments-default",
),
param(
[],
"yaml",
YAML_OUTPUT,
id="no-arguments-yaml",
),
param(
[],
"json",
JSON_OUTPUT,
id="no-arguments-json",
),
param(
["all"],
None,
DEFAULT_OUTPUT,
id="all-argument-default",
),
param(
["all", "current_version"],
None,
DEFAULT_OUTPUT,
id="all-argument-in-args-default",
),
param(
["current_version"],
"default",
"1.0.0",
id="current_version",
),
param(
["current_version", "allow_dirty", "sign_tags", "tag"],
"default",
FOUR_ARGS_DEFAULT_OUTPUT,
id="four_args_default",
),
param(
["files.1.filename", "current_version", "parts.release.values"],
"default",
DOTTED_DEFAULT_OUTPUT,
id="dotted-default",
),
param(
["files.1.filename", "current_version", "parts.release.values"],
"yaml",
DOTTED_YAML_OUTPUT,
id="dotted-yaml",
),
param(
["files.1.filename", "current_version", "parts.release.values"],
"json",
DOTTED_JSON_OUTPUT,
id="dotted-json",
),
),
)
def test_do_show(
req_args: list, format_: str, expected: str, tmp_path: Path, fixtures_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""Test the show method."""
config_path = tmp_path / "pyproject.toml"
toml_path = fixtures_path / "basic_cfg.toml"
shutil.copy(toml_path, config_path)
with inside_dir(tmp_path):
conf = config.get_configuration(config_file=fixtures_path.joinpath(config_path))
show.do_show(config=conf, format_=format_, *req_args)
captured = capsys.readouterr()
assert captured.out.strip() == expected
def test_do_show_increment(tmp_path: Path, fixtures_path: Path, capsys: pytest.CaptureFixture) -> None:
"""Test the show method with increment."""
config_path = tmp_path / "pyproject.toml"
toml_path = fixtures_path / "basic_cfg.toml"
shutil.copy(toml_path, config_path)
with inside_dir(tmp_path):
conf = config.get_configuration(config_file=fixtures_path.joinpath(config_path))
show.do_show("current_version", "new_version", config=conf, format_="default", increment="minor")
captured = capsys.readouterr()
assert captured.out.strip() == "{'current_version': '1.0.0', 'new_version': '1.1.0-dev'}"