Skip to content

Commit

Permalink
Raise ConfigError for missing newsfragment directory (#298)
Browse files Browse the repository at this point in the history
* Treat a missing newsfragment directory as if it is empty

#85

* OSError for py2

* add missing sys import

* Add test docstrings

* Raise a ConfigError when the newsfragment directory is missing

* Update src/towncrier/newsfragments/85.bugfix.rst

Co-authored-by: Adi Roiban <adiroiban@gmail.com>

* reduce unneeded test diff

* remove probably copy/paste error comment

* Update src/towncrier/test/test_build.py

Co-authored-by: Adi Roiban <adiroiban@gmail.com>

Co-authored-by: Adi Roiban <adiroiban@gmail.com>
  • Loading branch information
altendky and adiroiban authored Dec 19, 2020
1 parent 6023801 commit 96178dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
from __future__ import absolute_import, division, print_function

import os
import sys
import textwrap
import traceback

from collections import OrderedDict

from jinja2 import Template

from ._settings import ConfigError


def strip_if_integer_string(s):
try:
Expand Down Expand Up @@ -86,7 +90,18 @@ 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 as e:
message = "Failed to list the news fragment files.\n{}".format(
''.join(traceback.format_exception_only(type(e), e)),
)
raise ConfigError(message)

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 configuration error is triggered when the newsfragment files couldn't be discovered.
29 changes: 29 additions & 0 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ 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()
os.rmdir("foo/newsfragments")

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

self.assertEqual(1, result.exit_code, result.output)
self.assertIn("Failed to list the news fragment files.\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()

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

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

def test_collision(self):
runner = CliRunner()

Expand Down

0 comments on commit 96178dc

Please sign in to comment.