def create_python_package(git_repo): setuppy = git_repo / "setup.py" setuppy.write_text(SETUP_PY_TEMPLATE, encoding="utf-8") setuppy = git_repo / "setup.cfg" setuppy.write_text(SETUP_CFG_TEMPLATE, encoding="utf-8") tbump = git_repo / "tbump.toml" tbump.write_text(TBUMP_BASE_TEMPLATE + TBUMP_PY_TEMPLATE, encoding="utf-8") pyproject = git_repo / "pyproject.toml" pyproject.write_text(PYPROJECT_TEMPLATE, encoding="utf-8") readme = git_repo / "README.md" readme.write_text("Hello from foo project\n", encoding="utf-8") foopy = git_repo / "foo.py" foopy.write_text(PY_MODULE_TEMPLATE, encoding="utf-8") changelog = git_repo / "CHANGELOG.md" changelog.write_text(CHANGELOG_TEMPLATE, encoding="utf-8") manifest = git_repo / "MANIFEST.in" manifest.write_text(MANIFEST_TEMPLATE, encoding="utf-8") here = Path(__file__).parent text = here.parent.joinpath(".pre-commit-config.yaml").read_text( encoding="utf-8") pre_commit = git_repo / ".pre-commit-config.yaml" pre_commit.write_text(text, encoding="utf-8") run("git add .") run('git commit -m "initial python package"') return git_repo
def test_prep_changelog_existing(py_package): runner = CliRunner() changelog = py_package / "CHANGELOG.md" result = runner.invoke(cli.main, ["prep-env", "--version-spec", "1.0.1"]) assert result.exit_code == 0, result.output with patch("release_helper.cli.generate_activity_md") as mocked_gen: mocked_gen.return_value = CHANGELOG_ENTRY result = runner.invoke( cli.main, ["prep-changelog", "--changelog-path", changelog]) assert result.exit_code == 0, result.output text = changelog.read_text(encoding="utf-8") text = text.replace("defining contributions", "Definining contributions") changelog.write_text(text, encoding="utf-8") # Commit the change run('git commit -a -m "commit changelog"') with patch("release_helper.cli.generate_activity_md") as mocked_gen: mocked_gen.return_value = CHANGELOG_ENTRY result = runner.invoke( cli.main, ["prep-changelog", "--changelog-path", changelog]) assert result.exit_code == 0, result.output text = changelog.read_text(encoding="utf-8") assert "Definining contributions" in text, text assert not "defining contributions" in text, text assert len(re.findall(cli.START_MARKER, text)) == 1 assert len(re.findall(cli.END_MARKER, text)) == 1 run("pre-commit run -a")
def test_create_release_commit(py_package): bump_version("0.0.2a0") version = cli.get_version() run("python -m build .") shas = cli.create_release_commit(version) assert normalize_path("dist/foo-0.0.2a0.tar.gz") in shas assert normalize_path("dist/foo-0.0.2a0-py3-none-any.whl") in shas shutil.rmtree(py_package / "dist") # Add an npm package and test with that create_npm_package(py_package) with open(py_package / "package.json") as fid: data = json.load(fid) data["version"] = version with open(py_package / "package.json", "w") as fid: json.dump(data, fid, indent=4) txt = (py_package / "tbump.toml").read_text(encoding="utf-8") txt += TBUMP_NPM_TEMPLATE (py_package / "tbump.toml").write_text(txt, encoding="utf-8") bump_version("0.0.2a1") version = cli.get_version() run("python -m build .") shas = cli.create_release_commit(version) assert len(shas) == 3 assert normalize_path("dist/foo-0.0.2a1.tar.gz") in shas
def test_tag_release(py_package): runner = CliRunner() version_spec = "1.5.1" # Prep the env result = runner.invoke(cli.main, ["prep-env", "--version-spec", version_spec]) assert result.exit_code == 0, result.output # Create the dist files run("python -m build .") # Tag the release result = runner.invoke(cli.main, ["tag-release"]) assert result.exit_code == 0, result.output
def create_npm_package(git_repo): npm = normalize_path(shutil.which("npm")) run(f"{npm} init -y") git_repo.joinpath("index.js").write_text('console.log("hello")', encoding="utf-8") run("git add .") run('git commit -m "initial npm package"') return git_repo
def test_publish_release_final(py_package): runner = CliRunner() version_spec = "1.5.1rc0" changelog = py_package / "CHANGELOG.md" # Prep the env result = runner.invoke(cli.main, ["prep-env", "--version-spec", version_spec]) assert result.exit_code == 0, result.output # Prep the changelog with patch("release_helper.cli.generate_activity_md") as mocked_gen: mocked_gen.return_value = CHANGELOG_ENTRY result = runner.invoke( cli.main, ["prep-changelog", "--changelog-path", changelog]) assert result.exit_code == 0, result.output # Create the dist files run("python -m build .") # Finalize the release result = runner.invoke(cli.main, ["prep-release"]) # Publish the release repo = Repository(None, dict(), dict(), True) release = GitRelease(None, dict(), dict(), True) repo.create_git_release = release_mock = MagicMock(return_value=release) release.delete_release = delete_mock = MagicMock() with patch.object(cli.Github, "get_repo", return_value=repo) as mock_method: result = runner.invoke( cli.main, ["publish-release", "--post-version-spec", "1.5.2.dev0"]) assert result.exit_code == 0, result.output release_mock.assert_called_once() delete_mock.assert_not_called()
def test_prep_changelog(py_package): runner = CliRunner() run("pre-commit run -a") changelog = py_package / "CHANGELOG.md" result = runner.invoke(cli.main, ["prep-env", "--version-spec", "1.0.1"]) assert result.exit_code == 0, result.output with patch("release_helper.cli.generate_activity_md") as mocked_gen: mocked_gen.return_value = CHANGELOG_ENTRY result = runner.invoke( cli.main, ["prep-changelog", "--changelog-path", changelog]) assert result.exit_code == 0, result.output text = changelog.read_text(encoding="utf-8") assert cli.START_MARKER in text assert cli.END_MARKER in text assert PR_ENTRY in text assert len(re.findall(cli.START_MARKER, text)) == 1 assert len(re.findall(cli.END_MARKER, text)) == 1 run("pre-commit run -a")
def test_get_version_npm(npm_package): assert cli.get_version() == "1.0.0" npm = normalize_path(shutil.which("npm")) run(f"{npm} version patch") assert cli.get_version() == "1.0.1"
def npm_package(git_repo): pkg = create_npm_package(git_repo) run("git checkout -b bar foo") return pkg
def py_package(git_repo): pkg = create_python_package(git_repo) run("git checkout -b bar foo") return pkg
def git_repo(tmp_path): prev_dir = os.getcwd() os.chdir(tmp_path) run("git init") run("git config user.name snuffy") run("git config user.email [email protected]") run("git checkout -b foo") gitignore = tmp_path / ".gitignore" gitignore.write_text("dist/*\nbuild/*\n", encoding="utf-8") run("git add .") run('git commit -m "foo"') run("git tag v0.0.1") run(f"git remote add upstream {normalize_path(tmp_path)}") yield tmp_path os.chdir(prev_dir)