Skip to content

Commit

Permalink
feat: various improvements
Browse files Browse the repository at this point in the history
* Added sorting to test parameterisation, so that pytest-xdist works again - dramatic speedup for testing
* Reworked the CI verification code so it's a bit prettier
* Added more testing for the version CLI command, and split some logic out of the command itself
* Removed a redundant double-regex match in VersionTranslator and Version, for some speedup
  • Loading branch information
bernardcooke53 committed Apr 17, 2023
1 parent ce898fd commit e5df042
Show file tree
Hide file tree
Showing 41 changed files with 2,321 additions and 1,225 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
python -m pip install pytest-github-actions-annotate-failures
- name: pytest
run: python -m pytest -v tests
run: PYTHONHASHSEED=123345 python -m pytest -v tests

mypy:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -77,7 +77,6 @@ jobs:

- name: Install isort
run: python -m pip install "isort >=5,<6"

# https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html
# removed deprecated "-rc" and "-y" arguments
- name: Sort imports
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
python -m pip install pytest-github-actions-annotate-failures
- name: pytest
run: python -m pytest -v tests
run: PYTHONHASHSEED=123345 python -m pytest -v tests

mypy:
runs-on: ubuntu-latest
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies = [
"gitpython>=3.0.8,<4",
"twine>=4,<5",
"requests>=2.25,<3",
"wheel",
"jinja2>=3.1.2,<4",
"python-gitlab>=2,<4",
"tomlkit~=0.10",
Expand Down Expand Up @@ -59,6 +58,7 @@ mypy = ["mypy", "types-requests"]

[tool.pytest.ini_options]
addopts = [
"-nauto",
"-ra",
"--cache-clear",
"--cov=semantic_release",
Expand All @@ -74,7 +74,7 @@ omit = ["*/tests/*"]

[tool.isort]
profile = "black"
skip = [".tox", "venv"]
skip = [".tox", "venv", ".venv"]
combine_as_imports = true
group_by_package = true
default_section = "THIRDPARTY"
Expand All @@ -99,7 +99,7 @@ legacy_tox_ini = """
[tox]
envlist =
mypy,
py{37,38,39,310},
py{37,38,39,310,311},
coverage
skipsdist = True
Expand All @@ -108,6 +108,7 @@ passenv = CI
setenv =
PYTHONPATH = {toxinidir}
TESTING = True
PYTHONHASHSEED = 123456
deps = .[test]
commands =
coverage run -p --source=semantic_release -m pytest -v {posargs:tests}
Expand Down
13 changes: 6 additions & 7 deletions semantic_release/changelog/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def environment(
def recursive_render(
template_dir: str, environment: Environment, _root_dir: str = "."
) -> list[str]:
rendered_paths: list[str] = []
for root, file in (
(root, file)
for root, _, files in os.walk(template_dir)
for file in files
if not any(elem.startswith(".") for elem in root.split(os.sep))
and not file.startswith(".")
):
rendered_paths = []
src_path = Path(root)
output_path = (_root_dir / src_path.relative_to(template_dir)).resolve()
log.info("Rendering templates from %s to %s", src_path, output_path)
Expand All @@ -90,14 +90,13 @@ def recursive_render(
output_filename = file[:-3]
# Strip off the template directory from the front of the root path -
# that's the output location relative to the repo root
output_file_path = str((src_path / file).relative_to(template_dir))
src_file_path = str((src_path / file).relative_to(template_dir))
output_file_path = str((output_path / output_filename).resolve())

log.debug("rendering %s to %s", file, output_file_path)
stream = environment.get_template(output_file_path).stream()
log.debug("rendering %s to %s", src_file_path, output_file_path)
stream = environment.get_template(src_file_path).stream()

with open(
str((output_path / output_filename).resolve()), "wb+"
) as output_file:
with open(output_file_path, "wb+") as output_file:
stream.dump(output_file, encoding="utf-8")

rendered_paths.append(output_file_path)
Expand Down
138 changes: 0 additions & 138 deletions semantic_release/ci_checks.py

This file was deleted.

6 changes: 4 additions & 2 deletions semantic_release/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from semantic_release.cli.commands.changelog import changelog
from semantic_release.cli.commands.changelog import changelog as changelog
from semantic_release.cli.commands.generate_config import generate_config
from semantic_release.cli.commands.main import main as main
from semantic_release.cli.commands.publish import publish
from semantic_release.cli.commands.version import version
from semantic_release.cli.commands.verify_ci import verify_ci as verify_ci
from semantic_release.cli.commands.version import version as version

main.add_command(changelog)
main.add_command(generate_config)
main.add_command(version)
main.add_command(publish)
main.add_command(verify_ci)
30 changes: 11 additions & 19 deletions semantic_release/cli/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@

import click

# NOTE: use backport with newer API than stdlib
from importlib_resources import files

from semantic_release.changelog import ReleaseHistory, recursive_render
from semantic_release.changelog.context import make_changelog_context
from semantic_release.cli.common import render_default_changelog_file, render_release_notes
from semantic_release.cli.util import noop_report

log = logging.getLogger(__name__)


@click.command(short_help="Generate a changelog")
@click.command(
short_help="Generate a changelog",
context_settings={
"help_option_names": ["-h", "--help"],
},
)
@click.option(
"--post-to-release-tag",
"release_tag",
Expand Down Expand Up @@ -50,19 +53,15 @@ def changelog(ctx: click.Context, release_tag: str | None = None) -> None:

if not os.path.exists(template_dir):
log.info("Path %r not found, using default changelog template", template_dir)
changelog_text = (
files("semantic_release")
.joinpath("data/templates/CHANGELOG.md.j2")
.read_text(encoding="utf-8")
)
tmpl = env.from_string(changelog_text).stream()
if runtime.global_cli_options.noop:
noop_report(
f"would have written your changelog to {changelog_file.relative_to(repo.working_dir)}"
)
ctx.exit(0)

changelog_text = render_default_changelog_file(env)
with open(str(changelog_file), "w+", encoding="utf-8") as f:
tmpl.dump(f)
f.write(changelog_text)
else:
if runtime.global_cli_options.noop:
noop_report(
Expand All @@ -81,14 +80,7 @@ def changelog(ctx: click.Context, release_tag: str | None = None) -> None:
except KeyError:
ctx.fail(f"tag {release_tag} not in release history")

release_template = (
files("semantic_release")
.joinpath("data/templates/release_notes.md.j2")
.read_text(encoding="utf-8")
)
release_notes = env.from_string(release_template).render(
version=v, release=release
)
release_notes = render_release_notes(template_environment=env, version=v, release=release)
version = translator.from_tag(release_tag)
try:
hvcs_client.create_or_update_release(
Expand Down
7 changes: 6 additions & 1 deletion semantic_release/cli/commands/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from semantic_release.cli.config import RawConfig


@click.command(short_help="Generate semantic-release's default configuration")
@click.command(
short_help="Generate semantic-release's default configuration",
context_settings={
"help_option_names": ["-h", "--help"],
},
)
@click.option(
"-f",
"--format",
Expand Down
Loading

0 comments on commit e5df042

Please sign in to comment.