Skip to content

Commit 82a092b

Browse files
authored
Correct to start_string consistently (#301)
* Correct to start_string consistently * Add test docstring * add test for coverage and conditional to avoid exception * Add test docstring * Update test_build.py * flake8 * expound on docs * Update src/towncrier/newsfragments/277.doc.rst
1 parent 46c3157 commit 82a092b

File tree

7 files changed

+115
-11
lines changed

7 files changed

+115
-11
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Towncrier has the following global options, which can be specified in the toml f
137137
version = "1.2.3" # project version if maintained separately
138138
name = "arbitrary project name"
139139
template = "path/to/template.rst"
140-
start_line = "start of generated content"
140+
start_string = "Text used to detect where to add the generated content in the middle of a file. Generated content added after this text. Newline auto added."
141141
title_format = "{name} {version} ({project_date})" # or false if template includes title
142142
issue_format = "format string for {issue} (issue is the first part of fragment name)"
143143
underlines: "=-~"

src/towncrier/_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def parse_toml(base_path, config):
149149
"sections": sections,
150150
"types": types,
151151
"template": template,
152-
"start_line": config.get("start_string", _start_string),
152+
"start_string": config.get("start_string", _start_string),
153153
"title_format": config.get("title_format", _title_format),
154154
"issue_format": config.get("issue_format"),
155155
"underlines": config.get("underlines", _underlines),

src/towncrier/_writer.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
def append_to_newsfile(
16-
directory, filename, start_line, top_line, content, single_file=True
16+
directory, filename, start_string, top_line, content, single_file=True
1717
):
1818

1919
news_file = os.path.join(directory, filename)
@@ -24,7 +24,7 @@ def append_to_newsfile(
2424
else:
2525
with io.open(news_file, "r", encoding="utf8") as f:
2626
existing_content = f.read()
27-
existing_content = existing_content.split(start_line, 1)
27+
existing_content = existing_content.split(start_string, 1)
2828
else:
2929
existing_content = [u""]
3030

@@ -35,11 +35,12 @@ def append_to_newsfile(
3535

3636
if len(existing_content) > 1:
3737
f.write(existing_content.pop(0).rstrip().encode("utf8"))
38-
if start_line:
39-
f.write((u"\n\n" + start_line + u"\n").encode("utf8"))
38+
if start_string:
39+
f.write((u"\n\n" + start_string + u"\n").encode("utf8"))
4040

4141
f.write(top_line.encode("utf8"))
4242
f.write(content.encode("utf8"))
43-
if existing_content[0]:
44-
f.write(b"\n\n")
45-
f.write(existing_content[0].lstrip().encode("utf8"))
43+
if existing_content:
44+
if existing_content[0]:
45+
f.write(b"\n\n")
46+
f.write(existing_content[0].lstrip().encode("utf8"))

src/towncrier/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def __main(
167167
click.echo(rendered)
168168
else:
169169
click.echo("Writing to newsfile...", err=to_err)
170-
start_line = config["start_line"]
170+
start_string = config["start_string"]
171171
news_file = config["filename"]
172172

173173
if config["single_file"]:
@@ -179,7 +179,7 @@ def __main(
179179
append_to_newsfile(
180180
base_directory,
181181
news_file,
182-
start_line,
182+
start_string,
183183
top_line,
184184
rendered,
185185
single_file=config["single_file"],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``start_line`` corrected to ``start_string`` in the readme to match the long standing implementation.

src/towncrier/test/test_build.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,60 @@ def test_title_format_false(self):
779779

780780
self.assertEqual(0, result.exit_code)
781781
self.assertEqual(expected_output, result.output)
782+
783+
def test_start_string(self):
784+
"""
785+
The `start_string` configuration is used to detect the starting point
786+
for inserting the generated release notes. A newline is automatically
787+
added to the configured value.
788+
"""
789+
runner = CliRunner()
790+
791+
with runner.isolated_filesystem():
792+
with open("pyproject.toml", "w") as f:
793+
f.write(dedent("""\
794+
[tool.towncrier]
795+
start_string="Release notes start marker"
796+
"""))
797+
os.mkdir("newsfragments")
798+
with open("newsfragments/123.feature", "w") as f:
799+
f.write("Adds levitation")
800+
with open("NEWS.rst", "w") as f:
801+
f.write("a line\n\nanother\n\nRelease notes start marker\n")
802+
803+
result = runner.invoke(
804+
_main,
805+
[
806+
"--version",
807+
"7.8.9",
808+
"--name",
809+
"foo",
810+
"--date",
811+
"01-01-2001",
812+
"--yes",
813+
],
814+
)
815+
816+
self.assertEqual(0, result.exit_code, result.output)
817+
self.assertTrue(os.path.exists("NEWS.rst"), os.listdir("."))
818+
with open("NEWS.rst", "r") as f:
819+
output = f.read()
820+
821+
expected_output = dedent("""\
822+
a line
823+
824+
another
825+
826+
Release notes start marker
827+
foo 7.8.9 (01-01-2001)
828+
======================
829+
830+
Features
831+
--------
832+
833+
- Adds levitation (#123)
834+
835+
836+
""")
837+
838+
self.assertEqual(expected_output, output)

src/towncrier/test/test_write.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pkg_resources
77
import os
8+
from textwrap import dedent
89

910
from collections import OrderedDict
1011

@@ -226,3 +227,47 @@ def test_append_at_top_with_hint(self):
226227
output = f.read()
227228

228229
self.assertEqual(expected_output, output)
230+
231+
def test_multiple_file_no_start_string(self):
232+
"""
233+
When no `start_string` is defined, the generated content is added at
234+
the start of the file.
235+
"""
236+
tempdir = self.mktemp()
237+
os.mkdir(tempdir)
238+
239+
definitions = {}
240+
fragments = split_fragments(fragments={}, definitions=definitions)
241+
242+
template = pkg_resources.resource_string(
243+
"towncrier", "templates/default.rst"
244+
).decode("utf8")
245+
246+
content = render_fragments(
247+
template=template,
248+
issue_format=None,
249+
top_line="",
250+
fragments=fragments,
251+
definitions=definitions,
252+
underlines=["-", "~"],
253+
wrap=True,
254+
versiondata={"name": "MyProject", "version": "1.0", "date": "never"},
255+
)
256+
257+
append_to_newsfile(
258+
directory=tempdir,
259+
filename="NEWS.rst",
260+
start_string=None,
261+
top_line="",
262+
content=content,
263+
)
264+
265+
with open(os.path.join(tempdir, "NEWS.rst"), "r") as f:
266+
output = f.read()
267+
268+
expected_output = dedent("""\
269+
MyProject 1.0 (never)
270+
=====================
271+
""")
272+
273+
self.assertEqual(expected_output, output)

0 commit comments

Comments
 (0)