Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users can now write the content in the "create" method. #275

Merged
merged 12 commits into from
Dec 2, 2020
28 changes: 24 additions & 4 deletions src/towncrier/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
@click.command(name="create")
@click.option("--dir", "directory", default=None)
@click.option("--config", "config", default=None)
@click.option("-i", "--interactive", is_flag=True, default=False)
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
@click.argument("filename")
def _main(directory, config, filename):
return __main(directory, config, filename)
def _main(directory, config, filename, interactive):
return __main(directory, config, filename, interactive)


def __main(directory, config, filename):
def __main(directory, config, filename, interactive):
"""
The main entry point.
"""
Expand Down Expand Up @@ -59,11 +60,30 @@ def __main(directory, config, filename):
if os.path.exists(segment_file):
raise click.ClickException("{} already exists".format(segment_file))

content = _get_news_content(interactive)

with open(segment_file, "w") as f:
f.writelines(["Add your info here"])
f.write(content)

click.echo("Created news fragment at {}".format(segment_file))


def _get_news_content(interactive):
if not interactive:
return "Add your info here"
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
content = []
click.echo(
"Please write your news content. Press enter without content to finish"
)
count = 1
while True:
content_line = click.prompt("{}".format(count), default="", show_default=False)
if content_line == "":
break
content.append(content_line)
count += 1
return "\n".join(content)


if __name__ == "__main__": # pragma: no cover
_main()
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/275.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Users can now write the content in the "create" method.
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 14 additions & 3 deletions src/towncrier/test/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ def setup_simple_project(config=None, mkdir=True):
class TestCli(TestCase):
maxDiff = None

def _test_success(self, config=None, mkdir=True):
def _test_success(self, config=None, mkdir=True, interactive=False):
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project(config, mkdir)

result = runner.invoke(_main, ["123.feature.rst"])
input_content = None
args = ["123.feature.rst"]
content = ["Add your info here"]
if interactive:
args.append("-i")
input_content = "This is line 1\nThis is line 2\n"
content = ['This is line 1\n', 'This is line 2']
result = runner.invoke(_main, args, input=input_content)

self.assertEqual(["123.feature.rst"], os.listdir("foo/newsfragments"))

with open("foo/newsfragments/123.feature.rst") as fh:
self.assertEqual("Add your info here", fh.readlines()[0])
self.assertEqual(content, fh.readlines())

self.assertEqual(0, result.exit_code)

Expand All @@ -56,6 +63,10 @@ def test_directory_created(self):
"""Ensure both file and output directory created if necessary."""
self._test_success(mkdir=False)

def test_interactive(self):
"""Create file with dynamic content."""
self._test_success(interactive=True)

def test_different_directory(self):
"""Ensure non-standard directories are used."""
runner = CliRunner()
Expand Down