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
|
import builtins
import functools
import getpass
import glob
import importlib
from types import SimpleNamespace
import pytest
from click.testing import CliRunner
from ruamel.yaml import YAML
from changelogd import changelogd
from changelogd import commands
from changelogd.config import Config
from changelogd.config import DEFAULT_CONFIG
yaml = YAML()
class FakeContext:
def __enter__(self):
pass
def __exit__(self, *_):
pass
class FakePath:
def __init__(self, *args, **kwargs):
pass
def __truediv__(self, other):
return self
def open(self, *args, **kwargs):
return FakeContext()
def absolute(self):
return None
def fake_yaml_dump(data, _, namespace):
namespace.data = data
def test_incorrect_input_entry():
runner = CliRunner()
entry = runner.invoke(commands.entry)
assert entry.exit_code == 1
def test_entry_help(setup_env):
runner = CliRunner()
runner.invoke(commands.init)
# this is required to update the decorators which can be done
# after initializing configuration
importlib.reload(commands)
entry = runner.invoke(
commands.entry,
["--help"],
)
assert entry.exit_code == 0
assert (
entry.stdout
== """Usage: entry [OPTIONS]
Create a new changelog entry.
Options:
-v, --verbose Increase verbosity.
--message TEXT Changelog message
--issue-id TEXT Issue ID
--type TEXT Message type (as number or string).
--release TEXT Attach entry to a release.
--help Show this message and exit.
"""
)
@pytest.mark.parametrize("type_input", ["1", "feature"])
def test_non_interactive_data(setup_env, type_input):
runner = CliRunner()
runner.invoke(commands.init)
# this is required to update the decorators which can be done
# after initializing configuration
importlib.reload(commands)
entry = runner.invoke(
commands.entry,
["--type", type_input, "--message", "test message", "--issue-id", "100"],
)
assert entry.exit_code == 0
entries = glob.glob(str(setup_env / "changelog.d" / "*entry.yaml"))
assert len(entries) == 1
with open(entries[0]) as entry_fh:
entry_content = yaml.load(entry_fh)
assert entry_content.pop("timestamp")
assert entry_content == {
"git_email": "user@example.com",
"git_user": "Some User",
"issue_id": "100",
"message": "test message",
"os_user": "test-user",
"type": "feature",
}
def test_multi_value_string(setup_env):
runner = CliRunner()
runner.invoke(commands.init)
entry = runner.invoke(
commands.entry,
["--type", "1", "--message", "test message"],
input='a, b,"c,d", e,f',
)
assert entry.exit_code == 0
entries = glob.glob(str(setup_env / "changelog.d" / "*entry.yaml"))
assert len(entries) == 1
with open(entries[0]) as entry_fh:
entry_content = yaml.load(entry_fh)
assert entry_content.pop("timestamp")
assert entry_content == {
"git_email": "user@example.com",
"git_user": "Some User",
"issue_id": ["a", "b", "c,d", "e", "f"],
"message": "test message",
"os_user": "test-user",
"type": "feature",
}
def test_entry_missing_message_types(setup_env, caplog):
runner = CliRunner()
runner.invoke(commands.init)
with open(setup_env / "changelog.d" / "config.yaml") as config_fh:
config_content = yaml.load(config_fh)
config_content.pop("message_types")
with open(setup_env / "changelog.d" / "config.yaml", "w+") as config_fh:
yaml.dump(config_content, config_fh)
caplog.clear()
entry = runner.invoke(commands.entry)
assert entry.exit_code == 1
assert (
"The 'message_types' field is missing from the configuration" in caplog.messages
)
def test_entry_incorrect_entry_fields(setup_env, caplog):
runner = CliRunner()
runner.invoke(commands.init)
with open(setup_env / "changelog.d" / "config.yaml") as config_fh:
config_content = yaml.load(config_fh)
# just name with correct value, this should be fine
config_content["entry_fields"] = [{"name": "just_name", "required": False}]
with open(setup_env / "changelog.d" / "config.yaml", "w+") as config_fh:
yaml.dump(config_content, config_fh)
caplog.clear()
entry = runner.invoke(commands.entry, input="1\n\n")
assert entry.exit_code == 0
importlib.reload(commands)
# make sure that missing verbose_name doesn't cause a problem
entry = runner.invoke(
commands.entry,
["--help"],
)
assert entry.exit_code == 0
# also the `--just-name` option won't have any help
assert (
entry.stdout
== """Usage: entry [OPTIONS]
Create a new changelog entry.
Options:
-v, --verbose Increase verbosity.
--just-name TEXT
--type TEXT Message type (as number or string).
--release TEXT Attach entry to a release.
--help Show this message and exit.
"""
)
# name contains space, not good
config_content["entry_fields"] = [{"name": "just name", "required": False}]
with open(setup_env / "changelog.d" / "config.yaml", "w+") as config_fh:
yaml.dump(config_content, config_fh)
caplog.clear()
entry = runner.invoke(commands.entry, input="1\n\n")
assert entry.exit_code == 1
assert (
"The 'name' argument of an 'entry_fields' element cannot contain spaces."
in caplog.messages
)
# no name at all, also bad
config_content["entry_fields"] = [{"verbose_name": "just_name", "required": False}]
with open(setup_env / "changelog.d" / "config.yaml", "w+") as config_fh:
yaml.dump(config_content, config_fh)
caplog.clear()
entry = runner.invoke(commands.entry, input="1\n\n")
assert entry.exit_code == 1
assert "Each 'entry_fields' element needs to have 'name'." in caplog.messages
def test_user_data(monkeypatch, fake_process):
namespace = SimpleNamespace()
config = Config()
config._data = {**DEFAULT_CONFIG}
config._path = FakePath("/test")
fake_process.register(
["git", "config", "--list"],
stdout=("user.name=Some User\n" "user.email=user@example.com\n"),
)
fake_process.register(["git", "add", fake_process.any()])
fake_process.keep_last_process(True)
monkeypatch.setattr(getpass, "getuser", lambda: "test-user")
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 == {
"git_email": "user@example.com",
"git_user": "Some User",
"issue_id": ["1"],
"message": "1",
"os_user": "test-user",
"type": "feature",
}
config._data["user_data"] = ["os_user"]
changelogd.entry(config, None, {})
assert namespace.data.pop("timestamp")
assert namespace.data == {
"issue_id": ["1"],
"message": "1",
"os_user": "test-user",
"type": "feature",
}
config._data["user_data"] = [
"os_user:overridden_username",
"git_user:overridden_git_user",
]
changelogd.entry(config, None, {})
assert namespace.data.pop("timestamp")
assert namespace.data == {
"issue_id": ["1"],
"message": "1",
"type": "feature",
"overridden_username": "test-user",
"overridden_git_user": "Some User",
}
config._data["user_data"] = None
changelogd.entry(config, None, {})
assert namespace.data.pop("timestamp")
assert namespace.data == {
"issue_id": ["1"],
"message": "1",
"type": "feature",
}
config._data["user_data"] = ["not_exist"]
with pytest.raises(SystemExit) as exc:
changelogd.entry(config, None, {})
assert str(exc.value) == (
"The 'not_exist' variable is not supported in 'user_data'. "
"Available choices are: 'os_user, git_user, git_email'."
)
|