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

Make check subcommand detect if only configured news file has changed #296

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ def __main(comparewith, directory, config):
try:
files_changed = (
_run(
["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory
[
"git",
"diff",
"--name-only",
# Only show files that were Added (A), Copied (C), Modified (M)
# or Renamed (R).
"--diff-filter=ACMR",
comparewith + "..."
],
cwd=base_directory
)
.decode(encoding)
.strip()
Expand All @@ -55,6 +64,10 @@ def __main(comparewith, directory, config):
click.echo("On trunk, or no diffs, so no newsfragment required.")
sys.exit(0)

if files_changed == config["filename"]:
click.echo("Only the configured news file has changed.")
Copy link
Member

Choose a reason for hiding this comment

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

I would say add uppercase when skipped, to help detect accidental configuration errors.

Suggested change
click.echo("Only the configured news file has changed.")
click.echo("Checks SKIPPED. Only news file changes detected that looks like a release branch.")

sys.exit(0)

files = {
os.path.normpath(os.path.join(base_directory, path))
for path in files_changed.strip().splitlines()
Expand Down
6 changes: 6 additions & 0 deletions src/towncrier/newsfragments/174.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Make the ``check`` subcommand detect if only the configured news file has
changed.

This should enable the ``check`` subcommand to be used as a CI lint step and
not fail when a pull request only modifies the configured news file (i.e. when
the news file is being assembled for the next release).
31 changes: 30 additions & 1 deletion src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
from textwrap import dedent

from twisted.trial.unittest import TestCase
from click.testing import CliRunner
Expand All @@ -13,7 +14,11 @@

def create_project(pyproject_path):
with open(pyproject_path, "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
f.write(dedent("""\
Copy link
Member

Choose a reason for hiding this comment

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

I think that we should disable the linter warning for this type of indentation... and maybe just use black :)

I feel that dedent here has no technical merit, and is just added to please a tool :)

[tool.towncrier]
package = "foo"
filename = "NEWS.rst"
"""))
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
Expand All @@ -22,6 +27,9 @@ def create_project(pyproject_path):
with open(fragment_path, "w") as f:
f.write("Adds levitation")

with open("NEWS.rst", "w") as f:
f.write("added on default branch")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
Expand Down Expand Up @@ -148,3 +156,24 @@ def test_none_stdout_encoding_works(self):

self.assertEqual(0, proc.returncode)
self.assertEqual(b"", stderr)

def test_newsfile_modified(self):
"""
A modified newsfile satisfies the check.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
A modified newsfile satisfies the check.
When only the newsfile is modified on a branch --check will not complain about missing newsfile fragmants.

"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project("pyproject.toml")

newsfile_path = "NEWS.rst"

with open(newsfile_path, "w") as f:
f.write("on other branch")
call(["git", "add", newsfile_path])
call(["git", "commit", "-m", "modify NEWS.rst"])

result = runner.invoke(_main, ["--compare-with", "master"])

self.assertEqual(result.output, 'Only the configured news file has changed.\n')
self.assertEqual(0, result.exit_code)