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

Raise ConfigError for missing newsfragment directory #298

Merged
merged 12 commits into from
Dec 19, 2020
11 changes: 10 additions & 1 deletion src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import, division, print_function

import os
import sys
import textwrap

from collections import OrderedDict
Expand Down Expand Up @@ -86,7 +87,15 @@ def find_fragments(base_directory, sections, fragment_directory, definitions):
else:
section_dir = os.path.join(base_directory, val)

files = os.listdir(section_dir)
if sys.version_info >= (3,):
expected_exception = FileNotFoundError
else:
expected_exception = OSError

try:
files = os.listdir(section_dir)
except expected_exception:
files = []

file_content = {}

Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/85.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A missing newsfragment directory is treated as if it is empty rather than raising an exception.
altendky marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 33 additions & 2 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
from .._shell import cli


def setup_simple_project():
def setup_simple_project(create_newsfragments_directory=True):
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")
if create_newsfragments_directory:
altendky marked this conversation as resolved.
Show resolved Hide resolved
os.mkdir("foo/newsfragments")


class TestCli(TestCase):
Expand Down Expand Up @@ -82,6 +83,36 @@ def test_command(self):
def test_subcommand(self):
self._test_command(_main)

def test_no_newsfragment_directory(self):
"""
A missing newsfragment directory acts as if there are no changes.
"""
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project(create_newsfragments_directory=False)

result = runner.invoke(_main, ["--draft", "--date", "01-01-2001"])

# This should fail
self.assertEqual(result.exit_code, 0)
self.assertIn("No significant changes.\n", result.output)

def test_no_newsfragments(self):
"""
An empty newsfragment directory acts as if there are no changes.
"""
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project(create_newsfragments_directory=True)

result = runner.invoke(_main, ["--draft", "--date", "01-01-2001"])

# This should fail
self.assertEqual(result.exit_code, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what is going on here.

I guess that this is just a TODO due to some strange code in Click, and in the change we will have a non-zero exit code here.

Suggested change
# This should fail
self.assertEqual(result.exit_code, 0)
# For now, exit code is zero, but it should be non-zero.
# See: LINK TO ISSUE
self.assertEqual(result.exit_code, 0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's disregard my comment, I'll remove it. Do you want the pre-existing behavior of creating output with 'no significant changes' as the text and a zero exit code? Or, are we just confusing each other starting with my bad comment I left laying around.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the comment may have been a copy/paste error from the test after this.

altendky marked this conversation as resolved.
Show resolved Hide resolved
self.assertIn("No significant changes.\n", result.output)

def test_collision(self):
runner = CliRunner()

Expand Down