Skip to content

Commit

Permalink
More compact remove_files and use with_isolated_runner for new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fizyk committed Nov 28, 2022
1 parent 6dc3e63 commit bf049f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
27 changes: 14 additions & 13 deletions src/towncrier/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ def remove_files(
if not fragment_filenames:
return

if answer_yes:
click.echo("Removing the following files:")
elif answer_keep:
click.echo("Keeping the following files:")
else:
click.echo("I want to remove the following files:")

for filename in fragment_filenames:
click.echo(filename)

if answer_keep:
# Not proceeding with the removal of the files.
return
try:
if answer_keep:
click.echo("Keeping the following files:")
# Not proceeding with the removal of the files.
return

if answer_yes:
click.echo("Removing the following files:")
else:
click.echo("I want to remove the following files:")
finally:
# Will always be printed, even for answer_keep to help with possible troubleshooting
for filename in fragment_filenames:
click.echo(filename)

if answer_yes or click.confirm("Is it okay if I remove those files?", default=True):
call(["git", "rm", "--quiet"] + fragment_filenames)
Expand Down
86 changes: 41 additions & 45 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,39 +407,38 @@ def test_no_confirmation(self):
self.assertFalse(os.path.isfile(fragment_path1))
self.assertFalse(os.path.isfile(fragment_path2))

def test_keep_fragments(self):
@with_isolated_runner
def test_keep_fragments(self, runner):
"""
The `--keep` option will build the full final news file
without deleting the fragment files and without
any extra CLI interaction or confirmation.
"""
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project()
fragment_path1 = "foo/newsfragments/123.feature"
fragment_path2 = "foo/newsfragments/124.feature.rst"
with open(fragment_path1, "w") as f:
f.write("Adds levitation")
with open(fragment_path2, "w") as f:
f.write("Extends levitation")
setup_simple_project()
fragment_path1 = "foo/newsfragments/123.feature"
fragment_path2 = "foo/newsfragments/124.feature.rst"
with open(fragment_path1, "w") as f:
f.write("Adds levitation")
with open(fragment_path2, "w") as f:
f.write("Extends levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

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

self.assertEqual(0, result.exit_code)
# The NEWS file is created.
# So this is not just `--draft`.
self.assertTrue(os.path.isfile("NEWS.rst"))
self.assertTrue(os.path.isfile(fragment_path1))
self.assertTrue(os.path.isfile(fragment_path2))
self.assertEqual(0, result.exit_code)
# The NEWS file is created.
# So this is not just `--draft`.
self.assertTrue(os.path.isfile("NEWS.rst"))
self.assertTrue(os.path.isfile(fragment_path1))
self.assertTrue(os.path.isfile(fragment_path2))

def test_yes_keep_error(self):
@with_isolated_runner
def test_yes_keep_error(self, runner):
"""
It will fail to perform any action when the
conflicting --keep and --yes options are provided.
Expand All @@ -448,28 +447,25 @@ def test_yes_keep_error(self):
to make sure both orders are validated since click triggers the validator
in the order it parses the command line.
"""
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project()
fragment_path1 = "foo/newsfragments/123.feature"
fragment_path2 = "foo/newsfragments/124.feature.rst"
with open(fragment_path1, "w") as f:
f.write("Adds levitation")
with open(fragment_path2, "w") as f:
f.write("Extends levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

result = runner.invoke(_main, ["--date", "01-01-2001", "--yes", "--keep"])
self.assertEqual(1, result.exit_code)
setup_simple_project()
fragment_path1 = "foo/newsfragments/123.feature"
fragment_path2 = "foo/newsfragments/124.feature.rst"
with open(fragment_path1, "w") as f:
f.write("Adds levitation")
with open(fragment_path2, "w") as f:
f.write("Extends levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

result = runner.invoke(_main, ["--date", "01-01-2001", "--yes", "--keep"])
self.assertEqual(1, result.exit_code)

result = runner.invoke(_main, ["--date", "01-01-2001", "--keep", "--yes"])
self.assertEqual(1, result.exit_code)
result = runner.invoke(_main, ["--date", "01-01-2001", "--keep", "--yes"])
self.assertEqual(1, result.exit_code)

def test_confirmation_says_no(self):
"""
Expand Down

0 comments on commit bf049f7

Please sign in to comment.