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

Fix create for a bare orphan in a section not adding random hex #468

Merged
merged 8 commits into from
Feb 9, 2023
11 changes: 9 additions & 2 deletions src/towncrier/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ def __main(
"""
base_directory, config = load_config_from_options(directory, config_path)

if config.orphan_prefix and filename.startswith(f"{config.orphan_prefix}."):
file_dir, file_basename = os.path.split(filename)
if config.orphan_prefix and file_basename.startswith(f"{config.orphan_prefix}."):
# Append a random hex string to the orphan news fragment base name.
filename = f"{config.orphan_prefix}{os.urandom(4).hex()}{filename[1:]}"
filename = os.path.join(
file_dir,
(
f"{config.orphan_prefix}{os.urandom(4).hex()}"
f"{file_basename[len(config.orphan_prefix):]}"
),
)
if len(filename.split(".")) < 2 or (
filename.split(".")[-1] not in config.types
and filename.split(".")[-2] not in config.types
Expand Down
3 changes: 3 additions & 0 deletions src/towncrier/newsfragments/468.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix creating fragment in a section not adding random characters.

For example, `towncrier create some_section/+.feature` should end up as a fragment named something like `news/some_section/+a4e22da1.feature`.
34 changes: 28 additions & 6 deletions src/towncrier/test/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,39 @@ def test_create_orphan_fragment(self):
"""
runner = CliRunner()

fragments = []
with runner.isolated_filesystem():
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved
setup_simple_project()

self.assertEqual([], os.listdir("foo/newsfragments"))
os.mkdir("foo/newsfragments/subsection")

runner.invoke(_main, ["+.feature"])
fragments = os.listdir("foo/newsfragments")
result = runner.invoke(_main, ["+.feature"])
self.assertEqual(0, result.exit_code)
result = runner.invoke(
_main, ["subsection/+.feature"], catch_exceptions=False
)
self.assertEqual(0, result.exit_code, result.output)

for _, dirs, files in os.walk("foo/newsfragments", topdown=False):
for file in files:
fragments.append("/".join(dirs + [file]))
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved
print(dirs)
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved

self.assertEqual(2, len(fragments))

self.assertEqual(1, len(fragments))
filename = fragments[0]
self.assertTrue(filename.endswith(".feature"))
filename, ext = os.path.splitext(fragments[0])
self.assertEqual(ext, ".feature")
self.assertTrue(filename.startswith("+"))
# Length should be '+' character and 8 random hex characters.
self.assertEqual(len(filename.split(".")[0]), 9)
self.assertEqual(len(filename), 9)

subsection_dir = os.path.dirname(fragments[1])
subsection_filename, subsection_ext = os.path.splitext(
os.path.basename(fragments[1])
)
self.assertEqual(subsection_ext, ".feature")
self.assertTrue(subsection_filename.startswith("+"))
self.assertEqual(subsection_dir, "subsection")
# Length should be '+' character and 8 random hex characters.
self.assertEqual(len(subsection_filename), 9)