Skip to content

Commit

Permalink
Increase coverage and fix a few more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmerejkowsky committed Apr 9, 2018
1 parent 4965019 commit be665d6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
2 changes: 1 addition & 1 deletion tbump/file_bumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def should_replace(line, old_string, search=None):


def on_version_containing_none(src, verb, version, *, groups, template):
raise BadSubstitution(src=src, version=version, groups=groups, template=template)
raise BadSubstitution(src=src, verb=verb, version=version, groups=groups, template=template)


def find_replacements(file_path, old_string, new_string, search=None):
Expand Down
9 changes: 7 additions & 2 deletions tbump/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@


class InvalidConfig(tbump.Error):
def __init__(self, io_error=None, parse_error=None):
self.io_error = io_error
self.parse_error = parse_error

def print_error(self):
if self.io_error:
ui.error("Could not read config file:", self.io_error)
Expand All @@ -24,7 +28,8 @@ def print_error(self):


class Cancelled(tbump.Error):
pass
def print_error(self):
ui.error("Cancelled by user")


def main(args=None):
Expand Down Expand Up @@ -120,7 +125,7 @@ def check(self):
self.tracked_branch = False
proceed = ui.ask_yes_no("Continue anyway?", default=False)
if not proceed:
raise Cancelled()
raise Cancelled from None

def push(self):
if self.dry_run:
Expand Down
26 changes: 18 additions & 8 deletions tbump/test/test_git_bumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@
import pytest


def test_git_bumper_happy_path(test_repo):
@pytest.fixture
def test_git_bumper(test_repo):
config = tbump.config.parse(test_repo.joinpath("tbump.toml"))
git_bumper = tbump.git_bumper.GitBumper(test_repo)
git_bumper.set_config(config)
git_bumper.check_state("1.2.42")
return git_bumper


def test_git_bumper_happy_path(test_repo, test_git_bumper):
test_git_bumper.check_state("1.2.42")
test_repo.joinpath("VERSION").write_text("1.2.42")
git_bumper.bump("1.2.42")
test_git_bumper.bump("1.2.42")
_, out = tbump.git.run_git_captured(test_repo, "log", "--oneline")
assert "Bump to 1.2.42" in out


def test_git_bumper_no_tracking_ref(test_repo):
config = tbump.config.parse(test_repo.joinpath("tbump.toml"))
git_bumper = tbump.git_bumper.GitBumper(test_repo)
git_bumper.set_config(config)
def test_git_bumper_no_tracking_ref(test_repo, test_git_bumper):
tbump.git.run_git(test_repo, "checkout", "-b", "devel")

with pytest.raises(tbump.git_bumper.NoTrackedBranch):
git_bumper.check_state("1.2.42")
test_git_bumper.check_state("1.2.42")


def test_not_on_any_branch(test_repo, test_git_bumper):
tbump.git.run_git(test_repo, "commit", "--message", "test", "--allow-empty")
tbump.git.run_git(test_repo, "checkout", "HEAD~1")

with pytest.raises(tbump.git_bumper.NotOnAnyBranch):
test_git_bumper.check_state("1.2.42")
60 changes: 52 additions & 8 deletions tbump/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def test_replaces(test_repo):
assert_in_file("pub.js", "PUBLIC_VERSION = '1.2.41'")


def test_tbump_toml_not_found(test_repo, message_recorder):
toml_path = test_repo.joinpath("tbump.toml")
toml_path.remove()
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42", "--non-interactive"])
assert message_recorder.find("No such file")


def test_tbump_toml_bad_syntax(test_repo, message_recorder):
toml_path = test_repo.joinpath("tbump.toml")
bad_toml = toml.loads(toml_path.text())
del bad_toml["git"]
toml_path.write_text(toml.dumps(bad_toml))
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42", "--non-interactive"])
assert message_recorder.find("Missing keys")


def test_new_version_does_not_match(test_repo, message_recorder):
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.41a2", "--non-interactive"])
Expand Down Expand Up @@ -78,40 +96,66 @@ def test_abort_if_file_does_not_match(test_repo, message_recorder):
assert_in_file("VERSION", "1.2.41-alpha-1")


def test_no_tracked_branch__interactive__ask_to_skip_push(test_repo, mock):
def test_no_tracked_branch_proceed_and_skip_push(test_repo, mock):
ask_mock = mock.patch("ui.ask_yes_no")
ask_mock.return_value = True
tbump.git.run_git(test_repo, "checkout", "-b", "devel")

tbump.main.run(["-C", test_repo, "1.2.42"])
tbump.main.main(["-C", test_repo, "1.2.42"])

ask_mock.assert_called_with("Continue anyway?", default=False)
assert_in_file("VERSION", "1.2.42")


def test_no_tracked_branch__non_interactive__abort(test_repo, mock):
def test_no_tracked_branch_cancel(test_repo, mock, message_recorder):
ask_mock = mock.patch("ui.ask_yes_no")
ask_mock.return_value = False
tbump.git.run_git(test_repo, "checkout", "-b", "devel")

with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42"])

ask_mock.assert_called_with("Continue anyway?", default=False)
assert_in_file("VERSION", "1.2.41")
assert message_recorder.find("Cancelled")


def test_no_tracked_branch_non_interactive(test_repo, message_recorder):
tbump.git.run_git(test_repo, "checkout", "-b", "devel")

with pytest.raises(tbump.git_bumper.NoTrackedBranch):
tbump.main.run(["-C", test_repo, "1.2.42", "--non-interactive"])
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42", "--non-interactive"])
assert message_recorder.find("Cannot push")


def test_interactive_push(test_repo, mock):
ask_mock = mock.patch("ui.ask_yes_no")
ask_mock.return_value = True
tbump.main.run(["-C", test_repo, "1.2.42"])
tbump.main.main(["-C", test_repo, "1.2.42"])
ask_mock.assert_called_with("OK to push", default=False)
_, out = tbump.git.run_git_captured(test_repo, "ls-remote")
assert "tags/v1.2.42" in out


def test_do_not_add_untracked_files(test_repo):
test_repo.joinpath("untracked.txt").write_text("please don't add me")
tbump.main.run(["-C", test_repo, "1.2.42", "--non-interactive"])
tbump.main.main(["-C", test_repo, "1.2.42", "--non-interactive"])
_, out = tbump.git.run_git_captured(test_repo, "show", "--stat", "HEAD")
assert "untracked.txt" not in out


def test_dry_run(test_repo):
tbump.main.run(["-C", test_repo, "1.2.41-alpha-2", "--dry-run"])
tbump.main.main(["-C", test_repo, "1.2.41-alpha-2", "--dry-run"])
assert_in_file("VERSION", "1.2.41-alpha-1")


def test_bad_subsitiution(test_repo, message_recorder):
toml_path = test_repo.joinpath("tbump.toml")
new_toml = toml.loads(toml_path.text())
new_toml["file"][0]["version_template"] = "{release}"
toml_path.write_text(toml.dumps(new_toml))
tbump.git.run_git(test_repo, "add", ".")
tbump.git.run_git(test_repo, "commit", "--message", "update repo")
with pytest.raises(SystemExit):
tbump.main.main(["-C", test_repo, "1.2.42", "--dry-run"])
assert message_recorder.find("refusing to replace by version containing 'None'")

0 comments on commit be665d6

Please sign in to comment.