Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkowl committed May 6, 2019
1 parent ecd438c commit 41781d9
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 84 deletions.
39 changes: 24 additions & 15 deletions src/towncrier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,11 @@ def __main(draft, directory, project_name, project_version, project_date, answer

click.echo("Rendering news fragments...", err=to_err)
fragments = split_fragments(fragments, definitions)
rendered = render_fragments(
# The 0th underline is used for the top line
template,
config["issue_format"],
fragments,
definitions,
config["underlines"][1:],
config["wrap"],
)

if project_version is None:
project_version = get_version(
os.path.join(directory, config["package_dir"]), config["package"]
)
).strip()

if project_name is None:
package = config.get("package")
Expand All @@ -117,20 +108,38 @@ def __main(draft, directory, project_name, project_version, project_date, answer
project_name = ""

if project_date is None:
project_date = _get_date()
project_date = _get_date().strip()

if config["title_format"]:
top_line = config["title_format"].format(
name=project_name, version=project_version, project_date=project_date
)
top_line += u"\n" + (config["underlines"][0] * len(top_line)) + u"\n"
else:
top_line = ""

top_line = config["title_format"].format(
name=project_name, version=project_version, project_date=project_date
rendered = render_fragments(
# The 0th underline is used for the top line
template,
config["issue_format"],
fragments,
definitions,
config["underlines"][1:],
config["wrap"],
{"name": project_name, "version": project_version, "date": project_date},
top_underline=config["underlines"][0],
)
top_line += u"\n" + (config["underlines"][0] * len(top_line)) + u"\n"

if draft:
click.echo(
"Draft only -- nothing has been written.\n"
"What is seen below is what would be written.\n",
err=to_err,
)
click.echo("%s\n%s" % (top_line, rendered))
if top_line:
click.echo("\n%s\n%s" % (top_line, rendered))
else:
click.echo(rendered)
else:
click.echo("Writing to newsfile...", err=to_err)
start_line = config["start_line"]
Expand Down
18 changes: 16 additions & 2 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def indent(text, prefix):
def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if line.strip() else line)

return u"".join(prefixed_lines())


Expand Down Expand Up @@ -153,7 +154,16 @@ def render_issue(issue_format, issue):
return issue_format.format(issue=issue)


def render_fragments(template, issue_format, fragments, definitions, underlines, wrap):
def render_fragments(
template,
issue_format,
fragments,
definitions,
underlines,
wrap,
versiondata,
top_underline="=",
):
"""
Render the fragments into a news file.
"""
Expand Down Expand Up @@ -198,7 +208,11 @@ def render_fragments(template, issue_format, fragments, definitions, underlines,
done = []

res = jinja_template.render(
sections=data, definitions=definitions, underlines=underlines
sections=data,
definitions=definitions,
underlines=underlines,
versiondata=versiondata,
top_underline=top_underline,
)

for line in res.split(u"\n"):
Expand Down
6 changes: 3 additions & 3 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def get_version(package_dir, package):
raise Exception("No __version__, I don't know how else to look")

if isinstance(version, str):
return version
return version.strip()

if isinstance(version, Version):
return version.base()
return version.base().strip()

if isinstance(version, tuple):
return ".".join(map(str, version))
return ".".join(map(str, version)).strip()

raise Exception(
(
Expand Down
2 changes: 1 addition & 1 deletion src/towncrier/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


_start_string = u".. towncrier release notes start\n"
_title_format = u"{name} {version} ({project_date})"
_title_format = None
_template_fname = None
_default_types = OrderedDict(
[
Expand Down
3 changes: 2 additions & 1 deletion src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def append_to_newsfile(directory, filename, start_line, top_line, content):

if len(existing_content) > 1:
f.write(existing_content.pop(0).rstrip().encode("utf8"))
f.write((u"\n\n" + start_line + u"\n").encode("utf8"))
if start_line:
f.write((u"\n\n" + start_line + u"\n").encode("utf8"))

f.write(top_line.encode("utf8"))
f.write(content.encode("utf8"))
Expand Down
10 changes: 4 additions & 6 deletions src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

import click

from subprocess import (
CalledProcessError,
check_output,
STDOUT,
)
from subprocess import CalledProcessError, check_output, STDOUT

from ._settings import load_config, load_config_from_file
from ._builder import find_fragments
Expand Down Expand Up @@ -40,7 +36,9 @@ def __main(comparewith, directory, pyproject):

try:
files_changed = (
_run(["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory)
_run(
["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory
)
.decode(getattr(sys.stdout, "encoding", "utf8"))
.strip()
)
Expand Down
2 changes: 2 additions & 0 deletions src/towncrier/templates/template.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }})
{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}}
{% for section, _ in sections.items() %}
{% set underline = underlines[0] %}{% if section %}{{section}}
{{ underline * section|length }}{% set underline = underlines[1] %}
Expand Down
22 changes: 5 additions & 17 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,20 @@ def test_git_fails(self):
create_project("pyproject.toml")

result = runner.invoke(_main, ["--compare-with", "hblaugh"])
self.assertIn(
"git produced output while failing",
result.output,
)
self.assertIn(
"hblaugh",
result.output,
)
self.assertIn("git produced output while failing", result.output)
self.assertIn("hblaugh", result.output)

def test_no_changes_made(self):
self._test_no_changes_made(
"pyproject.toml",
lambda runner, main, argv: runner.invoke(main, argv),
"pyproject.toml", lambda runner, main, argv: runner.invoke(main, argv)
)

def test_no_changes_made_pyproject_path(self):
pyproject = "not-pyproject.toml"
self._test_no_changes_made(
pyproject,
lambda runner, main, argv: runner.invoke(
main,
argv + ["--pyproject", pyproject],
main, argv + ["--pyproject", pyproject]
),
)

Expand Down Expand Up @@ -108,11 +100,7 @@ def test_fragment_exists(self):
),
result,
)
self.assertEqual(
0,
result.exit_code,
result,
)
self.assertEqual(0, result.exit_code, result)

def test_fragment_missing(self):
runner = CliRunner()
Expand Down
15 changes: 5 additions & 10 deletions src/towncrier/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def test_happy_path(self):
u"Loading template...\nFinding news fragments...\nRendering news "
u"fragments...\nDraft only -- nothing has been written.\nWhat is "
u"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
u"\n======================\n"
u"\n\nFeatures\n--------\n\n- Adds levitation (#123)\n"
u"\n======================\n\n"
u"Features\n--------\n\n- Adds levitation (#123)\n"
u"- Extends levitation (#124)\n\n",
)

Expand Down Expand Up @@ -140,7 +140,7 @@ def run_order_scenario(sections, types):
u"Loading template...\nFinding news fragments...\nRendering news "
u"fragments...\nDraft only -- nothing has been written.\nWhat is "
u"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
u"\n======================\n"
u"\n======================"
+ dedent(
"""
section-a
Expand Down Expand Up @@ -183,7 +183,7 @@ def run_order_scenario(sections, types):
u"Loading template...\nFinding news fragments...\nRendering news "
u"fragments...\nDraft only -- nothing has been written.\nWhat is "
u"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
u"\n======================\n"
u"\n======================"
+ dedent(
"""
section-b
Expand Down Expand Up @@ -291,7 +291,6 @@ def test_projectless_changelog(self):
FooBarBaz 7.8.9 (01-01-2001)
============================
Features
--------
Expand All @@ -314,10 +313,6 @@ def test_no_package_changelog(self):
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write(
"[tool.towncrier]\n" 'title_format = "{version} ({project_date})"\n'
)
os.mkdir("newsfragments")
with open("newsfragments/123.feature", "w") as f:
f.write("Adds levitation")
Expand All @@ -326,6 +321,7 @@ def test_no_package_changelog(self):
_main, ["--version", "7.8.9", "--date", "01-01-2001", "--draft"]
)

print(result.stderr)
self.assertEqual(0, result.exit_code)
self.assertEqual(
result.output,
Expand All @@ -340,7 +336,6 @@ def test_no_package_changelog(self):
7.8.9 (01-01-2001)
==================
Features
--------
Expand Down
Loading

0 comments on commit 41781d9

Please sign in to comment.