def test_test(temp_configfile, mocker, requestsmock): with open(temp_configfile, "w") as f: saved = { "BUGZILLA": { "api_key": "secret", "bugzilla_url": "https://example.com" } } json.dump(saved, f) requestsmock.get( "https://example.com/rest/whoami?api_key=secret", content=json.dumps({ "real_name": "John Doe", "name": "*****@*****.**", "id": 1 }).encode("utf-8"), ) runner = CliRunner() config = Config() config.configfile = temp_configfile config.bugzilla_url = "https://example.com" result = runner.invoke(bugzilla.test, [], obj=config) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception assert "John Doe" in result.output
def test_login(temp_configfile, mocker, requestsmock): getpass = mocker.patch("getpass.getpass") getpass.return_value = "secret" requestsmock.get( "https://example.com/rest/whoami?api_key=secret", content=json.dumps({ "real_name": "Peter Bengtsson [:peterbe]", "name": "*****@*****.**", "id": 1, }).encode("utf-8"), ) runner = CliRunner() config = Config() config.configfile = temp_configfile config.bugzilla_url = "https://example.com" result = runner.invoke(bugzilla.login, [], input="myusername", obj=config) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception with open(temp_configfile) as f: saved = json.load(f) assert "BUGZILLA" in saved assert saved["BUGZILLA"]["api_key"] == "secret" assert saved["BUGZILLA"]["bugzilla_url"] == "https://example.com"
def test_get_summary_with_token(temp_configfile, requestsmock): requestsmock.get( "https://privatebugs.example.com/rest/bug/", content=json.dumps({ "bugs": [{ "assigned_to": "*****@*****.**", "assigned_to_detail": { "email": "*****@*****.**", "id": 1, "name": "*****@*****.**", "real_name": "Nobody; OK to take it and work on it", }, "id": 123456789, "status": "NEW", "summary": "This is a SECRET!", }], "faults": [], }).encode("utf-8"), ) with open(temp_configfile, "w") as f: saved = { "BUGZILLA": { "api_key": "secret", "bugzilla_url": "https://privatebugs.example.com", } } json.dump(saved, f) config = Config() config.configfile = temp_configfile config.bugzilla_url = "https://bugs.example.com" summary, url = bugzilla.get_summary(config, "123456789") assert summary == "This is a SECRET!" assert url == "https://privatebugs.example.com/show_bug.cgi?id=123456789"
def test_get_summary(temp_configfile, mocker, requestsmock): requestsmock.get( "https://bugs.example.com/rest/bug/", content=json.dumps({ "bugs": [{ "assigned_to": "*****@*****.**", "assigned_to_detail": { "email": "*****@*****.**", "id": 1, "name": "*****@*****.**", "real_name": "Nobody; OK to take it and work on it", }, "id": 123456789, "status": "RESOLVED", "summary": "This is the summary", }], "faults": [], }).encode("utf-8"), ) config = Config() config.configfile = temp_configfile config.bugzilla_url = "https://bugs.example.com" summary, url = bugzilla.get_summary(config, "123456789") assert summary == "This is the summary" assert url == "https://bugs.example.com/show_bug.cgi?id=123456789"
def test_test(temp_configfile, mocker): with open(temp_configfile, "w") as f: saved = { "GITHUB": { "token": "somelongapitokenhere", "login": "******", "github_url": "https://example.com", } } json.dump(saved, f) rget = mocker.patch("requests.get") def mocked_get(url, headers): assert url == "https://example.com/user" assert headers["Authorization"] == "token somelongapitokenhere" return Response({"id": 123456, "name": "Peter Bengtsson", "login": "******"}) rget.side_effect = mocked_get runner = CliRunner() config = Config() config.configfile = temp_configfile config.github_url = "https://example.com" result = runner.invoke(github.test, [], obj=config) assert result.exit_code == 0 assert not result.exception assert "Peter Bengtsson" in result.output
def test_token(temp_configfile, mocker): rget = mocker.patch("requests.get") getpass = mocker.patch("getpass.getpass") getpass.return_value = "somelongapitokenhere" def mocked_get(url, headers): assert url == "https://example.com/user" assert headers["Authorization"] == "token somelongapitokenhere" return Response({"name": "Peter", "login": "******"}) rget.side_effect = mocked_get runner = CliRunner() config = Config() config.configfile = temp_configfile config.github_url = "https://example.com" result = runner.invoke(github.token, [], obj=config) assert result.exit_code == 0 assert not result.exception with open(temp_configfile) as f: saved = json.load(f) assert "GITHUB" in saved assert saved["GITHUB"]["login"] == "peterbe" assert saved["GITHUB"]["token"] == "somelongapitokenhere" assert saved["GITHUB"]["github_url"] == "https://example.com"
def test_find_pull_requests(temp_configfile, mocker): rget = mocker.patch("requests.get") getpass = mocker.patch("getpass.getpass") getpass.return_value = "somelongapitokenhere" with open(temp_configfile, "w") as f: saved = { "GITHUB": { "token": "somelongapitokenhere", "login": "******", "github_url": "https://enterprise.github.com", } } json.dump(saved, f) def mocked_get(url, params, headers): assert url == "https://api.github.com/repos/myorg/myrepo/pulls" assert headers["Authorization"] == "token somelongapitokenhere" return Response([]) rget.side_effect = mocked_get config = Config() config.configfile = temp_configfile config.github_url = "https://example.com" found = github.find_pull_requests(config, "myorg", "myrepo") assert found == []
def test_commit_without_github(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-commit-test" mocked_git().active_branch.name = "my-topic-branch" mocked_git().index.diff.return_value = [MockDiff("foo.txt")] # first we have to fake some previous information state = json.load(open(temp_configfile)) state["gg-commit-test:my-topic-branch"] = { "description": "Some description", "bugnumber": None, } state["FORK_NAME"] = "myusername" with open(temp_configfile, "w") as f: json.dump(state, f) my_remote = mocker.MagicMock() origin_remote = mocker.MagicMock() origin_remote.url = "[email protected]:peterbe/gg-example.git" mocked_git().remotes = {"myusername": my_remote, "origin": origin_remote} my_pushinfo = mocker.MagicMock() my_pushinfo.flags = 0 my_remote.push.return_value = [my_pushinfo] runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(commit, [], input="\n\n", obj=config) if result.exception: # print(result.exception) # print(result.output) raise result.exception assert result.exit_code == 0 assert not result.exception
def test_rebase(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" active_branch = mocker.MagicMock() mocked_git().active_branch = active_branch mocked_remote = mocker.MagicMock() mocked_remote.name = "origin" mocked_git().remotes.__iter__.return_value = [mocked_remote] mocked_git().is_dirty.return_value = False state = json.load(open(temp_configfile)) state["FORK_NAME"] = "peterbe" with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(rebase, [], input="\n", obj=config) # if result.exception: # print(mocked_git.mock_calls) # print(result.output) # print(result.exception) # raise result.exception assert result.exit_code == 0 assert not result.exception mocked_git().git.rebase.assert_called_with("master")
def test_test_issue_url(temp_configfile, mocker): with open(temp_configfile, "w") as f: saved = { "GITHUB": { "token": "somelongapitokenhere", "login": "******", "github_url": "https://example.com", } } json.dump(saved, f) rget = mocker.patch("requests.get") def mocked_get(url, headers): assert url == "https://example.com/repos/peterbe/gg/issues/123" assert headers["Authorization"] == "token somelongapitokenhere" return Response( { "id": 123456, "title": "Issue Title Here", "html_url": "https://api.github.com/repos/peterbe/gg/issues/123", } ) rget.side_effect = mocked_get runner = CliRunner() config = Config() config.configfile = temp_configfile config.github_url = "https://example.com" result = runner.invoke( github.test, ["-i", "https://github.com/peterbe/gg/issues/123"], obj=config ) assert result.exit_code == 0 assert not result.exception assert "Issue Title Here" in result.output
def test_merge(temp_configfile, mocker): rget = mocker.patch("requests.get") def mocked_get(url, params, headers): assert url.endswith("/peterbe/gg-example/pulls") assert headers["Authorization"] == "token somelongapitokenhere" return Response([]) rget.side_effect = mocked_get mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-commit-test" branch1 = mocker.MagicMock() branch1.name = "this-branch" mocked_git().active_branch.name = branch1.name mocked_remote = mocker.MagicMock() mocked_remote.name = "origin" mocked_git().remotes.__iter__.return_value = [mocked_remote] mocked_git().is_dirty.return_value = False mocked_git().heads.__iter__.return_value = [branch1] my_remote = mocker.MagicMock() origin_remote = mocker.MagicMock() origin_remote.url = "[email protected]:peterbe/gg-example.git" my_pushinfo = mocker.MagicMock() my_pushinfo.flags = 0 my_remote.push.return_value = [my_pushinfo] # first we have to fake some previous information state = json.load(open(temp_configfile)) state["gg-commit-test:my-topic-branch"] = { "description": "Some description", "bugnumber": None, } # state["FORK_NAME"] = "myusername" state["GITHUB"] = { "token": "somelongapitokenhere", "github_url": "https://example.com", } with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(merge, [], input="\n\n", obj=config) if result.exception: # print(mocked_git.mock_calls) # print(result.output) # print(result.exception) raise result.exception assert result.exit_code == 0 mocked_git().git.merge.assert_called_with("this-branch") mocked_git().git.branch.assert_called_with("-d", "this-branch")
def test_commit_without_start(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-commit-test" runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(commit, [], input='foo "bar"\n', obj=config) assert result.exit_code > 0 assert result.exception assert "You're in a branch that was not created with gg." in result.output
def test_config(temp_configfile, mocker): # rget = mocker.patch('requests.get') getpass = mocker.patch("getpass.getpass") getpass.return_value = "somelongapitokenhere" runner = CliRunner() config_ = Config() config_.configfile = temp_configfile result = runner.invoke(config.config, ["-f", "myown"], obj=config_) assert result.exit_code == 0 assert not result.exception with open(temp_configfile) as f: saved = json.load(f) assert "FORK_NAME" in saved assert saved["FORK_NAME"] == "myown"
def test_commit(temp_configfile, mocker): rget = mocker.patch("requests.get") def mocked_get(url, params, headers): assert url.endswith("/peterbe/gg-example/pulls") assert headers["Authorization"] == "token somelongapitokenhere" return Response([]) rget.side_effect = mocked_get mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-commit-test" mocked_git().active_branch.name = "my-topic-branch" mocked_git().index.diff.return_value = [MockDiff("some/path.txt")] my_remote = mocker.MagicMock() origin_remote = mocker.MagicMock() origin_remote.url = "[email protected]:peterbe/gg-example.git" mocked_git().remotes = {"myusername": my_remote, "origin": origin_remote} my_pushinfo = mocker.MagicMock() my_pushinfo.flags = 0 my_remote.push.return_value = [my_pushinfo] # first we have to fake some previous information state = json.load(open(temp_configfile)) state["gg-commit-test:my-topic-branch"] = { "description": "Some description", "bugnumber": None, } state["FORK_NAME"] = "myusername" state["GITHUB"] = { "token": "somelongapitokenhere", "github_url": "https://example.com", } with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(commit, [], input="\n\n", obj=config) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception pr_url = ("https://github.com/peterbe/gg-example/compare/peterbe:master..." "myusername:my-topic-branch?expand=1") assert pr_url in result.output
def test_burn(temp_configfile, mocker): with open(temp_configfile, "w") as f: saved = {"GITHUB": {"token": "somelongapitokenhere", "login": "******"}} json.dump(saved, f) runner = CliRunner() config = Config() config.configfile = temp_configfile config.github_url = "https://example.com" result = runner.invoke(github.burn, [], obj=config) assert result.exit_code == 0 assert not result.exception with open(temp_configfile) as f: saved = json.load(f) assert "GITHUB" not in saved
def test_logout(temp_configfile, mocker): with open(temp_configfile, "w") as f: saved = {"BUGZILLA": {"api_key": "secret"}} json.dump(saved, f) runner = CliRunner() config = Config() config.configfile = temp_configfile config.bugzilla_url = "https://example.com" result = runner.invoke(bugzilla.logout, [], obj=config) assert result.exit_code == 0 assert not result.exception with open(temp_configfile) as f: saved = json.load(f) assert "BUGZILLA" not in saved
def test_cleanup(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" mocked_git().git.branch.return_value = """ this-branch * other-branch """ branch1 = mocker.MagicMock() branch1.name = "this-branch" branch2 = mocker.MagicMock() branch2.name = "other-branch" branch3 = mocker.MagicMock() branch3.name = "not-merged-branch" mocked_git().heads.__iter__.return_value = [branch1, branch2, branch3] active_branch = mocker.MagicMock() mocked_git().active_branch = active_branch mocked_remote = mocker.MagicMock() mocked_remote.name = "origin" mocked_git().remotes.__iter__.return_value = [mocked_remote] state = json.load(open(temp_configfile)) state["FORK_NAME"] = "peterbe" with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(cleanup, ["other"], input="\n", obj=config) if result.exception: # print(mocked_git.mock_calls) # print(result.output) # print(result.exception) raise result.exception assert result.exit_code == 0 assert not result.exception mocked_git().git.branch.assert_called_with("-D", branch2.name)
def test_start(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(start, [""], input='foo "bar"\n', obj=config) assert result.exit_code == 0 assert not result.exception mocked_git().create_head.assert_called_with("foo-bar") mocked_git().create_head().checkout.assert_called_with() with open(temp_configfile) as f: saved = json.load(f) assert "gg-start-test:foo-bar" in saved assert saved["gg-start-test:foo-bar"]["description"] == 'foo "bar"' assert saved["gg-start-test:foo-bar"]["date"]
def test_start_bugzilla_url(temp_configfile, mocker, requestsmock): requestsmock.get( "https://bugzilla.mozilla.org/rest/bug/?ids=123456&include_fields=summary%2Cid", content=json.dumps({ "bugs": [{ "assigned_to": "*****@*****.**", "id": 1234, "status": "NEW", "summary": "This is the summary", }], "faults": [], }).encode("utf-8"), ) mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke( start, ["https://bugzilla.mozilla.org/show_bug.cgi?id=123456"], input='foo "bar"\n', obj=config, ) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception mocked_git().create_head.assert_called_with("123456-foo-bar") mocked_git().create_head().checkout.assert_called_with() with open(temp_configfile) as f: saved = json.load(f) key = "gg-start-test:123456-foo-bar" assert key in saved assert saved[key]["description"] == 'foo "bar"' assert saved[key]["date"]
def test_branches(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" mocked_git().git.branch.return_value = """ * this-branch other-branch """ branch1 = mocker.MagicMock() branch1.name = "this-branch" branch2 = mocker.MagicMock() branch2.name = "other-branch" branch3 = mocker.MagicMock() branch3.name = "not-merged-branch" mocked_git().heads.__iter__.return_value = [branch1, branch2, branch3] state = json.load(open(temp_configfile)) state["FORK_NAME"] = "peterbe" with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(branches, ["other"], input="\n", obj=config) if result.exception: # print(mocked_git.mock_calls) # print(result.output) # print(result.exception) raise result.exception # print(result.output) assert "other-branch" in result.output assert "this-branch" not in result.output assert result.exit_code == 0 assert not result.exception # .assert_called_once() is new only in 3.6 # branch2.checkout.assert_called_once() branch2.checkout.assert_called_with()
def test_push(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" mocked_git().remotes.__getitem__().push.return_value = [ PushInfo(0, "All is well", None, None, "origin") ] state = json.load(open(temp_configfile)) state["FORK_NAME"] = "peterbe" with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(push, [], obj=config) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception mocked_git().remotes.__getitem__().push.assert_called_with()
def test_start_weird_description(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" runner = CliRunner() config = Config() config.configfile = temp_configfile summary = " a!@#$%^&*()_+{}[/]-= ;: --> ==> --- `foo` ,. <bar> " result = runner.invoke(start, [""], input=summary + "\n", obj=config) assert result.exit_code == 0 assert not result.exception expected_branchname = "a_+-foo-bar" mocked_git().create_head.assert_called_with(expected_branchname) mocked_git().create_head().checkout.assert_called_with() with open(temp_configfile) as f: saved = json.load(f) key = "gg-start-test:" + expected_branchname assert key in saved assert saved[key]["description"] == summary.strip()
def test_start_github_issue(temp_configfile, mocker, requestsmock): requestsmock.get( "https://api.github.com/repos/peterbe/gg-start/issues/7", content=json.dumps({ "title": "prefix branch name differently for github issues", "html_url": "https://github.com/peterbe/gg-start/issues/7", }).encode("utf-8"), ) mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke( start, ["https://github.com/peterbe/gg-start/issues/7"], input='foo "bar"\n', obj=config, ) if result.exception: raise result.exception assert result.exit_code == 0 assert not result.exception mocked_git().create_head.assert_called_with("7-foo-bar") mocked_git().create_head().checkout.assert_called_with() with open(temp_configfile) as f: saved = json.load(f) key = "gg-start-test:7-foo-bar" assert key in saved assert saved[key]["description"] == 'foo "bar"' assert saved[key]["date"]
def test_commit_no_files_to_add(temp_configfile, mocker): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-commit-test" mocked_git().active_branch.name = "my-topic-branch" mocked_git().index.entries.keys.return_value = [] # first we have to fake some previous information # print(repr(temp_configfile)) state = json.load(open(temp_configfile)) state["gg-commit-test:my-topic-branch"] = { "description": "Some description", "bugnumber": None, } with open(temp_configfile, "w") as f: json.dump(state, f) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(commit, [], input="\n", obj=config) assert result.exit_code == 0 assert not result.exception assert "No files to add" in result.output
def test_get_title(temp_configfile, mocker): rget = mocker.patch("requests.get") def mocked_get(url, headers): assert url == "https://api.github.com/repos/peterbe/gg/issues/1" # assert 'token' not in params return Response( { "html_url": "https://github.com/peterbe/gg/issues/1", "id": 85565047, "number": 1, "title": "This is the title", } ) rget.side_effect = mocked_get config = Config() config.configfile = temp_configfile # config.bugzilla_url = 'https://bugs.example.com' title, url = github.get_title(config, "peterbe", "gg", 1) assert title == "This is the title" assert url == "https://github.com/peterbe/gg/issues/1"
def test_start_a_digit(temp_configfile, mocker, requestsmock): mocked_git = mocker.patch("git.Repo") mocked_git().working_dir = "gg-start-test" remotes = [] class Remote: def __init__(self, name, url): self.name = name self.url = url remotes.append(Remote("origin", "[email protected]:myorg/myrepo.git")) remotes.append(Remote("other", "https://github.com/o/ther.git")) mocked_git().remotes.__iter__.return_value = remotes # rget = mocker.patch("requests.get") requestsmock.get( "https://bugzilla.mozilla.org/rest/bug/", content=json.dumps({ "bugs": [{ "assigned_to": "*****@*****.**", "assigned_to_detail": { "email": "*****@*****.**", "id": 1, "name": "*****@*****.**", "real_name": "Nobody; OK to take it and work on it", }, "id": 1234, "status": "NEW", "summary": "This is the summary", }], "faults": [], }).encode("utf-8"), ) requestsmock.get( "https://api.github.com/repos/myorg/myrepo/issues/1234", content=json.dumps({ "id": 1234, "title": "Issue Title Here", "html_url": ("https://api.github.com/repos/myorg/myrepo/issues/123"), }).encode("utf-8"), ) requestsmock.get( "https://api.github.com/repos/o/ther/issues/1234", status_code=404, content=json.dumps({ "not": "found" }).encode("utf-8"), ) runner = CliRunner() config = Config() config.configfile = temp_configfile result = runner.invoke(start, ["1234"], obj=config) assert "Input is ambiguous" in result.output assert "Issue Title Here" in result.output assert "This is the summary" in result.output assert result.exit_code == 1