Skip to content

Commit

Permalink
Replace toml with tomlkit
Browse files Browse the repository at this point in the history
We were hit by this bug: uiri/toml#280

Since tomlkit is a dependency of poetry, it's less likely to
happen again.

This fixes tests on Windows, which involved the string `python\3.7.6\x64\python.exe`

Note that tomlkit use its own clases for the contents of the TOML file, so

* we have to be more careful when modifying the contents in place
  (as in the hook tests)

* we can no longer put the regex directly into the parsed contents, and must
  do it when we instantiate the Config object
  • Loading branch information
dmerejkowsky committed Feb 4, 2020
1 parent f885858 commit d267c9e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
17 changes: 15 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ docopt = "^0.6.2"
path = "13.0"
cli-ui = "^0.10.0"
schema = "^0.7.1"
toml = "^0.10.0"
tomlkit = "^0.5.8"

[tool.poetry.dev-dependencies]
black = { version = "19.10b0", python = '^3.6' }
Expand Down
18 changes: 8 additions & 10 deletions tbump/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import attr
import schema
from path import Path
import toml
import tomlkit

from .hooks import HOOKS_CLASSES, Hook, BeforeCommitHook, AfterPushHook # noqa

Expand Down Expand Up @@ -69,12 +69,13 @@ def validate(config: Dict[str, Any]) -> Config:

hook_schema = schema.Schema({"name": str, "cmd": str})

def compile_re(regex: str) -> Pattern[str]:
return re.compile(regex, re.VERBOSE)
def validate_re(regex: str) -> str:
re.compile(regex, re.VERBOSE)
return regex

tbump_schema = schema.Schema(
{
"version": {"current": str, "regex": schema.Use(compile_re)},
"version": {"current": str, "regex": schema.Use(validate_re)},
"git": {"message_template": ValidMessage(), "tag_template": ValidTag()},
"file": [file_schema],
schema.Optional("hook"): [hook_schema], # retro-compat
Expand All @@ -87,13 +88,10 @@ def compile_re(regex: str) -> Pattern[str]:


def parse(cfg_path: Path) -> Config:
parsed = None
with cfg_path.open() as stream:
parsed = toml.load(stream)

parsed = validate(parsed) # type: ignore
parsed = tomlkit.loads(cfg_path.text())
parsed = validate(parsed)
current_version = parsed["version"]["current"]
version_regex = parsed["version"]["regex"]
version_regex = re.compile(parsed["version"]["regex"], re.VERBOSE)
match = version_regex.fullmatch(current_version)
if not match:
message = "Current version: %s does not match version regex" % current_version
Expand Down
6 changes: 3 additions & 3 deletions tbump/test/data/yarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json

import path
import toml
import tomlkit


def main() -> None:
Expand All @@ -16,10 +16,10 @@ def main() -> None:
current_version = parsed["version"]

yarn_lock = path.Path("yarn.lock")
parsed = toml.loads(yarn_lock.text())
parsed = tomlkit.loads(yarn_lock.text())
parsed["dependencies"]["hello"] = current_version
parsed["dependencies"]["some-dep"] = "1.2.0"
yarn_lock.write_text(toml.dumps(parsed))
yarn_lock.write_text(tomlkit.dumps(parsed))


if __name__ == "__main__":
Expand Down
13 changes: 8 additions & 5 deletions tbump/test/test_hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from path import Path
import sys
import toml
import tomlkit
import pytest

import tbump.git
Expand All @@ -14,16 +14,19 @@ def add_hook(test_repo: Path, name: str, cmd: str, after_push: bool = False) ->
"""
cfg_path = test_repo / "tbump.toml"
parsed = toml.loads(cfg_path.text())
parsed = tomlkit.loads(cfg_path.text())
if after_push:
key = "after_push"
else:
key = "before_commit"
if key not in parsed:
parsed[key] = list()
parsed[key].append({"cmd": cmd, "name": name})
parsed[key] = tomlkit.aot()
hook_config = tomlkit.table()
hook_config.add("cmd", cmd)
hook_config.add("name", name)
parsed[key].append(hook_config)

cfg_path.write_text(toml.dumps(parsed))
cfg_path.write_text(tomlkit.dumps(parsed))
tbump.git.run_git(test_repo, "add", ".")
tbump.git.run_git(test_repo, "commit", "--message", "update hooks")

Expand Down
4 changes: 2 additions & 2 deletions tbump/test/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from path import Path
import toml
import tomlkit

import tbump.config
import tbump.main
Expand All @@ -16,7 +16,7 @@ def test_creates_config(test_repo: Path) -> None:
tbump.main.main(["-C", test_repo, "init", current_version])

assert tbump_path.exists()
config = toml.loads(tbump_path.text())
config = tomlkit.loads(tbump_path.text())
assert config["version"]["current"] == "1.2.41-alpha1"


Expand Down
14 changes: 7 additions & 7 deletions tbump/test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any
import toml
import tomlkit
from cli_ui.tests import MessageRecorder
from tbump.test.conftest import file_contains

Expand All @@ -13,7 +13,7 @@

def files_bumped(test_repo: Path) -> bool:
toml_path = test_repo / "tbump.toml"
new_toml = toml.loads(toml_path.text())
new_toml = tomlkit.loads(toml_path.text())
assert new_toml["version"]["current"] == "1.2.41-alpha-2"

return all(
Expand All @@ -27,7 +27,7 @@ def files_bumped(test_repo: Path) -> bool:

def files_not_bumped(test_repo: Path) -> bool:
toml_path = test_repo / "tbump.toml"
new_toml = toml.loads(toml_path.text())
new_toml = tomlkit.loads(toml_path.text())
assert new_toml["version"]["current"] == "1.2.41-alpha-1"

return all(
Expand Down Expand Up @@ -172,9 +172,9 @@ def test_tbump_toml_bad_syntax(
test_repo: Path, message_recorder: MessageRecorder
) -> None:
toml_path = test_repo / "tbump.toml"
bad_toml = toml.loads(toml_path.text())
bad_toml = tomlkit.loads(toml_path.text())
del bad_toml["git"]
toml_path.write_text(toml.dumps(bad_toml))
toml_path.write_text(tomlkit.dumps(bad_toml))
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42", "--non-interactive"])
assert message_recorder.find("Invalid config")
Expand Down Expand Up @@ -291,9 +291,9 @@ def test_do_not_add_untracked_files(test_repo: Path) -> None:

def test_bad_substitution(test_repo: Path, message_recorder: MessageRecorder) -> None:
toml_path = test_repo / "tbump.toml"
new_toml = toml.loads(toml_path.text())
new_toml = tomlkit.loads(toml_path.text())
new_toml["file"][0]["version_template"] = "{release}"
toml_path.write_text(toml.dumps(new_toml))
toml_path.write_text(tomlkit.dumps(new_toml))
tbump.git.run_git(test_repo, "add", ".")
tbump.git.run_git(test_repo, "commit", "--message", "update repo")
with pytest.raises(SystemExit):
Expand Down

0 comments on commit d267c9e

Please sign in to comment.