def test_extract_dist_npm(npm_dist, runner, mocker, open_mock, tmp_path): os.makedirs("staging") shutil.move(f"{util.CHECKOUT_NAME}/dist", "staging") def helper(path, **kwargs): return MockRequestResponse(f"staging/dist/{path}") get_mock = mocker.patch("requests.get", side_effect=helper) dist_names = [osp.basename(f) for f in glob("staging/dist/*.tgz")] url = normalize_path(osp.join(os.getcwd(), util.CHECKOUT_NAME)) tag_name = f"v{VERSION_SPEC}" releases = [ dict( tag_name=tag_name, target_commitish="main", assets=[dict(name=dist_name, url=dist_name) for dist_name in dist_names], ) ] sha = run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME) tags = [dict(ref=f"refs/tags/{tag_name}", object=dict(sha=sha))] open_mock.side_effect = [ MockHTTPResponse(releases), MockHTTPResponse(tags), MockHTTPResponse(dict(html_url=url)), ] runner(["extract-release", HTML_URL]) assert len(open_mock.mock_calls) == 3 assert len(get_mock.mock_calls) == len(dist_names) == 3
def test_forwardport_changelog_no_new(npm_package, runner, mocker, open_mock, git_prep): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] # Create a branch with a changelog entry util.run("git checkout -b backport_branch", cwd=util.CHECKOUT_NAME) util.run("git push origin backport_branch", cwd=util.CHECKOUT_NAME) mock_changelog_entry(npm_package, runner, mocker) util.run('git commit -a -m "Add changelog entry"', cwd=util.CHECKOUT_NAME) util.run(f"git tag v{VERSION_SPEC}", cwd=util.CHECKOUT_NAME) # Run the forwardport workflow against default branch os.chdir(util.CHECKOUT_NAME) url = os.getcwd() runner(["forwardport-changelog", HTML_URL, "--git-url", url]) assert len(open_mock.mock_calls) == 1 expected = """ <!-- <START NEW CHANGELOG ENTRY> --> ## 1.0.1 """ assert expected in Path("CHANGELOG.md").read_text(encoding="utf-8")
def test_delete_release(npm_dist, runner, mocker, open_mock, git_prep): # Publish the release # Mimic being on GitHub actions so we get the magic output os.environ["GITHUB_ACTIONS"] = "true" open_mock.side_effect = [ MockHTTPResponse([REPO_DATA]), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), ] result = runner(["draft-release", "--dry-run"]) assert len(open_mock.call_args) == 2 url = "" for line in result.output.splitlines(): match = re.match(r"::set-output name=release_url::(.*)", line) if match: url = match.groups()[0] # Delete the release data = dict(assets=[dict(id="bar")]) open_mock.side_effect = [ MockHTTPResponse([data]), MockHTTPResponse(), MockHTTPResponse(), ] runner(["delete-release", url]) assert len(open_mock.call_args) == 2
def test_forwardport_changelog_has_new(npm_package, runner, mocker, open_mock, git_prep): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] current = util.run("git branch --show-current") # Create a branch with a changelog entry util.run("git checkout -b backport_branch", cwd=util.CHECKOUT_NAME) util.run("git push origin backport_branch", cwd=util.CHECKOUT_NAME) util.run(f"git checkout {current}") mock_changelog_entry(npm_package, runner, mocker) util.run(f'git commit -a -m "Add changelog entry {VERSION_SPEC}"', cwd=util.CHECKOUT_NAME) util.run(f"git tag v{VERSION_SPEC}", cwd=util.CHECKOUT_NAME) util.run(f"git checkout {current}", cwd=util.CHECKOUT_NAME) util.run("git push origin backport_branch --tags", cwd=util.CHECKOUT_NAME) # Add a new changelog entry in main branch util.run("git checkout backport_branch", cwd=str(npm_package)) util.run(f"git checkout {current}", cwd=util.CHECKOUT_NAME) mock_changelog_entry(npm_package, runner, mocker, version_spec="2.0.0") util.run('git commit -a -m "Add changelog entry v2.0.0"', cwd=util.CHECKOUT_NAME) util.run("git tag v2.0.0", cwd=util.CHECKOUT_NAME) util.run("git checkout backport_branch", cwd=npm_package) util.run(f"git push origin {current} --tags", cwd=util.CHECKOUT_NAME) # Run the forwardport workflow against default branch url = osp.abspath(npm_package) os.chdir(npm_package) runner([ "forwardport-changelog", HTML_URL, "--git-url", url, "--branch", current ]) assert len(open_mock.call_args) == 2 util.run(f"git checkout {current}", cwd=npm_package) expected = """ <!-- <START NEW CHANGELOG ENTRY> --> ## 2.0.0 """ text = Path("CHANGELOG.md").read_text(encoding="utf-8") assert expected in text, text expect = """ <!-- <END NEW CHANGELOG ENTRY> --> ## 1.0.1 """ assert expected in text, text
def test_publish_release_npm(npm_dist, runner, mocker, open_mock): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] dist_dir = npm_dist / util.CHECKOUT_NAME / "dist" runner([ "publish-release", HTML_URL, "--npm_token", "abc", "--npm_cmd", "npm publish --dry-run", "--dist-dir", dist_dir, ]) assert len(open_mock.call_args) == 2
def test_build_changelog_backport(py_package, mocker, runner, open_mock): changelog_file = "CHANGELOG.md" changelog_path = Path(util.CHECKOUT_NAME) / changelog_file data = dict(title="foo", html_url="bar", user=dict(login="******", html_url="baz")) open_mock.return_value = MockHTTPResponse(data) runner(["prep-git", "--git-url", py_package]) runner(["bump-version", "--version-spec", VERSION_SPEC]) entry = CHANGELOG_ENTRY.replace("consideRatio", "meeseeksmachine") entry = entry.replace( "Support git references etc.", "Backport PR #50 (original title" ) mocked_gen = mocker.patch("jupyter_releaser.changelog.generate_activity_md") mocked_gen.return_value = entry runner(["build-changelog", "--changelog-path", changelog_file]) text = changelog_path.read_text(encoding="utf-8") assert changelog.START_MARKER in text assert changelog.END_MARKER in text assert "- foo [#50](bar) ([@snuffy](baz))" in text, text assert len(re.findall(changelog.START_MARKER, text)) == 1 assert len(re.findall(changelog.END_MARKER, text)) == 1 run("pre-commit run -a")
def test_publish_release_py(py_dist, runner, mocker, open_mock): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] orig_run = util.run called = 0 def wrapped(cmd, **kwargs): nonlocal called if cmd.startswith("twine upload"): called += 1 return "" return orig_run(cmd, **kwargs) mock_run = mocker.patch("jupyter_releaser.util.run", wraps=wrapped) dist_dir = py_dist / util.CHECKOUT_NAME / "dist" runner(["publish-release", HTML_URL, "--dist-dir", dist_dir]) assert len(open_mock.call_args) == 2 assert called == 2, called
def test_extract_dist_py(py_package, runner, mocker, open_mock, tmp_path, git_prep): changelog_entry = mock_changelog_entry(py_package, runner, mocker) # Create the dist files run("python -m build .", cwd=util.CHECKOUT_NAME) # Finalize the release runner(["tag-release"]) os.makedirs("staging") shutil.move(f"{util.CHECKOUT_NAME}/dist", "staging") def helper(path, **kwargs): return MockRequestResponse(f"staging/dist/{path}") get_mock = mocker.patch("requests.get", side_effect=helper) tag_name = f"v{VERSION_SPEC}" dist_names = [osp.basename(f) for f in glob("staging/dist/*.*")] releases = [ dict( tag_name=tag_name, target_commitish=util.get_branch(), assets=[ dict(name=dist_name, url=dist_name) for dist_name in dist_names ], ) ] sha = run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME) tags = [dict(ref=f"refs/tags/{tag_name}", object=dict(sha=sha))] url = normalize_path(osp.join(os.getcwd(), util.CHECKOUT_NAME)) open_mock.side_effect = [ MockHTTPResponse(releases), MockHTTPResponse(tags), MockHTTPResponse(dict(html_url=url)), ] runner(["extract-release", HTML_URL]) assert len(open_mock.mock_calls) == 3 assert len(get_mock.mock_calls) == len(dist_names) == 2
def test_publish_release_py(py_package, runner, mocker, open_mock, git_prep): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] # Create the dist files changelog_entry = mock_changelog_entry(py_package, runner, mocker) run("python -m build .", cwd=util.CHECKOUT_NAME) orig_run = util.run called = 0 def wrapped(cmd, **kwargs): nonlocal called if cmd.startswith("twine upload"): called += 1 return orig_run(cmd, **kwargs) mock_run = mocker.patch("jupyter_releaser.util.run", wraps=wrapped) dist_dir = py_package / util.CHECKOUT_NAME / "dist" runner(["publish-release", HTML_URL, "--dist-dir", dist_dir, "--dry-run"]) assert len(open_mock.call_args) == 2 assert called == 2, called
def test_draft_release_final(npm_dist, runner, mocker, open_mock, git_prep): open_mock.side_effect = [ MockHTTPResponse([REPO_DATA]), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), MockHTTPResponse(), ] # Publish the release os.environ["GITHUB_ACTIONS"] = "true" runner(["draft-release"]) assert len(open_mock.call_args) == 2
def test_publish_release(npm_dist, runner, mocker, open_mock): open_mock.side_effect = [MockHTTPResponse([REPO_DATA]), MockHTTPResponse()] dist_dir = npm_dist / util.CHECKOUT_NAME / "dist" runner(["publish-release", HTML_URL]) assert len(open_mock.call_args) == 2